Skip to content

Commit

Permalink
Update server requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Jul 23, 2018
1 parent d1a4c0a commit bb26e71
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/server/qgsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
89 changes: 78 additions & 11 deletions src/server/qgsserverparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
49 changes: 42 additions & 7 deletions src/server/qgsserverparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 11 additions & 27 deletions src/server/qgsserverrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}


11 changes: 7 additions & 4 deletions src/server/qgsserverrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QUrl>
#include <QMap>
#include "qgis_server.h"
#include "qgsserverparameters.h"

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

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

/**
* Set a parameter
*/
Expand Down Expand Up @@ -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.