Skip to content
Permalink
Browse files

Update server requests

  • Loading branch information
pblottiere committed Jun 28, 2018
1 parent d1a4c0a commit bb26e71cb22153a80e1b6ef1f6a63909151930bc
@@ -303,8 +303,8 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
{
try
{
const QgsServerParameters params( request.parameters() );
printRequestParameters( request.parameters(), logLevel );
const QgsServerParameters params = request.serverParameters();
printRequestParameters( params.toMap(), logLevel );

//Config file path
if ( ! project )
@@ -56,17 +56,52 @@ QgsServerParameters::QgsServerParameters()
save( pFile );
}

QgsServerParameters::QgsServerParameters( const QgsServerRequest::Parameters &parameters )
QgsServerParameters::QgsServerParameters( const QUrlQuery &query )
: QgsServerParameters()
{
load( parameters );
load( query );
}

void QgsServerParameters::save( const Parameter &parameter )
{
mParameters[ parameter.mName ] = parameter;
}

void QgsServerParameters::add( const QString &key, const QString &value )
{
QUrlQuery query;
query.addQueryItem( key, value );
load( query );
}

QUrlQuery QgsServerParameters::urlQuery() const
{
QUrlQuery query;

for ( auto param : toMap().toStdMap() )
{
query.addQueryItem( param.first, param.second );
}

return query;
}

void QgsServerParameters::remove( const QString &key )
{
if ( mUnmanagedParameters.contains( key ) )
{
mUnmanagedParameters.take( key );
}
else
{
ParameterName paramName = name( key );
if ( mParameters.contains( paramName ) )
{
mParameters.take( paramName );
}
}
}

QString QgsServerParameters::map() const
{
return value( ParameterName::MAP ).toString();
@@ -99,46 +134,78 @@ QString QgsServerParameters::service() const
return serviceValue;
}

QMap<QString, QString> QgsServerParameters::toMap() const
{
QMap<QString, QString> params = mUnmanagedParameters;

const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );

for ( auto parameter : mParameters.toStdMap() )
{
const QString name = metaEnum.valueToKey( parameter.first );
params[name] = parameter.second.mValue.toString();
}

return params;
}

QString QgsServerParameters::request() const
{
return value( ParameterName::REQUEST ).toString();
}

QString QgsServerParameters::value( const QString &key ) const
{
return value( name( key ) ).toString();
}

QVariant QgsServerParameters::value( ParameterName name ) const
{
return mParameters[name].mValue;
}

void QgsServerParameters::load( const QgsServerRequest::Parameters &parameters )
void QgsServerParameters::load( const QUrlQuery &query )
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );

for ( const QString &key : parameters.keys() )
for ( const auto &item : query.queryItems() )
{
const ParameterName name = ( ParameterName ) metaEnum.keyToValue( key.toStdString().c_str() );
if ( name >= 0 )
const ParameterName paramName = name( item.first );
if ( paramName >= 0 )
{
const QVariant value( parameters[key] );
mParameters[name].mValue = value;
const QVariant value( item.second );
mParameters[paramName].mValue = value;

if ( !value.canConvert( mParameters[name].mType ) )
if ( !value.canConvert( mParameters[paramName].mType ) )
{
raiseError( name );
raiseError( paramName );
}
}
else
{
mUnmanagedParameters[key] = parameters[key];
mUnmanagedParameters[item.first.toUpper()] = item.second;
}
}
}

void QgsServerParameters::clear()
{
mParameters.clear();
mUnmanagedParameters.clear();
}

QString QgsServerParameters::name( ParameterName name ) const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
return metaEnum.valueToKey( name );
}

QgsServerParameters::ParameterName QgsServerParameters::name( const QString &key ) const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
return ( ParameterName ) metaEnum.keyToValue( key.toUpper().toStdString().c_str() );
}

