Skip to content

Commit a4fb2a1

Browse files
committedNov 4, 2014
Restored streaming capabilities, added mHeadersSent flag and accessor
Funded by ItOpen - http://www.itopen.it
1 parent 840fcf3 commit a4fb2a1

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed
 

‎src/mapserver/qgshttprequesthandler.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ QgsHttpRequestHandler::QgsHttpRequestHandler():
3636
QgsRequestHandler()
3737
{
3838
mException = NULL;
39+
mHeadersSent = FALSE;
3940
}
4041

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

60-
mBody.append( *ba );
61+
appendBody( *ba );
6162
mInfoFormat = format;
6263
}
6364

@@ -103,7 +104,7 @@ void QgsHttpRequestHandler::setInfoFormat( const QString &format )
103104
mInfoFormat = format;
104105
}
105106

106-
void QgsHttpRequestHandler::sendHeaders() const
107+
void QgsHttpRequestHandler::sendHeaders()
107108
{
108109
// Send default headers if they've not been set in a previous stage
109110
if ( mHeaders.empty() )
@@ -113,8 +114,11 @@ void QgsHttpRequestHandler::sendHeaders() const
113114
printf( "Content-Type: " );
114115
printf( mInfoFormat.toLocal8Bit() );
115116
printf( "\n" );
116-
printf( "Content-Length: %d\n", mBody.size() );
117-
117+
// size is not known when streaming
118+
if ( mBody.size() > 0)
119+
{
120+
printf( "Content-Length: %d\n", mBody.size() );
121+
}
118122
}
119123
else
120124
{
@@ -129,28 +133,34 @@ void QgsHttpRequestHandler::sendHeaders() const
129133
printf( "\n" );
130134
}
131135
printf( "\n" );
136+
mHeadersSent = TRUE;
132137
}
133138

134139
void QgsHttpRequestHandler::sendBody() const
135140
{
136-
size_t result = fwrite( (void*)mBody.data(), mBody.size(), 1, FCGI_stdout );
141+
fwrite( (void*)mBody.data(), mBody.size(), 1, FCGI_stdout );
137142
#ifdef QGISDEBUG
138-
QgsDebugMsg( QString( "Sent %1 bytes" ).arg( result ) );
143+
QgsDebugMsg( QString( "Sent %1 bytes" ).arg( mBody.size() ) );
139144
#else
140145
Q_UNUSED( result );
141146
#endif
142147
}
143148

144-
void QgsHttpRequestHandler::sendResponse() const
149+
void QgsHttpRequestHandler::sendResponse()
145150
{
146151
QgsDebugMsg( QString( "Sending HTTP response" ) );
147152
if ( ! responseReady() )
148153
{
149154
QgsDebugMsg( QString( "Trying to send out an empty reponse" ) );
150155
return;
151156
}
152-
sendHeaders();
157+
if (! mHeadersSent )
158+
{
159+
sendHeaders();
160+
}
153161
sendBody();
162+
//Clear the body to allow for streaming content to stdout
163+
clearBody();
154164
}
155165

156166

@@ -232,7 +242,6 @@ void QgsHttpRequestHandler::setGetMapResponse( const QString& service, QImage* i
232242
{
233243
ba = ba.toBase64();
234244
}
235-
236245
setHttpResponse( &ba, formatToMimeType( mFormat ) );
237246
}
238247
}
@@ -426,6 +435,8 @@ bool QgsHttpRequestHandler::startGetFeatureResponse( QByteArray* ba, const QStri
426435

427436
setHeader( "Content-Type", format );
428437
appendBody( *ba );
438+
// Streaming
439+
sendResponse();
429440
return true;
430441
}
431442

@@ -441,6 +452,8 @@ void QgsHttpRequestHandler::setGetFeatureResponse( QByteArray* ba )
441452
return;
442453
}
443454
appendBody( *ba );
455+
// Streaming
456+
sendResponse();
444457
}
445458

446459
void QgsHttpRequestHandler::endGetFeatureResponse( QByteArray* ba )
@@ -449,7 +462,9 @@ void QgsHttpRequestHandler::endGetFeatureResponse( QByteArray* ba )
449462
{
450463
return;
451464
}
452-
mBody.append(ba->data());
465+
appendBody( *ba );
466+
// Streaming
467+
sendResponse();
453468
}
454469

455470
void QgsHttpRequestHandler::setGetCoverageResponse( QByteArray* ba )

