QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
server
qgsserverstatichandler.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsserverstatichandler.cpp - QgsServerStaticHandler
3
4
---------------------
5
begin : 30.7.2020
6
copyright : (C) 2020 by Alessandro Pasotti
7
email : elpaso at itopen dot it
8
***************************************************************************
9
* *
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
14
* *
15
***************************************************************************/
16
#include "
qgsserverstatichandler.h
"
17
18
#include "
qgsmessagelog.h
"
19
#include "
qgsserverresponse.h
"
20
21
#include <QFile>
22
#include <QMimeDatabase>
23
#include <QString>
24
25
using namespace
Qt::StringLiterals;
26
27
QgsServerStaticHandler::QgsServerStaticHandler
(
const
QString &pathRegExp,
const
QString &staticPathSuffix )
28
: mPathRegExp( pathRegExp )
29
, mStaticPathSuffix( staticPathSuffix )
30
{
31
setContentTypes
( {
QgsServerOgcApi::ContentType::HTML
} );
32
}
33
34
void
QgsServerStaticHandler::handleRequest
(
const
QgsServerApiContext
&context )
const
35
{
36
const
QRegularExpressionMatch match {
path
().match( context.
request
()->
url
().path() ) };
37
if
( !match.hasMatch() )
38
{
39
throw
QgsServerApiNotFoundError
( u
"Static file was not found"
_s );
40
}
41
42
const
QString staticFilePath { match.captured( u
"staticFilePath"
_s ) };
43
// Calculate real path
44
QString filePath {
staticPath
( context ) };
45
if
( !mStaticPathSuffix.isEmpty() )
46
{
47
filePath +=
'/'
+ mStaticPathSuffix;
48
}
49
filePath +=
'/'
+ staticFilePath;
50
if
( !QFile::exists( filePath ) )
51
{
52
QgsMessageLog::logMessage
( u
"Static file was not found: %1"
_s.arg( filePath ), u
"Server"
_s,
Qgis::MessageLevel::Info
);
53
throw
QgsServerApiNotFoundError
( u
"Static file %1 was not found"
_s.arg( staticFilePath ) );
54
}
55
56
QFile f( filePath );
57
if
( !f.open( QIODevice::ReadOnly ) )
58
{
59
throw
QgsServerApiInternalServerError
( u
"Could not open static file %1"
_s.arg( staticFilePath ) );
60
}
61
62
const
qint64 size { f.size() };
63
const
QByteArray content { f.readAll() };
64
const
QMimeType mimeType { QMimeDatabase().mimeTypeForFile( filePath ) };
65
context.
response
()->
setHeader
( u
"Content-Type"
_s, mimeType.name() );
66
context.
response
()->
setHeader
( u
"Content-Length"
_s, QString::number( size ) );
67
context.
response
()->
write
( content );
68
}
Qgis::Info
@ Info
Information message.
Definition
qgis.h:161
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Definition
qgsmessagelog.cpp:35
QgsServerApiContext
Encapsulates the resources for a particular client request.
Definition
qgsserverapicontext.h:41
QgsServerApiContext::response
QgsServerResponse * response() const
Returns the server response object.
Definition
qgsserverapicontext.cpp:37
QgsServerApiContext::request
const QgsServerRequest * request() const
Returns the server request object.
Definition
qgsserverapicontext.cpp:31
QgsServerApiInternalServerError
Internal server error API exception.
Definition
qgsserverexception.h:185
QgsServerApiNotFoundError
Not found error API exception.
Definition
qgsserverexception.h:205
QgsServerOgcApiHandler::staticPath
virtual const QString staticPath(const QgsServerApiContext &context) const
Returns the absolute path to the base directory where static resources for this handler are stored in...
Definition
qgsserverogcapihandler.cpp:279
QgsServerOgcApiHandler::setContentTypes
void setContentTypes(const QList< QgsServerOgcApi::ContentType > &contentTypes)
Set the content types to contentTypes.
Definition
qgsserverogcapihandler.cpp:743
QgsServerOgcApi::HTML
@ HTML
Definition
qgsserverogcapi.h:89
QgsServerRequest::url
QUrl url() const
Returns the request URL as seen by QGIS server.
Definition
qgsserverrequest.cpp:88
QgsServerResponse::write
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
Definition
qgsserverresponse.cpp:29
QgsServerResponse::setHeader
virtual void setHeader(const QString &key, const QString &value)=0
Set a single header value replacing any existing value(s) for the same key.
QgsServerStaticHandler::path
QRegularExpression path() const override
URL pattern for this handler, named capture group are automatically extracted and returned by values(...
Definition
qgsserverstatichandler.h:47
QgsServerStaticHandler::handleRequest
void handleRequest(const QgsServerApiContext &context) const override
Handles the request within its context.
Definition
qgsserverstatichandler.cpp:34
QgsServerStaticHandler::QgsServerStaticHandler
QgsServerStaticHandler(const QString &pathRegExp=u"/static/(?<staticFilePath>.*)$"_s, const QString &staticPathSuffix=QString())
Creates QgsServerStaticHandler.
Definition
qgsserverstatichandler.cpp:27
qgsmessagelog.h
qgsserverresponse.h
qgsserverstatichandler.h
Generated on
for QGIS API Documentation by
1.17.0