QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
22#include "qgsstringutils.h"
23
24#include <QString>
25#include <QUrlQuery>
26
27#include "moc_qgsserverrequest.cpp"
28
29using namespace Qt::StringLiterals;
30
35
37 : mUrl( url )
38 , mOriginalUrl( url )
39 , mBaseUrl( url )
40 , mMethod( method )
41 , mHeaders( headers )
42{
43 mParams.load( QUrlQuery( url ) );
44}
45
47 : mUrl( other.mUrl )
48 , mOriginalUrl( other.mOriginalUrl )
49 , mBaseUrl( other.mBaseUrl )
50 , mMethod( other.mMethod )
51 , mHeaders( other.mHeaders )
52 , mParams( other.mParams )
53{
54}
55
57{
58 static const QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerRequest::Method>();
59 return QString( metaEnum.valueToKey( method ) ).remove( u"Method"_s ).toUpper();
60}
61
62QString QgsServerRequest::header( const QString &name ) const
63{
64 return mHeaders.value( name );
65}
66
67
69{
70 const QString headerKey = QString( qgsEnumValueToKey<QgsServerRequest::RequestHeader>( headerEnum ) );
71 const QString headerName = QgsStringUtils::capitalize(
72 QString( headerKey ).replace( '_'_L1, ' '_L1 ), Qgis::Capitalization::TitleCase
73 )
74 .replace( ' '_L1, '-'_L1 );
75 return header( headerName );
76}
77
78void QgsServerRequest::setHeader( const QString &name, const QString &value )
79{
80 mHeaders.insert( name, value );
81}
82
83QMap<QString, QString> QgsServerRequest::headers() const
84{
85 return mHeaders;
86}
87
88void QgsServerRequest::removeHeader( const QString &name )
89{
90 mHeaders.remove( name );
91}
92
94{
95 return mUrl;
96}
97
99{
100 return mOriginalUrl;
101}
102
104{
105 mOriginalUrl = url;
106}
107
109{
110 return mBaseUrl;
111}
112
114{
115 mBaseUrl = url;
116}
117
119{
120 return mMethod;
121}
122
123QMap<QString, QString> QgsServerRequest::parameters() const
124{
125 return mParams.toMap();
126}
127
129{
130 return mParams;
131}
132
133QByteArray QgsServerRequest::data() const
134{
135 return QByteArray();
136}
137
138void QgsServerRequest::setParameter( const QString &key, const QString &value )
139{
140 mParams.add( key, value );
141 mUrl.setQuery( mParams.urlQuery() );
142}
143
144QString QgsServerRequest::parameter( const QString &key, const QString &defaultValue ) const
145{
146 const auto value { mParams.value( key ) };
147 if ( value.isEmpty() )
148 {
149 return defaultValue;
150 }
151 return value;
152}
153
154void QgsServerRequest::removeParameter( const QString &key )
155{
156 mParams.remove( key );
157 mUrl.setQuery( mParams.urlQuery() );
158}
159
160void QgsServerRequest::setUrl( const QUrl &url )
162 mUrl = url;
163 mParams.clear();
164 mParams.load( QUrlQuery( mUrl ) );
165}
166
168{
169 mMethod = method;
170}
171
172const QString QgsServerRequest::queryParameter( const QString &name, const QString &defaultValue ) const
173{
174 if ( !QUrlQuery( mUrl ).hasQueryItem( name ) )
175 {
176 return defaultValue;
177 }
178 return QUrl::fromPercentEncoding( QUrlQuery( mUrl ).queryItemValue( name ).toUtf8() );
179}
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
Definition qgis.h:3454
Provides an interface to retrieve and manipulate global parameters received from the client.
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
virtual void setUrl(const QUrl &url)
Set the request url.
QUrl url() const
Returns the request URL as seen by QGIS server.
QMap< QString, QString > headers() const
Returns the header map.
QUrl baseUrl() const
Returns the base URL of QGIS server.
QgsServerRequest::Method method() const
Returns the request method.
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.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7091