QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 )
116 .replace( QLatin1Char( ' ' ), QLatin1Char( '-' ) );
117 const char *result = getenv( QStringLiteral( "HTTP_%1" ).arg( headerKey ).toStdString().c_str() );
118 if ( result && strlen( result ) > 0 )
119 {
120 setHeader( headerName, result );
121 }
122 }
123
124 // Output debug infos
126 if ( logLevel <= Qgis::MessageLevel::Info )
127 {
128 printRequestInfos( url );
129 }
130}
131
132void QgsFcgiServerRequest::fillUrl( QUrl &url ) const
133{
134 // Check if host is defined
135 if ( url.host().isEmpty() )
136 {
137 url.setHost( getenv( "SERVER_NAME" ) );
138 }
139
140 // Port ?
141 if ( url.port( -1 ) == -1 )
142 {
143 const QString portString = getenv( "SERVER_PORT" );
144 if ( !portString.isEmpty() )
145 {
146 bool portOk;
147 const int portNumber = portString.toInt( &portOk );
148 if ( portOk && portNumber != 80 )
149 {
150 url.setPort( portNumber );
151 }
152 }
153 }
154
155 // scheme
156 if ( url.scheme().isEmpty() )
157 {
158 QString( getenv( "HTTPS" ) ).compare( QLatin1String( "on" ), Qt::CaseInsensitive ) == 0
159 ? url.setScheme( QStringLiteral( "https" ) )
160 : url.setScheme( QStringLiteral( "http" ) );
161 }
162}
163
165{
166 return mData;
167}
168
169// Read post put data
170void QgsFcgiServerRequest::readData()
171{
172 // Check if we have CONTENT_LENGTH defined
173 const char *lengthstr = getenv( "CONTENT_LENGTH" );
174 if ( lengthstr )
175 {
176 bool success = false;
177 const int length = QString( lengthstr ).toInt( &success );
178 if ( !success || length < 0 )
179 {
180 QgsMessageLog::logMessage( "fcgi: Invalid CONTENT_LENGTH", 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", QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
202 mHasError = true;
203 }
204 mData = QByteArray::fromRawData( requestBody, actualLength );
205 }
206 else
207 {
208 mData.resize( length );
209 const int actualLength = static_cast<int>( fread( mData.data(), 1, length, stdin ) );
210 if ( actualLength < length )
211 {
212 mData.resize( actualLength );
213 QgsMessageLog::logMessage( "fcgi: CONTENT_LENGTH is larger than actual length of stdin", QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
214 mHasError = true;
215 }
216 }
217 }
218 }
219 else
220 {
221 QgsMessageLog::logMessage( "fcgi: No POST data" );
222 }
223}
224
225void QgsFcgiServerRequest::printRequestInfos( const QUrl &url ) const
226{
227 QgsMessageLog::logMessage( QStringLiteral( "******************** New request ***************" ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
228
229 const QStringList envVars {
230 QStringLiteral( "SERVER_NAME" ),
231 QStringLiteral( "REQUEST_URI" ),
232 QStringLiteral( "SCRIPT_NAME" ),
233 QStringLiteral( "PATH_INFO" ),
234 QStringLiteral( "HTTPS" ),
235 QStringLiteral( "REMOTE_ADDR" ),
236 QStringLiteral( "REMOTE_HOST" ),
237 QStringLiteral( "SERVER_PORT" ),
238 QStringLiteral( "QUERY_STRING" ),
239 QStringLiteral( "REMOTE_USER" ),
240 QStringLiteral( "REMOTE_IDENT" ),
241 QStringLiteral( "CONTENT_TYPE" ),
242 QStringLiteral( "REQUEST_METHOD" ),
243 QStringLiteral( "AUTH_TYPE" ),
244 QStringLiteral( "HTTP_PROXY" ),
245 QStringLiteral( "NO_PROXY" ),
246 QStringLiteral( "QGIS_PROJECT_FILE" ),
247 QStringLiteral( "QGIS_SERVER_IGNORE_BAD_LAYERS" ),
248 QStringLiteral( "QGIS_SERVER_SERVICE_URL" ),
249 QStringLiteral( "QGIS_SERVER_WMS_SERVICE_URL" ),
250 QStringLiteral( "QGIS_SERVER_WFS_SERVICE_URL" ),
251 QStringLiteral( "QGIS_SERVER_WMTS_SERVICE_URL" ),
252 QStringLiteral( "QGIS_SERVER_WCS_SERVICE_URL" ),
253 QStringLiteral( "SERVER_PROTOCOL" )
254 };
255
256 QgsMessageLog::logMessage( QStringLiteral( "Request URL: %2" ).arg( url.url() ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
257
258 QgsMessageLog::logMessage( QStringLiteral( "Environment:" ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
259 QgsMessageLog::logMessage( QStringLiteral( "------------------------------------------------" ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
260 for ( const auto &envVar : envVars )
261 {
262 if ( getenv( envVar.toStdString().c_str() ) )
263 {
264 QgsMessageLog::logMessage( QStringLiteral( "%1: %2" ).arg( envVar ).arg( QString( getenv( envVar.toStdString().c_str() ) ) ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
265 }
266 }
267
268 qDebug() << "Headers:";
269 qDebug() << "------------------------------------------------";
270 const QMap<QString, QString> &hdrs = headers();
271 for ( auto it = hdrs.constBegin(); it != hdrs.constEnd(); it++ )
272 {
273 qDebug() << it.key() << ": " << it.value();
274 }
275}
276
277QString QgsFcgiServerRequest::header( const QString &name ) const
278{
279 // Get from internal dictionary
280 QString result = QgsServerRequest::header( name );
281
282 // Or from standard environment variable
283 // https://tools.ietf.org/html/rfc3875#section-4.1.18
284 if ( result.isEmpty() )
285 {
286 result = qgetenv( QStringLiteral( "HTTP_%1" ).arg( name.toUpper().replace( QLatin1Char( '-' ), QLatin1Char( '_' ) ) ).toStdString().c_str() );
287 }
288 return result;
289}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
@ 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.