QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 <QUrlQuery>
22 
23 QgsServerRequest::QgsServerRequest( const QString &url, Method method, const Headers &headers )
24  : QgsServerRequest( QUrl( url ), method, headers )
25 {
26 }
27 
28 QgsServerRequest::QgsServerRequest( const QUrl &url, Method method, const Headers &headers )
29  : mUrl( url )
30  , mOriginalUrl( url )
31  , mMethod( method )
32  , mHeaders( headers )
33 {
34  mParams.load( QUrlQuery( url ) );
35 }
36 
38 {
39  static QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerRequest::Method>();
40  return QString( metaEnum.valueToKey( method ) ).remove( QStringLiteral( "Method" ) ).toUpper( );
41 }
42 
43 QString QgsServerRequest::header( const QString &name ) const
44 {
45  return mHeaders.value( name );
46 }
47 
48 
49 void QgsServerRequest::setHeader( const QString &name, const QString &value )
50 {
51  mHeaders.insert( name, value );
52 }
53 
54 QMap<QString, QString> QgsServerRequest::headers() const
55 {
56  return mHeaders;
57 }
58 
59 
60 void QgsServerRequest::removeHeader( const QString &name )
61 {
62  mHeaders.remove( name );
63 }
64 
66 {
67  return mUrl;
68 }
69 
71 {
72  return mOriginalUrl;
73 }
74 
75 void QgsServerRequest::setOriginalUrl( const QUrl &url )
76 {
77  mOriginalUrl = url;
78 }
79 
81 {
82  return mMethod;
83 }
84 
85 QMap<QString, QString> QgsServerRequest::parameters() const
86 {
87  return mParams.toMap();
88 }
89 
91 {
92  return mParams;
93 }
94 
95 QByteArray QgsServerRequest::data() const
96 {
97  return QByteArray();
98 }
99 
100 void QgsServerRequest::setParameter( const QString &key, const QString &value )
101 {
102  mParams.add( key, value );
103  mUrl.setQuery( mParams.urlQuery() );
104 }
105 
106 QString QgsServerRequest::parameter( const QString &key, const QString &defaultValue ) const
107 {
108  const auto value { mParams.value( key ) };
109  if ( value.isEmpty() )
110  {
111  return defaultValue;
112  }
113  return value;
114 }
115 
116 void QgsServerRequest::removeParameter( const QString &key )
117 {
118  mParams.remove( key );
119  mUrl.setQuery( mParams.urlQuery() );
120 }
121 
122 void QgsServerRequest::setUrl( const QUrl &url )
123 {
124  mUrl = url;
125  mParams.clear();
126  mParams.load( QUrlQuery( mUrl ) );
127 }
128 
130 {
131  mMethod = method;
132 }
133 
134 const QString QgsServerRequest::queryParameter( const QString &name, const QString &defaultValue ) const
135 {
136  if ( !QUrlQuery( mUrl ).hasQueryItem( name ) )
137  {
138  return defaultValue;
139  }
140  return QUrl::fromPercentEncoding( QUrlQuery( mUrl ).queryItemValue( name ).toUtf8() );
141 }
142 
QgsServerRequest::parameters
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
Definition: qgsserverrequest.cpp:85
QgsServerRequest::setHeader
void setHeader(const QString &name, const QString &value)
Set an header.
Definition: qgsserverrequest.cpp:49
QgsServerParameters::toMap
QMap< QString, QString > toMap() const
Returns all parameters in a map.
Definition: qgsserverparameters.cpp:504
QgsServerParameters::load
void load(const QUrlQuery &query)
Loads new parameters.
Definition: qgsserverparameters.cpp:549
QgsServerRequest::setOriginalUrl
void setOriginalUrl(const QUrl &url)
Set the request original url (the request url as seen by the web server)
Definition: qgsserverrequest.cpp:75
QgsServerRequest
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
Definition: qgsserverrequest.h:39
QgsServerParameters::value
QString value(const QString &key) const
Returns the value of a parameter.
Definition: qgsserverparameters.cpp:532
QgsServerRequest::removeHeader
void removeHeader(const QString &name)
Remove an header.
Definition: qgsserverrequest.cpp:60
QgsServerParameters::remove
void remove(const QString &key)
Removes a parameter.
Definition: qgsserverparameters.cpp:456
QgsServerRequest::removeParameter
void removeParameter(const QString &key)
Remove a parameter.
Definition: qgsserverrequest.cpp:116
QgsServerRequest::setMethod
void setMethod(QgsServerRequest::Method method)
Set the request method.
Definition: qgsserverrequest.cpp:129
QgsServerParameters::add
void add(const QString &key, const QString &value)
Adds a parameter.
Definition: qgsserverparameters.cpp:427
QgsServerRequest::serverParameters
QgsServerParameters serverParameters() const
Returns parameters.
Definition: qgsserverrequest.cpp:90
QgsServerRequest::setParameter
void setParameter(const QString &key, const QString &value)
Set a parameter.
Definition: qgsserverrequest.cpp:100
qgsserverrequest.h
QgsServerRequest::method
QgsServerRequest::Method method() const
Definition: qgsserverrequest.cpp:80
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:134
QgsServerRequest::QgsServerRequest
QgsServerRequest()=default
Constructor.
QgsServerRequest::header
QString header(const QString &name) const
Returns the header value.
Definition: qgsserverrequest.cpp:43
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:70
QgsServerRequest::data
virtual QByteArray data() const
Returns post/put data Check for QByteArray::isNull() to check if data is available.
Definition: qgsserverrequest.cpp:95
QgsServerRequest::parameter
QString parameter(const QString &key, const QString &defaultValue=QString()) const
Gets a parameter value.
Definition: qgsserverrequest.cpp:106
QgsServerParameters::urlQuery
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
Definition: qgsserverparameters.cpp:434
QgsServerRequest::url
QUrl url() const
Definition: qgsserverrequest.cpp:65
QgsServerRequest::Method
Method
HTTP Method (or equivalent) used for the request.
Definition: qgsserverrequest.h:51
QgsServerRequest::Headers
QMap< QString, QString > Headers
Definition: qgsserverrequest.h:45
QgsServerParameters
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
Definition: qgsserverparameters.h:229
QgsServerRequest::methodToString
static QString methodToString(const Method &method)
Returns a string representation of an HTTP request method.
Definition: qgsserverrequest.cpp:37
QgsServerRequest::setUrl
void setUrl(const QUrl &url)
Set the request url.
Definition: qgsserverrequest.cpp:122
QgsServerParameters::clear
void clear()
Removes all parameters.
Definition: qgsserverparameters.cpp:588
QgsServerRequest::headers
QMap< QString, QString > headers() const
Returns the header map.
Definition: qgsserverrequest.cpp:54