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>().values() )
114 QString( headerKey ).replace( QLatin1Char(
'_' ), QLatin1Char(
' ' ) ), QgsStringUtils::Capitalization::TitleCase
115 ).replace( QLatin1Char(
' ' ), QLatin1Char(
'-' ) );
116 const char *result = getenv( QStringLiteral(
"HTTP_%1" ).arg( headerKey ).toStdString().c_str() );
117 if ( result && strlen( result ) > 0 )
125 if ( logLevel <= Qgis::MessageLevel::Info )
127 printRequestInfos(
url );
131 void 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" ) );
169 void QgsFcgiServerRequest::readData()
172 const char *lengthstr = getenv(
"CONTENT_LENGTH" );
175 bool success =
false;
176 int length = QString( lengthstr ).toInt( &success );
181 const char *request_body = getenv(
"REQUEST_BODY" );
182 if ( success && request_body )
184 QString body( request_body );
185 body.truncate( length );
186 mData.append( body.toUtf8() );
190 qDebug() <<
"fcgi: reading " << lengthstr <<
" bytes from " << ( request_body ?
"REQUEST_BODY" :
"stdin" );
195 for (
int i = 0; i < length; ++i )
197 mData.append( getchar() );
203 QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
213 void QgsFcgiServerRequest::printRequestInfos(
const QUrl &url )
215 QgsMessageLog::logMessage( QStringLiteral(
"******************** New request ***************" ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
217 const QStringList envVars
219 QStringLiteral(
"SERVER_NAME" ),
220 QStringLiteral(
"REQUEST_URI" ),
221 QStringLiteral(
"SCRIPT_NAME" ),
222 QStringLiteral(
"PATH_INFO" ),
223 QStringLiteral(
"HTTPS" ),
224 QStringLiteral(
"REMOTE_ADDR" ),
225 QStringLiteral(
"REMOTE_HOST" ),
226 QStringLiteral(
"SERVER_PORT" ),
227 QStringLiteral(
"QUERY_STRING" ),
228 QStringLiteral(
"REMOTE_USER" ),
229 QStringLiteral(
"REMOTE_IDENT" ),
230 QStringLiteral(
"CONTENT_TYPE" ),
231 QStringLiteral(
"REQUEST_METHOD" ),
232 QStringLiteral(
"AUTH_TYPE" ),
233 QStringLiteral(
"HTTP_PROXY" ),
234 QStringLiteral(
"NO_PROXY" ),
235 QStringLiteral(
"QGIS_PROJECT_FILE" ),
236 QStringLiteral(
"QGIS_SERVER_IGNORE_BAD_LAYERS" ),
237 QStringLiteral(
"QGIS_SERVER_SERVICE_URL" ),
238 QStringLiteral(
"QGIS_SERVER_WMS_SERVICE_URL" ),
239 QStringLiteral(
"QGIS_SERVER_WFS_SERVICE_URL" ),
240 QStringLiteral(
"QGIS_SERVER_WMTS_SERVICE_URL" ),
241 QStringLiteral(
"QGIS_SERVER_WCS_SERVICE_URL" ),
242 QStringLiteral(
"SERVER_PROTOCOL" )
245 QgsMessageLog::logMessage( QStringLiteral(
"Request URL: %2" ).arg(
url.url() ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
248 QgsMessageLog::logMessage( QStringLiteral(
"------------------------------------------------" ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
249 for (
const auto &envVar : envVars )
251 if ( getenv( envVar.toStdString().c_str() ) )
253 QgsMessageLog::logMessage( QStringLiteral(
"%1: %2" ).arg( envVar ).arg( QString( getenv( envVar.toStdString().c_str() ) ) ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
257 qDebug() <<
"Headers:";
258 qDebug() <<
"------------------------------------------------";
259 for (
const auto &headerName :
headers().keys() )
261 qDebug() << headerName <<
": " <<
headers().value( headerName );
272 if ( result.isEmpty() )
274 result = qgetenv( QStringLiteral(
"HTTP_%1" ).arg(
275 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.
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, Capitalization capitalization)
Converts a string by applying capitalization rules to the string.