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 class HttpWorkerThread;
46 class QScriptEngine;
47 class HttpServer;
48 #ifndef QT_NO_OPENSSL
49 class QSslKey;
50 class QSslCertificate;
51 class QSslConfiguration;
52 #endif
53 
55 {
56  cpLocalNoAuth = 0x00, // Can only be accessed locally, but no authentication is required
57  cpLocalAuth = 0x01, // Can only be accessed locally, authentication is required
58  cpRemoteAuth = 0x02 // Can be accessed remotely, authentication is required
59 };
60 
63 //
64 // HttpServerExtension Class Definition
65 //
68 
69 class UPNP_PUBLIC HttpServerExtension : public QObject
70 {
71  Q_OBJECT
72 
73  public:
74 
75  QString m_sName;
76  QString m_sSharePath;
77  int m_nSocketTimeout; // Extension may wish to adjust the default e.g. UPnP
78 
79  uint m_nSupportedMethods; // Bit flags - HTTP::RequestType
80 
81  public:
82 
83  HttpServerExtension( QString sName, QString sSharePath)
84  : m_sName(std::move( sName )), m_sSharePath(std::move( sSharePath )),
85  m_nSocketTimeout(-1),
86  m_nSupportedMethods((RequestTypeGet | RequestTypePost | // Defaults, extensions may extend the list
88 
89  ~HttpServerExtension() override = default;
90 
91  virtual bool ProcessRequest(HTTPRequest *pRequest) = 0;
92 
93  virtual bool ProcessOptions(HTTPRequest *pRequest);
94 
95  virtual QStringList GetBasePaths() = 0;
96 
97  virtual int GetSocketTimeout() const { return m_nSocketTimeout; }// -1 = Use config value
98 };
99 
100 using HttpServerExtensionList = QList<QPointer<HttpServerExtension> >;
101 
104 //
105 // HttpServer Class Definition
106 //
109 
111 {
112  Q_OBJECT
113 
114  public:
115  HttpServer();
116  ~HttpServer() override;
117 
118  void RegisterExtension(HttpServerExtension *pExtension);
119  void UnregisterExtension(HttpServerExtension *pExtension);
120  void DelegateRequest(HTTPRequest *pRequest);
124  uint GetSocketTimeout(HTTPRequest *pRequest) const;
125 
126  QString GetSharePath(void) const
127  { // never modified after creation, so no need to lock
128  return m_sSharePath;
129  }
130 
131  bool IsRunning(void) const
132  {
133  m_rwlock.lockForRead();
134  bool tmp = m_running;
135  m_rwlock.unlock();
136  return tmp;
137  }
138 
139  static QString GetPlatform(void);
140  static QString GetServerVersion(void);
141 
142  protected:
143  mutable QReadWriteLock m_rwlock;
145  // This multimap does NOT take ownership of the HttpServerExtension*
146  QMultiMap< QString, HttpServerExtension* > m_basePaths;
147  QString m_sSharePath;
149  bool m_running { true }; // protected by m_rwlock
150 
151  static QMutex s_platformLock;
152  static QString s_platform;
153 
154 #ifndef QT_NO_OPENSSL
155  QSslConfiguration m_sslConfig;
156 #endif
157 
158  const QString m_privateToken; // Private token; Used to salt digest auth nonce, changes on backend restart
159 
160  protected slots:
161  void newTcpConnection(qt_socket_fd_t socket) override; // QTcpServer
162 
163  private:
164  void LoadSSLConfig();
165 };
166 
169 //
170 // HttpWorkerThread Class Definition
171 //
174 
175 class HttpWorker : public QRunnable
176 {
177  public:
178 
186 #ifndef QT_NO_OPENSSL
187  , const QSslConfiguration& sslConfig
188 #endif
189  );
190 
191  void run(void) override; // QRunnable
192 
193  protected:
196  std::chrono::milliseconds m_socketTimeout;
198 
199 #ifndef QT_NO_OPENSSL
200  QSslConfiguration m_sslConfig;
201 #endif
202 };
203 
204 
205 #endif // HTTPSERVER_H
HttpServerExtension::m_nSupportedMethods
uint m_nSupportedMethods
Definition: httpserver.h:79
HTTPRequest
Definition: httprequest.h:108
HttpWorker::m_socket
qt_socket_fd_t m_socket
Definition: httpserver.h:195
HttpWorker
Definition: httpserver.h:175
HttpWorker::m_sslConfig
QSslConfiguration m_sslConfig
Definition: httpserver.h:200
HttpServer::IsRunning
bool IsRunning(void) const
Definition: httpserver.h:131
HttpServer::m_sSharePath
QString m_sSharePath
Definition: httpserver.h:147
ServerPool
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
HttpServerExtension::GetSocketTimeout
virtual int GetSocketTimeout() const
Definition: httpserver.h:97
RequestTypePost
@ RequestTypePost
Definition: httprequest.h:49
RequestTypeOptions
@ RequestTypeOptions
Definition: httprequest.h:53
HttpWorker::run
void run(void) override
Definition: httpserver.cpp:465
HttpServer::s_platform
static QString s_platform
Definition: httpserver.h:152
HttpServer::m_privateToken
const QString m_privateToken
Definition: httpserver.h:158
HttpServerExtension::HttpServerExtension
HttpServerExtension(QString sName, QString sSharePath)
Definition: httpserver.h:83
HttpWorker::m_httpServer
HttpServer & m_httpServer
Definition: httpserver.h:194
tmp
static guint32 * tmp
Definition: goom_core.cpp:31
HttpServer::m_rwlock
QReadWriteLock m_rwlock
Definition: httpserver.h:143
compat.h
RequestTypeHead
@ RequestTypeHead
Definition: httprequest.h:48
HttpServer::m_threadPool
MThreadPool m_threadPool
Definition: httpserver.h:148
ServerPool::newTcpConnection
virtual void newTcpConnection(qt_socket_fd_t socket)
Definition: serverpool.cpp:672
PoolServerType
PoolServerType
Definition: serverpool.h:29
HttpServerExtension::m_nSocketTimeout
int m_nSocketTimeout
Definition: httpserver.h:77
HttpWorker::m_connectionType
PoolServerType m_connectionType
Definition: httpserver.h:197
uint
unsigned int uint
Definition: compat.h:140
ContentProtection
ContentProtection
Definition: httpserver.h:54
mthreadpool.h
HttpServer::GetSharePath
QString GetSharePath(void) const
Definition: httpserver.h:126
MThreadPool
Definition: mthreadpool.h:18
cpLocalAuth
@ cpLocalAuth
Definition: httpserver.h:57
cpLocalNoAuth
@ cpLocalNoAuth
Definition: httpserver.h:56
std
Definition: mythchrono.h:23
HttpServer::m_basePaths
QMultiMap< QString, HttpServerExtension * > m_basePaths
Definition: httpserver.h:146
serverpool.h
HttpServerExtensionList
QList< QPointer< HttpServerExtension > > HttpServerExtensionList
Definition: httpserver.h:100
HttpServer::m_sslConfig
QSslConfiguration m_sslConfig
Definition: httpserver.h:155
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
HttpServer
Definition: httpserver.h:110
cpRemoteAuth
@ cpRemoteAuth
Definition: httpserver.h:58
HttpServer::m_extensions
HttpServerExtensionList m_extensions
Definition: httpserver.h:144
HttpServer::s_platformLock
static QMutex s_platformLock
Definition: httpserver.h:151
HttpWorker::HttpWorker
HttpWorker(HttpServer &httpServer, qt_socket_fd_t sock, PoolServerType type, const QSslConfiguration &sslConfig)
Definition: httpserver.cpp:445
HttpServerExtension::m_sName
QString m_sName
Definition: httpserver.h:75
RequestTypeGet
@ RequestTypeGet
Definition: httprequest.h:47
HttpServerExtension
Definition: httpserver.h:69
HttpWorker::m_socketTimeout
std::chrono::milliseconds m_socketTimeout
Definition: httpserver.h:196
upnputil.h
httprequest.h
qt_socket_fd_t
qintptr qt_socket_fd_t
Definition: mythqtcompat.h:4
HttpServerExtension::m_sSharePath
QString m_sSharePath
Definition: httpserver.h:76
mythqtcompat.h