Skip to content

Commit

Permalink
Add parameter map accessor from QgsServerRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarteau committed Jan 10, 2017
1 parent ec226ee commit 2adbaf0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
12 changes: 9 additions & 3 deletions python/server/qgsserverrequest.sip
Expand Up @@ -30,7 +30,8 @@ class QgsServerRequest
%End
public:

enum Method {
enum Method
{
HeadMethod, PutMethod, GetMethod, PostMethod, DeleteMethod
};

Expand Down Expand Up @@ -61,12 +62,17 @@ class QgsServerRequest
/**
* @return the request url
*/
virtual QUrl url() const;
QUrl url() const;

/**
* @return the request method
*/
virtual Method method() const;
Method method() const;

/**
* * @return query params
* */
QMap<QString, QString> parameters() const;

/**
* Return post/put data
Expand Down
19 changes: 18 additions & 1 deletion src/server/qgsserverrequest.cpp
Expand Up @@ -18,7 +18,7 @@
***************************************************************************/

#include "qgsserverrequest.h"

#include <QUrlQuery>

QgsServerRequest::QgsServerRequest( const QString& url, Method method )
: mUrl( url )
Expand Down Expand Up @@ -56,6 +56,23 @@ QgsServerRequest::Method QgsServerRequest::method() const
return mMethod;
}

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

QUrlQuery query( mUrl );
QList<pair_t> items = query.queryItems();
Q_FOREACH ( const pair_t& pair, items )
{
mParams.insert( pair.first.toUpper(), pair.second );
}
}
return mParams;
}

QByteArray QgsServerRequest::data() const
{
return QByteArray();
Expand Down
31 changes: 21 additions & 10 deletions src/server/qgsserverrequest.h
Expand Up @@ -20,6 +20,7 @@
#define QGSSERVERREQUEST_H

#include <QUrl>
#include <QMap>

/**
* \ingroup server
Expand Down Expand Up @@ -58,20 +59,21 @@ class SERVER_EXPORT QgsServerRequest
//! destructor
virtual ~QgsServerRequest();

/**
* @return the value of the header field for that request
*/
virtual QString getHeader( const QString& name ) const;

/**
* @return the request url
*/
virtual QUrl url() const;
QUrl url() const;

/**
* @return the request method
*/
virtual Method method() const;
Method method() const;

/**
* Return a map of query parameters with keys converted
* to uppercase
*/
QMap<QString, QString> parameters() const;

/**
* Return post/put data
Expand All @@ -80,10 +82,19 @@ class SERVER_EXPORT QgsServerRequest
*/
virtual QByteArray data() const;

protected:
QUrl mUrl;
Method mMethod;
/**
* @return the value of the header field for that request
*/
virtual QString getHeader( const QString& name ) const;

private:
QUrl mUrl;
Method mMethod;
// We mark as mutable in order
// to support lazy initialization
// Use QMap here because it will be faster for small
// number of elements
mutable QMap<QString, QString> mParams;
};

#endif
2 changes: 2 additions & 0 deletions src/server/qgsserviceregistry.h
Expand Up @@ -98,6 +98,8 @@ class SERVER_EXPORT QgsServiceRegistry
void cleanUp();

private:
// XXX consider using QMap because of the few numbers of
// elements to handle
typedef QHash<QString, std::shared_ptr<QgsService> > ServiceTable;
typedef QHash<QString, QPair<QString, QString> > VersionTable;

Expand Down

0 comments on commit 2adbaf0

Please sign in to comment.