QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 ).replace( QLatin1Char( ' ' ), QLatin1Char( '-' ) );
69 return header( headerName );
70}
71
72void QgsServerRequest::setHeader( const QString &name, const QString &value )
73{
74 mHeaders.insert( name, value );
75}
76
77QMap<QString, QString> QgsServerRequest::headers() const
78{
79 return mHeaders;
80}
81
82void QgsServerRequest::removeHeader( const QString &name )
83{
84 mHeaders.remove( name );
85}
86
88{
89 return mUrl;
90}
91
93{
94 return mOriginalUrl;
95}
96
97void QgsServerRequest::setOriginalUrl( const QUrl &url )
98{
99 mOriginalUrl = url;
100}
101
103{
104 return mBaseUrl;
105}
106
107void QgsServerRequest::setBaseUrl( const QUrl &url )
108{
109 mBaseUrl = url;
110}
111
113{
114 return mMethod;
115}
116
117QMap<QString, QString> QgsServerRequest::parameters() const
118{
119 return mParams.toMap();
120}
121
123{
124 return mParams;
125}
126
127QByteArray QgsServerRequest::data() const
128{
129 return QByteArray();
130}
131
132void QgsServerRequest::setParameter( const QString &key, const QString &value )
133{
134 mParams.add( key, value );
135 mUrl.setQuery( mParams.urlQuery() );
136}
137
138QString QgsServerRequest::parameter( const QString &key, const QString &defaultValue ) const
139{
140 const auto value { mParams.value( key ) };
141 if ( value.isEmpty() )
142 {
143 return defaultValue;
144 }
145 return value;
146}
147
148void QgsServerRequest::removeParameter( const QString &key )
149{
150 mParams.remove( key );
151 mUrl.setQuery( mParams.urlQuery() );
152}
153
154void QgsServerRequest::setUrl( const QUrl &url )
155{
156 mUrl = url;
157 mParams.clear();
158 mParams.load( QUrlQuery( mUrl ) );
159}
160
162{
163 mMethod = method;
164}
165
166const QString QgsServerRequest::queryParameter( const QString &name, const QString &defaultValue ) const
167{
168 if ( !QUrlQuery( mUrl ).hasQueryItem( name ) )
169 {
170 return defaultValue;
171 }
172 return QUrl::fromPercentEncoding( QUrlQuery( mUrl ).queryItemValue( name ).toUtf8() );
173}
@ 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.