-
Notifications
You must be signed in to change notification settings - Fork 342
/
httprequest.h
319 lines (232 loc) · 10.2 KB
/
httprequest.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//////////////////////////////////////////////////////////////////////////////
// Program Name: httprequest.h
// Created : Oct. 21, 2005
//
// Purpose : Http Request/Response
//
// Copyright (c) 2005 David Blain <dblain@mythtv.org>
//
// Licensed under the GPL v2 or later, see COPYING for details
//
//////////////////////////////////////////////////////////////////////////////
#ifndef HTTPREQUEST_H_
#define HTTPREQUEST_H_
#include <QFile>
#include <QRegExp>
#include <QBuffer>
#include <QTextStream>
#include <QTcpSocket>
#include <QDateTime>
#include "mythsession.h"
#include "upnpexp.h"
#include "upnputil.h"
#include "serializers/serializer.h"
#define SOAP_ENVELOPE_BEGIN "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " \
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" \
"<s:Body>"
#define SOAP_ENVELOPE_END "</s:Body>\r\n</s:Envelope>";
/////////////////////////////////////////////////////////////////////////////
// Typedefs / Defines
/////////////////////////////////////////////////////////////////////////////
// SECURITY: We intentionally want to ignore TRACE requests because of XST attacks
typedef enum
{
RequestTypeUnknown = 0x0000,
RequestTypeGet = 0x0001,
RequestTypeHead = 0x0002,
RequestTypePost = 0x0004,
RequestTypeMSearch = 0x0008,
RequestTypeSubscribe = 0x0010,
RequestTypeUnsubscribe = 0x0020,
RequestTypeNotify = 0x0040,
RequestTypeResponse = 0x0080
} RequestType;
typedef enum
{
ContentType_Unknown = 0,
ContentType_Urlencoded = 1,
ContentType_XML = 2
} ContentType;
typedef enum
{
ResponseTypeNone = -1,
ResponseTypeUnknown = 0,
ResponseTypeXML = 1,
ResponseTypeHTML = 2,
ResponseTypeJS = 3,
ResponseTypeCSS = 4,
ResponseTypeText = 5,
ResponseTypeSVG = 6,
ResponseTypeFile = 7,
ResponseTypeOther = 8
} ResponseType;
typedef struct
{
const char *pszExtension;
const char *pszType;
} MIMETypes;
/////////////////////////////////////////////////////////////////////////////
class IPostProcess
{
public:
virtual void ExecutePostProcess( ) = 0;
virtual ~IPostProcess() {};
};
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
class UPNP_PUBLIC HTTPRequest
{
protected:
static const char *m_szServerHeaders;
QRegExp m_procReqLineExp;
QRegExp m_parseRangeExp;
public:
RequestType m_eType;
ContentType m_eContentType;
QString m_sRawRequest;
QString m_sBaseUrl;
QString m_sResourceUrl;
QString m_sMethod;
QStringMap m_mapParams;
QStringMap m_mapHeaders;
QStringMap m_mapCookies;
QString m_sPayload;
QString m_sProtocol;
int m_nMajor;
int m_nMinor;
bool m_bProtected;
bool m_bEncrypted;
bool m_bSOAPRequest;
QString m_sNameSpace;
// Response
ResponseType m_eResponseType;
QString m_sResponseTypeText; // used for ResponseTypeOther
long m_nResponseStatus;
QStringMap m_mapRespHeaders;
QString m_sFileName;
QBuffer m_response;
IPostProcess *m_pPostProcess;
QString m_sPrivateToken;
MythUserSession m_userSession;
private:
bool m_bKeepAlive;
uint m_nKeepAliveTimeout;
protected:
RequestType SetRequestType ( const QString &sType );
void SetRequestProtocol ( const QString &sLine );
ContentType SetContentType ( const QString &sType );
void ProcessRequestLine ( const QString &sLine );
bool ProcessSOAPPayload ( const QString &sSOAPAction );
void ExtractMethodFromURL( );
QString GetResponseStatus ( void );
QString GetResponseType ( void );
QString GetResponseHeaders ( void );
bool ParseRange ( QString sRange,
long long llSize,
long long *pllStart,
long long *pllEnd );
bool ParseKeepAlive ( void );
void ParseCookies ( void );
QString BuildResponseHeader ( long long nSize );
qint64 SendData ( QIODevice *pDevice, qint64 llStart, qint64 llBytes );
qint64 SendFile ( QFile &file, qint64 llStart, qint64 llBytes );
bool IsProtected () const { return m_bProtected; }
bool IsEncrypted () const { return m_bEncrypted; }
bool Authenticated ();
QString GetAuthenticationHeader (bool isStale = false);
QString CalculateDigestNonce ( const QString &timeStamp);
bool BasicAuthentication ();
bool DigestAuthentication ();
public:
HTTPRequest ();
virtual ~HTTPRequest () {};
bool ParseRequest ();
void FormatErrorResponse ( bool bServerError,
const QString &sFaultString,
const QString &sDetails );
void FormatActionResponse( Serializer *ser );
void FormatActionResponse( const NameValues &pArgs );
void FormatFileResponse ( const QString &sFileName );
void FormatRawResponse ( const QString &sXML );
qint64 SendResponse ( void );
qint64 SendResponseFile( QString sFileName );
void SetResponseHeader ( const QString &sKey,
const QString &sValue,
bool replace = false );
void SetCookie ( const QString &sKey, const QString &sValue,
const QDateTime &dtExpires,
bool secure );
QString GetRequestHeader ( const QString &sKey, QString sDefault );
bool GetKeepAlive () { return m_bKeepAlive; }
Serializer * GetSerializer ();
QString GetRequestProtocol () const;
QString GetResponseProtocol () const;
QString GetRequestType () const;
static QString GetMimeType ( const QString &sFileExtension );
static QStringList GetSupportedMimeTypes ();
static QString TestMimeType ( const QString &sFileName );
static long GetParameters ( QString sParams, QStringMap &mapParams );
static QString Encode ( const QString &sIn );
static QString Decode ( const QString &sIn );
static QString GetETagHash ( const QByteArray &data );
void SetKeepAliveTimeout ( int nTimeout ) { m_nKeepAliveTimeout = nTimeout; }
static bool IsUrlProtected ( const QString &sBaseUrl );
// ------------------------------------------------------------------
virtual QString ReadLine ( int msecs ) = 0;
virtual qint64 ReadBlock ( char *pData, qint64 nMaxLen, int msecs = 0 ) = 0;
virtual qint64 WriteBlock ( const char *pData,
qint64 nLen ) = 0;
virtual QString GetHostName (); // RFC 3875 - The name in the client request
virtual QString GetHostAddress () = 0;
virtual quint16 GetHostPort () = 0;
virtual QString GetPeerAddress () = 0;
virtual int getSocketHandle () = 0;
};
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
class BufferedSocketDeviceRequest : public HTTPRequest
{
public:
QTcpSocket *m_pSocket;
public:
BufferedSocketDeviceRequest( QTcpSocket *pSocket );
virtual ~BufferedSocketDeviceRequest() {};
virtual QString ReadLine ( int msecs );
virtual qint64 ReadBlock ( char *pData, qint64 nMaxLen, int msecs = 0 );
virtual qint64 WriteBlock ( const char *pData, qint64 nLen );
virtual QString GetHostAddress ();
virtual quint16 GetHostPort ();
virtual QString GetPeerAddress ();
virtual int getSocketHandle () {return( m_pSocket->socketDescriptor() ); }
};
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
class UPNP_PUBLIC HttpException
{
public:
int code;
QString msg;
HttpException( int nCode = -1, const QString &sMsg = "")
: code( nCode ), msg ( sMsg )
{}
// Needed to force a v-table.
virtual ~HttpException()
{}
};
class UPNP_PUBLIC HttpRedirectException : public HttpException
{
public:
QString hostName;
//int port;
HttpRedirectException( const QString &sHostName = "",
int nCode = -1,
const QString &sMsg = "" )
: HttpException( nCode, sMsg ), hostName( sHostName )
{}
virtual ~HttpRedirectException()
{}
};
#endif