QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 }
@ 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
Constructor.
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.