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