QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsfcgiserverrequest.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfcgiserverrequest.cpp
3
4 Define response wrapper for fcgi request
5 -------------------
6 begin : 2017-01-03
7 copyright : (C) 2017 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
21
22#include "qgis.h"
23#include "qgsmessagelog.h"
24#include "qgsserverlogger.h"
25#include "qgsstringutils.h"
26
27#include <QDebug>
28#include <QString>
29
30#include <fcgi_stdio.h>
31
32using namespace Qt::StringLiterals;
33
35{
36 // Get the REQUEST_URI from the environment
37 QString uri = getenv( "REQUEST_URI" );
38
39 if ( uri.isEmpty() )
40 {
41 uri = getenv( "SCRIPT_NAME" );
42 }
43
44 QUrl url;
45 url.setUrl( uri );
46 fillUrl( url );
47 // Store the URL before the server rewrite that could have been set in QUERY_STRING
49
50 const QString qs = getenv( "QUERY_STRING" );
51 const QString questionMark = qs.isEmpty() ? QString() : QChar( '?' );
52 const QString extraPath = u"%1%2%3"_s.arg( getenv( "PATH_INFO" ) ).arg( questionMark ).arg( qs );
53
54 QUrl baseUrl;
55 if ( uri.endsWith( extraPath ) )
56 {
57 baseUrl.setUrl( uri.left( uri.length() - extraPath.length() ) );
58 }
59 else
60 {
61 baseUrl.setUrl( uri );
62 }
63 fillUrl( baseUrl );
64 setBaseUrl( url );
65
66 // OGC parameters are passed with the query string, which is normally part of
67 // the REQUEST_URI, we override the query string url in case it is defined
68 // independently of REQUEST_URI
69 if ( !qs.isEmpty() )
70 {
71 url.setQuery( qs );
72 }
73
74#ifdef QGISDEBUG
75 qDebug() << "fcgi query string: " << url.query();
76#endif
77
79
80 // Get method
81 const char *me = getenv( "REQUEST_METHOD" );
82
83 if ( me )
84 {
85 if ( strcmp( me, "POST" ) == 0 )
86 {
88 }
89 else if ( strcmp( me, "PUT" ) == 0 )
90 {
92 }
93 else if ( strcmp( me, "DELETE" ) == 0 )
94 {
96 }
97 else if ( strcmp( me, "HEAD" ) == 0 )
98 {
100 }
101 else if ( strcmp( me, "PATCH" ) == 0 )
102 {
104 }
105 }
106
107 if ( method == PostMethod || method == PutMethod || method == PatchMethod )
108 {
109 // Get post/put/patch data
110 readData();
111 }
112
113 setUrl( url );
114 setMethod( method );
115
116 // Fill the headers dictionary
117 for ( const auto &headerKey : qgsEnumMap<QgsServerRequest::RequestHeader>() )
118 {
119 const QString headerName = QgsStringUtils::capitalize( QString( headerKey ).replace( '_'_L1, ' '_L1 ), Qgis::Capitalization::TitleCase ).replace( ' '_L1, '-'_L1 );
120 const char *result = getenv( u"HTTP_%1"_s.arg( headerKey ).toStdString().c_str() );
121 if ( result && strlen( result ) > 0 )
122 {
123 setHeader( headerName, result );
124 }
125 }
126
127 // Output debug infos
129 if ( logLevel <= Qgis::MessageLevel::Info )
130 {
131 printRequestInfos( url );
132 }
133}
134
135void QgsFcgiServerRequest::fillUrl( QUrl &url ) const
136{
137 // Check if host is defined
138 if ( url.host().isEmpty() )
139 {
140 url.setHost( getenv( "SERVER_NAME" ) );
141 }
142
143 // Port ?
144 if ( url.port( -1 ) == -1 )
145 {
146 const QString portString = getenv( "SERVER_PORT" );
147 if ( !portString.isEmpty() )
148 {
149 bool portOk;
150 const int portNumber = portString.toInt( &portOk );
151 if ( portOk && portNumber != 80 )
152 {
153 url.setPort( portNumber );
154 }
155 }
156 }
157
158 // scheme
159 if ( url.scheme().isEmpty() )
160 {
161 QString( getenv( "HTTPS" ) ).compare( "on"_L1, Qt::CaseInsensitive ) == 0 ? url.setScheme( u"https"_s ) : url.setScheme( u"http"_s );
162 }
163}
164
166{
167 return mData;
168}
169
170// Read post put data
171void QgsFcgiServerRequest::readData()
172{
173 // Check if we have CONTENT_LENGTH defined
174 const char *lengthstr = getenv( "CONTENT_LENGTH" );
175 if ( lengthstr )
176 {
177 bool success = false;
178 const int length = QString( lengthstr ).toInt( &success );
179 if ( !success || length < 0 )
180 {
181 QgsMessageLog::logMessage( "fcgi: Invalid CONTENT_LENGTH", u"Server"_s, Qgis::MessageLevel::Critical );
182 mHasError = true;
183 }
184 else
185 {
186 // Note: REQUEST_BODY is not part of CGI standard, and it is not
187 // normally passed by any CGI web server and it is implemented only
188 // to allow unit tests to inject a request body and simulate a POST
189 // request
190 const char *requestBody = getenv( "REQUEST_BODY" );
191
192#ifdef QGISDEBUG
193 qDebug() << "fcgi: reading " << lengthstr << " bytes from " << ( requestBody ? "REQUEST_BODY" : "stdin" );
194#endif
195
196 if ( requestBody )
197 {
198 const size_t requestBodyLength = strlen( requestBody );
199 const int actualLength = static_cast<int>( std::min<size_t>( length, requestBodyLength ) );
200 if ( static_cast<size_t>( actualLength ) < requestBodyLength )
201 {
202 QgsMessageLog::logMessage( "fcgi: CONTENT_LENGTH is larger than actual length of REQUEST_BODY", u"Server"_s, Qgis::MessageLevel::Critical );
203 mHasError = true;
204 }
205 mData = QByteArray::fromRawData( requestBody, actualLength );
206 }
207 else
208 {
209 mData.resize( length );
210 const int actualLength = static_cast<int>( fread( mData.data(), 1, length, stdin ) );
211 if ( actualLength < length )
212 {
213 mData.resize( actualLength );
214 QgsMessageLog::logMessage( "fcgi: CONTENT_LENGTH is larger than actual length of stdin", u"Server"_s, Qgis::MessageLevel::Critical );
215 mHasError = true;
216 }
217 }
218 }
219 }
220 else
221 {
222 QgsMessageLog::logMessage( "fcgi: No POST/PUT/PATCH data" );
223 }
224}
225
226void QgsFcgiServerRequest::printRequestInfos( const QUrl &url ) const
227{
228 QgsMessageLog::logMessage( u"******************** New request ***************"_s, u"Server"_s, Qgis::MessageLevel::Info );
229
230 const QStringList envVars {
231 u"SERVER_NAME"_s,
232 u"REQUEST_URI"_s,
233 u"SCRIPT_NAME"_s,
234 u"PATH_INFO"_s,
235 u"HTTPS"_s,
236 u"REMOTE_ADDR"_s,
237 u"REMOTE_HOST"_s,
238 u"SERVER_PORT"_s,
239 u"QUERY_STRING"_s,
240 u"REMOTE_USER"_s,
241 u"REMOTE_IDENT"_s,
242 u"CONTENT_TYPE"_s,
243 u"REQUEST_METHOD"_s,
244 u"AUTH_TYPE"_s,
245 u"HTTP_PROXY"_s,
246 u"NO_PROXY"_s,
247 u"QGIS_PROJECT_FILE"_s,
248 u"QGIS_SERVER_IGNORE_BAD_LAYERS"_s,
249 u"QGIS_SERVER_SERVICE_URL"_s,
250 u"QGIS_SERVER_WMS_SERVICE_URL"_s,
251 u"QGIS_SERVER_WFS_SERVICE_URL"_s,
252 u"QGIS_SERVER_WMTS_SERVICE_URL"_s,
253 u"QGIS_SERVER_WCS_SERVICE_URL"_s,
254 u"SERVER_PROTOCOL"_s
255 };
256
257 QgsMessageLog::logMessage( u"Request URL: %2"_s.arg( url.url() ), u"Server"_s, Qgis::MessageLevel::Info );
258
259 QgsMessageLog::logMessage( u"Environment:"_s, u"Server"_s, Qgis::MessageLevel::Info );
260 QgsMessageLog::logMessage( u"------------------------------------------------"_s, u"Server"_s, Qgis::MessageLevel::Info );
261 for ( const auto &envVar : envVars )
262 {
263 if ( getenv( envVar.toStdString().c_str() ) )
264 {
265 QgsMessageLog::logMessage( u"%1: %2"_s.arg( envVar ).arg( QString( getenv( envVar.toStdString().c_str() ) ) ), u"Server"_s, Qgis::MessageLevel::Info );
266 }
267 }
268
269 qDebug() << "Headers:";
270 qDebug() << "------------------------------------------------";
271 const QMap<QString, QString> &hdrs = headers();
272 for ( auto it = hdrs.constBegin(); it != hdrs.constEnd(); it++ )
273 {
274 qDebug() << it.key() << ": " << it.value();
275 }
276}
277
278QString QgsFcgiServerRequest::header( const QString &name ) const
279{
280 // Get from internal dictionary
281 QString result = QgsServerRequest::header( name );
282
283 // Or from standard environment variable
284 // https://tools.ietf.org/html/rfc3875#section-4.1.18
285 if ( result.isEmpty() )
286 {
287 result = qgetenv( u"HTTP_%1"_s.arg( name.toUpper().replace( '-'_L1, '_'_L1 ) ).toStdString().c_str() );
288 }
289 return result;
290}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:160
@ Critical
Critical/error message.
Definition qgis.h:163
@ Info
Information message.
Definition qgis.h:161
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
Definition qgis.h:3509
QString header(const QString &name) const override
Returns the header value.
QByteArray data() const override
Returns post/put data Check for QByteArray::isNull() to check if data is available.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
static QgsServerLogger * instance()
Gets the singleton instance.
Qgis::MessageLevel logLevel() const
Gets the current log level.
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.
virtual QString header(const QString &name) const
Returns the header value.
virtual void setUrl(const QUrl &url)
Set the request url.
QUrl url() const
Returns the request URL as seen by QGIS server.
QMap< QString, QString > headers() const
Returns the header map.
QUrl baseUrl() const
Returns the base URL of QGIS server.
QgsServerRequest::Method method() const
Returns the request method.
void setMethod(QgsServerRequest::Method method)
Set the request method.
void setBaseUrl(const QUrl &url)
Set the base URL of QGIS server.
void setHeader(const QString &name, const QString &value)
Set an header.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
const QMap< T, QString > qgsEnumMap()
Returns a map of all enum entries.
Definition qgis.h:7140