Skip to content

Commit bb9ec39

Browse files
committedJan 9, 2019
[server] Fix URL rewrite from fcgi servers
The URL exposed in the XML documents generated by the server was wrong because instead of using the original URL (from REQUEST_URI) the rewritten query string (from QUERY_STRING) was applied to the internal mUrl variable. This patch also adds some tests for the FCGI request, that handle most common scenarios with bot rewritten and not rewritten URLs. QgsFcgiServerRequest is now exposed to Python mainly for testability purposes.
1 parent 2c8acf1 commit bb9ec39

File tree

6 files changed

+114
-54
lines changed

6 files changed

+114
-54
lines changed
 
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/server/qgsfcgiserverrequest.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
13+
class QgsFcgiServerRequest: QgsServerRequest
14+
{
15+
%Docstring
16+
Class defining fcgi request
17+
18+
.. versionadded:: 3.0
19+
%End
20+
21+
%TypeHeaderCode
22+
#include "qgsfcgiserverrequest.h"
23+
%End
24+
public:
25+
QgsFcgiServerRequest();
26+
27+
virtual QByteArray data() const;
28+
29+
30+
bool hasError() const;
31+
%Docstring
32+
Returns true if an error occurred during initialization
33+
%End
34+
35+
virtual QUrl url() const;
36+
37+
%Docstring
38+
39+
:return: the request url
40+
41+
Subclasses may override in case the original URL needs to be
42+
returned instead of the rewritten one (i.e. from a web server
43+
rewrite module).
44+
%End
45+
46+
47+
};
48+
49+
/************************************************************************
50+
* This file has been generated automatically from *
51+
* *
52+
* src/server/qgsfcgiserverrequest.h *
53+
* *
54+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
55+
************************************************************************/

‎python/server/auto_generated/qgsserverrequest.sip.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ Constructor
5656

5757
virtual ~QgsServerRequest();
5858

59-
QUrl url() const;
59+
virtual QUrl url() const;
6060
%Docstring
6161

6262
:return: the request url
63+
64+
Subclasses may override in case the original URL needs to be
65+
returned instead of the rewritten one (i.e. from a web server
66+
rewrite module).
6367
%End
6468

6569
QgsServerRequest::Method method() const;

‎python/server/server_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
%Include auto_generated/qgsserverparameters.sip
99
%Include auto_generated/qgsbufferserverrequest.sip
1010
%Include auto_generated/qgsbufferserverresponse.sip
11+
%Include auto_generated/qgsfcgiserverrequest.sip
1112
%Include auto_generated/qgsrequesthandler.sip
1213
%Include auto_generated/qgsserver.sip
1314
%Include auto_generated/qgsserverexception.sip

‎src/server/qgsfcgiserverrequest.cpp

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@
2929

