24 #include <fcgi_stdio.h>
30 QString uri = getenv(
"REQUEST_URI" );
34 uri = getenv(
"SCRIPT_NAME" );
43 const QString qs = getenv(
"QUERY_STRING" );
44 const QString questionMark = qs.isEmpty() ? QString() : QChar(
'?' );
45 const QString extraPath = QStringLiteral(
"%1%2%3" ).arg( getenv(
"PATH_INFO" ) ).arg( questionMark ).arg( qs );
48 if ( uri.endsWith( extraPath ) )
50 baseUrl.setUrl( uri.left( uri.length() - extraPath.length() ) );
68 qDebug() <<
"fcgi query string: " <<
url.query();
74 const char *me = getenv(
"REQUEST_METHOD" );
78 if ( strcmp( me,
"POST" ) == 0 )
82 else if ( strcmp( me,
"PUT" ) == 0 )
86 else if ( strcmp( me,
"DELETE" ) == 0 )
90 else if ( strcmp( me,
"HEAD" ) == 0 )
94 else if ( strcmp( me,
"PATCH" ) == 0 )
110 const char *accept = getenv(
"HTTP_ACCEPT" );
113 setHeader( QStringLiteral(
"Accept" ), accept );
118 if ( logLevel <= Qgis::MessageLevel::Info )
120 printRequestInfos(
url );
124 void QgsFcgiServerRequest::fillUrl( QUrl &url )
const
127 if (
url.host().isEmpty() )
129 url.setHost( getenv(
"SERVER_NAME" ) );
133 if (
url.port( -1 ) == -1 )
135 const QString portString = getenv(
"SERVER_PORT" );
136 if ( !portString.isEmpty() )
139 const int portNumber = portString.toInt( &portOk );
140 if ( portOk && portNumber != 80 )
142 url.setPort( portNumber );
148 if (
url.scheme().isEmpty() )
150 QString( getenv(
"HTTPS" ) ).compare( QLatin1String(
"on" ), Qt::CaseInsensitive ) == 0
151 ?
url.setScheme( QStringLiteral(
"https" ) )
152 :
url.setScheme( QStringLiteral(
"http" ) );
162 void QgsFcgiServerRequest::readData()
165 const char *lengthstr = getenv(
"CONTENT_LENGTH" );
168 bool success =
false;
169 int length = QString( lengthstr ).toInt( &success );
174 const char *request_body = getenv(
"REQUEST_BODY" );
175 if ( success && request_body )
177 QString body( request_body );
178 body.truncate( length );
179 mData.append( body.toUtf8() );
183 qDebug() <<
"fcgi: reading " << lengthstr <<
" bytes from " << ( request_body ?
"REQUEST_BODY" :
"stdin" );
188 for (
int i = 0; i < length; ++i )
190 mData.append( getchar() );
196 QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
206 void QgsFcgiServerRequest::printRequestInfos(
const QUrl &url )
208 QgsMessageLog::logMessage( QStringLiteral(
"******************** New request ***************" ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
210 const QStringList envVars
212 QStringLiteral(
"SERVER_NAME" ),
213 QStringLiteral(
"REQUEST_URI" ),
214 QStringLiteral(
"SCRIPT_NAME" ),
215 QStringLiteral(
"PATH_INFO" ),
216 QStringLiteral(
"HTTPS" ),
217 QStringLiteral(
"REMOTE_ADDR" ),
218 QStringLiteral(
"REMOTE_HOST" ),
219 QStringLiteral(
"SERVER_PORT" ),
220 QStringLiteral(
"QUERY_STRING" ),
221 QStringLiteral(
"REMOTE_USER" ),
222 QStringLiteral(
"REMOTE_IDENT" ),
223 QStringLiteral(
"CONTENT_TYPE" ),
224 QStringLiteral(
"REQUEST_METHOD" ),
225 QStringLiteral(
"AUTH_TYPE" ),
226 QStringLiteral(
"HTTP_ACCEPT" ),
227 QStringLiteral(
"HTTP_USER_AGENT" ),
228 QStringLiteral(
"HTTP_PROXY" ),
229 QStringLiteral(
"NO_PROXY" ),
230 QStringLiteral(
"HTTP_AUTHORIZATION" ),
231 QStringLiteral(
"QGIS_PROJECT_FILE" ),
232 QStringLiteral(
"QGIS_SERVER_IGNORE_BAD_LAYERS" ),
233 QStringLiteral(
"QGIS_SERVER_SERVICE_URL" ),
234 QStringLiteral(
"QGIS_SERVER_WMS_SERVICE_URL" ),
235 QStringLiteral(
"QGIS_SERVER_WFS_SERVICE_URL" ),
236 QStringLiteral(
"QGIS_SERVER_WMTS_SERVICE_URL" ),
237 QStringLiteral(
"QGIS_SERVER_WCS_SERVICE_URL" ),
238 QStringLiteral(
"HTTP_X_QGIS_SERVICE_URL" ),
239 QStringLiteral(
"HTTP_X_QGIS_WMS_SERVICE_URL" ),
240 QStringLiteral(
"HTTP_X_QGIS_WFS_SERVICE_URL" ),
241 QStringLiteral(
"HTTP_X_QGIS_WCS_SERVICE_URL" ),
242 QStringLiteral(
"HTTP_X_QGIS_WMTS_SERVICE_URL" ),
243 QStringLiteral(
"HTTP_FORWARDED" ),
244 QStringLiteral(
"HTTP_X_FORWARDED_HOST" ),
245 QStringLiteral(
"HTTP_X_FORWARDED_PROTO" ),
246 QStringLiteral(
"HTTP_HOST" ),
247 QStringLiteral(
"SERVER_PROTOCOL" )
250 QgsMessageLog::logMessage( QStringLiteral(
"Request URL: %2" ).arg(
url.url() ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
252 QgsMessageLog::logMessage( QStringLiteral(
"------------------------------------------------" ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
254 for (
const auto &envVar : envVars )
256 if ( getenv( envVar.toStdString().c_str() ) )
258 QgsMessageLog::logMessage( QStringLiteral(
"%1: %2" ).arg( envVar ).arg( QString( getenv( envVar.toStdString().c_str() ) ) ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
270 if ( result.isEmpty() )
272 result = qgetenv( QStringLiteral(
"HTTP_%1" ).arg(
273 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.
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.