void QgsServerParameters::raiseError( ParameterName paramName ) const
{
const QString value = mParameters[paramName].mValue.toString();
@@ -21,15 +21,15 @@
#include <QMap>
#include <QObject>
#include <QMetaEnum>

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

/**
* QgsServerParameters provides an interface to retrieve and manipulate
* global parameters received from the client.
* \since QGIS 3.4
*/
class QgsServerParameters
class SERVER_EXPORT QgsServerParameters
{
Q_GADGET

@@ -59,15 +59,48 @@ class QgsServerParameters

/**
* Constructor.
* \param map of parameters where keys are parameters' names.
*/
QgsServerParameters( const QgsServerRequest::Parameters &parameters );
QgsServerParameters( const QUrlQuery &query );

/**
* Loads new parameters.
* \param map of parameters
* \param query url query
*/
void load( const QUrlQuery &query );

/**
* Removes all parameters.
*/
void clear();

/**
* Adds a parameter.
* \param key the name of the parameter
* \param value the value of the parameter
*/
void add( const QString &key, const QString &value );

/**
* Removes a parameter.
* \param key the name of the parameter
*/
void load( const QgsServerRequest::Parameters &parameters );
void remove( const QString &key );

/**
* Returns the value of a parameter.
* \param key the name of the parameter
*/
QString value( const QString &key ) const;

/**
* Returns a url query with underlying parameters.
*/
QUrlQuery urlQuery() const;

/**
* Returns all parameters in a map.
*/
QMap<QString, QString> toMap() const;

/**
* Returns SERVICE parameter as a string or an empty string if not
@@ -107,6 +140,8 @@ class QgsServerParameters
private:
void save( const Parameter &parameter );
QVariant value( ParameterName name ) const;

ParameterName name( const QString &name ) const;
QString name( ParameterName name ) const;

void raiseError( ParameterName name ) const;
@@ -21,19 +21,16 @@
#include <QUrlQuery>

QgsServerRequest::QgsServerRequest( const QString &url, Method method, const Headers &headers )
: mUrl( url )
, mMethod( method )
, mHeaders( headers )
: QgsServerRequest( QUrl( url ), method, headers )
{

}

QgsServerRequest::QgsServerRequest( const QUrl &url, Method method, const Headers &headers )
: mUrl( url )
, mMethod( method )
, mHeaders( headers )
{

mParams.load( QUrlQuery( url ) );
}

QString QgsServerRequest::header( const QString &name ) const
@@ -70,21 +67,11 @@ QgsServerRequest::Method QgsServerRequest::method() const

QMap<QString, QString> QgsServerRequest::parameters() const
{
// Lazy build of the parameter map
if ( !mDecoded && mUrl.hasQuery() )
{
typedef QPair<QString, QString> pair_t;

QUrlQuery query( mUrl );
query.setQuery( query.query().replace( '+', QStringLiteral( "%20" ) ) );

QList<pair_t> items = query.queryItems( QUrl::FullyDecoded );
Q_FOREACH ( const pair_t &pair, items )
{
mParams.insert( pair.first.toUpper(), pair.second );
}
mDecoded = true;
}
return mParams.toMap();
}

QgsServerParameters QgsServerRequest::serverParameters() const
{
return mParams;
}

@@ -95,32 +82,29 @@ QByteArray QgsServerRequest::data() const

void QgsServerRequest::setParameter( const QString &key, const QString &value )
{
parameters();
mParams.insert( key, value );
mParams.add( key, value );
mUrl.setQuery( mParams.urlQuery() );
}

QString QgsServerRequest::parameter( const QString &key ) const
{
parameters();
return mParams.value( key );
}

void QgsServerRequest::removeParameter( const QString &key )
{
parameters();
mParams.remove( key );
mUrl.setQuery( mParams.urlQuery() );
}

void QgsServerRequest::setUrl( const QUrl &url )
{
mUrl = url;
mDecoded = false;
mParams.clear();
mParams.load( QUrlQuery( mUrl ) );
}

void QgsServerRequest::setMethod( Method method )
{
mMethod = method;
}


@@ -22,6 +22,7 @@
#include <QUrl>
#include <QMap>
#include "qgis_server.h"
#include "qgsserverparameters.h"

/**
* \ingroup server
@@ -96,6 +97,11 @@ class SERVER_EXPORT QgsServerRequest
*/
QgsServerRequest::Parameters parameters() const;

/**
* Returns parameters
*/
QgsServerParameters serverParameters() const;

/**
* Set a parameter
*/
@@ -159,11 +165,8 @@ class SERVER_EXPORT QgsServerRequest
Method mMethod = GetMethod;
// We mark as mutable in order
// to support lazy initialization
// Use QMap here because it will be faster for small
// number of elements
mutable bool mDecoded = false;
mutable Parameters mParams;
mutable Headers mHeaders;
QgsServerParameters mParams;
};

#endif

0 comments on commit bb26e71

Please sign in to comment.
You can’t perform that action at this time.