3030
QgsFcgiServerRequest::QgsFcgiServerRequest()
3131
{
32-
mHasError = false;
33-
34-
// Rebuild the full URL
3532

3633
// Get the REQUEST_URI from the environment
3734
QUrl url;
3835
QString uri = getenv( "REQUEST_URI" );
36+
3937
if ( uri.isEmpty() )
4038
{
4139
uri = getenv( "SCRIPT_NAME" );
@@ -72,9 +70,12 @@ QgsFcgiServerRequest::QgsFcgiServerRequest()
7270
: url.setScheme( QStringLiteral( "http" ) );
7371
}
7472

75-
// XXX OGC paremetrs are passed with the query string
76-
// we override the query string url in case it is
77-
// defined independently of REQUEST_URI
73+
// Store the URL before the server rewrite that could have been set in QUERY_STRING
74+
mOriginalUrl = url;
75+
76+
// OGC parametrs are passed with the query string, which is normally part of
77+
// the REQUEST_URI, we override the query string url in case it is defined
78+
// independently of REQUEST_URI
7879
const char *qs = getenv( "QUERY_STRING" );
7980
if ( qs )
8081
{
@@ -132,6 +133,11 @@ QByteArray QgsFcgiServerRequest::data() const
132133
return mData;
133134
}
134135

136+
QUrl QgsFcgiServerRequest::url() const
137+
{
138+
return mOriginalUrl;
139+
}
140+
135141
// Read post put data
136142
void QgsFcgiServerRequest::readData()
137143
{
@@ -168,48 +174,28 @@ void QgsFcgiServerRequest::readData()
168174
void QgsFcgiServerRequest::printRequestInfos()
169175
{
170176
QgsMessageLog::logMessage( QStringLiteral( "******************** New request ***************" ), QStringLiteral( "Server" ), Qgis::Info );
171-
if ( getenv( "REMOTE_ADDR" ) )
172-
{
173-
QgsMessageLog::logMessage( "REMOTE_ADDR: " + QString( getenv( "REMOTE_ADDR" ) ), QStringLiteral( "Server" ), Qgis::Info );
174-
}
175-
if ( getenv( "REMOTE_HOST" ) )
176-
{
177-
QgsMessageLog::logMessage( "REMOTE_HOST: " + QString( getenv( "REMOTE_HOST" ) ), QStringLiteral( "Server" ), Qgis::Info );
178-
}
179-
if ( getenv( "REMOTE_USER" ) )
180-
{
181-
QgsMessageLog::logMessage( "REMOTE_USER: " + QString( getenv( "REMOTE_USER" ) ), QStringLiteral( "Server" ), Qgis::Info );
182-
}
183-
if ( getenv( "REMOTE_IDENT" ) )
184-
{
185-
QgsMessageLog::logMessage( "REMOTE_IDENT: " + QString( getenv( "REMOTE_IDENT" ) ), QStringLiteral( "Server" ), Qgis::Info );
186-
}
187-
if ( getenv( "CONTENT_TYPE" ) )
188-
{
189-
QgsMessageLog::logMessage( "CONTENT_TYPE: " + QString( getenv( "CONTENT_TYPE" ) ), QStringLiteral( "Server" ), Qgis::Info );
190-
}
191-
if ( getenv( "AUTH_TYPE" ) )
192-
{
193-
QgsMessageLog::logMessage( "AUTH_TYPE: " + QString( getenv( "AUTH_TYPE" ) ), QStringLiteral( "Server" ), Qgis::Info );
194-
}
195-
if ( getenv( "HTTP_USER_AGENT" ) )
196-
{
197-
QgsMessageLog::logMessage( "HTTP_USER_AGENT: " + QString( getenv( "HTTP_USER_AGENT" ) ), QStringLiteral( "Server" ), Qgis::Info );
198-
}
199-
if ( getenv( "HTTP_PROXY" ) )
200-
{
201-
QgsMessageLog::logMessage( "HTTP_PROXY: " + QString( getenv( "HTTP_PROXY" ) ), QStringLiteral( "Server" ), Qgis::Info );
202-
}
203-
if ( getenv( "HTTPS_PROXY" ) )
204-
{
205-
QgsMessageLog::logMessage( "HTTPS_PROXY: " + QString( getenv( "HTTPS_PROXY" ) ), QStringLiteral( "Server" ), Qgis::Info );
206-
}
207-
if ( getenv( "NO_PROXY" ) )
208-
{
209-
QgsMessageLog::logMessage( "NO_PROXY: " + QString( getenv( "NO_PROXY" ) ), QStringLiteral( "Server" ), Qgis::Info );
210-
}
211-
if ( getenv( "HTTP_AUTHORIZATION" ) )
212-
{
213-
QgsMessageLog::logMessage( "HTTP_AUTHORIZATION: " + QString( getenv( "HTTP_AUTHORIZATION" ) ), QStringLiteral( "Server" ), Qgis::Info );
177+
178+
const QStringList envVars
179+
{
180+
QStringLiteral( "SERVER_NAME" ),
181+
QStringLiteral( "REQUEST_URI" ),
182+
QStringLiteral( "REMOTE_ADDR" ),
183+
QStringLiteral( "REMOTE_HOST" ),
184+
QStringLiteral( "REMOTE_USER" ),
185+
QStringLiteral( "REMOTE_IDENT" ),
186+
QStringLiteral( "CONTENT_TYPE" ),
187+
QStringLiteral( "AUTH_TYPE" ),
188+
QStringLiteral( "HTTP_USER_AGENT" ),
189+
QStringLiteral( "HTTP_PROXY" ),
190+
QStringLiteral( "NO_PROXY" ),
191+
QStringLiteral( "HTTP_AUTHORIZATION" )
192+
};
193+
194+
for ( const auto &envVar : envVars )
195+
{
196+
if ( getenv( envVar.toStdString().c_str() ) )
197+
{
198+
QgsMessageLog::logMessage( envVar + QString( getenv( envVar.toStdString().c_str() ) ), QStringLiteral( "Server" ), Qgis::Info );
199+
}
214200
}
215201
}

‎src/server/qgsfcgiserverrequest.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#ifndef QGSFCGISERVERREQUEST_H
2020
#define QGSFCGISERVERREQUEST_H
2121

22-
#define SIP_NO_FILE
23-
2422

2523
#include "qgsserverrequest.h"
2624

@@ -44,6 +42,16 @@ class SERVER_EXPORT QgsFcgiServerRequest: public QgsServerRequest
4442
*/
4543
bool hasError() const { return mHasError; }
4644

45+
/**
46+
* \returns the request url
47+
*
48+
* Subclasses may override in case the original URL needs to be
49+
* returned instead of the rewritten one (i.e. from a web server
50+
* rewrite module).
51+
*/
52+
QUrl url() const override;
53+
54+
4755
private:
4856
void readData();
4957

@@ -53,7 +61,9 @@ class SERVER_EXPORT QgsFcgiServerRequest: public QgsServerRequest
5361

5462

5563
QByteArray mData;
56-
bool mHasError;
64+
bool mHasError = false;
65+
//! Url before the server rewrite
66+
QUrl mOriginalUrl;
5767
};
5868

5969
#endif

‎src/server/qgsserverrequest.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ class SERVER_EXPORT QgsServerRequest
8383

8484
/**
8585
* \returns the request url
86+
*
87+
* Subclasses may override in case the original URL needs to be
88+
* returned instead of the rewritten one (i.e. from a web server
89+
* rewrite module).
8690
*/
87-
QUrl url() const;
91+
virtual QUrl url() const;
8892

8993
/**
9094
* \returns the request method

0 commit comments

Comments
 (0)
Please sign in to comment.