QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsstringutils.h"
22 #include <QUrlQuery>
23 
24 
25 QgsServerRequest::QgsServerRequest( const QString &url, Method method, const Headers &headers )
26  : QgsServerRequest( QUrl( url ), method, headers )
27 {
28 }
29 
30 QgsServerRequest::QgsServerRequest( const QUrl &url, Method method, const Headers &headers )
31  : mUrl( url )
32  , mOriginalUrl( url )
33  , mBaseUrl( url )
34  , mMethod( method )
35  , mHeaders( headers )
36 {
37  mParams.load( QUrlQuery( url ) );
38 }
39 
41  : mUrl( other.mUrl )
42  , mOriginalUrl( other.mOriginalUrl )
43  , mBaseUrl( other.mBaseUrl )
44  , mMethod( other.mMethod )
45  , mHeaders( other.mHeaders )
46  , mParams( other.mParams )
47 {
48 }
49 
51 {
52  static const QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerRequest::Method>();
53  return QString( metaEnum.valueToKey( method ) ).remove( QStringLiteral( "Method" ) ).toUpper( );
54 }
55 
56 QString QgsServerRequest::header( const QString &name ) const
57 {
58  return mHeaders.value( name );
59 }
60 
61 
63 {
64  const QString headerKey = QString( qgsEnumValueToKey<QgsServerRequest::RequestHeader>( headerEnum ) );
65  const QString headerName = QgsStringUtils::capitalize(
66  QString( headerKey ).replace( QLatin1Char( '_' ), QLatin1Char( ' ' ) ), Qgis::Capitalization::TitleCase
67  ).replace( QLatin1Char( ' ' ), QLatin1Char( '-' ) );
68  return header( headerName );
69 }
70 
71 void QgsServerRequest::setHeader( const QString &name, const QString &value )
72 {
73  mHeaders.insert( name, value );
74 }
75 
76 QMap<QString, QString> QgsServerRequest::headers() const
77 {
78  return mHeaders;
79 }
80 
81 void QgsServerRequest::removeHeader( const QString &name )
82 {
83  mHeaders.remove( name );
84 }
85 
87 {
88  return mUrl;
89 }
90 
92 {
93  return mOriginalUrl;
94 }
95 
96 void QgsServerRequest::setOriginalUrl( const QUrl &url )
97 {
98  mOriginalUrl = url;
99 }
100 
102 {
103  return mBaseUrl;
104 }
105 
106 void QgsServerRequest::setBaseUrl( const QUrl &url )
107 {
108  mBaseUrl = url;
109 }
110 
112 {
113  return mMethod;
114 }
115 
116 QMap<QString, QString> QgsServerRequest::parameters() const
117 {
118  return mParams.toMap();
119 }
120 
122 {
123  return mParams;
124 }
125 
126 QByteArray QgsServerRequest::data() const
127 {
128  return QByteArray();
129 }
130 
131 void QgsServerRequest::setParameter( const QString &key, const QString &value )
132 {
133  mParams.add( key, value );
134  mUrl.setQuery( mParams.urlQuery() );
135 }
136 
137 QString QgsServerRequest::parameter( const QString &key, const QString &defaultValue ) const
138 {
139  const auto value { mParams.value( key ) };
140  if ( value.isEmpty() )
141  {
142  return defaultValue;
143  }
144  return value;
145 }
146 
147 void QgsServerRequest::removeParameter( const QString &key )
148 {
149  mParams.remove( key );
150  mUrl.setQuery( mParams.urlQuery() );
151 }
152 
153 void QgsServerRequest::setUrl( const QUrl &url )
154 {
155  mUrl = url;
156  mParams.clear();
157  mParams.load( QUrlQuery( mUrl ) );
158 }
159 
161 {
162  mMethod = method;
163 }
164 
165 const QString QgsServerRequest::queryParameter( const QString &name, const QString &defaultValue ) const
166 {
167  if ( !QUrlQuery( mUrl ).hasQueryItem( name ) )
168  {
169  return defaultValue;
170  }
171  return QUrl::fromPercentEncoding( QUrlQuery( mUrl ).queryItemValue( name ).toUtf8() );
172 }
QgsServerRequest::parameters
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
Definition: qgsserverrequest.cpp:116
QgsStringUtils::capitalize
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Definition: qgsstringutils.cpp:24
QgsServerRequest::baseUrl
QUrl baseUrl() const
Returns the base URL of QGIS server.
Definition: qgsserverrequest.cpp:101
QgsServerRequest::setHeader
void setHeader(const QString &name, const QString &value)
Set an header.
Definition: qgsserverrequest.cpp:71
QgsServerParameters::toMap
QMap< QString, QString > toMap() const
Returns all parameters in a map.
Definition: qgsserverparameters.cpp:602
qgsstringutils.h
QgsServerParameters::load
void load(const QUrlQuery &query)
Loads new parameters.
Definition: qgsserverparameters.cpp:647
QgsServerRequest::setOriginalUrl
void setOriginalUrl(const QUrl &url)
Set the request original url (the request url as seen by the web server)
Definition: qgsserverrequest.cpp:96
QgsServerRequest
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
Definition: qgsserverrequest.h:38
Qgis::Capitalization::TitleCase
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
QgsServerParameters::value
QString value(const QString &key) const
Returns the value of a parameter.
Definition: qgsserverparameters.cpp:630
QgsServerRequest::removeHeader
void removeHeader(const QString &name)
Remove an header.
Definition: qgsserverrequest.cpp:81
QgsServerParameters::remove
void remove(const QString &key)
Removes a parameter.
Definition: qgsserverparameters.cpp:554
QgsServerRequest::removeParameter
virtual void removeParameter(const QString &key)
Remove a parameter.
Definition: qgsserverrequest.cpp:147
QgsServerRequest::setMethod
void setMethod(QgsServerRequest::Method method)
Set the request method.
Definition: qgsserverrequest.cpp:160
QgsServerRequest::setBaseUrl
void setBaseUrl(const QUrl &url)
Set the base URL of QGIS server.
Definition: qgsserverrequest.cpp:106
QgsServerParameters::add
void add(const QString &key, const QString &value)
Adds a parameter.
Definition: qgsserverparameters.cpp:523
QgsServerRequest::serverParameters
QgsServerParameters serverParameters() const
Returns parameters.
Definition: qgsserverrequest.cpp:121
QgsServerRequest::setParameter
virtual void setParameter(const QString &key, const QString &value)
Set a parameter.
Definition: qgsserverrequest.cpp:131
qgsserverrequest.h
QgsServerRequest::method
QgsServerRequest::Method method() const
Definition: qgsserverrequest.cpp:111
QgsServerRequest::queryParameter
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...
Definition: qgsserverrequest.cpp:165
QgsServerRequest::QgsServerRequest
QgsServerRequest()=default
Constructor.
QgsServerRequest::header
virtual QString header(const QString &name) const
Returns the header value.
Definition: qgsserverrequest.cpp:56
QgsServerRequest::originalUrl
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...
Definition: qgsserverrequest.cpp:91
QgsServerRequest::data
virtual QByteArray data() const
Returns post/put data Check for QByteArray::isNull() to check if data is available.
Definition: qgsserverrequest.cpp:126
QgsServerRequest::parameter
QString parameter(const QString &key, const QString &defaultValue=QString()) const
Gets a parameter value.
Definition: qgsserverrequest.cpp:137
QgsServerRequest::RequestHeader
RequestHeader
The internal HTTP Header used for the request as enum.
Definition: qgsserverrequest.h:79
QgsServerParameters::urlQuery
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
Definition: qgsserverparameters.cpp:530
QgsServerRequest::url
QUrl url() const
Definition: qgsserverrequest.cpp:86
QgsServerRequest::Method
Method
HTTP Method (or equivalent) used for the request.
Definition: qgsserverrequest.h:65
QgsServerRequest::Headers
QMap< QString, QString > Headers
Definition: qgsserverrequest.h:60
QgsServerParameters
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
Definition: qgsserverparameters.h:242
QgsServerRequest::methodToString
static QString methodToString(const Method &method)
Returns a string representation of an HTTP request method.
Definition: qgsserverrequest.cpp:50
QgsServerRequest::setUrl
virtual void setUrl(const QUrl &url)
Set the request url.
Definition: qgsserverrequest.cpp:153
QgsServerParameters::clear
void clear()
Removes all parameters.
Definition: qgsserverparameters.cpp:687
QgsServerRequest::headers
QMap< QString, QString > headers() const
Returns the header map.
Definition: qgsserverrequest.cpp:76