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 #include "qgsmessagelog.h"
18 #include "qgsserverresponse.h"
19 
20 #include <QFile>
21 #include <QMimeDatabase>
22 
23 
QgsServerStaticHandler(const QString & pathRegExp,const QString & staticPathSuffix)24 QgsServerStaticHandler::QgsServerStaticHandler( const QString &pathRegExp, const QString &staticPathSuffix )
25   : mPathRegExp( pathRegExp )
26   , mStaticPathSuffix( staticPathSuffix )
27 {
28   setContentTypes( { QgsServerOgcApi::ContentType::HTML } );
29 }
30 
handleRequest(const QgsServerApiContext & context) const31 void QgsServerStaticHandler::handleRequest( const QgsServerApiContext &context ) const
32 {
33   const QRegularExpressionMatch match { path().match( context.request()->url().path( ) ) };
34   if ( ! match.hasMatch() )
35   {
36     throw QgsServerApiNotFoundError( QStringLiteral( "Static file was not found" ) );
37   }
38 
39   const QString staticFilePath { match.captured( QStringLiteral( "staticFilePath" ) ) };
40   // Calculate real path
41   QString filePath { staticPath( context ) };
42   if ( ! mStaticPathSuffix.isEmpty() )
43   {
44     filePath += '/' + mStaticPathSuffix;
45   }
46   filePath += '/' + staticFilePath;
47   if ( ! QFile::exists( filePath ) )
48   {
49     QgsMessageLog::logMessage( QStringLiteral( "Static file was not found: %1" ).arg( filePath ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
50     throw QgsServerApiNotFoundError( QStringLiteral( "Static file %1 was not found" ).arg( staticFilePath ) );
51   }
52 
53   QFile f( filePath );
54   if ( ! f.open( QIODevice::ReadOnly ) )
55   {
56     throw QgsServerApiInternalServerError( QStringLiteral( "Could not open static file %1" ).arg( staticFilePath ) );
57   }
58 
59   const qint64 size { f.size() };
60   const QByteArray content { f.readAll() };
61   const QMimeType mimeType { QMimeDatabase().mimeTypeForFile( filePath )};
62   context.response()->setHeader( QStringLiteral( "Content-Type" ), mimeType.name() );
63   context.response()->setHeader( QStringLiteral( "Content-Length" ), QString::number( size ) );
64   context.response()->write( content );
65 }
66