Skip to content
Permalink
Browse files
[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 c2ba23173a3199c63b18e43094760470873a0755
@@ -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();
@@ -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 );
@@ -40,8 +40,8 @@
#include "qgsserverfilter.h"
#include "qgsserverinterfaceimpl.h"
#include "qgis_server.h"
#include "qgsserverrequest.h"

class QgsServerRequest;
class QgsServerResponse;
class QgsProject;

@@ -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; }
@@ -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
@@ -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),
@@ -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):
@@ -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):
@@ -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)


@@ -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):

0 comments on commit c2ba231

Please sign in to comment.