QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "moc_qgsserverrequest.cpp"
22#include "qgsstringutils.h"
23#include <QUrlQuery>
24
25
26QgsServerRequest::QgsServerRequest( const QString &url, Method method, const Headers &headers )
27 : QgsServerRequest( QUrl( url ), method, headers )
28{
29}
30
31QgsServerRequest::QgsServerRequest( const QUrl &url, Method method, const Headers &headers )
32 : mUrl( url )
33 , mOriginalUrl( url )
34 , mBaseUrl( url )
35 , mMethod( method )
36 , mHeaders( headers )
37{
38 mParams.load( QUrlQuery( url ) );
39}
40
42 : mUrl( other.mUrl )
43 , mOriginalUrl( other.mOriginalUrl )
44 , mBaseUrl( other.mBaseUrl )
45 , mMethod( other.mMethod )
46 , mHeaders( other.mHeaders )
47 , mParams( other.mParams )
48{
49}
50
52{
53 static const QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerRequest::Method>();
54 return QString( metaEnum.valueToKey( method ) ).remove( QStringLiteral( "Method" ) ).toUpper();
55}
56
57QString QgsServerRequest::header( const QString &name ) const
58{
59 return mHeaders.value( name );
60}
61
62
64{
65 const QString headerKey = QString( qgsEnumValueToKey<QgsServerRequest::RequestHeader>( headerEnum ) );
66 const QString headerName = QgsStringUtils::capitalize(
67 QString( headerKey ).replace( QLatin1Char( '_' ), QLatin1Char( ' ' ) ), Qgis::Capitalization::TitleCase
68 )
69 .replace( QLatin1Char( ' ' ), QLatin1Char( '-' ) );
70 return header( headerName );
71}
72
73void QgsServerRequest::setHeader( const QString &name, const QString &value )
74{
75 mHeaders.insert( name, value );
76}
77
78QMap<QString, QString> QgsServerRequest::headers() const
79{
80 return mHeaders;
81}
82
83void QgsServerRequest::removeHeader( const QString &name )
84{
85 mHeaders.remove( name );
86}
87
89{
90 return mUrl;
91}
92
94{
95 return mOriginalUrl;
96}
97
98void QgsServerRequest::setOriginalUrl( const QUrl &url )
99{
100 mOriginalUrl = url;
101}
102
104{
105 return mBaseUrl;
106}
107
108void QgsServerRequest::setBaseUrl( const QUrl &url )
109{
110 mBaseUrl = url;
111}
112
114{
115 return mMethod;
116}
117
118QMap<QString, QString> QgsServerRequest::parameters() const
119{
120 return mParams.toMap();
121}
122
124{
125 return mParams;
126}
127
128QByteArray QgsServerRequest::data() const
129{
130 return QByteArray();
131}
132
133void QgsServerRequest::setParameter( const QString &key, const QString &value )
134{
135 mParams.add( key, value );
136 mUrl.setQuery( mParams.urlQuery() );
137}
138
139QString QgsServerRequest::parameter( const QString &key, const QString &defaultValue ) const
140{
141 const auto value { mParams.value( key ) };
142 if ( value.isEmpty() )
143 {
144 return defaultValue;
145 }
146 return value;
147}
148
149void QgsServerRequest::removeParameter( const QString &key )
150{
151 mParams.remove( key );
152 mUrl.setQuery( mParams.urlQuery() );
153}
154
155void QgsServerRequest::setUrl( const QUrl &url )
156{
157 mUrl = url;
158 mParams.clear();
159 mParams.load( QUrlQuery( mUrl ) );
160}
161
163{
164 mMethod = method;
165}
166
167const QString QgsServerRequest::queryParameter( const QString &name, const QString &defaultValue ) const
168{
169 if ( !QUrlQuery( mUrl ).hasQueryItem( name ) )
170 {
171 return defaultValue;
172 }
173 return QUrl::fromPercentEncoding( QUrlQuery( mUrl ).queryItemValue( name ).toUtf8() );
174}
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
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
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.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.