23#include <fcgi_stdio.h>
28#if defined(Q_OS_UNIX) && !defined(Q_OS_ANDROID)
30#include <sys/socket.h>
35typedef struct QgsFCGXStreamData
40 unsigned char *buffStop;
50 int isAnythingWritten;
52 FCGX_Request *reqDataPtr;
57 : mIsResponseFinished( isResponseFinished )
58 , mFeedback( feedback )
61 setObjectName(
"FCGI socket monitor" );
62 Q_ASSERT( mIsResponseFinished );
63 Q_ASSERT( mFeedback );
65#if defined(Q_OS_UNIX) && !defined(Q_OS_ANDROID)
66 if ( FCGI_stdout && FCGI_stdout->fcgx_stream && FCGI_stdout->fcgx_stream->data )
68 QgsFCGXStreamData *stream =
static_cast<QgsFCGXStreamData *
>( FCGI_stdin->fcgx_stream->data );
69 if ( stream && stream->reqDataPtr )
71 mIpcFd = stream->reqDataPtr->ipcFd;
76 QStringLiteral(
"FCGIServer" ),
83 QStringLiteral(
"FCGIServer" ),
94 QStringLiteral(
"FCGIServer" ),
99#if defined(Q_OS_UNIX) && !defined(Q_OS_ANDROID)
101 while ( !*mIsResponseFinished )
103 const ssize_t x = recv( mIpcFd, &
c, 1, MSG_PEEK | MSG_DONTWAIT );
107 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: remote socket still connected. errno: %1" ).arg( errno ), 5 );
112 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: remote socket has been closed! errno: %1" ).arg( errno ), 2 );
117 QThread::msleep( 333L );
120 if ( *mIsResponseFinished )
122 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: socket monitoring quits normally." ), 2 );
126 QgsDebugMsgLevel( QStringLiteral(
"FCGIServer: socket monitoring quits: no more socket." ), 2 );
140 mBuffer.open( QIODevice::ReadWrite );
143 mSocketMonitoringThread = std::make_unique<QgsSocketMonitoringThread>( &mFinished, mFeedback.get() );
144 mSocketMonitoringThread->start();
150 mSocketMonitoringThread->exit();
151 mSocketMonitoringThread->wait();
156 mHeaders.remove( key );
161 mHeaders.insert( key, value );
166 return mHeaders.value( key );
177 mHeaders.insert( QStringLiteral(
"Status" ), QStringLiteral(
" %1" ).arg( code ) );
187 QStringLiteral(
"FCGIServer" ),
194 setHeader( QStringLiteral(
"Content-Type" ), QStringLiteral(
"text/html;charset=utf-8" ) );
195 write( QStringLiteral(
"<html><body>%1</body></html>" ).arg( message ) );
209 QStringLiteral(
"FCGIServer" ),
214 if ( mFeedback->isCanceled() )
217 FCGI_stdout->fcgx_stream->wasFCloseCalled =
true;
224 if ( ! mHeaders.contains(
"Content-Length" ) )
226 mHeaders.insert( QStringLiteral(
"Content-Length" ), QString::number( mBuffer.pos() ) );
235 if ( ! mHeadersSent )
238 QMap<QString, QString>::const_iterator it;
239 for ( it = mHeaders.constBegin(); it != mHeaders.constEnd(); ++it )
241 fputs( it.key().toUtf8(), FCGI_stdout );
242 fputs(
": ", FCGI_stdout );
243 fputs( it.value().toUtf8(), FCGI_stdout );
244 fputs(
"\n", FCGI_stdout );
246 fputs(
"\n", FCGI_stdout );
255 mBuffer.buffer().clear();
257 else if ( mBuffer.bytesAvailable() > 0 )
259 QByteArray &ba = mBuffer.buffer();
260 const size_t count = fwrite( (
void * )ba.data(), ba.size(), 1, FCGI_stdout );
262 qDebug() << QStringLiteral(
"Sent %1 blocks of %2 bytes" ).arg( count ).arg( ba.size() );
276 mBuffer.buffer().clear();
285 return mBuffer.data();
292 mBuffer.buffer().clear();
298 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)