Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Server: pass full url string and data to handleRequest()
    Fix tests by removing passing url pieces from environment
  • Loading branch information
dmarteau committed Apr 21, 2017
1 parent 11bb234 commit 018d2a2
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 124 deletions.
5 changes: 3 additions & 2 deletions python/server/qgsserver.sip
Expand Up @@ -190,10 +190,11 @@ class QgsServer
* but can be also passed in args and in this case overrides the environment
* variable.
*
* @param queryString QString containing the query string
* @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
*/
QPair<QByteArray, QByteArray> handleRequest( const QString& queryString );
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QString &requestMethod = QString(), const char *data = nullptr );

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

QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString &queryString )
QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString &urlstr, const QString &requestMethod, const char *data )
{
/*
* This is mainly for python bindings, passing QUERY_STRING
Expand All @@ -435,26 +435,24 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString &queryStri
* XXX To be removed because query string is now handled in QgsServerRequest
*
*/
if ( ! queryString.isEmpty() )
putenv( QStringLiteral( "QUERY_STRING" ), queryString );
QUrl url( urlstr );

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

// XXX This is mainly used in tests
char *requestMethod = getenv( "REQUEST_METHOD" );
if ( requestMethod && strcmp( requestMethod, "POST" ) == 0 )
if ( !requestMethod.isEmpty() && requestMethod.compare( QStringLiteral( "POST" ), Qt::CaseInsensitive ) == 0 )
{
method = QgsServerRequest::PostMethod;
const char *data = getenv( "REQUEST_BODY" );
if ( data )
{
ba.append( data );
}
}

QUrl url;
url.setQuery( queryString );
else if ( !requestMethod.isEmpty() && requestMethod.compare( QStringLiteral( "GET" ), Qt::CaseInsensitive ) != 0 )
{
throw QgsServerException( QStringLiteral( "Invalid method in handleRequest(): only GET or POST is supported" ) );
}

QgsBufferServerRequest request( url, method, &ba );
QgsBufferServerResponse response;
Expand Down
6 changes: 4 additions & 2 deletions src/server/qgsserver.h
Expand Up @@ -78,10 +78,12 @@ class SERVER_EXPORT QgsServer
* but can be also passed in args and in this case overrides the environment
* variable.
*
* \param queryString QString containing the query string
* \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 data array of bytes containing post data
* \returns the response headers and body QPair of QByteArray
*/
QPair<QByteArray, QByteArray> handleRequest( const QString &queryString );
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QString &requestMethod = QString(), const char *data = nullptr );

//! Returns a pointer to the server interface
QgsServerInterfaceImpl *serverInterface() { return sServerInterface; }
Expand Down
8 changes: 1 addition & 7 deletions tests/src/python/qgis_wrapped_server.py
Expand Up @@ -104,13 +104,7 @@ def do_GET(self):
# CGI vars:
for k, v in self.headers.items():
qgs_server.putenv('HTTP_%s' % k.replace(' ', '-').replace('-', '_').replace(' ', '-').upper(), v)
qgs_server.putenv('SERVER_PORT', str(self.server.server_port))
if https:
qgs_server.putenv('HTTPS', 'ON')
qgs_server.putenv('SERVER_NAME', self.server.server_name)
qgs_server.putenv('REQUEST_URI', self.path)
parsed_path = urllib.parse.urlparse(self.path)
headers, body = qgs_server.handleRequest(parsed_path.query)
headers, body = qgs_server.handleRequest(self.path)
headers_dict = dict(h.split(': ', 1) for h in headers.decode().split('\n') if h)
try:
self.send_response(int(headers_dict['Status'].split(' ')[0]))
Expand Down

0 comments on commit 018d2a2

Please sign in to comment.