QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
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.
QString header(const QString &name) const
Returns the header value.
QString parameter(const QString &key, const QString &defaultValue=QString()) const
Gets a parameter value.
void setParameter(const QString &key, const QString &value)
Set a parameter.
QgsServerRequest()=default
Constructor.
void setUrl(const QUrl &url)
Set the request url.
QMap< QString, QString > headers() const
Returns the header map.
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
void removeParameter(const QString &key)
Remove a parameter.
void setHeader(const QString &name, const QString &value)
Set an header.
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.