Skip to content

Commit

Permalink
[server] Python plugins API cleanup part 3
Browse files Browse the repository at this point in the history
This removes the handleRequest method that returns the
headers and body as byte array.

This superceeded by the implementation that takes a
request and response instances.
  • Loading branch information
elpaso committed May 3, 2017
1 parent 2afcad2 commit 569593b
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 324 deletions.
4 changes: 2 additions & 2 deletions python/server/qgsbufferserverrequest.sip
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/************************************************************************
* This file has been generated automatically from *
* *
* ../src/server/qgsbufferserverrequest.h *
* src/server/qgsbufferserverrequest.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
Expand Down Expand Up @@ -46,7 +46,7 @@ class QgsBufferServerRequest : QgsServerRequest
/************************************************************************
* This file has been generated automatically from *
* *
* ../src/server/qgsbufferserverrequest.h *
* src/server/qgsbufferserverrequest.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
6 changes: 3 additions & 3 deletions python/server/qgsbufferserverresponse.sip
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/************************************************************************
* This file has been generated automatically from *
* *
* ../src/server/qgsbufferserverresponse.h *
* src/server/qgsbufferserverresponse.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
Expand Down Expand Up @@ -141,13 +141,13 @@ class QgsBufferServerResponse: QgsServerResponse


private:
QgsBufferServerResponse(const QgsBufferServerResponse &) ;
QgsBufferServerResponse( const QgsBufferServerResponse & ) ;
};

/************************************************************************
* This file has been generated automatically from *
* *
* ../src/server/qgsbufferserverresponse.h *
* src/server/qgsbufferserverresponse.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
147 changes: 0 additions & 147 deletions python/server/qgsserver.sip
Original file line number Diff line number Diff line change
Expand Up @@ -17,141 +17,6 @@
***************************************************************************/


%MappedType QPair<QByteArray, QByteArray>
{
%TypeHeaderCode
#include <QPair>
#include <QByteArray>
%End


%TypeCode
// Convenience function for converting a QByteArray to a Python str object. (from QtCore/qbytearray.sip)
static PyObject *QByteArrayToPyStr(QByteArray *ba)
{
char *data = ba->data();

if (data)
// QByteArrays may have embedded '\0's so set the size explicitly.
return SIPBytes_FromStringAndSize(data, ba->size());
return SIPBytes_FromString("");
}

%End


%ConvertFromTypeCode
// Create the tuple.
return Py_BuildValue((char *)"OO", QByteArrayToPyStr( &sipCpp->first ), QByteArrayToPyStr( &sipCpp->second ) );
%End

%ConvertToTypeCode

// See if we are just being asked to check the type of the Python
// object.
if (!sipIsErr)
{
// Checking whether or not None has been passed instead of a list
// has already been done.
if (!PyTuple_Check(sipPy) || PyTuple_Size(sipPy) != 2)
return 0;

// Check the type of each element. We specify SIP_NOT_NONE to
// disallow None because it is a list of QPoint, not of a pointer
// to a QPoint, so None isn't appropriate.
for (int i = 0; i < PyTuple_Size(sipPy); ++i)
if (!sipCanConvertToType(PyTuple_GET_ITEM(sipPy, i),
sipType_QByteArray, SIP_NOT_NONE))
return 0;

// The type is valid.
return 1;
}

// Create the instance on the heap.
QPair<QByteArray, QByteArray> *qp = new QPair<QByteArray, QByteArray>;

QByteArray *qba1;
int state;

// Get the address of the element's C++ instance. Note that, in
// this case, we don't apply any ownership changes to the list
// elements, only to the list itself.
qba1 = reinterpret_cast<QByteArray *>(sipConvertToType(
PyTuple_GET_ITEM(sipPy, 0),
sipType_QByteArray, 0,
SIP_NOT_NONE,
&state, sipIsErr));

// Deal with any errors.
if (*sipIsErr)
{
sipReleaseType(qba1, sipType_QByteArray, state);

// Tidy up.
delete qp;

// There is no temporary instance.
return 0;
}

qp->first = *qba1;

// A copy of the QByteArray was assigned to the pair so we no longer
// need it. It may be a temporary instance that should be
// destroyed, or a wrapped instance that should not be destroyed.
// sipReleaseType() will do the right thing.
sipReleaseType(qba1, sipType_QByteArray, state);

/////////////////////////////////////////////
// Second item

QByteArray *qba2;

// Get the address of the element's C++ instance. Note that, in
// this case, we don't apply any ownership changes to the list
// elements, only to the list itself.
qba2 = reinterpret_cast<QByteArray *>(sipConvertToType(
PyTuple_GET_ITEM(sipPy, 1),
sipType_QByteArray, 0,
SIP_NOT_NONE,
&state, sipIsErr));

// Deal with any errors.
if (*sipIsErr)
{
sipReleaseType(qba1, sipType_QByteArray, state);
sipReleaseType(qba2, sipType_QByteArray, state);

// Tidy up.
delete qp;

// There is no temporary instance.
return 0;
}

qp->second = *qba2;


// A copy of the QByteArray was assigned to the pair so we no longer
// need it. It may be a temporary instance that should be
// destroyed, or a wrapped instance that should not be destroyed.
// sipReleaseType() will do the right thing.
sipReleaseType(qba2, sipType_QByteArray, state);


// Return the instance.
*sipCppPtr = qp;

// The instance should be regarded as temporary (and be destroyed as
// soon as it has been used) unless it has been transferred from
// Python. sipGetState() is a convenience function that implements
// this common transfer behavior.
return sipGetState(sipTransferObj);

%End
};

