Skip to content
Permalink
Browse files
Merge pull request #4393 from elpaso/server-api-mods
[server] Server api mods
  • Loading branch information
elpaso committed Apr 24, 2017
2 parents c540ab3 + cbcd193 commit 2a7d5d4b5b99c0b36e8531671aa926d702b9a221
@@ -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>

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

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

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

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

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

/**
@@ -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 ); }

@@ -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
@@ -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
@@ -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;
@@ -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
@@ -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; }
@@ -87,7 +87,7 @@ void QgsServerResponse::write( const QgsServerException &ex )
}

clear();
setReturnCode( ex.responseCode() );
setStatusCode( ex.responseCode() );
setHeader( "Content-Type", responseFormat );
write( ba );
}
@@ -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
@@ -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
@@ -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
*/
@@ -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
Loading

0 comments on commit 2a7d5d4

Please sign in to comment.