Skip to content

Commit

Permalink
Merge pull request #4393 from elpaso/server-api-mods
Browse files Browse the repository at this point in the history
[server] Server api mods
  • Loading branch information
elpaso authored Apr 24, 2017
2 parents c540ab3 + cbcd193 commit 2a7d5d4
Show file tree
Hide file tree
Showing 21 changed files with 335 additions and 138 deletions.
8 changes: 8 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Renamed Classes {#qgis_api_break_3_0_renamed_classes}
<tr><td>QgsGraphBuilder<td>addArc<td>addEdge
<tr><td>QgsGraphBuilderInterface<td>addArc<td>addEdge
<tr><td>QgsGraphDirectory<td>addProperter<td>addStrategy
<tr><td>QgsRequestHandler<td>getHeader<td>header
<tr><td>QgsStrategy<td>addProperter<td>addStrategy
</table>

Expand Down Expand Up @@ -1836,6 +1837,13 @@ QgsRuntimeProfiler {#qgis_api_break_3_0_QgsRuntimeProfiler}

- This class is no longer a singleton and instance() has been removed. Instead use QgsApplication::profiler() to access an application-wide profiler.


QgsServer {#qgis_api_break_3_0_QgsServer}
----------

- QgsServer::handleRequest( const QString &urlstr ) now also takes two optional arguments: QgsServerRequest::Method and POST data


QgsSimpleMarkerSymbolLayer {#qgis_api_break_3_0_QgsSimpleMarkerSymbolLayer}
--------------------------

Expand Down
8 changes: 7 additions & 1 deletion python/server/qgsrequesthandler.sip
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class QgsRequestHandler /Abstract/
void setHeader( const QString &name, const QString &value );

//! Retrieve header value
QString getHeader( const QString &name ) const;
QString header( const QString &name ) const;

//! Return the list of all header keys
QList<QString> headerKeys() const;
Expand All @@ -50,6 +50,12 @@ class QgsRequestHandler /Abstract/
/** Send out HTTP headers and flush output buffer*/
void sendResponse();

//! Set response http status code
void setStatusCode( int code );

//! Return response http status code
int statusCode( ) const;

/** Pointer to last raised exception*/
bool exceptionRaised() const;

Expand Down
11 changes: 6 additions & 5 deletions python/server/qgsserver.sip
Original file line number Diff line number Diff line change
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
10 changes: 7 additions & 3 deletions python/server/qgsserverresponse.sip
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class QgsServerResponse
/**
* Return the header value
*/
virtual QString getHeader( const QString &key ) const = 0;
virtual QString header( const QString &key ) const = 0;

/**
* Return the list of all header keys
Expand All @@ -63,10 +63,14 @@ class QgsServerResponse
*/
virtual bool headersSent() const = 0;

/** Set the http return code
/** Return the http status code
*/
virtual int statusCode( ) const = 0;

/** Set the http status code
* @param code HTTP return code value
*/
virtual void setReturnCode( int code ) = 0;
virtual void setStatusCode( int code ) = 0;

/**
* Send error
Expand Down
6 changes: 3 additions & 3 deletions src/server/qgsbufferserverresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ void QgsBufferServerResponse::setHeader( const QString &key, const QString &valu
mHeaders.insert( key, value );
}

void QgsBufferServerResponse::setReturnCode( int code )
void QgsBufferServerResponse::setStatusCode( int code )
{
mReturnCode = code;
}

QString QgsBufferServerResponse::getHeader( const QString &key ) const
QString QgsBufferServerResponse::header( const QString &key ) const
{
return mHeaders.value( key );
}
Expand All @@ -78,7 +78,7 @@ void QgsBufferServerResponse::sendError( int code, const QString &message )
}

clear();
setReturnCode( code );
setStatusCode( code );
setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/plain; charset=utf-8" ) );
write( message );
finish();
Expand Down
10 changes: 4 additions & 6 deletions src/server/qgsbufferserverresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class QgsBufferServerResponse: public QgsServerResponse

void clearHeader( const QString &key ) override;

QString getHeader( const QString &key ) const override;
QString header( const QString &key ) const override;

QList<QString> headerKeys() const override;

bool headersSent() const override;

void setReturnCode( int code ) override;
void setStatusCode( int code ) override;

int statusCode( ) const override { return mReturnCode; }

void sendError( int code, const QString &message ) override;

Expand All @@ -74,10 +76,6 @@ class QgsBufferServerResponse: public QgsServerResponse
*/
QMap<QString, QString> headers() const { return mHeaders; }

/**
* Return the status code
*/
int returnCode() const { return mReturnCode; }

private:
QMap<QString, QString> mHeaders;
Expand Down
8 changes: 5 additions & 3 deletions src/server/qgsfcgiserverresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void QgsFcgiServerResponse::setHeader( const QString &key, const QString &value
mHeaders.insert( key, value );
}

QString QgsFcgiServerResponse::getHeader( const QString &key ) const
QString QgsFcgiServerResponse::header( const QString &key ) const
{
return mHeaders.value( key );
}
Expand All @@ -66,10 +66,12 @@ bool QgsFcgiServerResponse::headersSent() const
return mHeadersSent;
}

void QgsFcgiServerResponse::setReturnCode( int code )
void QgsFcgiServerResponse::setStatusCode( int code )
{
// fcgi applications must return HTTP status in header
mHeaders.insert( QStringLiteral( "Status" ), QStringLiteral( " %1" ).arg( code ) );
// Store the code to make it available for plugins
mStatusCode = code;
}

void QgsFcgiServerResponse::sendError( int code, const QString &message )
Expand All @@ -81,7 +83,7 @@ void QgsFcgiServerResponse::sendError( int code, const QString &message )
}

clear();
setReturnCode( code );
setStatusCode( code );
setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/html;charset=utf-8" ) );
write( QStringLiteral( "<html><body>%1</body></html>" ).arg( message ) );
finish();
Expand Down
7 changes: 5 additions & 2 deletions src/server/qgsfcgiserverresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ class SERVER_EXPORT QgsFcgiServerResponse: public QgsServerResponse

void clearHeader( const QString &key ) override;

QString getHeader( const QString &key ) const override;
QString header( const QString &key ) const override;

QList<QString> headerKeys() const override;

bool headersSent() const override;

void setReturnCode( int code ) override;
void setStatusCode( int code ) override;

int statusCode( ) const override { return mStatusCode; }

void sendError( int code, const QString &message ) override;

Expand All @@ -73,6 +75,7 @@ class SERVER_EXPORT QgsFcgiServerResponse: public QgsServerResponse
bool mFinished = false;
bool mHeadersSent = false;
QgsServerRequest::Method mMethod;
int mStatusCode = 0;
};

/**
Expand Down
6 changes: 4 additions & 2 deletions src/server/qgsfilterresponsedecorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ class QgsFilterResponseDecorator: public QgsServerResponse

void clearHeader( const QString &key ) override { mResponse.clearHeader( key ); }

QString getHeader( const QString &key ) const override { return mResponse.getHeader( key ); }
QString header( const QString &key ) const override { return mResponse.header( key ); }

QList<QString> headerKeys() const override { return mResponse.headerKeys(); }

bool headersSent() const override { return mResponse.headersSent(); }

void setReturnCode( int code ) override { mResponse.setReturnCode( code ); }
void setStatusCode( int code ) override { mResponse.setStatusCode( code ); }

int statusCode( ) const override { return mResponse.statusCode( ); }

void sendError( int code, const QString &message ) override { mResponse.sendError( code, message ); }

Expand Down
14 changes: 12 additions & 2 deletions src/server/qgsrequesthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ void QgsRequestHandler::removeHeader( const QString &name )
mResponse.clearHeader( name );
}

QString QgsRequestHandler::getHeader( const QString &name ) const
QString QgsRequestHandler::header( const QString &name ) const
{
return mResponse.getHeader( name );
return mResponse.header( name );
}

QList<QString> QgsRequestHandler::headerKeys() const
Expand Down Expand Up @@ -99,6 +99,16 @@ QByteArray QgsRequestHandler::body() const
return mResponse.data();
}

void QgsRequestHandler::setStatusCode( int code )
{
mResponse.setStatusCode( code );
}

int QgsRequestHandler::statusCode() const
{
return mResponse.statusCode();
}

void QgsRequestHandler::sendResponse()
{
// Send data to output
Expand Down
10 changes: 8 additions & 2 deletions src/server/qgsrequesthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class SERVER_EXPORT QgsRequestHandler
void removeHeader( const QString &name );

//! Retrieve header value
QString getHeader( const QString &name ) const;
QString header( const QString &name ) const;

//! Return the list of all header keys
QList<QString> headerKeys() const;
Expand All @@ -88,9 +88,15 @@ class SERVER_EXPORT QgsRequestHandler
//! Clear response buffer
void clearBody();

//! Return body data
//! Return response body data
QByteArray body() const;

//! Set response http status code
void setStatusCode( int code );

//! Return response http status code
int statusCode( ) const;

/** Return the parsed parameters as a key-value pair, to modify
* a parameter setParameter( const QString &key, const QString &value)
* and removeParameter(const QString &key) must be used
Expand Down
19 changes: 5 additions & 14 deletions src/server/qgsserver.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion src/server/qgsserverresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void QgsServerResponse::write( const QgsServerException &ex )
}

clear();
setReturnCode( ex.responseCode() );
setStatusCode( ex.responseCode() );
setHeader( "Content-Type", responseFormat );
write( ba );
}
Expand Down
16 changes: 10 additions & 6 deletions src/server/qgsserverresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SERVER_EXPORT QgsServerResponse
/**
* Return the header value
*/
virtual QString getHeader( const QString &key ) const = 0;
virtual QString header( const QString &key ) const = 0;

/**
* Return the list of all header keys
Expand All @@ -77,10 +77,14 @@ class SERVER_EXPORT QgsServerResponse
virtual bool headersSent() const = 0;


/** Set the http return code
* \param code HTTP return code value
/** Set the http status code
* \param code HTTP status code value
*/
virtual void setReturnCode( int code ) = 0;
virtual void setStatusCode( int code ) = 0;

/** Return the http status code
*/
virtual int statusCode( ) const = 0;

/**
* Send error
Expand Down Expand Up @@ -113,7 +117,7 @@ class SERVER_EXPORT QgsServerResponse
*
* This is a convenient method that will write directly
* to the underlying I/O device
* \returns the number of bytes written
* \returns the number of bytes written
*
* \note not available in Python bindings
*/
Expand Down Expand Up @@ -162,7 +166,7 @@ class SERVER_EXPORT QgsServerResponse
* Get the data written so far
*
* This is implementation dependent: some implementations may not
* give access to the underlyng and return an empty array.
* give access to the underlying and return an empty array.
*
* Note that each call to 'flush' may empty the buffer and in case
* of streaming process you may get partial content
Expand Down
Loading

0 comments on commit 2a7d5d4

Please sign in to comment.