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#include <QtGlobal>
18#if QT_VERSION >= QT_VERSION_CHECK(6,5,0)
19#include <QtSystemDetection>
20#endif
21// POSIX headers
22#include <sys/types.h>
23#ifndef Q_OS_WINDOWS
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#endif
27#include <utility>
28
29// Qt headers
30#include <QChar> // Fix Qt6 GCC SFINAE warning
31#include <QReadWriteLock>
32#include <QMultiMap>
33#include <QRunnable>
34#include <QPointer>
35#include <QMutex>
36#include <QList>
37#include <QSslConfiguration>
38#include <QSslError>
39#include <QSslKey>
40#include <QSslSocket>
41
42// MythTV headers
43#include "libmythbase/compat.h"
46
47#include "httprequest.h"
48
49class HttpWorkerThread;
50class HttpServer;
51#ifndef QT_NO_OPENSSL
52class QSslKey;
53class QSslCertificate;
54class QSslConfiguration;
55#endif
56
57enum ContentProtection : std::uint8_t
58{
59 cpLocalNoAuth = 0x00, // Can only be accessed locally, but no authentication is required
60 cpLocalAuth = 0x01, // Can only be accessed locally, authentication is required
61 cpRemoteAuth = 0x02 // Can be accessed remotely, authentication is required
62};
63
66//
67// HttpServerExtension Class Definition
68//
71
72class UPNP_PUBLIC HttpServerExtension : public QObject
73{
74 Q_OBJECT
75
76 public:
77
78 QString m_sName;
79 QString m_sSharePath;
80 int m_nSocketTimeout { -1 }; // Extension may wish to adjust the default e.g. UPnP
81
82 // HTTP::RequestType. Defaults, extensions may extend the list
83 uint m_nSupportedMethods
86
87 public:
88
89 HttpServerExtension( QString sName, QString sSharePath)
90 : m_sName(std::move( sName )), m_sSharePath(std::move( sSharePath )) {};
91
92 ~HttpServerExtension() override = default;
93
94 virtual bool ProcessRequest(HTTPRequest *pRequest) = 0;
95
96 virtual bool ProcessOptions(HTTPRequest *pRequest);
97
98 virtual QStringList GetBasePaths() = 0;
99
100 virtual int GetSocketTimeout() const { return m_nSocketTimeout; }// -1 = Use config value
101};
102
103using HttpServerExtensionList = QList<QPointer<HttpServerExtension> >;
104
107//
108// HttpServer Class Definition
109//
112
114{
115 Q_OBJECT
116
117 public:
118 HttpServer();
119 ~HttpServer() override;
120
121 void RegisterExtension(HttpServerExtension *pExtension);
122 void UnregisterExtension(HttpServerExtension *pExtension);
123 void DelegateRequest(HTTPRequest *pRequest);
127 uint GetSocketTimeout(HTTPRequest *pRequest) const;
128
129 QString GetSharePath(void) const
130 { // never modified after creation, so no need to lock
131 return m_sSharePath;
132 }
133
134 bool IsRunning(void) const
135 {
136 m_rwlock.lockForRead();
137 bool tmp = m_running;
138 m_rwlock.unlock();
139 return tmp;
140 }
141
142 static QString GetPlatform(void);
143 static QString GetServerVersion(void);
144
145 protected:
146 mutable QReadWriteLock m_rwlock;
148 // This multimap does NOT take ownership of the HttpServerExtension*
149 QMultiMap< QString, HttpServerExtension* > m_basePaths;
152 bool m_running { true }; // protected by m_rwlock
153
154 static QMutex s_platformLock;
155 static QString s_platform;
156
157#ifndef QT_NO_OPENSSL
158 QSslConfiguration m_sslConfig;
159#endif
160
161 const QString m_privateToken; // Private token; Used to salt digest auth nonce, changes on backend restart
162
163 protected slots:
164 void newTcpConnection(qintptr socket) override; // QTcpServer
165
166 private:
167 void LoadSSLConfig();
168};
169
172//
173// HttpWorkerThread Class Definition
174//
177
178class HttpWorker : public QRunnable
179{
180 public:
181
188 HttpWorker(HttpServer &httpServer, qintptr sock, PoolServerType type
189#ifndef QT_NO_OPENSSL
190 , const QSslConfiguration& sslConfig
191#endif
192 );
193
194 void run(void) override; // QRunnable
195
196 protected:
198 qintptr m_socket;
199 std::chrono::milliseconds m_socketTimeout;
201
202#ifndef QT_NO_OPENSSL
203 QSslConfiguration m_sslConfig;
204#endif
205};
206
207
208#endif // HTTPSERVER_H
~HttpServerExtension() override=default
HttpServerExtension(QString sName, QString sSharePath)
Definition: httpserver.h:89
virtual int GetSocketTimeout() const
Definition: httpserver.h:100
virtual QStringList GetBasePaths()=0
virtual bool ProcessRequest(HTTPRequest *pRequest)=0
QString m_sSharePath
Definition: httpserver.h:79
QMultiMap< QString, HttpServerExtension * > m_basePaths
Definition: httpserver.h:149
QString m_sSharePath
Definition: httpserver.h:150
static QMutex s_platformLock
Definition: httpserver.h:154
MThreadPool m_threadPool
Definition: httpserver.h:151
QReadWriteLock m_rwlock
Definition: httpserver.h:146
bool IsRunning(void) const
Definition: httpserver.h:134
HttpServerExtensionList m_extensions
Definition: httpserver.h:147
static QString s_platform
Definition: httpserver.h:155
QString GetSharePath(void) const
Definition: httpserver.h:129
QSslConfiguration m_sslConfig
Definition: httpserver.h:158
const QString m_privateToken
Definition: httpserver.h:161
HttpWorker(HttpServer &httpServer, qintptr sock, PoolServerType type, const QSslConfiguration &sslConfig)
Definition: httpserver.cpp:440
HttpServer & m_httpServer
Definition: httpserver.h:197
PoolServerType m_connectionType
Definition: httpserver.h:200
qintptr m_socket
Definition: httpserver.h:198
std::chrono::milliseconds m_socketTimeout
Definition: httpserver.h:199
void run(void) override
Definition: httpserver.cpp:460
QSslConfiguration m_sslConfig
Definition: httpserver.h:203
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:60
unsigned int uint
Definition: compat.h:60
@ RequestTypePost
Definition: httprequest.h:52
@ RequestTypeOptions
Definition: httprequest.h:56
@ RequestTypeGet
Definition: httprequest.h:50
@ RequestTypeHead
Definition: httprequest.h:51
QList< QPointer< HttpServerExtension > > HttpServerExtensionList
Definition: httpserver.h:103
ContentProtection
Definition: httpserver.h:58
@ cpLocalNoAuth
Definition: httpserver.h:59
@ cpLocalAuth
Definition: httpserver.h:60
@ cpRemoteAuth
Definition: httpserver.h:61
PoolServerType
Definition: serverpool.h:30
#define UPNP_PUBLIC
Definition: upnpexp.h:9