/
httpserver.h
150 lines (118 loc) · 4.15 KB
/
httpserver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//////////////////////////////////////////////////////////////////////////////
// Program Name: httpserver.h
// Created : Oct. 1, 2005
//
// Purpose : HTTP 1.1 Mini Server Implmenetation
// Used for UPnp/AV implementation & status information
//
// Copyright (c) 2005 David Blain <dblain@mythtv.org>
//
// Licensed under the GPL v2 or later, see COPYING for details
//
//////////////////////////////////////////////////////////////////////////////
#ifndef __HTTPSERVER_H__
#define __HTTPSERVER_H__
// POSIX headers
#include <sys/types.h>
#ifndef USING_MINGW
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
// Qt headers
#include <QReadWriteLock>
#include <QMultiMap>
#include <QRunnable>
#include <QPointer>
#include <QMutex>
#include <QList>
// MythTV headers
#include "serverpool.h"
#include "httprequest.h"
#include "mthreadpool.h"
#include "refcounted.h"
#include "upnputil.h"
#include "compat.h"
typedef struct timeval TaskTime;
class HttpWorkerThread;
class QScriptEngine;
class HttpServer;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// HttpServerExtension Class Definition
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
class UPNP_PUBLIC HttpServerExtension : public QObject
{
Q_OBJECT
public:
QString m_sName;
QString m_sSharePath;
public:
HttpServerExtension( const QString &sName, const QString &sSharePath )
:m_sName( sName ), m_sSharePath( sSharePath ) {};
virtual ~HttpServerExtension() {};
virtual bool ProcessRequest(HTTPRequest *pRequest) = 0;
virtual QStringList GetBasePaths() = 0;
};
typedef QList<QPointer<HttpServerExtension> > HttpServerExtensionList;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// HttpServer Class Definition
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
class UPNP_PUBLIC HttpServer : public ServerPool
{
protected:
mutable QReadWriteLock m_rwlock;
HttpServerExtensionList m_extensions;
// This multimap does NOT take ownership of the HttpServerExtension*
QMultiMap< QString, HttpServerExtension* > m_basePaths;
QString m_sSharePath;
HttpServerExtension *m_pHtmlServer;
MThreadPool m_threadPool;
bool m_running; // protected by m_rwlock
static QMutex s_platformLock;
static QString s_platform;
public:
HttpServer(const QString sApplicationPrefix = QString(""));
virtual ~HttpServer();
void RegisterExtension(HttpServerExtension*);
void UnregisterExtension(HttpServerExtension*);
void DelegateRequest(HTTPRequest*);
QScriptEngine *ScriptEngine(void);
virtual void newTcpConnection(int socket); // QTcpServer
QString GetSharePath(void) const
{ // never modified after creation, so no need to lock
return m_sSharePath;
}
bool IsRunning(void) const
{
m_rwlock.lockForRead();
bool tmp = m_running;
m_rwlock.unlock();
return tmp;
}
static QString GetPlatform(void);
};
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// HttpWorkerThread Class Definition
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
class HttpWorker : public QRunnable
{
public:
HttpWorker(HttpServer &httpServer, int sock);
virtual void run(void);
protected:
HttpServer &m_httpServer;
int m_socket;
int m_socketTimeout;
};
#endif