[server] handleRequest accept QgsServerRequest.Method instead of a string

Also made this optional
This commit is contained in:
Alessandro Pasotti 2017-04-22 10:57:08 +02:00
parent 7f08e7206f
commit c2ba23173a
6 changed files with 22 additions and 30 deletions

View File

@ -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();

View File

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

View File

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

View File

@ -21,8 +21,8 @@ import urllib.error
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 @@ class TestQgsServer(unittest.TestCase):
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),

View File

@ -18,7 +18,7 @@ class Response(QgsServerResponse):
self._buffer = QBuffer()
self._buffer.open(QIODevice.ReadWrite)
def setReturnCode(self, code):
def setStatusCode(self, code):
pass
def setHeader(self, key, val):

View File

@ -17,10 +17,10 @@ class Response(QgsServerResponse):
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 @@ class MyService(QgsService):
return self._version
def executeRequest(self, request, response):
response.setReturnCode(201)
response.setStatusCode(201)
response.write(self._response)
@ -96,7 +96,7 @@ class TestServices(unittest.TestCase):
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):