QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsserverrequest.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsserverrequest.cpp
3 
4  Define ruquest class for getting request contents
5  -------------------
6  begin : 2016-12-05
7  copyright : (C) 2016 by David Marteau
8  email : david dot marteau at 3liz dot com
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 #include "qgsserverrequest.h"
21 #include <QUrlQuery>
22 
23 
24 QgsServerRequest::QgsServerRequest( const QString &url, Method method, const Headers &headers )
25  : QgsServerRequest( QUrl( url ), method, headers )
26 {
27 }
28 
29 QgsServerRequest::QgsServerRequest( const QUrl &url, Method method, const Headers &headers )
30  : mUrl( url )
31  , mOriginalUrl( url )
32  , mBaseUrl( url )
33  , mMethod( method )
34  , mHeaders( headers )
35  , mRequestHeaderConv()
36 {
37  mRequestHeaderConv.insert( HOST, QStringLiteral( "Host" ) );
38  mRequestHeaderConv.insert( FORWARDED, QStringLiteral( "Forwarded" ) );
39  mRequestHeaderConv.insert( X_FORWARDED_FOR, QStringLiteral( "X-Forwarded-For" ) );
40  mRequestHeaderConv.insert( X_FORWARDED_HOST, QStringLiteral( "X-Forwarded-Host" ) );
41  mRequestHeaderConv.insert( X_FORWARDED_PROTO, QStringLiteral( "X-Forwarded-Proto" ) );
42  mRequestHeaderConv.insert( X_QGIS_SERVICE_URL, QStringLiteral( "X-Qgis-Service-Url" ) );
43  mRequestHeaderConv.insert( X_QGIS_WMS_SERVICE_URL, QStringLiteral( "X-Qgis-Wms-Service-Url" ) );
44  mRequestHeaderConv.insert( X_QGIS_WFS_SERVICE_URL, QStringLiteral( "X-Qgis-Wfs-Service-Url" ) );
45  mRequestHeaderConv.insert( X_QGIS_WCS_SERVICE_URL, QStringLiteral( "X-Qgis-Wcs-Service-Url" ) );
46  mRequestHeaderConv.insert( X_QGIS_WMTS_SERVICE_URL, QStringLiteral( "X-Qgis-Wmts-Service-Url" ) );
47 
48  mParams.load( QUrlQuery( url ) );
49 }
50 
52  : mUrl( other.mUrl )
53  , mOriginalUrl( other.mOriginalUrl )
54  , mBaseUrl( other.mBaseUrl )
55  , mMethod( other.mMethod )
56  , mHeaders( other.mHeaders )
57  , mParams( other.mParams )
58  , mRequestHeaderConv( other.mRequestHeaderConv )
59 {
60 }
61 
63 {
64  static QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerRequest::Method>();
65  return QString( metaEnum.valueToKey( method ) ).remove( QStringLiteral( "Method" ) ).toUpper( );
66 }
67 
68 QString QgsServerRequest::header( const QString &name ) const
69 {
70  return mHeaders.value( name );
71 }
72 
73 
75 {
76  return header( mRequestHeaderConv[ headerEnum ] );
77 }
78 
79 void QgsServerRequest::setHeader( const QString &name, const QString &value )
80 {
81  mHeaders.insert( name, value );
82 }
83 
84 QMap<QString, QString> QgsServerRequest::headers() const
85 {
86  return mHeaders;
87 }
88 
89 void QgsServerRequest::removeHeader( const QString &name )
90 {
91  mHeaders.remove( name );
92 }
93 
95 {
96  return mUrl;
97 }
98 
100 {
101  return mOriginalUrl;
102 }
103 
104 void QgsServerRequest::setOriginalUrl( const QUrl &url )
105 {
106  mOriginalUrl = url;
107 }
108 
110 {
111  return mBaseUrl;
112 }
113 
114 void QgsServerRequest::setBaseUrl( const QUrl &url )
115 {
116  mBaseUrl = url;
117 }
118 
120 {
121  return mMethod;
122 }
123 
124 QMap<QString, QString> QgsServerRequest::parameters() const
125 {
126  return mParams.toMap();
127 }
128 
130 {
131  return mParams;
132 }
133 
134 QByteArray QgsServerRequest::data() const
135 {
136  return QByteArray();
137 }
138 
139 void QgsServerRequest::setParameter( const QString &key, const QString &value )
140 {
141  mParams.add( key, value );
142  mUrl.setQuery( mParams.urlQuery() );
143 }
144 
145 QString QgsServerRequest::parameter( const QString &key, const QString &defaultValue ) const
146 {
147  const auto value { mParams.value( key ) };
148  if ( value.isEmpty() )
149  {
150  return defaultValue;
151  }
152  return value;
153 }
154 
155 void QgsServerRequest::removeParameter( const QString &key )
156 {
157  mParams.remove( key );
158  mUrl.setQuery( mParams.urlQuery() );
159 }
160 
161 void QgsServerRequest::setUrl( const QUrl &url )
162 {
163  mUrl = url;
164  mParams.clear();
165  mParams.load( QUrlQuery( mUrl ) );
166 }
167 
169 {
170  mMethod = method;
171 }
172 
173 const QString QgsServerRequest::queryParameter( const QString &name, const QString &defaultValue ) const
174 {
175  if ( !QUrlQuery( mUrl ).hasQueryItem( name ) )
176  {
177  return defaultValue;
178  }
179  return QUrl::fromPercentEncoding( QUrlQuery( mUrl ).queryItemValue( name ).toUtf8() );
180 }
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
QMap< QString, QString > toMap() const
Returns all parameters in a map.
void add(const QString &key, const QString &value)
Adds a parameter.
void clear()
Removes all parameters.
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
void load(const QUrlQuery &query)
Loads new parameters.
void remove(const QString &key)
Removes a parameter.
QString value(const QString &key) const
Returns the value of a parameter.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
void setOriginalUrl(const QUrl &url)
Set the request original url (the request url as seen by the web server)
Method
HTTP Method (or equivalent) used for the request.
QgsServerParameters serverParameters() const
Returns parameters.
const QString queryParameter(const QString &name, const QString &defaultValue=QString()) const
Returns the query string parameter with the given name from the request URL, a defaultValue can be sp...
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
virtual QString header(const QString &name) const
Returns the header value.
QString parameter(const QString &key, const QString &defaultValue=QString()) const
Gets a parameter value.
virtual void setParameter(const QString &key, const QString &value)
Set a parameter.
QgsServerRequest()=default
Constructor.
virtual void setUrl(const QUrl &url)
Set the request url.
QMap< QString, QString > headers() const
Returns the header map.
QUrl baseUrl() const
Returns the base URL of QGIS server.
QgsServerRequest::Method method() const
QUrl originalUrl() const
Returns the request url as seen by the web server, by default this is equal to the url seen by QGIS s...
void setMethod(QgsServerRequest::Method method)
Set the request method.
void removeHeader(const QString &name)
Remove an header.
QMap< QString, QString > Headers
virtual void removeParameter(const QString &key)
Remove a parameter.
void setBaseUrl(const QUrl &url)
Set the base URL of QGIS server.
void setHeader(const QString &name, const QString &value)
Set an header.
RequestHeader
The internal HTTP Header used for the request as enum.
static QString methodToString(const Method &method)
Returns a string representation of an HTTP request method.
virtual QByteArray data() const
Returns post/put data Check for QByteArray::isNull() to check if data is available.