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