28 #include <QDomDocument> 31 #include <QTextStream> 32 #include <QStringList> 37 : mExceptionRaised( false )
39 , mResponse( response )
50 return mExceptionRaised;
70 return mResponse.
header( name );
90 return mRequest.
header( name );
107 mResponse.
write( body );
117 return mResponse.
data();
122 return mRequest.
data();
127 return mRequest.
url().toString();
149 mExceptionRaised =
true;
150 mResponse.
write( ex );
153 void QgsRequestHandler::setupParameters()
158 QString infoFormat = parameters.value( QStringLiteral(
"INFO_FORMAT" ) );
159 if ( !infoFormat.isEmpty() )
161 mFormat = infoFormat;
165 mFormatString = parameters.value( QStringLiteral(
"FORMAT" ) );
166 QString formatString = mFormatString;
167 if ( !formatString.isEmpty() )
172 if ( formatString.contains( QLatin1String(
"image/png" ), Qt::CaseInsensitive ) || formatString.compare( QLatin1String(
"png" ), Qt::CaseInsensitive ) == 0 )
174 formatString = QStringLiteral(
"PNG" );
176 else if ( formatString.contains( QLatin1String(
"image/jpeg" ), Qt::CaseInsensitive ) || formatString.contains( QLatin1String(
"image/jpg" ), Qt::CaseInsensitive )
177 || formatString.compare( QLatin1String(
"jpg" ), Qt::CaseInsensitive ) == 0 )
179 formatString = QStringLiteral(
"JPG" );
181 else if ( formatString.compare( QLatin1String(
"svg" ), Qt::CaseInsensitive ) == 0 )
183 formatString = QStringLiteral(
"SVG" );
185 else if ( formatString.contains( QLatin1String(
"pdf" ), Qt::CaseInsensitive ) )
187 formatString = QStringLiteral(
"PDF" );
190 mFormat = formatString;
200 QString inputString( mRequest.
data() );
206 if ( !doc.setContent( inputString,
true, &errorMsg, &line, &column ) )
209 QgsMessageLog::logMessage( QStringLiteral(
"Warning: error parsing post data as XML: at line %1, column %2: %3. Assuming urlencoded query string sent in the post body." )
210 .arg( line ).arg( column ).arg( errorMsg ) );
214 typedef QPair<QString, QString> pair_t;
215 QUrlQuery query( inputString );
216 QList<pair_t> items = query.queryItems();
217 for ( pair_t pair : items )
220 const QString key = QUrl::fromPercentEncoding( pair.first.replace(
'+',
' ' ).toUtf8() );
221 const QString value = QUrl::fromPercentEncoding( pair.second.replace(
'+',
' ' ).toUtf8() );
232 QDomElement docElem = doc.documentElement();
234 mRequest.
setParameter( QStringLiteral(
"REQUEST" ), docElem.tagName() );
237 QDomNamedNodeMap map = docElem.attributes();
238 for (
int i = 0 ; i < map.length() ; ++i )
240 if ( map.item( i ).isNull() )
243 const QDomNode attrNode = map.item( i );
244 const QDomAttr attr = attrNode.toAttr();
248 const QString attrName = attr.name();
249 if ( attrName.startsWith(
"xmlns" ) || attrName.startsWith(
"xsi:" ) )
252 mRequest.
setParameter( attrName.toUpper(), attr.value() );
254 mRequest.
setParameter( QStringLiteral(
"REQUEST_BODY" ), inputString );
266 if ( !( key.isEmpty() || value.isEmpty() ) )
270 if ( key.compare( QLatin1String(
"MAP" ), Qt::CaseInsensitive ) == 0 )
272 QgsMessageLog::logMessage( QStringLiteral(
"Changing the 'MAP' parameter will have no effect on config path: use QgsSerververInterface::setConfigFilePath instead" ),
bool exceptionRaised() const
Pointer to last raised exception.
void clearBody()
Clear response buffer.
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
virtual void truncate()=0
Truncate data.
void setParameter(const QString &key, const QString &value)
Set a parameter.
void setParameter(const QString &key, const QString &value)
Sets a request parameter.
QgsRequestHandler(QgsServerRequest &request, QgsServerResponse &response)
Constructor.
QByteArray data() const
Returns the request POST data (can be null)
virtual int statusCode() const =0
Returns the http status code.
virtual void clear()=0
Reset all headers and content for this response.
QMap< QString, QString > responseHeaders() const
Returns the response headers.
QString url() const
Returns the request url.
int statusCode() const
Returns the response http status code.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device...
QMap< QString, QString > headers() const
Returns the header map.
virtual void removeHeader(const QString &key)=0
Clear header Undo a previous 'setHeader' call.
QString responseHeader(const QString &name) const
Retrieve response header value.
QString parameter(const QString &key) const
Gets a parameter value.
void sendResponse()
Send out HTTP headers and flush output buffer.
void setStatusCode(int code)
Sets response http status code.
virtual void setStatusCode(int code)=0
Set the http status code.
bool headersSent() const
Returns true if the HTTP headers were already sent to the client.
void setResponseHeader(const QString &name, const QString &value)
Sets an HTTP response header.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
virtual QByteArray data() const
Returns post/put data Check for QByteArray::isNull() to check if data is available.
QMap< QString, QString > parameterMap() const
Returns the parsed parameters as a key-value pair, to modify a parameter setParameter( const QString ...
QString header(const QString &name) const
Returns the header value.
virtual void flush()=0
Flushes the current output buffer to the network.
void setServiceException(const QgsServerException &ex)
Allow plugins to return a QgsMapServiceException.
QByteArray body() const
Returns the response body data.
void appendBody(const QByteArray &body)
Sets the info format string such as "text/xml".
Exception base class for server exceptions.
QString requestHeader(const QString &name) const
Retrieve request header value.
virtual QString header(const QString &key) const =0
Returns the header value.
void setHeader(const QString &name, const QString &value)
Set an header.
virtual QByteArray data() const =0
Gets the data written so far.
QgsServerRequest::Method method() const
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
void parseInput()
Parses the input and creates a request neutral Parameter/Value map.
void setRequestHeader(const QString &name, const QString &value)
Sets an HTTP request header.
void removeHeader(const QString &name)
Remove an header.
void removeParameter(const QString &key)
Remove a request parameter.
void removeParameter(const QString &key)
Remove a parameter.
virtual QMap< QString, QString > headers() const =0
Returns the header value.
virtual bool headersSent() const =0
Returns true if the headers have already been sent.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
QString parameter(const QString &key) const
Returns a request parameter.
void removeRequestHeader(const QString &name)
Remove an HTTP request header.
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
QMap< QString, QString > requestHeaders() const
Returns the the Request headers.
void clear()
Clears the response body and headers.
void removeResponseHeader(const QString &name)
Remove an HTTP response header.
QMap< QString, QString > Parameters