Skip to content
Permalink
Browse files
Use QMetaEnum
  • Loading branch information
sbrunner authored and github-actions committed Jan 13, 2022
1 parent f57f90b commit 24af658f44fcb12375a942ebb4f487f9c2a61541
@@ -57,6 +57,12 @@ class QgsServerRequest
X_QGIS_WCS_SERVICE_URL,
// The QGIS WMTS service URL
X_QGIS_WMTS_SERVICE_URL,
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
ACCEPT,
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
USER_AGENT,
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
AUTHORIZATION,
};

QgsServerRequest();
@@ -21,6 +21,7 @@
#include "qgsfcgiserverrequest.h"
#include "qgsserverlogger.h"
#include "qgsmessagelog.h"
#include "qgsstringutils.h"
#include <fcgi_stdio.h>
#include <QDebug>

@@ -107,27 +108,13 @@ QgsFcgiServerRequest::QgsFcgiServerRequest()
setMethod( method );

// Fill the headers dictionary
const QStringList headers
for ( const auto &headerKey : qgsEnumMap<QgsServerRequest::RequestHeader>().values() )
{
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 )
{
const char *result = qgetenv( QStringLiteral( "HTTP_%1" ).arg(
headerName.toUpper().replace( QLatin1Char( '-' ), QLatin1Char( '_' ) ) ).toStdString().c_str() );
if ( result )
const QString headerName = QgsStringUtils::capitalize(
QString( headerKey ).replace( QLatin1Char( '_' ), QLatin1Char( ' ' ) ), Qgis::Capitalization::TitleCase
).replace( QLatin1Char( ' ' ), QLatin1Char( '-' ) );
const char *result = getenv( QStringLiteral( "HTTP_%1" ).arg( headerKey ).toStdString().c_str() );
if ( result && strlen( result ) > 0 )
{
setHeader( headerName, result );
}
@@ -243,6 +230,7 @@ void QgsFcgiServerRequest::printRequestInfos( const QUrl &url )
QStringLiteral( "CONTENT_TYPE" ),
QStringLiteral( "REQUEST_METHOD" ),
QStringLiteral( "AUTH_TYPE" ),
QStringLiteral( "HTTP_PROXY" ),
QStringLiteral( "NO_PROXY" ),
QStringLiteral( "QGIS_PROJECT_FILE" ),
QStringLiteral( "QGIS_SERVER_IGNORE_BAD_LAYERS" ),
@@ -268,7 +256,6 @@ 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().keys() )
{
QgsMessageLog::logMessage( QStringLiteral( "%1: %2" ).arg( headerName ).arg( headers().value( headerName ) ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
@@ -355,7 +355,7 @@ QString QgsServerProjectUtils::serviceUrl( const QString &service, const QgsServ
QString proto;
QString host;

QString forwarded = request.header( QgsServerRequest::FORWARDED );
QString forwarded = request.header( QgsServerRequest::FORWARDED );
if ( ! forwarded.isEmpty() )
{
forwarded = forwarded.split( QLatin1Char( ',' ) )[0];
@@ -18,14 +18,10 @@
***************************************************************************/

#include "qgsserverrequest.h"
#include "qgsstringutils.h"
#include <QUrlQuery>


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

QgsServerRequest::QgsServerRequest( const QString &url, Method method, const Headers &headers )
: QgsServerRequest( QUrl( url ), method, headers )
{
@@ -37,10 +33,7 @@ QgsServerRequest::QgsServerRequest( const QUrl &url, Method method, const Header
, mBaseUrl( url )
, mMethod( method )
, mHeaders( headers )
, mRequestHeaderConv()
{
init();

mParams.load( QUrlQuery( url ) );
}

@@ -51,22 +44,7 @@ QgsServerRequest::QgsServerRequest( const QgsServerRequest &other )
, mMethod( other.mMethod )
, mHeaders( other.mHeaders )
, mParams( other.mParams )
, mRequestHeaderConv( other.mRequestHeaderConv )
{
}

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 )
@@ -83,7 +61,11 @@ QString QgsServerRequest::header( const QString &name ) const

QString QgsServerRequest::header( const QgsServerRequest::RequestHeader &headerEnum ) const
{
return header( mRequestHeaderConv[ headerEnum ] );
const QString headerKey = QString( qgsEnumValueToKey<QgsServerRequest::RequestHeader>( headerEnum ) );
const QString headerName = QgsStringUtils::capitalize(
QString( headerKey ).replace( QLatin1Char( '_' ), QLatin1Char( ' ' ) ), Qgis::Capitalization::TitleCase
).replace( QLatin1Char( ' ' ), QLatin1Char( '-' ) );
return header( headerName );
}

void QgsServerRequest::setHeader( const QString &name, const QString &value )
@@ -84,13 +84,19 @@ class SERVER_EXPORT QgsServerRequest
X_QGIS_WCS_SERVICE_URL,
// The QGIS WMTS service URL
X_QGIS_WMTS_SERVICE_URL,
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
ACCEPT,
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
USER_AGENT,
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
AUTHORIZATION,
};
Q_ENUM( RequestHeader )

/**
* Constructor
*/
QgsServerRequest();
QgsServerRequest() = default;

/**
* Constructor
@@ -260,13 +266,6 @@ 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
@@ -277,7 +276,6 @@ class SERVER_EXPORT QgsServerRequest
// to support lazy initialization
mutable Headers mHeaders;
QgsServerParameters mParams;
QMap<RequestHeader, QString> mRequestHeaderConv;
};

#endif
@@ -217,13 +217,30 @@ def test_add_parameters(self):

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"]
for header, env, enum, value in (
("Host", "HTTP_HOST", QgsServerRequest.HOST, "example.com"),
("Forwarded", "HTTP_FORWARDED", QgsServerRequest.FORWARDED, "aaa"),
("X-Forwarded-For", "HTTP_X_FORWARDED_FOR", QgsServerRequest.X_FORWARDED_FOR, "bbb"),
("X-Forwarded-Host", "HTTP_X_FORWARDED_HOST", QgsServerRequest.X_FORWARDED_HOST, "ccc"),
("X-Forwarded-Proto", "HTTP_X_FORWARDED_PROTO", QgsServerRequest.X_FORWARDED_PROTO, "ddd"),
("X-Qgis-Service-Url", "HTTP_X_QGIS_SERVICE_URL", QgsServerRequest.X_QGIS_SERVICE_URL, "eee"),
("X-Qgis-Wms-Service-Url", "HTTP_X_QGIS_WMS_SERVICE_URL", QgsServerRequest.X_QGIS_WMS_SERVICE_URL, "fff"),
("X-Qgis-Wfs-Service-Url", "HTTP_X_QGIS_WFS_SERVICE_URL", QgsServerRequest.X_QGIS_WFS_SERVICE_URL, "ggg"),
("X-Qgis-Wcs-Service-Url", "HTTP_X_QGIS_WCS_SERVICE_URL", QgsServerRequest.X_QGIS_WCS_SERVICE_URL, "hhh"),
("X-Qgis-Wmts-Service-Url", "HTTP_X_QGIS_WMTS_SERVICE_URL", QgsServerRequest.X_QGIS_WMTS_SERVICE_URL, "iii"),
("Accept", "HTTP_ACCEPT", QgsServerRequest.ACCEPT, "jjj"),
("User-Agent", "HTTP_USER_AGENT", QgsServerRequest.USER_AGENT, "kkk"),
("Authorization", "HTTP_AUTHORIZATION", QgsServerRequest.AUTHORIZATION, "lll"),
):
try:
os.environ[env] = value
request = QgsFcgiServerRequest()
self.assertEquals(request.headers(), {header: value})
request = QgsServerRequest(request)
self.assertEquals(request.headers(), {header: value})
self.assertEquals(request.header(enum), value)
finally:
del os.environ[env]


if __name__ == '__main__':

0 comments on commit 24af658

Please sign in to comment.