MythTV  master
httpserver.h
Go to the documentation of this file.
1 // Program Name: httpserver.h
3 // Created : Oct. 1, 2005
4 //
5 // Purpose : HTTP 1.1 Mini Server Implmenetation
6 // Used for UPnp/AV implementation & status information
7 //
8 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
9 //
10 // Licensed under the GPL v2 or later, see COPYING for details
11 //
13 
14 #ifndef __HTTPSERVER_H__
15 #define __HTTPSERVER_H__
16 
17 // POSIX headers
18 #include <sys/types.h>
19 #ifndef _WIN32
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #endif
23 #include <utility>
24 
25 // Qt headers
26 #include <QReadWriteLock>
27 #include <QMultiMap>
28 #include <QRunnable>
29 #include <QPointer>
30 #include <QMutex>
31 #include <QList>
32 #include <QSslConfiguration>
33 #include <QSslError>
34 #include <QSslKey>
35 #include <QSslSocket>
36 
37 // MythTV headers
38 #include "mythqtcompat.h"
39 #include "serverpool.h"
40 #include "httprequest.h"
41 #include "mthreadpool.h"
42 #include "upnputil.h"
43 #include "compat.h"
44 
45 using TaskTime = struct timeval;
46 
47 class HttpWorkerThread;
48 class QScriptEngine;
49 class HttpServer;
50 #ifndef QT_NO_OPENSSL
51 class QSslKey;
52 class QSslCertificate;
53 class QSslConfiguration;
54 #endif
55 
57 {
58  cpLocalNoAuth = 0x00, // Can only be accessed locally, but no authentication is required
59  cpLocalAuth = 0x01, // Can only be accessed locally, authentication is required
60  cpRemoteAuth = 0x02 // Can be accessed remotely, authentication is required
61 };
62 
65 //
66 // HttpServerExtension Class Definition
67 //
70 
71 class UPNP_PUBLIC HttpServerExtension : public QObject
72 {
73  Q_OBJECT
74 
75  public:
76 
77  QString m_sName;
78  QString m_sSharePath;
79  int m_nSocketTimeout; // Extension may wish to adjust the default e.g. UPnP
80 
81  uint m_nSupportedMethods; // Bit flags - HTTP::RequestType
82 
83  public:
84 
85  HttpServerExtension( QString sName, QString sSharePath)
86  : m_sName(std::move( sName )), m_sSharePath(std::move( sSharePath )),
87  m_nSocketTimeout(-1),
88  m_nSupportedMethods((RequestTypeGet | RequestTypePost | // Defaults, extensions may extend the list
90 
91  ~HttpServerExtension() override = default;
92 
93  virtual bool ProcessRequest(HTTPRequest *pRequest) = 0;
94 
95  virtual bool ProcessOptions(HTTPRequest *pRequest);
96 
97  virtual QStringList GetBasePaths() = 0;
98 
99  virtual int GetSocketTimeout() const { return m_nSocketTimeout; }// -1 = Use config value
100 };
101 
102 using HttpServerExtensionList = QList<QPointer<HttpServerExtension> >;
103 
106 //
107 // HttpServer Class Definition
108 //
111 
113 {
114  Q_OBJECT
115 
116  public:
117  HttpServer();
118  ~HttpServer() override;
119 
120  void RegisterExtension(HttpServerExtension *pExtension);
121  void UnregisterExtension(HttpServerExtension *pExtension);
122  void DelegateRequest(HTTPRequest *pRequest);
126  uint GetSocketTimeout(HTTPRequest *pRequest) const;
127 
128  QString GetSharePath(void) const
129  { // never modified after creation, so no need to lock
130  return m_sSharePath;
131  }
132 
133  bool IsRunning(void) const
134  {
135  m_rwlock.lockForRead();
136  bool tmp = m_running;
137  m_rwlock.unlock();
138  return tmp;
139  }
140 
141  static QString GetPlatform(void);
142  static QString GetServerVersion(void);
143 
144  protected:
145  mutable QReadWriteLock m_rwlock;
147  // This multimap does NOT take ownership of the HttpServerExtension*
148  QMultiMap< QString, HttpServerExtension* > m_basePaths;
149  QString m_sSharePath;
151  bool m_running { true }; // protected by m_rwlock
152 
153  static QMutex s_platformLock;
154  static QString s_platform;
155 
156 #ifndef QT_NO_OPENSSL
157  QSslConfiguration m_sslConfig;
158 #endif
159 
160  const QString m_privateToken; // Private token; Used to salt digest auth nonce, changes on backend restart
161 
162  protected slots:
163  void newTcpConnection(qt_socket_fd_t socket) override; // QTcpServer
164 
165  private:
166  void LoadSSLConfig();
167 };
168 
171 //
172 // HttpWorkerThread Class Definition
173 //
176 
177 class HttpWorker : public QRunnable
178 {
179  public:
180 
188 #ifndef QT_NO_OPENSSL
189  , const QSslConfiguration& sslConfig
190 #endif
191  );
192 
193  void run(void) override; // QRunnable
194 
195  protected:
200 
201 #ifndef QT_NO_OPENSSL
202  QSslConfiguration m_sslConfig;
203 #endif
204 };
205 
206 
207 #endif
static QMutex s_platformLock
Definition: httpserver.h:153
MThreadPool m_threadPool
Definition: httpserver.h:150
unsigned int slots[4]
Definition: element.c:38
HttpServerExtensionList m_extensions
Definition: httpserver.h:146
QString m_sSharePath
Definition: httpserver.h:78
static QString s_platform
Definition: httpserver.h:154
struct timeval TaskTime
Definition: httpserver.h:45
PoolServerType m_connectionType
Definition: httpserver.h:199
HttpServerExtension(QString sName, QString sSharePath)
Definition: httpserver.h:85
QMultiMap< QString, HttpServerExtension * > m_basePaths
Definition: httpserver.h:148
static guint32 * tmp
Definition: goom_core.c:35
QString GetSharePath(void) const
Definition: httpserver.h:128
PoolServerType
Definition: serverpool.h:29
QSslConfiguration m_sslConfig
Definition: httpserver.h:202
bool IsRunning(void) const
Definition: httpserver.h:133
int m_socketTimeout
Definition: httpserver.h:198
qt_socket_fd_t m_socket
Definition: httpserver.h:197
qintptr qt_socket_fd_t
Definition: mythqtcompat.h:4
void run(void) override
Definition: httpserver.cpp:468
HttpWorker(HttpServer &httpServer, qt_socket_fd_t sock, PoolServerType type, const QSslConfiguration &sslConfig)
Definition: httpserver.cpp:448
unsigned int uint
Definition: compat.h:140
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
QSslConfiguration m_sslConfig
Definition: httpserver.h:157
#define UPNP_PUBLIC
Definition: upnpexp.h:9
HttpServer & m_httpServer
Definition: httpserver.h:196
ContentProtection
Definition: httpserver.h:56
QReadWriteLock m_rwlock
Definition: httpserver.h:145
QString m_sSharePath
Definition: httpserver.h:149
virtual void newTcpConnection(qt_socket_fd_t socket)
Definition: serverpool.cpp:640
QList< QPointer< HttpServerExtension > > HttpServerExtensionList
Definition: httpserver.h:102
const QString m_privateToken
Definition: httpserver.h:160
virtual int GetSocketTimeout() const
Definition: httpserver.h:99