Skip to content
Permalink
Browse files

Add headers keys and value accessors

  • Loading branch information
dmarteau committed Jan 10, 2017
1 parent 12f3b5f commit 262969015d9693cac64f9e94b183f5df7a77c00b
@@ -69,7 +69,13 @@ class QgsRequestHandler /Abstract/
/** Set an HTTP header*/
void setHeader( const QString &name, const QString &value );

/** Remove an HTTP header*/
//! Retrieve header value
QString getHeader( const QString& name ) const;

//! Return the list of all header keys
QList<QString> headerKeys() const;

/** Remove an HTTP header*/
void removeHeader( const QString &name );

/** Delete all HTTP headers*/
@@ -91,13 +97,13 @@ class QgsRequestHandler /Abstract/
* a parameter setParameter( const QString &key, const QString &value)
* and removeParameter(const QString &key) must be used
*/
QMap<QString, QString> parameterMap();
QMap<QString, QString> parameterMap() const;

/** Set a request parameter*/
void setParameter( const QString &key, const QString &value );

/** Remove a request parameter*/
int removeParameter( const QString &key );
void removeParameter( const QString &key );

/** Return a request parameter*/
QString parameter( const QString &key ) const;
@@ -109,7 +115,7 @@ class QgsRequestHandler /Abstract/
QString infoFormat() const;

/** Return true if the HTTP headers were already sent to the client*/
bool headersSent();
bool headersSent() const;


//! @note not available in Python bindings
@@ -183,7 +183,7 @@ class QgsServer
* @param request a QgsServerRequest holding request parameters
* @param response a QgsServerResponse for handling response I/O)
*/
void handleRequest( const QgsServerRequest& request, QgsServerResponse& response );
void handleRequest( QgsServerRequest& request, QgsServerResponse& response );

/** Handles the request from query strinf
* The query string is normally read from environment
@@ -202,3 +202,4 @@ class QgsServer
QgsServer( const QgsServer& );
QgsServer & operator=( const QgsServer& );
};

@@ -48,7 +48,21 @@ class QgsServerResponse
*/
virtual void clearHeader( const QString& key ) = 0;

/**
* Return the header value
*/
virtual QString getHeader( const QString& key ) const = 0;

/**
* Return the list of all header keys
*/
virtual QList<QString> headerKeys() const = 0;

/**
* Return true if the headers have alredy beeing written
*/
virtual bool headersWritten() const = 0;

/** Set the http return code
* @param code HTTP return code value
*/
@@ -57,6 +57,21 @@ void QgsBufferServerResponse::setReturnCode( int code )
mReturnCode = code;
}

QString QgsBufferServerResponse::getHeader( const QString& key ) const
{
return mHeaders.value( key );
}

QList<QString> QgsBufferServerResponse::headerKeys() const
{
return mHeaders.keys();
}

bool QgsBufferServerResponse::headersWritten() const
{
return mHeadersWritten;
}

