QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgshttpheaders.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshttpheaders.cpp
3  This class implements simple http header management.
4 
5  -------------------
6  begin : 2021-09-09
7  copyright : (C) 2021 B. De Mezzo
8  email : benoit dot de dot mezzo at oslandia dot com
9 
10 ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #include "qgshttpheaders.h"
22 
23 //
24 // QgsHttpHeaders
25 //
26 
27 const QString QgsHttpHeaders::KEY_PREFIX = "http-header/";
28 
30 
31 QgsHttpHeaders::QgsHttpHeaders( const QMap<QString, QVariant> &headers )
32  : mHeaders( headers )
33 {
34  mHeaders.detach(); // clone like
35 }
36 
37 QgsHttpHeaders::QgsHttpHeaders( const QgsSettings &settings, const QString &key )
38 {
39  setFromSettings( settings, key );
40 }
41 
43 
44 bool QgsHttpHeaders::updateNetworkRequest( QNetworkRequest &request ) const
45 {
46  for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
47  {
48  request.setRawHeader( ite.key().toUtf8(), ite.value().toString().toUtf8() );
49  }
50  return true;
51 }
52 
53 void QgsHttpHeaders::updateSettings( QgsSettings &settings, const QString &key ) const
54 {
55  QString keyFixed = key;
56  if ( !keyFixed.isEmpty() && !keyFixed.endsWith( "/" ) )
57  keyFixed = keyFixed + "/";
58  QString keyHH = keyFixed + QgsHttpHeaders::KEY_PREFIX;
59  for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
60  {
61  settings.setValue( keyHH + ite.key(), ite.value() );
62  }
63 
64  if ( !mHeaders["referer"].toString().isEmpty() && settings.contains( keyFixed + "referer" ) ) // backward comptibility
65  {
66  settings.setValue( keyFixed + "referer", mHeaders["referer"].toString() );
67  }
68 }
69 
70 void QgsHttpHeaders::setFromSettings( const QgsSettings &settings, const QString &key )
71 {
72  QString keyFixed = key;
73  if ( !keyFixed.isEmpty() && !keyFixed.endsWith( "/" ) )
74  keyFixed = keyFixed + "/";
75  QString keyHH = keyFixed + QgsHttpHeaders::KEY_PREFIX;
76  QStringList keys = settings.allKeys();
77  for ( auto ite = keys.cbegin(); ite != keys.cend(); ++ite )
78  {
79  if ( ite->startsWith( keyHH ) )
80  {
81  QString name = ite->right( ite->size() - keyHH.size() );
82  mHeaders.insert( name, settings.value( *ite ).toString() );
83  }
84  }
85  if ( mHeaders["referer"].toString().isEmpty() ) // backward comptibility
86  {
87  mHeaders["referer"] = settings.value( keyFixed + "referer" ).toString(); // retrieve value from old location
88  }
89 }
90 
91 
92 QVariant &QgsHttpHeaders::operator[]( const QString &key )
93 {
94  return mHeaders[key];
95 }
96 
97 const QVariant QgsHttpHeaders::operator[]( const QString &key ) const
98 {
99  return mHeaders[key];
100 }
101 
102 QgsHttpHeaders &QgsHttpHeaders::operator = ( const QMap<QString, QVariant> &headers )
103 {
104  mHeaders = headers;
105  return *this;
106 }
107 
108 QList<QString> QgsHttpHeaders::keys() const
109 {
110  return mHeaders.keys();
111 }
This class implements simple http header management.
QList< QString > keys() const
QgsHttpHeaders & operator=(const QMap< QString, QVariant > &headers)
static const QString KEY_PREFIX
Used in settings.
bool updateNetworkRequest(QNetworkRequest &request) const
Updates a request by adding all the HTTP headers.
void setFromSettings(const QgsSettings &settings, const QString &key=QString())
Loads headers from the settings.
void updateSettings(QgsSettings &settings, const QString &key=QString()) const
Updates the settings by adding all the http headers in the path "key/KEY_PREFIX/".
QgsHttpHeaders()
default constructor
QVariant & operator[](const QString &key)
virtual ~QgsHttpHeaders()
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QStringList allKeys() const
Returns a list of all keys, including subkeys, that can be read using the QSettings object.