Overte C++ Documentation
HTTPManager.h
1 //
2 // HTTPManager.h
3 // libraries/embedded-webserver/src
4 //
5 // Created by Stephen Birarda on 1/16/14.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Heavily based on Andrzej Kapolka's original HTTPManager class
9 // found from another one of his projects.
10 // https://github.com/ey6es/witgap/tree/master/src/cpp/server/http
11 //
12 // Distributed under the Apache License, Version 2.0.
13 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
14 //
15 
16 #ifndef hifi_HTTPManager_h
17 #define hifi_HTTPManager_h
18 
19 #include <QtNetwork/QTcpServer>
20 #include <QtCore/QTimer>
21 
22 class HTTPConnection;
23 class HTTPSConnection;
24 
25 class HTTPRequestHandler {
26 public:
28  virtual bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url, bool skipSubHandler = false) = 0;
29 };
30 
32 class HTTPManager : public QTcpServer, public HTTPRequestHandler {
33  Q_OBJECT
34 public:
36  HTTPManager(const QHostAddress& listenAddress, quint16 port, const QString& documentRoot, HTTPRequestHandler* requestHandler = nullptr);
37 
38  bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url, bool skipSubHandler = false) override;
39 
40 private slots:
41  void isTcpServerListening();
42  void queuedExit(QString errorMessage);
43 
44 private:
45  bool bindSocket();
46 
47 protected:
49  virtual void incomingConnection(qintptr socketDescriptor) override;
50  virtual bool requestHandledByRequestHandler(HTTPConnection* connection, const QUrl& url);
51 
52  QHostAddress _listenAddress;
53  QString _documentRoot;
54  HTTPRequestHandler* _requestHandler;
55  QTimer* _isListeningTimer;
56  const quint16 _port;
57 };
58 
59 #endif // hifi_HTTPManager_h
Handles a single HTTP connection.
Definition: HTTPConnection.h:43
Handles HTTP connections.
Definition: HTTPManager.h:32
HTTPManager(const QHostAddress &listenAddress, quint16 port, const QString &documentRoot, HTTPRequestHandler *requestHandler=nullptr)
Initializes the manager.
Definition: HTTPManager.cpp:27
virtual void incomingConnection(qintptr socketDescriptor) override
Accepts all pending connections.
Definition: HTTPManager.cpp:40