Skip to content
Permalink
Browse files
Fix the mRequestHeaderConv initialization on FCGI request
  • Loading branch information
sbrunner committed Jan 6, 2022
1 parent a0e4529 commit 8c6251f77a7b7ac4cd4a143fe782aaa6cbf6e4a3
Showing with 65 additions and 39 deletions.
  1. +27 −28 src/server/qgsfcgiserverrequest.cpp
  2. +20 −10 src/server/qgsserverrequest.cpp
  3. +8 −1 src/server/qgsserverrequest.h
  4. +10 −0 tests/src/python/test_qgsserver_request.py
@@ -106,11 +106,31 @@ QgsFcgiServerRequest::QgsFcgiServerRequest()
setUrl( url );
setMethod( method );

// Get accept header for content-type negotiation
const char *accept = getenv( "HTTP_ACCEPT" );
if ( accept )
// Fill the headers dictionary
const QStringList headers
{
QStringLiteral( "Accept" ),
QStringLiteral( "User-Agent" ),
QStringLiteral( "Proxy" ),
QStringLiteral( "Authorization" ),
QStringLiteral( "X-Qgis-Service-Url" ),
QStringLiteral( "X-Qgis-WMS-Service-Url" ),
QStringLiteral( "X-Qgis-WFS-Service-Url" ),
QStringLiteral( "X-Qgis-WCS-Service-Url" ),
QStringLiteral( "X-Qgis-WMTS-Service-Url" ),
QStringLiteral( "Forwarded" ),
QStringLiteral( "X-Forwarded-Host" ),
QStringLiteral( "X-Forwarded-Proto" ),
QStringLiteral( "Host" )
};
for ( const auto &headerName : headers )
{
setHeader( QStringLiteral( "Accept" ), accept );
const char *result = qgetenv( QStringLiteral( "HTTP_%1" ).arg(
headerName.toUpper().replace( QLatin1Char( '-' ), QLatin1Char( '_' ) ) ).toStdString().c_str() );
if ( result )
{
setHeader( headerName, result );
}
}

// Output debug infos
@@ -233,23 +253,6 @@ void QgsFcgiServerRequest::printRequestInfos( const QUrl &url )
QStringLiteral( "QGIS_SERVER_WCS_SERVICE_URL" ),
QStringLiteral( "SERVER_PROTOCOL" )
};
const QStringList headers
{
QStringLiteral( "Accept" ),
QStringLiteral( "User-Agent" ),
QStringLiteral( "Proxy" ),
QStringLiteral( "Authorization" ),
QStringLiteral( "X-Qgis-Service-Url" ),
QStringLiteral( "X-Qgis-WMS-Service-Url" ),
QStringLiteral( "X-Qgis-WFS-Service-Url" ),
QStringLiteral( "X-Qgis-WCS-Service-Url" ),
QStringLiteral( "X-Qgis-WMTS-Service-Url" ),
QStringLiteral( "Forwarded" ),
QStringLiteral( "X-Forwarded-Host" ),
QStringLiteral( "X-Forwarded-Proto" ),
QStringLiteral( "Host" )
};


