Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[server] handleRequest accept QgsServerRequest.Method instead of a st…
…ring

Also made this optional
  • Loading branch information
elpaso committed Apr 22, 2017
1 parent 7f08e72 commit c2ba231
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 30 deletions.
11 changes: 6 additions & 5 deletions python/server/qgsserver.sip
Expand Up @@ -185,16 +185,17 @@ class QgsServer
*/
void handleRequest( QgsServerRequest& request, QgsServerResponse& response );

/** Handles the request from query strinf
/** Handles the request from query string
* The query string is normally read from environment
* but can be also passed in args and in this case overrides the environment
* variable.
*
* @param requestMethod QString that indicates the method. Only "GET" or "POST" are supported.
* @param data array of bytes containing post data
* @return the response headers and body QPair of QByteArray
* \param urlstr QString containing the request url (simple quely string must be preceded by '?')
* \param requestMethod QgsServerRequest::Method that indicates the method. Only "GET" or "POST" are supported.
* \param data array of bytes containing post data
* \returns the response headers and body QPair of QByteArray
*/
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QString &requestMethod = QString(), const char *data = nullptr );
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QgsServerRequest::Method &requestMethod = QgsServerRequest::GetMethod, const char *data = nullptr );

/** Returns a pointer to the server interface */
QgsServerInterface* serverInterface();
Expand Down
19 changes: 5 additions & 14 deletions src/server/qgsserver.cpp
Expand Up @@ -426,35 +426,26 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
}
}

QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString &urlstr, const QString &requestMethod, const char *data )
QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString &urlstr, const QgsServerRequest::Method &requestMethod, const char *data )
{
/*
* This is mainly for python bindings, passing QUERY_STRING
* to handleRequest without using os.environment
*
* XXX To be removed because query string is now handled in QgsServerRequest
*
*/

QUrl url( urlstr );

QgsServerRequest::Method method = QgsServerRequest::GetMethod;
QByteArray ba;

// XXX This is mainly used in tests
if ( !requestMethod.isEmpty() && requestMethod.compare( QStringLiteral( "POST" ), Qt::CaseInsensitive ) == 0 )
if ( requestMethod == QgsServerRequest::PostMethod )
{
method = QgsServerRequest::PostMethod;
if ( data )
{
ba.append( data );
}
}
else if ( !requestMethod.isEmpty() && requestMethod.compare( QStringLiteral( "GET" ), Qt::CaseInsensitive ) != 0 )
else if ( requestMethod != QgsServerRequest::GetMethod )
{
throw QgsServerException( QStringLiteral( "Invalid method in handleRequest(): only GET or POST is supported" ) );
}

QgsBufferServerRequest request( url, method, &ba );
QgsBufferServerRequest request( url, requestMethod, &ba );
QgsBufferServerResponse response;

handleRequest( request, response );
Expand Down
6 changes: 3 additions & 3 deletions src/server/qgsserver.h
Expand Up @@ -40,8 +40,8 @@
#include "qgsserverfilter.h"
#include "qgsserverinterfaceimpl.h"
#include "qgis_server.h"
#include "qgsserverrequest.h"

class QgsServerRequest;
class QgsServerResponse;
class QgsProject;

Expand Down Expand Up @@ -79,11 +79,11 @@ class SERVER_EXPORT QgsServer
* variable.
*
* \param urlstr QString containing the request url (simple quely string must be preceded by '?')
* \param requestMethod QString that indicates the method. Only "GET" or "POST" are supported.
* \param requestMethod QgsServerRequest::Method that indicates the method. Only "GET" or "POST" are supported.
* \param data array of bytes containing post data
* \returns the response headers and body QPair of QByteArray
*/
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QString &requestMethod = QString(), const char *data = nullptr );
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QgsServerRequest::Method &requestMethod = QgsServerRequest::GetMethod, const char *data = nullptr );

//! Returns a pointer to the server interface
QgsServerInterfaceImpl *serverInterface() { return sServerInterface; }
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_qgsserver.py
Expand Up @@ -21,8 +21,8 @@
import email

from io import StringIO
from qgis.server import QgsServer
from qgis.core import QgsMessageLog, QgsRenderChecker, QgsApplication
from qgis.server import QgsServer, QgsServerRequest
from qgis.core import QgsRenderChecker, QgsApplication
from qgis.testing import unittest
from qgis.PyQt.QtCore import QSize
from utilities import unitTestDataPath
Expand Down Expand Up @@ -346,7 +346,7 @@ def wfs_getfeature_post_compare(self, requestid, request):
assert os.path.exists(project), "Project file not found: " + project

query_string = '?MAP={}'.format(urllib.parse.quote(project))
header, body = self.server.handleRequest(query_string, requestMethod="POST", data=request)
header, body = self.server.handleRequest(query_string, requestMethod=QgsServerRequest.PostMethod, data=request)

self.result_compare(
'wfs_getfeature_{}.txt'.format(requestid),
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsserver_modules.py
Expand Up @@ -18,7 +18,7 @@ def __init__(self):
self._buffer = QBuffer()
self._buffer.open(QIODevice.ReadWrite)

def setReturnCode(self, code):
def setStatusCode(self, code):
pass

def setHeader(self, key, val):
Expand Down
8 changes: 4 additions & 4 deletions tests/src/python/test_qgsserver_services.py
Expand Up @@ -17,10 +17,10 @@ def __init__(self):
self._buffer = QBuffer()
self._buffer.open(QIODevice.ReadWrite)

def setReturnCode(self, code):
def setStatusCode(self, code):
self.code = code

def returnCode(self):
def statusCode(self):
return self.code

def setHeader(self, key, val):
Expand Down Expand Up @@ -60,7 +60,7 @@ def version(self):
return self._version

def executeRequest(self, request, response):
response.setReturnCode(201)
response.setStatusCode(201)
response.write(self._response)


Expand Down Expand Up @@ -96,7 +96,7 @@ def test_register(self):
io.seek(0)

self.assertEqual(QTextStream(io).readLine(), "Hello world")
self.assertEqual(response.returnCode(), 201)
self.assertEqual(response.statusCode(), 201)

def test_0_version_registration(self):

Expand Down

0 comments on commit c2ba231

Please sign in to comment.