22#include "moc_qgsfcgiserverresponse.cpp"
24#include <fcgi_stdio.h>
29#if defined( Q_OS_UNIX ) && !defined( Q_OS_ANDROID )
31#include <sys/socket.h>
36typedef struct QgsFCGXStreamData
41 unsigned char *buffStop;
51 int isAnythingWritten;
53 FCGX_Request *reqDataPtr;
58 : mIsResponseFinished( isResponseFinished )
59 , mFeedback( feedback )
62 setObjectName(
"FCGI socket monitor" );
63 Q_ASSERT( mIsResponseFinished );
64 Q_ASSERT( mFeedback );
66#if defined( Q_OS_UNIX ) && !defined( Q_OS_ANDROID )
67 if ( FCGI_stdout && FCGI_stdout->fcgx_stream && FCGI_stdout->fcgx_stream->data )
69 QgsFCGXStreamData *stream =
static_cast<QgsFCGXStreamData *
>( FCGI_stdin->fcgx_stream->data );
70 if ( stream && stream->reqDataPtr )
72 mIpcFd = stream->reqDataPtr->ipcFd;
94#if defined( Q_OS_UNIX ) && !defined( Q_OS_ANDROID )
96 while ( !*mIsResponseFinished )
98 const ssize_t x = recv( mIpcFd, &
c, 1, MSG_PEEK | MSG_DONTWAIT );
102 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: remote socket still connected. errno: %1" ).arg( errno ), 5 );
107 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: remote socket has been closed! errno: %1" ).arg( errno ), 2 );
112 QThread::msleep( 333L );
115 if ( *mIsResponseFinished )
117 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: socket monitoring quits normally." ), 2 );
121 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: socket monitoring quits: no more socket." ), 2 );
134 mBuffer.open( QIODevice::ReadWrite );
137 mSocketMonitoringThread = std::make_unique<QgsSocketMonitoringThread>( &mFinished, mFeedback.get() );
138 mSocketMonitoringThread->start();
144 mSocketMonitoringThread->exit();
145 mSocketMonitoringThread->wait();
150 mHeaders.remove( key );
155 mHeaders.insert( key, value );
160 return mHeaders.value( key );
171 mHeaders.insert( QStringLiteral(
"Status" ), QStringLiteral(
" %1" ).arg( code ) );
186 setHeader( QStringLiteral(
"Content-Type" ), QStringLiteral(
"text/html;charset=utf-8" ) );
187 write( QStringLiteral(
"<html><body>%1</body></html>" ).arg( message ) );
204 if ( mFeedback->isCanceled() )
207 FCGI_stdout->fcgx_stream->wasFCloseCalled =
true;
214 if ( !mHeaders.contains(
"Content-Length" ) )
216 mHeaders.insert( QStringLiteral(
"Content-Length" ), QString::number( mBuffer.pos() ) );
228 QMap<QString, QString>::const_iterator it;
229 for ( it = mHeaders.constBegin(); it != mHeaders.constEnd(); ++it )
231 fputs( it.key().toUtf8(), FCGI_stdout );
232 fputs(
": ", FCGI_stdout );
233 fputs( it.value().toUtf8(), FCGI_stdout );
234 fputs(
"\n", FCGI_stdout );
236 fputs(
"\n", FCGI_stdout );
245 mBuffer.buffer().clear();
247 else if ( mBuffer.bytesAvailable() > 0 )
249 QByteArray &ba = mBuffer.buffer();
250 const size_t count = fwrite( (
void * ) ba.data(), ba.size(), 1, FCGI_stdout );
252 qDebug() << QStringLiteral(
"Sent %1 blocks of %2 bytes" ).arg( count ).arg( ba.size() );
266 mBuffer.buffer().clear();
275 return mBuffer.data();
282 mBuffer.buffer().clear();
288 setHeader( QStringLiteral(
"Server" ), QStringLiteral(
" QGIS FCGI server - QGIS version %1" ).arg(
Qgis::version() ) );
static QString version()
Version string.
@ Warning
Warning message.
void setDefaultHeaders()
Set the default headers.
void clear() override
Reset all headers and content for this response.
void setHeader(const QString &key, const QString &value) override
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
void flush() override
Flushes the current output buffer to the network.
virtual ~QgsFcgiServerResponse()
void removeHeader(const QString &key) override
Clear header Undo a previous 'setHeader' call.
QByteArray data() const override
Gets the data written so far.
QIODevice * io() override
Returns the underlying QIODevice.
bool headersSent() const override
Returns true if the headers have already been sent.
void setStatusCode(int code) override
Set the http status code.
QgsFcgiServerResponse(QgsServerRequest::Method method=QgsServerRequest::GetMethod)
Constructor for QgsFcgiServerResponse.
void sendError(int code, const QString &message) override
Send error This method delegates error handling at the server level.
void truncate() override
Truncate data.
void finish() override
Finish the response, ending the transaction.
QString header(const QString &key) const override
Returns the header value.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Method
HTTP Method (or equivalent) used for the request.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
QgsSocketMonitoringThread(bool *isResponseFinished, QgsFeedback *feedback)
QgsSocketMonitoringThread.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define QgsDebugMsgLevel(str, level)