/** \ingroup server
* The QgsServer class provides OGC web services.
*/
Expand Down Expand Up @@ -185,18 +50,6 @@ class QgsServer
*/
void handleRequest( QgsServerRequest &request, QgsServerResponse &response );

/** 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 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
* \param map of request headers
* \returns the response headers and body QPair of QByteArray
*/
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QgsServerRequest::Method requestMethod = QgsServerRequest::GetMethod, const QgsServerRequest::Headers &headers = QgsServerRequest::Headers(), const char *data = nullptr );

/** Returns a pointer to the server interface */
QgsServerInterface *serverInterface();
Expand Down
43 changes: 0 additions & 43 deletions src/server/qgsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,49 +427,6 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
}
}

QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString &urlstr, const QgsServerRequest::Method requestMethod, const QgsServerRequest::Headers &headers, const char *data )
{

QUrl url( urlstr );

QByteArray ba;

if ( requestMethod == QgsServerRequest::PostMethod )
{
if ( data )
{
ba.append( data );
}
}
else if ( requestMethod != QgsServerRequest::GetMethod )
{
throw QgsServerException( QStringLiteral( "Invalid method in handleRequest(): only GET or POST is supported" ) );
}

QgsBufferServerRequest request( url, requestMethod, headers, &ba );
QgsBufferServerResponse response;

handleRequest( request, response );

/*
* XXX For compatibility only:
* We should return a (moved) QgsBufferServerResponse instead
*/
QByteArray headerBuffer;
QMap<QString, QString>::const_iterator it;
for ( it = response.headers().constBegin(); it != response.headers().constEnd(); ++it )
{
headerBuffer.append( it.key().toUtf8() );
headerBuffer.append( ": " );
headerBuffer.append( it.value().toUtf8() );
headerBuffer.append( "\n" );
}
headerBuffer.append( "\n" );

// TODO: check that this is not an evil bug!
return QPair<QByteArray, QByteArray>( headerBuffer, response.body() );

}

#ifdef HAVE_SERVER_PYTHON_PLUGINS
void QgsServer::initPython()
Expand Down
12 changes: 0 additions & 12 deletions src/server/qgsserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,6 @@ class SERVER_EXPORT QgsServer
*/
void handleRequest( QgsServerRequest &request, QgsServerResponse &response );

/** 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 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
* \param map of request headers
* \returns the response headers and body QPair of QByteArray
*/
QPair<QByteArray, QByteArray> handleRequest( const QString &urlstr, const QgsServerRequest::Method requestMethod = QgsServerRequest::GetMethod, const QgsServerRequest::Headers &headers = QgsServerRequest::Headers(), const char *data = nullptr );

//! Returns a pointer to the server interface
QgsServerInterfaceImpl *serverInterface() { return sServerInterface; }
Expand Down
Loading

0 comments on commit 569593b

Please sign in to comment.