Skip to content

Commit

Permalink
Restored streaming capabilities, added mHeadersSent flag and accessor
Browse files Browse the repository at this point in the history
Funded by ItOpen - http://www.itopen.it
  • Loading branch information
elpaso committed Nov 4, 2014
1 parent 840fcf3 commit a4fb2a1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
35 changes: 25 additions & 10 deletions src/mapserver/qgshttprequesthandler.cpp
Expand Up @@ -36,6 +36,7 @@ QgsHttpRequestHandler::QgsHttpRequestHandler():
QgsRequestHandler() QgsRequestHandler()
{ {
mException = NULL; mException = NULL;
mHeadersSent = FALSE;
} }


QgsHttpRequestHandler::~QgsHttpRequestHandler() QgsHttpRequestHandler::~QgsHttpRequestHandler()
Expand All @@ -57,7 +58,7 @@ void QgsHttpRequestHandler::setHttpResponse( QByteArray *ba, const QString &form
} }
QgsDebugMsg( "Byte array looks good, setting response..." ); QgsDebugMsg( "Byte array looks good, setting response..." );


mBody.append( *ba ); appendBody( *ba );
mInfoFormat = format; mInfoFormat = format;
} }


Expand Down Expand Up @@ -103,7 +104,7 @@ void QgsHttpRequestHandler::setInfoFormat( const QString &format )
mInfoFormat = format; mInfoFormat = format;
} }


void QgsHttpRequestHandler::sendHeaders() const void QgsHttpRequestHandler::sendHeaders()
{ {
// Send default headers if they've not been set in a previous stage // Send default headers if they've not been set in a previous stage
if ( mHeaders.empty() ) if ( mHeaders.empty() )
Expand All @@ -113,8 +114,11 @@ void QgsHttpRequestHandler::sendHeaders() const
printf( "Content-Type: " ); printf( "Content-Type: " );
printf( mInfoFormat.toLocal8Bit() ); printf( mInfoFormat.toLocal8Bit() );
printf( "\n" ); printf( "\n" );
printf( "Content-Length: %d\n", mBody.size() ); // size is not known when streaming

if ( mBody.size() > 0)
{
printf( "Content-Length: %d\n", mBody.size() );
}
} }
else else
{ {
Expand All @@ -129,28 +133,34 @@ void QgsHttpRequestHandler::sendHeaders() const
printf( "\n" ); printf( "\n" );
} }
printf( "\n" ); printf( "\n" );
mHeadersSent = TRUE;
} }


void QgsHttpRequestHandler::sendBody() const void QgsHttpRequestHandler::sendBody() const
{ {
size_t result = fwrite( (void*)mBody.data(), mBody.size(), 1, FCGI_stdout ); fwrite( (void*)mBody.data(), mBody.size(), 1, FCGI_stdout );
#ifdef QGISDEBUG #ifdef QGISDEBUG
QgsDebugMsg( QString( "Sent %1 bytes" ).arg( result ) ); QgsDebugMsg( QString( "Sent %1 bytes" ).arg( mBody.size() ) );
#else #else
Q_UNUSED( result ); Q_UNUSED( result );
#endif #endif
} }


void QgsHttpRequestHandler::sendResponse() const void QgsHttpRequestHandler::sendResponse()
{ {
QgsDebugMsg( QString( "Sending HTTP response" ) ); QgsDebugMsg( QString( "Sending HTTP response" ) );
if ( ! responseReady() ) if ( ! responseReady() )
{ {
QgsDebugMsg( QString( "Trying to send out an empty reponse" ) ); QgsDebugMsg( QString( "Trying to send out an empty reponse" ) );
return; return;
} }
sendHeaders(); if (! mHeadersSent )
{
sendHeaders();
}
sendBody(); sendBody();
//Clear the body to allow for streaming content to stdout
clearBody();
} }




