25#include <fcgi_stdio.h>
31 QString uri = getenv(
"REQUEST_URI" );
35 uri = getenv(
"SCRIPT_NAME" );
44 const QString qs = getenv(
"QUERY_STRING" );
45 const QString questionMark = qs.isEmpty() ? QString() : QChar(
'?' );
46 const QString extraPath = QStringLiteral(
"%1%2%3" ).arg( getenv(
"PATH_INFO" ) ).arg( questionMark ).arg( qs );
49 if ( uri.endsWith( extraPath ) )
51 baseUrl.setUrl( uri.left( uri.length() - extraPath.length() ) );
69 qDebug() <<
"fcgi query string: " <<
url.query();
75 const char *me = getenv(
"REQUEST_METHOD" );
79 if ( strcmp( me,
"POST" ) == 0 )
83 else if ( strcmp( me,
"PUT" ) == 0 )
87 else if ( strcmp( me,
"DELETE" ) == 0 )
91 else if ( strcmp( me,
"HEAD" ) == 0 )
95 else if ( strcmp( me,
"PATCH" ) == 0 )
111 for (
const auto &headerKey : qgsEnumMap<QgsServerRequest::RequestHeader>() )
115 ).replace( QLatin1Char(
' ' ), QLatin1Char(
'-' ) );
116 const char *result = getenv( QStringLiteral(
"HTTP_%1" ).arg( headerKey ).toStdString().c_str() );
117 if ( result && strlen( result ) > 0 )
127 printRequestInfos(
url );
131void QgsFcgiServerRequest::fillUrl( QUrl &url )
const
134 if (
url.host().isEmpty() )
136 url.setHost( getenv(
"SERVER_NAME" ) );
140 if (
url.port( -1 ) == -1 )
142 const QString portString = getenv(
"SERVER_PORT" );
143 if ( !portString.isEmpty() )
146 const int portNumber = portString.toInt( &portOk );
147 if ( portOk && portNumber != 80 )
149 url.setPort( portNumber );
155 if (
url.scheme().isEmpty() )
157 QString( getenv(
"HTTPS" ) ).compare( QLatin1String(
"on" ), Qt::CaseInsensitive ) == 0
158 ?
url.setScheme( QStringLiteral(
"https" ) )
159 :
url.setScheme( QStringLiteral(
"http" ) );
169void QgsFcgiServerRequest::readData()
172 const char *lengthstr = getenv(
"CONTENT_LENGTH" );
175 bool success =
false;
176 const int length = QString( lengthstr ).toInt( &success );
177 if ( !success || length < 0 )
189 const char *requestBody = getenv(
"REQUEST_BODY" );
192 qDebug() <<
"fcgi: reading " << lengthstr <<
" bytes from " << ( requestBody ?
"REQUEST_BODY" :
"stdin" );
197 const size_t requestBodyLength = strlen( requestBody );
198 const int actualLength =
static_cast<int>( std::min<size_t>( length, requestBodyLength ) );
199 if (
static_cast<size_t>( actualLength ) < requestBodyLength )
205 mData = QByteArray::fromRawData( requestBody, actualLength );
209 mData.resize( length );
210 const int actualLength =
static_cast<int>( fread( mData.data(), 1, length, stdin ) );
211 if ( actualLength < length )
213 mData.resize( actualLength );
227void QgsFcgiServerRequest::printRequestInfos(
const QUrl &url )
const
231 const QStringList envVars
233 QStringLiteral(
"SERVER_NAME" ),
234 QStringLiteral(
"REQUEST_URI" ),
235 QStringLiteral(
"SCRIPT_NAME" ),
236 QStringLiteral(
"PATH_INFO" ),
237 QStringLiteral(
"HTTPS" ),
238 QStringLiteral(
"REMOTE_ADDR" ),
239 QStringLiteral(
"REMOTE_HOST" ),
240 QStringLiteral(
"SERVER_PORT" ),
241 QStringLiteral(
"QUERY_STRING" ),
242 QStringLiteral(
"REMOTE_USER" ),
243 QStringLiteral(
"REMOTE_IDENT" ),
244 QStringLiteral(
"CONTENT_TYPE" ),
245 QStringLiteral(
"REQUEST_METHOD" ),
246 QStringLiteral(
"AUTH_TYPE" ),
247 QStringLiteral(
"HTTP_PROXY" ),
248 QStringLiteral(
"NO_PROXY" ),
249 QStringLiteral(
"QGIS_PROJECT_FILE" ),
250 QStringLiteral(
"QGIS_SERVER_IGNORE_BAD_LAYERS" ),
251 QStringLiteral(
"QGIS_SERVER_SERVICE_URL" ),
252 QStringLiteral(
"QGIS_SERVER_WMS_SERVICE_URL" ),
253 QStringLiteral(
"QGIS_SERVER_WFS_SERVICE_URL" ),
254 QStringLiteral(
"QGIS_SERVER_WMTS_SERVICE_URL" ),
255 QStringLiteral(
"QGIS_SERVER_WCS_SERVICE_URL" ),
256 QStringLiteral(
"SERVER_PROTOCOL" )
263 for (
const auto &envVar : envVars )
265 if ( getenv( envVar.toStdString().c_str() ) )
271 qDebug() <<
"Headers:";
272 qDebug() <<
"------------------------------------------------";
273 const QMap<QString, QString> &hdrs =
headers();
274 for (
auto it = hdrs.constBegin(); it != hdrs.constEnd(); it++ )
276 qDebug() << it.key() <<
": " << it.value();
287 if ( result.isEmpty() )
289 result = qgetenv( QStringLiteral(
"HTTP_%1" ).arg(
290 name.toUpper().replace( QLatin1Char(
'-' ), QLatin1Char(
'_' ) ) ).toStdString().c_str() );
MessageLevel
Level for messages This will be used both for message log and message bar in application.
@ Critical
Critical/error message.
@ Info
Information message.
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
QString header(const QString &name) const override
Returns the header value.
QByteArray data() const override
Returns post/put data Check for QByteArray::isNull() to check if data is available.
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).
static QgsServerLogger * instance()
Gets the singleton instance.
Qgis::MessageLevel logLevel() const
Gets the current log level.
void setOriginalUrl(const QUrl &url)
Set the request original url (the request url as seen by the web server)
Method
HTTP Method (or equivalent) used for the request.
virtual QString header(const QString &name) const
Returns the header value.
virtual void setUrl(const QUrl &url)
Set the request url.
QMap< QString, QString > headers() const
Returns the header map.
QUrl baseUrl() const
Returns the base URL of QGIS server.
QgsServerRequest::Method method() const
void setMethod(QgsServerRequest::Method method)
Set the request method.
void setBaseUrl(const QUrl &url)
Set the base URL of QGIS server.
void setHeader(const QString &name, const QString &value)
Set an header.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.