‎src/mapserver/qgshttprequesthandler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class QgsHttpRequestHandler: public QgsRequestHandler
4545
virtual void setGetFeatureResponse( QByteArray* ba );
4646
virtual void endGetFeatureResponse( QByteArray* ba );
4747
virtual void setGetCoverageResponse( QByteArray* ba );
48-
virtual void sendResponse() const;
48+
/**Send out HTTP headers and flush output buffer*/
49+
virtual void sendResponse();
4950
virtual void setHeader( const QString &name, const QString &value );
5051
virtual int removeHeader( const QString &name );
5152
virtual void clearHeaders( );
@@ -59,7 +60,7 @@ class QgsHttpRequestHandler: public QgsRequestHandler
5960
virtual int removeParameter(const QString &key);
6061

6162
protected:
62-
virtual void sendHeaders( ) const;
63+
virtual void sendHeaders( );
6364
virtual void sendBody( ) const;
6465
void setHttpResponse(QByteArray *ba, const QString &format );
6566
/**Converts format to official mimetype (e.g. 'jpg' to 'image/jpeg')

‎src/mapserver/qgsrequesthandler.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class QgsRequestHandler
6363
/**Clears the response body*/
6464
virtual void clearBody( ) = 0;
6565
virtual void setInfoFormat( const QString &format ) = 0;
66-
virtual void sendResponse( ) const = 0;
66+
/**Send out HTTP headers and flush output buffer*/
67+
virtual void sendResponse( ) = 0;
6768
virtual bool responseReady() const = 0;
6869
/**Pointer to last raised exception*/
6970
virtual bool exceptionRaised() const = 0;
@@ -75,14 +76,16 @@ class QgsRequestHandler
7576
/**Return a request parameter*/
7677
virtual QString parameter(const QString &key) const = 0;
7778
QString format() const { return mFormat; }
79+
bool headersSent() { return mHeadersSent; }
7880

7981
protected:
8082

81-
virtual void sendHeaders( ) const = 0;
83+
virtual void sendHeaders( ) = 0;
8284
virtual void sendBody( ) const = 0;
8385
/**This is set by the parseInput methods of the subclasses (parameter FORMAT, e.g. 'FORMAT=PNG')*/
8486
QString mFormat;
8587
QString mFormatString; //format string as it is passed in the request (with base)
88+
bool mHeadersSent;
8689
QString mService;
8790
QString mInfoFormat;
8891
QgsMapServiceException* mException; // Stores the exception

‎src/mapserver/qgswfsserver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void QgsWFSServer::executeRequest()
106106
mRequestHandler->setServiceException( ex );
107107
return;
108108
}
109-
QgsDebugMsg( "seting GetCapabilities response" );
109+
QgsDebugMsg( "Setting GetCapabilities response" );
110110
mRequestHandler->setGetCapabilitiesResponse( capabilitiesDocument );
111111
return;
112112
}
@@ -122,7 +122,7 @@ void QgsWFSServer::executeRequest()
122122
mRequestHandler->setServiceException( ex );
123123
return;
124124
}
125-
QgsDebugMsg( "seting GetCapabilities response" );
125+
QgsDebugMsg( "Setting GetCapabilities response" );
126126
mRequestHandler->setGetCapabilitiesResponse( describeDocument );
127127
return;
128128
}
@@ -153,7 +153,7 @@ void QgsWFSServer::executeRequest()
153153
mRequestHandler->setServiceException( ex );
154154
return;
155155
}
156-
QgsDebugMsg( "seting Transaction response" );
156+
QgsDebugMsg( "Setting Transaction response" );
157157
mRequestHandler->setGetCapabilitiesResponse( transactionDocument );
158158
return;
159159
}

‎src/mapserver/qgswmsserver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void QgsWMSServer::executeRequest()
160160

161161
if ( result )
162162
{
163-
QgsDebugMsg( "seting GetMap response" );
163+
QgsDebugMsg( "Setting GetMap response" );
164164
mRequestHandler->setGetMapResponse( "WMS", result, getImageQuality() );
165165
QgsDebugMsg( "Response sent" );
166166
}
@@ -258,8 +258,8 @@ void QgsWMSServer::executeRequest()
258258

259259
if ( result )
260260
{
261-
QgsDebugMsg( "seting GetLegendGraphic response" );
262-
//seting is the same for GetMap and GetLegendGraphic
261+
QgsDebugMsg( "Setting GetLegendGraphic response" );
262+
//setting is the same for GetMap and GetLegendGraphic
263263
mRequestHandler->setGetMapResponse( "WMS", result, getImageQuality() );
264264
QgsDebugMsg( "Response sent" );
265265
}

0 commit comments

Comments
 (0)
Please sign in to comment.