Expand Down Expand Up @@ -232,7 +242,6 @@ void QgsHttpRequestHandler::setGetMapResponse( const QString& service, QImage* i
{ {
ba = ba.toBase64(); ba = ba.toBase64();
} }

setHttpResponse( &ba, formatToMimeType( mFormat ) ); setHttpResponse( &ba, formatToMimeType( mFormat ) );
} }
} }
Expand Down Expand Up @@ -426,6 +435,8 @@ bool QgsHttpRequestHandler::startGetFeatureResponse( QByteArray* ba, const QStri


setHeader( "Content-Type", format ); setHeader( "Content-Type", format );
appendBody( *ba ); appendBody( *ba );
// Streaming
sendResponse();
return true; return true;
} }


Expand All @@ -441,6 +452,8 @@ void QgsHttpRequestHandler::setGetFeatureResponse( QByteArray* ba )
return; return;
} }
appendBody( *ba ); appendBody( *ba );
// Streaming
sendResponse();
} }


void QgsHttpRequestHandler::endGetFeatureResponse( QByteArray* ba ) void QgsHttpRequestHandler::endGetFeatureResponse( QByteArray* ba )
Expand All @@ -449,7 +462,9 @@ void QgsHttpRequestHandler::endGetFeatureResponse( QByteArray* ba )
{ {
return; return;
} }
mBody.append(ba->data()); appendBody( *ba );
// Streaming
sendResponse();
} }


void QgsHttpRequestHandler::setGetCoverageResponse( QByteArray* ba ) void QgsHttpRequestHandler::setGetCoverageResponse( QByteArray* ba )
Expand Down
5 changes: 3 additions & 2 deletions src/mapserver/qgshttprequesthandler.h
Expand Up @@ -45,7 +45,8 @@ class QgsHttpRequestHandler: public QgsRequestHandler
virtual void setGetFeatureResponse( QByteArray* ba ); virtual void setGetFeatureResponse( QByteArray* ba );
virtual void endGetFeatureResponse( QByteArray* ba ); virtual void endGetFeatureResponse( QByteArray* ba );
virtual void setGetCoverageResponse( QByteArray* ba ); virtual void setGetCoverageResponse( QByteArray* ba );
virtual void sendResponse() const; /**Send out HTTP headers and flush output buffer*/
virtual void sendResponse();
virtual void setHeader( const QString &name, const QString &value ); virtual void setHeader( const QString &name, const QString &value );
virtual int removeHeader( const QString &name ); virtual int removeHeader( const QString &name );
virtual void clearHeaders( ); virtual void clearHeaders( );
Expand All @@ -59,7 +60,7 @@ class QgsHttpRequestHandler: public QgsRequestHandler
virtual int removeParameter(const QString &key); virtual int removeParameter(const QString &key);


protected: protected:
virtual void sendHeaders( ) const; virtual void sendHeaders( );
virtual void sendBody( ) const; virtual void sendBody( ) const;
void setHttpResponse(QByteArray *ba, const QString &format ); void setHttpResponse(QByteArray *ba, const QString &format );
/**Converts format to official mimetype (e.g. 'jpg' to 'image/jpeg') /**Converts format to official mimetype (e.g. 'jpg' to 'image/jpeg')
Expand Down
7 changes: 5 additions & 2 deletions src/mapserver/qgsrequesthandler.h
Expand Up @@ -63,7 +63,8 @@ class QgsRequestHandler
/**Clears the response body*/ /**Clears the response body*/
virtual void clearBody( ) = 0; virtual void clearBody( ) = 0;
virtual void setInfoFormat( const QString &format ) = 0; virtual void setInfoFormat( const QString &format ) = 0;
virtual void sendResponse( ) const = 0; /**Send out HTTP headers and flush output buffer*/
virtual void sendResponse( ) = 0;
virtual bool responseReady() const = 0; virtual bool responseReady() const = 0;
/**Pointer to last raised exception*/ /**Pointer to last raised exception*/
virtual bool exceptionRaised() const = 0; virtual bool exceptionRaised() const = 0;
Expand All @@ -75,14 +76,16 @@ class QgsRequestHandler
/**Return a request parameter*/ /**Return a request parameter*/
virtual QString parameter(const QString &key) const = 0; virtual QString parameter(const QString &key) const = 0;
QString format() const { return mFormat; } QString format() const { return mFormat; }
bool headersSent() { return mHeadersSent; }