QgsMessageLog::logMessage( QStringLiteral( "Request URL: %2" ).arg( url.url() ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );

@@ -265,14 +268,10 @@ void QgsFcgiServerRequest::printRequestInfos( const QUrl &url )

QgsMessageLog::logMessage( QStringLiteral( "Headers:" ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
QgsMessageLog::logMessage( QStringLiteral( "------------------------------------------------" ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
for ( const auto &headerName : headers )

for ( const auto &headerName : headers().keys() )
{
if ( !header( headerName ).isEmpty() )
{
QgsMessageLog::logMessage( QStringLiteral( "%1: %2" ).arg( headerName, header( headerName ) ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
// Fill the headers dictionary
setHeader( headerName, header( headerName ) );
}
QgsMessageLog::logMessage( QStringLiteral( "%1: %2" ).arg( headerName ).arg( headers().value( headerName ) ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
}
}

@@ -21,6 +21,11 @@
#include <QUrlQuery>


QgsServerRequest::QgsServerRequest( )
{
init();
}

QgsServerRequest::QgsServerRequest( const QString &url, Method method, const Headers &headers )
: QgsServerRequest( QUrl( url ), method, headers )
{
@@ -34,16 +39,7 @@ QgsServerRequest::QgsServerRequest( const QUrl &url, Method method, const Header
, mHeaders( headers )
, mRequestHeaderConv()
{
mRequestHeaderConv.insert( HOST, QStringLiteral( "Host" ) );
mRequestHeaderConv.insert( FORWARDED, QStringLiteral( "Forwarded" ) );
mRequestHeaderConv.insert( X_FORWARDED_FOR, QStringLiteral( "X-Forwarded-For" ) );
mRequestHeaderConv.insert( X_FORWARDED_HOST, QStringLiteral( "X-Forwarded-Host" ) );
mRequestHeaderConv.insert( X_FORWARDED_PROTO, QStringLiteral( "X-Forwarded-Proto" ) );
mRequestHeaderConv.insert( X_QGIS_SERVICE_URL, QStringLiteral( "X-Qgis-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WMS_SERVICE_URL, QStringLiteral( "X-Qgis-Wms-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WFS_SERVICE_URL, QStringLiteral( "X-Qgis-Wfs-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WCS_SERVICE_URL, QStringLiteral( "X-Qgis-Wcs-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WMTS_SERVICE_URL, QStringLiteral( "X-Qgis-Wmts-Service-Url" ) );
init();

mParams.load( QUrlQuery( url ) );
}
@@ -59,6 +55,20 @@ QgsServerRequest::QgsServerRequest( const QgsServerRequest &other )
{
}

void QgsServerRequest::init( )
{
mRequestHeaderConv.insert( HOST, QStringLiteral( "Host" ) );
mRequestHeaderConv.insert( FORWARDED, QStringLiteral( "Forwarded" ) );
mRequestHeaderConv.insert( X_FORWARDED_FOR, QStringLiteral( "X-Forwarded-For" ) );
mRequestHeaderConv.insert( X_FORWARDED_HOST, QStringLiteral( "X-Forwarded-Host" ) );
mRequestHeaderConv.insert( X_FORWARDED_PROTO, QStringLiteral( "X-Forwarded-Proto" ) );
mRequestHeaderConv.insert( X_QGIS_SERVICE_URL, QStringLiteral( "X-Qgis-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WMS_SERVICE_URL, QStringLiteral( "X-Qgis-Wms-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WFS_SERVICE_URL, QStringLiteral( "X-Qgis-Wfs-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WCS_SERVICE_URL, QStringLiteral( "X-Qgis-Wcs-Service-Url" ) );
mRequestHeaderConv.insert( X_QGIS_WMTS_SERVICE_URL, QStringLiteral( "X-Qgis-Wmts-Service-Url" ) );
}

QString QgsServerRequest::methodToString( const QgsServerRequest::Method &method )
{
static const QMetaEnum metaEnum = QMetaEnum::fromType<QgsServerRequest::Method>();
@@ -90,7 +90,7 @@ class SERVER_EXPORT QgsServerRequest
/**
* Constructor
*/
QgsServerRequest() = default;
QgsServerRequest();

/**
* Constructor
@@ -260,6 +260,13 @@ class SERVER_EXPORT QgsServerRequest
void setBaseUrl( const QUrl &url );

private:

/**
* Initialize the map used to convert the header enumeration value
* into header name.
*/
void init();

// Url as seen by QGIS server after web server rewrite
QUrl mUrl;
// Unrewritten url as seen by the web server
@@ -215,6 +215,16 @@ def test_add_parameters(self):
self.assertEqual(request.parameter('FOOBAR'), 'foobar')
self.assertEqual(request.parameter('UNKNOWN'), '')

def test_headers(self):
"""Tests that the headers are working in Fcgi mode"""
os.environ["HTTP_HOST"] = "example.com"
request = QgsFcgiServerRequest()
self.assertEquals(request.header("Host"), "example.com")
request = QgsServerRequest(request)
self.assertEquals(request.header("Host"), "example.com")
self.assertEquals(request.header(QgsServerRequest.HOST), "example.com")
del os.environ["HTTP_HOST"]


if __name__ == '__main__':
unittest.main()

0 comments on commit 8c6251f

Please sign in to comment.