QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "qgssettings.h"
23
24
25#include <QDir>
26#include <QNetworkRequest>
27#include <QUrlQuery>
28#include <QDomElement>
29
30//
31// QgsHttpHeaders
32//
33
34const QString QgsHttpHeaders::PATH_PREFIX = "http-header/";
35const QString QgsHttpHeaders::PARAM_PREFIX = "http-header:";
36const QString QgsHttpHeaders::KEY_REFERER = "referer";
37
39
40QgsHttpHeaders::QgsHttpHeaders( const QVariantMap &headers )
41 : mHeaders( headers )
42{
43 mHeaders.detach(); // clone like
44}
45
46QgsHttpHeaders::QgsHttpHeaders( const QgsSettings &settings, const QString &key )
47{
48 setFromSettings( settings, key );
49}
50
51QgsHttpHeaders::QgsHttpHeaders( const QString &key )
52{
54}
55
56QgsHttpHeaders::QgsHttpHeaders( const QDomElement &element )
57{
58 setFromDomElement( element );
59}
60
62
63bool QgsHttpHeaders::updateNetworkRequest( QNetworkRequest &request ) const
64{
65 for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
66 {
67 request.setRawHeader( ite.key().toUtf8(), ite.value().toString().toUtf8() );
68 }
69 return true;
70}
71
72bool QgsHttpHeaders::updateUrlQuery( QUrlQuery &uri ) const
73{
74 for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
75 {
76 uri.addQueryItem( QgsHttpHeaders::PARAM_PREFIX + ite.key().toUtf8(), ite.value().toString().toUtf8() );
77 }
78 return true;
79}
80
81bool QgsHttpHeaders::updateSettings( QgsSettings &settings, const QString &key ) const
82{
83 QString keyFixed = sanitizeKey( key );
84 if ( !keyFixed.isEmpty() )
85 keyFixed = keyFixed + "/";
86
87 QString keyHH = keyFixed + QgsHttpHeaders::PATH_PREFIX;
88 settings.remove( keyHH ); // cleanup
89 for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
90 {
91 settings.setValue( keyHH + ite.key(), ite.value() );
92 }
93
94 if ( !mHeaders[QgsHttpHeaders::KEY_REFERER].toString().isEmpty() && settings.contains( keyFixed + QgsHttpHeaders::KEY_REFERER ) ) // backward comptibility
95 {
96 settings.setValue( keyFixed + QgsHttpHeaders::KEY_REFERER, mHeaders[QgsHttpHeaders::KEY_REFERER].toString() );
97 }
98
99// TODO REMOVE!
100#if 0
101 QgsLogger::debug( QString( "updateSettings key: %1" ).arg( keyFixed ) );
102 for ( auto k : settings.allKeys() )
103 if ( k.startsWith( keyFixed ) )
104 QgsLogger::debug( QString( "updateSettings in settings: %1=%2" ).arg( k, settings.value( k ).toString() ) );
105#endif
106 return true;
107}
108
109bool QgsHttpHeaders::updateMap( QVariantMap &map ) const
110{
111 for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
112 {
113 map.insert( QgsHttpHeaders::PARAM_PREFIX + ite.key().toUtf8(), ite.value().toString() );
114 }
115
116 if ( mHeaders.contains( QgsHttpHeaders::KEY_REFERER ) )
117 {
118 map.insert( QgsHttpHeaders::KEY_REFERER, mHeaders[QgsHttpHeaders::KEY_REFERER].toString() );
119 }
120
121 return true;
122}
123
124bool QgsHttpHeaders::updateDomElement( QDomElement &el ) const
125{
126 for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
127 {
128 el.setAttribute( QgsHttpHeaders::PARAM_PREFIX + ite.key().toUtf8(), ite.value().toString() );
129 }
130
131 if ( mHeaders.contains( QgsHttpHeaders::KEY_REFERER ) )
132 {
133 el.setAttribute( QgsHttpHeaders::KEY_REFERER, mHeaders[QgsHttpHeaders::KEY_REFERER].toString() );
134 }
135
136 return true;
137}
138
139
140void QgsHttpHeaders::setFromSettings( const QgsSettings &settings, const QString &key )
141{
142 QString keyFixed = sanitizeKey( key );
143 if ( !keyFixed.isEmpty() )
144 keyFixed = keyFixed + "/";
145 QString keyHH = keyFixed + QgsHttpHeaders::PATH_PREFIX;
146
147#if 0
148 QgsLogger::debug( QString( "setFromSettings key: %1" ).arg( keyFixed ) );
149 for ( auto k : settings.allKeys() )
150 if ( k.startsWith( keyFixed ) )
151 QgsLogger::debug( QString( "setFromSettings called: %1=%2" ).arg( k, settings.value( k ).toString() ) );
152 QgsLogger::debug( QString( "setFromSettings keyHH: %1" ).arg( keyHH ) );
153#endif
154
155 QStringList keys = settings.allKeys();
156 for ( auto ite = keys.cbegin(); ite != keys.cend(); ++ite )
157 {
158 if ( ite->startsWith( keyHH ) )
159 {
160 QString name = ite->right( ite->size() - keyHH.size() );
161 mHeaders.insert( name, settings.value( *ite ).toString() );
162 }
163 }
164
165 if ( settings.contains( keyFixed + QgsHttpHeaders::KEY_REFERER ) ) // backward comptibility
166 {
167 mHeaders[QgsHttpHeaders::KEY_REFERER] = settings.value( keyFixed + QgsHttpHeaders::KEY_REFERER ).toString(); // retrieve value from old location
168 }
169
170#if 0
171 for ( auto k : mHeaders.keys() )
172 QgsLogger::debug( QString( "setFromSettings mHeaders[%1]=%2" ).arg( k, mHeaders[k].toString() ) );
173#endif
174}
175
176void QgsHttpHeaders::setFromUrlQuery( const QUrlQuery &uri )
177{
178 const auto constQueryItems = uri.queryItems( QUrl::ComponentFormattingOption::FullyDecoded );
179 for ( const QPair<QString, QString> &item : constQueryItems )
180 {
181 const QString &key = item.first;
182 if ( key.startsWith( QgsHttpHeaders::PARAM_PREFIX ) )
183 {
184 QString name = key.right( key.size() - QgsHttpHeaders::PARAM_PREFIX.size() );
185 mHeaders[sanitizeKey( name )] = item.second;
186 }
187 else if ( key == QgsHttpHeaders::KEY_REFERER ) // backward comptibility
188 {
189 mHeaders[QgsHttpHeaders::KEY_REFERER] = item.second;
190 }
191 }
192}
193
194void QgsHttpHeaders::setFromMap( const QVariantMap &map )
195{
196 for ( auto ite = map.keyBegin(); ite != map.keyEnd(); ++ite )
197 {
198 QString key = *ite;
199 if ( key.startsWith( QgsHttpHeaders::PARAM_PREFIX ) )
200 {
201 QString name = key.right( key.size() - QgsHttpHeaders::PARAM_PREFIX.size() );
202 mHeaders[sanitizeKey( name )] = map [key].toString();
203 }
204 }
205
206 if ( map.contains( QgsHttpHeaders::KEY_REFERER ) ) // backward comptibility
207 {
208 mHeaders[QgsHttpHeaders::KEY_REFERER] = map [QgsHttpHeaders::KEY_REFERER].toString();
209 }
210}
211
212
213void QgsHttpHeaders::setFromDomElement( const QDomElement &el )
214{
215 QDomNamedNodeMap attribs = el.attributes();
216
217 for ( int i = 0; i < attribs.length(); i++ )
218 {
219 QDomNode item = attribs.item( i );
220 QString key = item.nodeName();
221 if ( key.startsWith( QgsHttpHeaders::PARAM_PREFIX ) )
222 {
223 QString name = key.right( key.size() - QgsHttpHeaders::PARAM_PREFIX.size() );
224 mHeaders[sanitizeKey( name )] = item.nodeValue();
225 }
226 }
227
228 if ( attribs.contains( QgsHttpHeaders::KEY_REFERER ) ) // backward comptibility
229 {
230 mHeaders[QgsHttpHeaders::KEY_REFERER] = attribs.namedItem( QgsHttpHeaders::KEY_REFERER ).nodeValue();
231 }
232
233}
234
236{
237 QString out;
238 for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
239 {
240 out += QStringLiteral( " %1%2='%3'" ).arg( QgsHttpHeaders::PARAM_PREFIX, ite.key(), ite.value().toString() );
241 }
242
243 if ( !mHeaders [ QgsHttpHeaders::KEY_REFERER ].toString().isEmpty() )
244 out += QStringLiteral( " %1='%2'" ).arg( QgsHttpHeaders::KEY_REFERER, mHeaders [QgsHttpHeaders::KEY_REFERER].toString() );
245
246 return out;
247}
248
249
250
251// To clean the path
252QString QgsHttpHeaders::sanitizeKey( const QString &key ) const
253{
254 QString out = QDir::cleanPath( key );
255 return out;
256}
257
258
259QVariant &QgsHttpHeaders::operator[]( const QString &key )
260{
261 return mHeaders[sanitizeKey( key )];
262}
263
264const QVariant QgsHttpHeaders::operator[]( const QString &key ) const
265{
266 return mHeaders[sanitizeKey( key )];
267}
268
269QgsHttpHeaders &QgsHttpHeaders::operator = ( const QMap<QString, QVariant> &headers )
270{
271 mHeaders = headers;
272 return *this;
273}
274
275QList<QString> QgsHttpHeaders::keys() const
276{
277 return mHeaders.keys();
278}
279
280
281void QgsHttpHeaders::insert( const QString &key, const QVariant &val )
282{
283 QString k2 = key;
284
285 if ( k2.startsWith( QgsHttpHeaders::PARAM_PREFIX ) )
286 {
287 k2 = k2.right( k2.length() - QgsHttpHeaders::PARAM_PREFIX.length() );
288 }
289 mHeaders.insert( k2, val );
290}
This class implements simple http header management.
bool updateDomElement(QDomElement &el) const
Updates a map by adding all the HTTP headers.
QString toSpacedString() const
Returns key/value pairs as strings separated by space.
QString sanitizeKey(const QString &key) const
Returns a cleansed key.
Q_DECL_DEPRECATED bool updateSettings(QgsSettings &settings, const QString &key=QString()) const
Updates the settings by adding all the http headers in the path "key/PATH_PREFIX/".
QList< QString > keys() const
static const QString PARAM_PREFIX
Used in uri to pass headers as params.
QgsHttpHeaders & operator=(const QMap< QString, QVariant > &headers)
static const QString KEY_REFERER
Used in settings as the referer key.
bool updateMap(QVariantMap &map) const
Updates a map by adding all the HTTP headers.
QVariantMap headers() const
Returns the headers as a variant map.
bool updateUrlQuery(QUrlQuery &uri) const
Updates an uri by adding all the HTTP headers.
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 setFromMap(const QVariantMap &map)
Loads headers from the map.
void setFromUrlQuery(const QUrlQuery &uri)
Loads headers from the uri.
QVariant & operator[](const QString &key)
static const QString PATH_PREFIX
Used in settings as the group name.
void setFromDomElement(const QDomElement &element)
Loads headers from the element.
virtual ~QgsHttpHeaders()
default detructor
void insert(const QString &key, const QVariant &value)
insert a key with the specific value
static void debug(const QString &msg, int debuglevel=1, const char *file=nullptr, const char *function=nullptr, int line=-1)
Goes to qDebug.
Definition qgslogger.cpp:59
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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 remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
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.