Torc  0.1
torchttpserver.h
Go to the documentation of this file.
1 #ifndef TORCHTTPSERVER_H
2 #define TORCHTTPSERVER_H
3 
4 // Qt
5 #include <QTcpSocket>
6 #include <QMutex>
7 #include <QReadWriteLock>
8 
9 // Torc
10 #include "torcuser.h"
11 #include "torchtmlhandler.h"
12 #include "torchttpservices.h"
13 #include "torchtmlstaticcontent.h"
14 #include "torchtmldynamiccontent.h"
15 #include "torcupnpcontent.h"
16 #include "torcwebsocketpool.h"
17 #include "torcwebsocketthread.h"
18 #include "torchttpserverlistener.h"
19 
20 class TorcSetting;
21 class TorcHTTPHandler;
22 class TorcSSDPThread;
24 
25 class TorcHTTPServer final : public QObject
26 {
27  Q_OBJECT
28 
29  friend class TorcHTTPServerObject;
30 
31  public:
32  class Status
33  {
34  public:
35  Status();
36  bool operator==(Status Other) const;
37  int port;
38  bool secure;
39  bool ipv4;
40  bool ipv6;
41  };
42 
43  public:
44  // Content/service handlers
45  static QString ServerDescription (void);
46  static Status GetStatus (void);
47  static void Authorise (const QString &Host, TorcHTTPRequest &Request, bool ForceCheck);
48  static void AuthenticateUser (TorcHTTPRequest &Request);
49  static void AddAuthenticationHeader(TorcHTTPRequest &Request);
50  static void ValidateOrigin (TorcHTTPRequest &Request);
51  static void RegisterHandler (TorcHTTPHandler *Handler);
52  static void DeregisterHandler (TorcHTTPHandler *Handler);
53  static void HandleRequest (const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request);
54  static QVariantMap HandleRequest (const QString &Method, const QVariant &Parameters, QObject *Connection, bool Authenticated);
55  static QVariantMap GetServiceHandlers (void);
56  static QVariantMap GetServiceDescription(const QString &Service);
58  static QString PlatformName (void);
59 
60  public:
61  virtual ~TorcHTTPServer ();
62 
63  public slots:
64  void NewConnection (qintptr SocketDescriptor);
65 
66  protected slots:
67  void PortChanged (int Port);
68  void SecureChanged (bool Secure);
69  void UPnPChanged (bool UPnP);
70  void UPnPSearchChanged (bool Search);
71  void UPnPAdvertChanged (bool Advert);
72  void BonjourChanged (bool Bonjour);
73  void BonjourSearchChanged(bool Search);
74  void BonjourAdvertChanged(bool Advert);
75  void IPv6Changed (bool IPv6);
76  void Restart (void);
77 
78  signals:
79  void HandlersChanged (void);
80 
81  protected:
82  TorcHTTPServer ();
83  bool event (QEvent *Event) override final;
84  bool Open (void);
85  void Close (void);
87 
88  protected:
91  static QMutex gWebServerLock;
92  static QString gPlatform;
93  static QString gOriginWhitelist;
94  static QReadWriteLock gOriginWhitelistLock;
95 
96  private:
97  static void UpdateOriginWhitelist (TorcHTTPServer::Status Status);
98  void StartBonjour (void);
99  void StopBonjour (void);
100  void StopBonjourBrowse (void);
101  void StopBonjourAdvert (void);
102  void StartUPnP (void);
103  void StopUPnP (void);
104 
105  private:
106  TorcSettingGroup *m_serverSettings;
107  TorcSetting *m_port;
108  TorcSetting *m_secure;
109  TorcSetting *m_upnp;
110  TorcSetting *m_upnpSearch;
111  TorcSetting *m_upnpAdvertise;
112  TorcSetting *m_bonjour;
113  TorcSetting *m_bonjourSearch;
114  TorcSetting *m_bonjourAdvert;
115  TorcSetting *m_ipv6;
116  TorcHTTPServerListener *m_listener;
117  TorcUser m_user;
118  TorcHTMLHandler m_defaultHandler;
119  TorcHTTPServices m_servicesHandler;
120  TorcHTMLStaticContent m_staticContent;
121  TorcHTMLDynamicContent m_dynamicContent;
122  TorcUPnPContent m_upnpContent;
123  TorcSSDPThread *m_ssdpThread;
124  quint32 m_bonjourBrowserReference;
125  quint32 m_httpBonjourReference;
126  quint32 m_torcBonjourReference;
127  TorcWebSocketPool m_webSocketPool;
128 
129  private:
130  Q_DISABLE_COPY(TorcHTTPServer)
131 };
132 
133 Q_DECLARE_METATYPE(TorcHTTPRequest*)
134 Q_DECLARE_METATYPE(QTcpSocket*)
135 Q_DECLARE_METATYPE(QHostAddress)
136 #endif // TORCHTTPSERVER_H
bool operator==(Status Other) const
void Restart(void)
A class to encapsulate an incoming HTTP request.
void UPnPAdvertChanged(bool Advert)
High level group of related settings.
Definition: torcsetting.h:123
Base HTTP response handler class.
static void Authorise(const QString &Host, TorcHTTPRequest &Request, bool ForceCheck)
Ensures remote user is authorised to access this request.
A wrapper around a database setting.
Definition: torcsetting.h:15
Top level interface into services.
static QMutex gWebServerLock
static void AddAuthenticationHeader(TorcHTTPRequest &Request)
TorcWebSocketThread * TakeSocketPriv(TorcWebSocketThread *Socket)
bool event(QEvent *Event) overridefinal
Handles the provision of dynamic content as typically located in ~/.torc/content. ...
static void HandleRequest(const QString &PeerAddress, int PeerPort, const QString &LocalAddress, int LocalPort, TorcHTTPRequest &Request)
void BonjourSearchChanged(bool Search)
static void ValidateOrigin(TorcHTTPRequest &Request)
Check the Origin header for validity and respond appropriately.
void BonjourAdvertChanged(bool Advert)
void HandlersChanged(void)
static QString gPlatform
void BonjourChanged(bool Bonjour)
void UPnPSearchChanged(bool Search)
static QVariantMap GetServiceHandlers(void)
static void RegisterHandler(TorcHTTPHandler *Handler)
void PortChanged(int Port)
static Status gWebServerStatus
static Status GetStatus(void)
static void AuthenticateUser(TorcHTTPRequest &Request)
static QString gOriginWhitelist
Handles the provision of static server content such as html, css, js etc.
static void DeregisterHandler(TorcHTTPHandler *Handler)
Base HTML handler.
static QString PlatformName(void)
#define IPv6
Definition: torcssdp.cpp:331
static TorcHTTPServer * gWebServer
void IPv6Changed(bool IPv6)
Wraps a TorcQThread around a TorcWebsocket.
void NewConnection(qintptr SocketDescriptor)
static QString ServerDescription(void)
An HTTP server.
void UPnPChanged(bool UPnP)
void SecureChanged(bool Secure)
static TorcWebSocketThread * TakeSocket(TorcWebSocketThread *Socket)
static QVariantMap GetServiceDescription(const QString &Service)
static QReadWriteLock gOriginWhitelistLock
virtual ~TorcHTTPServer()