void QgsBufferServerResponse::sendError( int code, const QString& message )
{
if ( mHeadersWritten )
@@ -112,9 +127,7 @@ void QgsBufferServerResponse::flush()

void QgsBufferServerResponse::clear()
{
if ( !mHeadersWritten )
mHeaders.clear();

mHeaders.clear();
mBuffer.seek( 0 );
mBuffer.buffer().clear();
}
@@ -42,6 +42,12 @@ class QgsBufferServerResponse: public QgsServerResponse

virtual void clearHeader( const QString& key ) override;

virtual QString getHeader( const QString& key ) const override;

virtual QList<QString> headerKeys() const override;

virtual bool headersWritten() const override;

virtual void setReturnCode( int code ) override;

virtual void sendError( int code, const QString& message ) override;
@@ -52,6 +52,21 @@ void QgsFcgiServerResponse::setHeader( const QString& key, const QString& value
mHeaders.insert( key, value );
}

QString QgsFcgiServerResponse::getHeader( const QString& key ) const
{
return mHeaders.value( key );
}

QList<QString> QgsFcgiServerResponse::headerKeys() const
{
return mHeaders.keys();
}

bool QgsFcgiServerResponse::headersWritten() const
{
return mHeadersWritten;
}

void QgsFcgiServerResponse::setReturnCode( int code )
{
// fcgi applications must return HTTP status in header
@@ -40,6 +40,12 @@ class QgsFcgiServerResponse: public QgsServerResponse

virtual void clearHeader( const QString& key ) override;

virtual QString getHeader( const QString& key ) const override;

virtual QList<QString> headerKeys() const override;

virtual bool headersWritten() const override;

virtual void setReturnCode( int code ) override;

virtual void sendError( int code, const QString& message ) override;
@@ -37,23 +37,23 @@
#include <QUrl>
#include <QUrlQuery>

QgsRequestHandler::QgsRequestHandler( const QgsServerRequest& request, QgsServerResponse& response )
: mHeadersSent(false)
, mException(nullptr)
QgsRequestHandler::QgsRequestHandler( QgsServerRequest& request, QgsServerResponse& response )
: mException( nullptr )
, mRequest( request )
, mResponse( response )
{
mException = nullptr;
mHeadersSent = false;

mParameterMap = mRequest.parameters();
}

QgsRequestHandler::~QgsRequestHandler()
{
delete mException;
}

QMap<QString, QString> QgsRequestHandler::parameterMap() const
{
return mRequest.parameters();
}

void QgsRequestHandler::setHttpResponse( const QByteArray& ba, const QString &format )
{
QgsMessageLog::logMessage( QStringLiteral( "Checking byte array is ok to set..." ) );
@@ -88,6 +88,22 @@ void QgsRequestHandler::removeHeader( const QString &name )
mResponse.clearHeader( name );
}

QString QgsRequestHandler::getHeader( const QString& name ) const
{
return mResponse.getHeader( name );
}

QList<QString> QgsRequestHandler::headerKeys() const
{
return mResponse.headerKeys();
}

bool QgsRequestHandler::headersSent() const
{
return mResponse.headersWritten();
}


void QgsRequestHandler::appendBody( const QByteArray &body )
{
mResponse.write( body );
@@ -129,9 +145,6 @@ void QgsRequestHandler::sendResponse()

// Send data to output
mResponse.flush();
// XXX for compatibility only
// Headers are always sent first
mHeadersSent = true;
}


@@ -385,7 +398,7 @@ void QgsRequestHandler::setServiceException( const QgsMapServiceException& ex )

void QgsRequestHandler::setGetPrintResponse( QByteArray* ba )
{
if(ba)
if ( ba )
{
setHttpResponse( *ba, formatToMimeType( mFormat ) );
}
@@ -446,15 +459,18 @@ void QgsRequestHandler::endGetFeatureResponse( QByteArray* ba )

void QgsRequestHandler::setGetCoverageResponse( QByteArray* ba )
{
if(ba)
if ( ba )
{
setHttpResponse( *ba, QStringLiteral( "image/tiff" ) );
}
}

void QgsRequestHandler::requestStringToParameterMap( QMap<QString, QString>& parameters )
void QgsRequestHandler::setupParameters()
{
const QgsServerRequest::Parameters parameters = mRequest.parameters();

// SLD

QString value = parameters.value( QStringLiteral( "SLD" ) );
if ( !value.isEmpty() )
{
@@ -483,7 +499,7 @@ void QgsRequestHandler::requestStringToParameterMap( QMap<QString, QString>& par

if fileContents.size() > 0 )
{
parameters.insert( QStringLiteral( "SLD" ), QUrl::fromPercentEncoding( fileContents ) );
mRequest.setParameter( QStringLiteral( "SLD" ), QUrl::fromPercentEncoding( fileContents ) );
}
#else
QgsMessageLog::logMessage( QStringLiteral( "http and ftp methods not supported with Qt5." ) );
@@ -495,7 +511,7 @@ void QgsRequestHandler::requestStringToParameterMap( QMap<QString, QString>& par
value = parameters.value( QStringLiteral( "SLD_BODY" ) );
if ( ! value.isEmpty() )
{
parameters.insert( QStringLiteral( "SLD" ), value );
mRequest.setParameter( QStringLiteral( "SLD" ), value );
}

//feature info format?
@@ -560,33 +576,32 @@ void QgsRequestHandler::parseInput()
QList<pair_t> items = query.queryItems();
Q_FOREACH ( const pair_t& pair, items )
{
mParameterMap.insert( pair.first.toUpper(), pair.second );
mRequest.setParameter( pair.first.toUpper(), pair.second );
}
requestStringToParameterMap( mParameterMap );
setupParameters();
}
else
{
// we have an XML document

requestStringToParameterMap( mParameterMap );
setupParameters();

QDomElement docElem = doc.documentElement();
if ( docElem.hasAttribute( QStringLiteral( "version" ) ) )
{
mParameterMap.insert( QStringLiteral( "VERSION" ), docElem.attribute( QStringLiteral( "version" ) ) );
mRequest.setParameter( QStringLiteral( "VERSION" ), docElem.attribute( QStringLiteral( "version" ) ) );
}
if ( docElem.hasAttribute( QStringLiteral( "service" ) ) )
{
mParameterMap.insert( QStringLiteral( "SERVICE" ), docElem.attribute( QStringLiteral( "service" ) ) );
mRequest.setParameter( QStringLiteral( "SERVICE" ), docElem.attribute( QStringLiteral( "service" ) ) );
}
mParameterMap.insert( QStringLiteral( "REQUEST" ), docElem.tagName() );
mParameterMap.insert( QStringLiteral( "REQUEST_BODY" ), inputString );

mRequest.setParameter( QStringLiteral( "REQUEST" ), docElem.tagName() );
mRequest.setParameter( QStringLiteral( "REQUEST_BODY" ), inputString );
}
}
else
{
requestStringToParameterMap( mParameterMap );
setupParameters();
}

}
@@ -595,19 +610,19 @@ void QgsRequestHandler::setParameter( const QString &key, const QString &value )
{
if ( !( key.isEmpty() || value.isEmpty() ) )
{
mParameterMap.insert( key, value );
mRequest.setParameter( key, value );
}
}


QString QgsRequestHandler::parameter( const QString &key ) const
{
return mParameterMap.value( key );
return mRequest.getParameter( key );
}

int QgsRequestHandler::removeParameter( const QString &key )
void QgsRequestHandler::removeParameter( const QString &key )
{
return mParameterMap.remove( key );
mRequest.removeParameter( key );
}


@@ -712,7 +727,7 @@ void QgsRequestHandler::imageColors( QHash<QRgb, int>& colors, const QImage& ima
}

void QgsRequestHandler::splitColorBox( QgsColorBox& colorBox, QgsColorBoxMap& colorBoxMap,
QMap<int, QgsColorBox>::iterator colorBoxMapIt )
QMap<int, QgsColorBox>::iterator colorBoxMapIt )
{

if ( colorBox.size() < 2 )

0 comments on commit 2629690

Please sign in to comment.
You can’t perform that action at this time.