QGIS API Documentation  3.12.1-București (121cc00ff0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
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 
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 
QMap< QString, QString > toMap() const
Returns all parameters in a map.
QgsServerRequest()=default
Constructor.
void setMethod(QgsServerRequest::Method method)
Set the request method.
void setParameter(const QString &key, const QString &value)
Set a parameter.
QString header(const QString &name) const
Returns the header value.
QString parameter(const QString &key, const QString &defaultValue=QString()) const
Gets a parameter value.
QString value(const QString &key) const
Returns the value of a parameter.
void remove(const QString &key)
Removes a parameter.
void load(const QUrlQuery &query)
Loads new parameters.
void setUrl(const QUrl &url)
Set the request url.
static QString methodToString(const Method &method)
Returns a string representation of an HTTP request method.
Method
HTTP Method (or equivalent) used for the request.
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...
QMap< QString, QString > Headers
virtual QByteArray data() const
Returns post/put data Check for QByteArray::isNull() to check if data is available.
QMap< QString, QString > headers() const
Returns the header map.
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
void setHeader(const QString &name, const QString &value)
Set an header.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
void add(const QString &key, const QString &value)
Adds a parameter.
void removeHeader(const QString &name)
Remove an header.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
QgsServerParameters serverParameters() const
Returns parameters.
void removeParameter(const QString &key)
Remove a parameter.
QgsServerRequest::Method method() const
void setOriginalUrl(const QUrl &url)
Set the request original url (the request url as seen by the web server)
void clear()
Removes all 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...