QGIS API Documentation 4.1.0-Master (376402f9aeb)
Loading...
Searching...
No Matches
qgis_mapserver.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgs_mapserver.cpp
3
4A QGIS development HTTP server for testing/development purposes.
5The server listens to localhost:8000, the address and port can be changed with the
6environment variable QGIS_SERVER_ADDRESS and QGIS_SERVER_PORT or passing <address>:<port>
7on the command line.
8
9All requests and application messages are printed to the standard output,
10while QGIS server internal logging is printed to stderr.
11
12 -------------------
13 begin : Jan 17 2020
14 copyright : (C) 2020 by Alessandro Pasotti
15 email : elpaso at itopen dot it
16 ***************************************************************************/
17
18/***************************************************************************
19 * *
20 * This program is free software; you can redistribute it and/or modify *
21 * it under the terms of the GNU General Public License as published by *
22 * the Free Software Foundation; either version 2 of the License, or *
23 * (at your option) any later version. *
24 * *
25 ***************************************************************************/
26
27#include <chrono>
28#include <condition_variable>
29#include <string>
30#include <thread>
31
32#include <QString>
33
34using namespace Qt::StringLiterals;
35
36//for CMAKE_INSTALL_PREFIX
37#include "qgscommandlineutils.h"
38#include "qgsconfig.h"
39#include "qgsserver.h"
42#include "qgsapplication.h"
43#include "qgsmessagelog.h"
44
45#include <QFontDatabase>
46#include <QString>
47#include <QTcpServer>
48#include <QTcpSocket>
49#include <QNetworkInterface>
50#include <QCommandLineParser>
51#include <QObject>
52#include <QQueue>
53#include <QThread>
54#include <QPointer>
55
56#ifndef Q_OS_WIN
57#include <csignal>
58#endif
59
61
62// For the signal exit handler
63QAtomicInt IS_RUNNING = 1;
64
65QString ipAddress;
66QString serverPort;
67
68std::condition_variable REQUEST_WAIT_CONDITION;
69std::mutex REQUEST_QUEUE_MUTEX;
70std::mutex SERVER_MUTEX;
71
72struct RequestContext
73{
74 QPointer<QTcpSocket> clientConnection;
75 QString httpHeader;
76 std::chrono::steady_clock::time_point startTime;
77 QgsBufferServerRequest request;
78 QgsBufferServerResponse response;
79};
80
81
82QQueue<RequestContext *> REQUEST_QUEUE;
83
84const QMap<int, QString> knownStatuses {
85 { 200, u"OK"_s },
86 { 201, u"Created"_s },
87 { 202, u"Accepted"_s },
88 { 204, u"No Content"_s },
89 { 301, u"Moved Permanently"_s },
90 { 302, u"Moved Temporarily"_s },
91 { 304, u"Not Modified"_s },
92 { 400, u"Bad Request"_s },
93 { 401, u"Unauthorized"_s },
94 { 403, u"Forbidden"_s },
95 { 404, u"Not Found"_s },
96 { 500, u"Internal Server Error"_s },
97 { 501, u"Not Implemented"_s },
98 { 502, u"Bad Gateway"_s },
99 { 503, u"Service Unavailable"_s }
100};
101
105class HttpException : public std::exception
106{
107 public:
111 HttpException( const QString &message )
112 : mMessage( message )
113 {}
114
118 QString message() const { return mMessage; }
119
120 private:
121 QString mMessage;
122};
123
124
125class TcpServerWorker : public QObject
126{
127 Q_OBJECT
128
129 public:
130 TcpServerWorker( const QString &ipAddress, int port )
131 {
132 QHostAddress address { QHostAddress::AnyIPv4 };
133 address.setAddress( ipAddress );
134
135 if ( !mTcpServer.listen( address, port ) )
136 {
137 std::cerr << tr( "Unable to start the server: %1." ).arg( mTcpServer.errorString() ).toStdString() << std::endl;
138 }
139 else
140 {
141 const int port { mTcpServer.serverPort() };
142
143 std::cout << tr( "QGIS Development Server listening on http://%1:%2" ).arg( ipAddress ).arg( port ).toStdString() << std::endl;
144#ifndef Q_OS_WIN
145 std::cout << tr( "CTRL+C to exit" ).toStdString() << std::endl;
146#endif
147
148 mIsListening = true;
149
150 // Incoming connection handler
151 QTcpServer::connect( &mTcpServer, &QTcpServer::newConnection, this, [this, ipAddress, port] {
152 QTcpSocket *clientConnection = mTcpServer.nextPendingConnection();
153
154 mConnectionCounter++;
155
156 //qDebug() << "Active connections: " << mConnectionCounter;
157
158 QString *incomingData = new QString();
159
160 // Lambda disconnect context
161 QObject *context { new QObject };
162
163 // Deletes the connection later
164 auto connectionDeleter = [this, clientConnection, incomingData]() {
165 clientConnection->deleteLater();
166 mConnectionCounter--;
167 delete incomingData;
168 };
169
170 // This will delete the connection
171 QObject::connect( clientConnection, &QAbstractSocket::disconnected, clientConnection, connectionDeleter, Qt::QueuedConnection );
172
173#if 0 // Debugging output
174 clientConnection->connect( clientConnection, &QAbstractSocket::errorOccurred, clientConnection, []( QAbstractSocket::SocketError socketError )
175 {
176 qDebug() << "Socket error #" << socketError;
177 }, Qt::QueuedConnection );
178#endif
179
180 // Incoming connection parser
181 QObject::connect( clientConnection, &QIODevice::readyRead, context, [clientConnection, incomingData, context, ipAddress, port] {
182 // Read all incoming data
183 while ( clientConnection->bytesAvailable() > 0 )
184 {
185 incomingData->append( clientConnection->readAll() );
186 }
187
188 try
189 {
190 // Parse protocol and URL GET /path HTTP/1.1
191 const auto firstLinePos { incomingData->indexOf( "\r\n" ) };
192 if ( firstLinePos == -1 )
193 {
194 throw HttpException( u"HTTP error finding protocol header"_s );
195 }
196
197 const QString firstLine { incomingData->left( firstLinePos ) };
198 const QStringList firstLinePieces { firstLine.split( ' ' ) };
199 if ( firstLinePieces.size() != 3 )
200 {
201 throw HttpException( u"HTTP error splitting protocol header"_s );
202 }
203
204 const QString methodString { firstLinePieces.at( 0 ) };
205
207 if ( methodString == "GET" )
208 {
210 }
211 else if ( methodString == "POST" )
212 {
214 }
215 else if ( methodString == "HEAD" )
216 {
218 }
219 else if ( methodString == "PUT" )
220 {
222 }
223 else if ( methodString == "PATCH" )
224 {
226 }
227 else if ( methodString == "DELETE" )
228 {
230 }
231 else if ( methodString == "OPTIONS" )
232 {
234 }
235 else
236 {
237 throw HttpException( u"HTTP error unsupported method: %1"_s.arg( methodString ) );
238 }
239
240 // cppcheck-suppress containerOutOfBounds
241 const QString protocol { firstLinePieces.at( 2 ) };
242 if ( protocol != "HTTP/1.0"_L1 && protocol != "HTTP/1.1"_L1 )
243 {
244 throw HttpException( u"HTTP error unsupported protocol: %1"_s.arg( protocol ) );
245 }
246
247 // Headers
249 const auto endHeadersPos { incomingData->indexOf( "\r\n\r\n" ) };
250
251 if ( endHeadersPos == -1 )
252 {
253 throw HttpException( u"HTTP error finding headers"_s );
254 }
255
256 const QStringList httpHeaders { incomingData->mid( firstLinePos + 2, endHeadersPos - firstLinePos ).split( "\r\n" ) };
257
258 for ( const auto &headerLine : httpHeaders )
259 {
260 const auto headerColonPos { headerLine.indexOf( ':' ) };
261 if ( headerColonPos > 0 )
262 {
263 headers.insert( headerLine.left( headerColonPos ), headerLine.mid( headerColonPos + 2 ) );
264 }
265 }
266
267 const auto headersSize { endHeadersPos + 4 };
268
269 // Check for content length and if we have got all data
270 if ( headers.contains( u"Content-Length"_s ) )
271 {
272 bool ok;
273 const int contentLength { headers.value( u"Content-Length"_s ).toInt( &ok ) };
274 if ( ok && contentLength > incomingData->length() - headersSize )
275 {
276 return;
277 }
278 }
279
280 // At this point we should have read all data:
281 // disconnect the lambdas
282 delete context;
283
284 // Build URL from env ...
285 QString url { qgetenv( "REQUEST_URI" ) };
286 // ... or from server ip/port and request path
287 if ( url.isEmpty() )
288 {
289 // cppcheck-suppress containerOutOfBounds
290 const QString path { firstLinePieces.at( 1 ) };
291 // Take Host header if defined
292 if ( headers.contains( u"Host"_s ) )
293 {
294 url = u"http://%1%2"_s.arg( headers.value( u"Host"_s ), path );
295 }
296 else
297 {
298 url = u"http://%1:%2%3"_s.arg( ipAddress ).arg( port ).arg( path );
299 }
300 }
301
302 // Inefficient copy :(
303 QByteArray data { incomingData->mid( headersSize ).toUtf8() };
304
305 if ( !incomingData->isEmpty() && clientConnection->state() == QAbstractSocket::SocketState::ConnectedState )
306 {
307 auto requestContext = new RequestContext {
308 clientConnection,
309 firstLinePieces.join( ' ' ),
310 std::chrono::steady_clock::now(),
311 { url, method, headers, &data },
312 {},
313 };
314 REQUEST_QUEUE_MUTEX.lock();
315 REQUEST_QUEUE.enqueue( requestContext );
316 REQUEST_QUEUE_MUTEX.unlock();
317 REQUEST_WAIT_CONDITION.notify_one();
318 }
319 }
320 catch ( HttpException &ex )
321 {
322 if ( clientConnection->state() == QAbstractSocket::SocketState::ConnectedState )
323 {
324 // Output stream: send error
325 clientConnection->write( u"HTTP/1.0 %1 %2\r\n"_s.arg( 500 ).arg( knownStatuses.value( 500 ) ).toUtf8() );
326 clientConnection->write( u"Server: QGIS\r\n"_s.toUtf8() );
327 clientConnection->write( "\r\n" );
328 clientConnection->write( ex.message().toUtf8() );
329
330 std::cout
331 << u"\033[1;31m%1 [%2] \"%3\" - - 500\033[0m"_s.arg( clientConnection->peerAddress().toString() ).arg( QDateTime::currentDateTime().toString() ).arg( ex.message() ).toStdString()
332 << std::endl;
333
334 clientConnection->disconnectFromHost();
335 }
336 }
337 } );
338 } );
339 }
340 }
341
342 ~TcpServerWorker() override { mTcpServer.close(); }
343
344 bool isListening() const { return mIsListening; }
345
346 public slots:
347
348 // Outgoing connection handler
349 void responseReady( RequestContext *requestContext ) //#spellok
350 {
351 std::unique_ptr<RequestContext> request { requestContext };
352 const auto elapsedTime { std::chrono::steady_clock::now() - request->startTime };
353
354 const auto &response { request->response };
355 const auto &clientConnection { request->clientConnection };
356
357 if ( !clientConnection || clientConnection->state() != QAbstractSocket::SocketState::ConnectedState )
358 {
359 std::cout << "Connection reset by peer" << std::endl;
360 return;
361 }
362
363 // Output stream
364 if ( -1 == clientConnection->write( u"HTTP/1.0 %1 %2\r\n"_s.arg( response.statusCode() ).arg( knownStatuses.value( response.statusCode(), u"Unknown response code"_s ) ).toUtf8() ) )
365 {
366 std::cout << "Cannot write to output socket" << std::endl;
367 clientConnection->disconnectFromHost();
368 return;
369 }
370
371 clientConnection->write( u"Server: QGIS\r\n"_s.toUtf8() );
372 const auto responseHeaders { response.headers() };
373 for ( auto it = responseHeaders.constBegin(); it != responseHeaders.constEnd(); ++it )
374 {
375 clientConnection->write( u"%1: %2\r\n"_s.arg( it.key(), it.value() ).toUtf8() );
376 }
377 clientConnection->write( "\r\n" );
378 const QByteArray body { response.body() };
379 clientConnection->write( body );
380
381 // 10.185.248.71 [09/Jan/2015:19:12:06 +0000] 808840 <time> "GET / HTTP/1.1" 500"
382 std::cout
383 << u"\033[1;92m%1 [%2] %3 %4ms \"%5\" %6\033[0m"_s
384 .arg(
385 clientConnection->peerAddress().toString(),
386 QDateTime::currentDateTime().toString(),
387 QString::number( body.size() ),
388 QString::number( std::chrono::duration_cast<std::chrono::milliseconds>( elapsedTime ).count() ),
389 request->httpHeader,
390 QString::number( response.statusCode() )
391 )
392 .toStdString()
393 << std::endl;
394
395 // This will trigger delete later on the socket object
396 clientConnection->disconnectFromHost();
397 }
398
399 private:
400 QTcpServer mTcpServer;
401 qlonglong mConnectionCounter = 0;
402 bool mIsListening = false;
403};
404
405
406class TcpServerThread : public QThread
407{
408 Q_OBJECT
409
410 public:
411 TcpServerThread( const QString &ipAddress, const int port )
412 : mIpAddress( ipAddress )
413 , mPort( port )
414 {}
415
416 void emitResponseReady( RequestContext *requestContext ) //#spellok
417 {
418 if ( requestContext->clientConnection )
419 emit responseReady( requestContext ); //#spellok
420 }
421
422 void run() override
423 {
424 const TcpServerWorker worker( mIpAddress, mPort );
425 if ( !worker.isListening() )
426 {
427 emit serverError();
428 }
429 else
430 {
431 // Forward signal to worker
432 connect( this, &TcpServerThread::responseReady, &worker, &TcpServerWorker::responseReady ); //#spellok
433 QThread::run();
434 }
435 }
436
437 signals:
438
439 void responseReady( RequestContext *requestContext ); //#spellok
440 void serverError();
441
442 private:
443 QString mIpAddress;
444 int mPort;
445};
446
447
448class QueueMonitorThread : public QThread
449{
450 Q_OBJECT
451
452 public:
453 void run() override
454 {
455 while ( mIsRunning )
456 {
457 std::unique_lock<std::mutex> requestLocker( REQUEST_QUEUE_MUTEX );
458 REQUEST_WAIT_CONDITION.wait( requestLocker, [this] { return !mIsRunning || !REQUEST_QUEUE.isEmpty(); } );
459 if ( mIsRunning )
460 {
461 // Lock if server is running
462 SERVER_MUTEX.lock();
463 emit requestReady( REQUEST_QUEUE.dequeue() );
464 }
465 }
466 }
467
468 signals:
469
470 void requestReady( RequestContext *requestContext );
471
472 public slots:
473
474 void stop() { mIsRunning = false; }
475
476 private:
477 bool mIsRunning = true;
478};
479
480int main( int argc, char *argv[] )
481{
482 // Test if the environ variable DISPLAY is defined
483 // if it's not, the server is running in offscreen mode
484 // Qt supports using various QPA (Qt Platform Abstraction) back ends
485 // for rendering. You can specify the back end to use with the environment
486 // variable QT_QPA_PLATFORM when invoking a Qt-based application.
487 // Available platform plugins are: directfbegl, directfb, eglfs, linuxfb,
488 // minimal, minimalegl, offscreen, wayland-egl, wayland, xcb.
489 // https://www.ics.com/blog/qt-tips-and-tricks-part-1
490 // http://doc.qt.io/qt-5/qpa.html
491 const QString display { qgetenv( "DISPLAY" ) };
492 bool withDisplay = true;
493 if ( display.isEmpty() )
494 {
495 withDisplay = false;
496 qputenv( "QT_QPA_PLATFORM", "offscreen" );
497 }
498
499 // since version 3.0 QgsServer now needs a qApp so initialize QgsApplication
500 const QgsApplication app( argc, argv, withDisplay, QString(), u"QGIS Development Server"_s );
501
502 QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME );
503 QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN );
504 QCoreApplication::setApplicationName( "QGIS Development Server" );
505 QCoreApplication::setApplicationVersion( VERSION );
506
507 if ( !withDisplay )
508 {
510 "DISPLAY environment variable is not set, running in offscreen mode, all printing capabilities will not be available.\n"
511 "Consider installing an X server like 'xvfb' and export DISPLAY to the actual display value.",
512 "Server",
514 );
515 }
516
517#ifdef Q_OS_WIN
518 // Initialize font database before fcgi_accept.
519 // When using FCGI with IIS, environment variables (QT_QPA_FONTDIR in this case) are lost after fcgi_accept().
520 QFontDatabase fontDB;
521#endif
522
523 // The port to listen
524 serverPort = qgetenv( "QGIS_SERVER_PORT" );
525 // The address to listen
526 ipAddress = qgetenv( "QGIS_SERVER_ADDRESS" );
527
528 if ( serverPort.isEmpty() )
529 {
530 serverPort = u"8000"_s;
531 }
532
533 if ( ipAddress.isEmpty() )
534 {
535 ipAddress = u"localhost"_s;
536 }
537
538 QCommandLineParser parser;
539 parser.setApplicationDescription( QObject::tr( "QGIS Development Server %1" ).arg( VERSION ) );
540 parser.addHelpOption();
541
542 const QCommandLineOption versionOption( QStringList() << "v" << "version", QObject::tr( "Version of QGIS and libraries" ) );
543 parser.addOption( versionOption );
544
545 parser.addPositionalArgument(
546 u"addressAndPort"_s,
547 QObject::tr(
548 "Address and port (default: \"localhost:8000\")\n"
549 "address and port can also be specified with the environment\n"
550 "variables QGIS_SERVER_ADDRESS and QGIS_SERVER_PORT."
551 ),
552 u"[address:port]"_s
553 );
554 const QCommandLineOption logLevelOption(
555 "l",
556 QObject::tr(
557 "Log level (default: 0)\n"
558 "0: INFO\n"
559 "1: WARNING\n"
560 "2: CRITICAL"
561 ),
562 "logLevel",
563 "0"
564 );
565 parser.addOption( logLevelOption );
566
567 const QCommandLineOption projectOption(
568 "p",
569 QObject::tr(
570 "Path to a QGIS project file (*.qgs or *.qgz),\n"
571 "if specified it will override the query string MAP argument\n"
572 "and the QGIS_PROJECT_FILE environment variable."
573 ),
574 "projectPath",
575 ""
576 );
577 parser.addOption( projectOption );
578
579 parser.process( app );
580
581 if ( parser.isSet( versionOption ) )
582 {
583 std::cout << QgsCommandLineUtils::allVersions().toStdString();
584 return 0;
585 }
586
587 const QStringList args = parser.positionalArguments();
588
589 if ( args.size() == 1 )
590 {
591 const QStringList addressAndPort { args.at( 0 ).split( ':' ) };
592 if ( addressAndPort.size() == 2 )
593 {
594 ipAddress = addressAndPort.at( 0 );
595 // cppcheck-suppress containerOutOfBounds
596 serverPort = addressAndPort.at( 1 );
597 }
598 }
599
600 const QString logLevel = parser.value( logLevelOption );
601 qunsetenv( "QGIS_SERVER_LOG_FILE" );
602 qputenv( "QGIS_SERVER_LOG_LEVEL", logLevel.toUtf8() );
603 qputenv( "QGIS_SERVER_LOG_STDERR", "1" );
604
605 QgsServer server;
606
607 if ( !parser.value( projectOption ).isEmpty() )
608 {
609 // Check it!
610 const QString projectFilePath { parser.value( projectOption ) };
613 {
614 std::cout << QObject::tr( "Project file not found, the option will be ignored." ).toStdString() << std::endl;
615 }
616 else
617 {
618 qputenv( "QGIS_PROJECT_FILE", projectFilePath.toUtf8() );
619 }
620 }
621
622 // Disable parallel rendering because if its internal loop
623 //qputenv( "QGIS_SERVER_PARALLEL_RENDERING", "0" );
624
625
626#ifdef HAVE_SERVER_PYTHON_PLUGINS
627 server.initPython();
628#endif
629
630 // TCP thread
631 TcpServerThread tcpServerThread { ipAddress, serverPort.toInt() };
632
633 bool isTcpError = false;
634 TcpServerThread::connect(
635 &tcpServerThread,
636 &TcpServerThread::serverError,
637 qApp,
638 [&] {
639 isTcpError = true;
640 qApp->quit();
641 },
642 Qt::QueuedConnection
643 );
644
645 // Monitoring thread
646 QueueMonitorThread queueMonitorThread;
647 QueueMonitorThread::connect( &queueMonitorThread, &QueueMonitorThread::requestReady, qApp, [&]( RequestContext *requestContext ) {
648 if ( requestContext->clientConnection && requestContext->clientConnection->isValid() )
649 {
650 server.handleRequest( requestContext->request, requestContext->response );
651 SERVER_MUTEX.unlock();
652 }
653 else
654 {
655 delete requestContext;
656 SERVER_MUTEX.unlock();
657 return;
658 }
659 if ( requestContext->clientConnection && requestContext->clientConnection->isValid() )
660 tcpServerThread.emitResponseReady( requestContext ); //#spellok
661 else
662 delete requestContext;
663 } );
664
665 // Exit handlers
666#ifndef Q_OS_WIN
667
668 auto exitHandler = []( int signal ) {
669 std::cout << u"Signal %1 received: quitting"_s.arg( signal ).toStdString() << std::endl;
670 IS_RUNNING = 0;
671 qApp->quit();
672 };
673
674 signal( SIGTERM, exitHandler );
675 signal( SIGABRT, exitHandler );
676 signal( SIGINT, exitHandler );
677 signal( SIGPIPE, []( int ) { std::cerr << u"Signal SIGPIPE received: ignoring"_s.toStdString() << std::endl; } );
678
679#endif
680
681 tcpServerThread.start();
682 queueMonitorThread.start();
683
684 QgsApplication::exec();
685 // Wait for threads
686 tcpServerThread.exit();
687 tcpServerThread.wait();
688 queueMonitorThread.stop();
689 REQUEST_WAIT_CONDITION.notify_all();
690 queueMonitorThread.wait();
692
693 return isTcpError ? 1 : 0;
694}
695
696#include "qgis_mapserver.moc"
697
@ DontLoad3DViews
Skip loading 3D views.
Definition qgis.h:4579
@ DontStoreOriginalStyles
Skip the initial XML style storage for layers. Useful for minimising project load times in non-intera...
Definition qgis.h:4578
@ DontUpgradeAnnotations
Don't upgrade old annotation items to QgsAnnotationItem.
Definition qgis.h:4582
@ DontLoadLayouts
Don't load print layouts. Improves project read time if layouts are not required, and allows projects...
Definition qgis.h:4574
@ DontResolveLayers
Don't resolve layer paths (i.e. don't load any layer content). Dramatically improves project read tim...
Definition qgis.h:4572
@ Warning
Warning message.
Definition qgis.h:162
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
static void exitQgis()
deletes provider registry and map layer registry
static const char * QGIS_ORGANIZATION_DOMAIN
static const char * QGIS_ORGANIZATION_NAME
static QString allVersions()
Display all versions in the standard output stream.
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).
static QgsProject * instance()
Returns the QgsProject singleton instance.
Method
HTTP Method (or equivalent) used for the request.
QMap< QString, QString > Headers
A server which provides OGC web services.
Definition qgsserver.h:50
void handleRequest(QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project=nullptr)
Handles the request.
int main(int argc, char *argv[])