protected: protected:


virtual void sendHeaders( ) const = 0; virtual void sendHeaders( ) = 0;
virtual void sendBody( ) const = 0; virtual void sendBody( ) const = 0;
/**This is set by the parseInput methods of the subclasses (parameter FORMAT, e.g. 'FORMAT=PNG')*/ /**This is set by the parseInput methods of the subclasses (parameter FORMAT, e.g. 'FORMAT=PNG')*/
QString mFormat; QString mFormat;
QString mFormatString; //format string as it is passed in the request (with base) QString mFormatString; //format string as it is passed in the request (with base)
bool mHeadersSent;
QString mService; QString mService;
QString mInfoFormat; QString mInfoFormat;
QgsMapServiceException* mException; // Stores the exception QgsMapServiceException* mException; // Stores the exception
Expand Down
6 changes: 3 additions & 3 deletions src/mapserver/qgswfsserver.cpp
Expand Up @@ -106,7 +106,7 @@ void QgsWFSServer::executeRequest()
mRequestHandler->setServiceException( ex ); mRequestHandler->setServiceException( ex );
return; return;
} }
QgsDebugMsg( "seting GetCapabilities response" ); QgsDebugMsg( "Setting GetCapabilities response" );
mRequestHandler->setGetCapabilitiesResponse( capabilitiesDocument ); mRequestHandler->setGetCapabilitiesResponse( capabilitiesDocument );
return; return;
} }
Expand All @@ -122,7 +122,7 @@ void QgsWFSServer::executeRequest()
mRequestHandler->setServiceException( ex ); mRequestHandler->setServiceException( ex );
return; return;
} }
QgsDebugMsg( "seting GetCapabilities response" ); QgsDebugMsg( "Setting GetCapabilities response" );
mRequestHandler->setGetCapabilitiesResponse( describeDocument ); mRequestHandler->setGetCapabilitiesResponse( describeDocument );
return; return;
} }
Expand Down Expand Up @@ -153,7 +153,7 @@ void QgsWFSServer::executeRequest()
mRequestHandler->setServiceException( ex ); mRequestHandler->setServiceException( ex );
return; return;
} }
QgsDebugMsg( "seting Transaction response" ); QgsDebugMsg( "Setting Transaction response" );
mRequestHandler->setGetCapabilitiesResponse( transactionDocument ); mRequestHandler->setGetCapabilitiesResponse( transactionDocument );
return; return;
} }
Expand Down
6 changes: 3 additions & 3 deletions src/mapserver/qgswmsserver.cpp
Expand Up @@ -160,7 +160,7 @@ void QgsWMSServer::executeRequest()


if ( result ) if ( result )
{ {
QgsDebugMsg( "seting GetMap response" ); QgsDebugMsg( "Setting GetMap response" );
mRequestHandler->setGetMapResponse( "WMS", result, getImageQuality() ); mRequestHandler->setGetMapResponse( "WMS", result, getImageQuality() );
QgsDebugMsg( "Response sent" ); QgsDebugMsg( "Response sent" );
} }
Expand Down Expand Up @@ -258,8 +258,8 @@ void QgsWMSServer::executeRequest()


if ( result ) if ( result )
{ {
QgsDebugMsg( "seting GetLegendGraphic response" ); QgsDebugMsg( "Setting GetLegendGraphic response" );
//seting is the same for GetMap and GetLegendGraphic //setting is the same for GetMap and GetLegendGraphic
mRequestHandler->setGetMapResponse( "WMS", result, getImageQuality() ); mRequestHandler->setGetMapResponse( "WMS", result, getImageQuality() );
QgsDebugMsg( "Response sent" ); QgsDebugMsg( "Response sent" );
} }
Expand Down

0 comments on commit a4fb2a1

Please sign in to comment.