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