-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTTP.cpp
436 lines (363 loc) · 11.5 KB
/
HTTP.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*========================================================
** University of Illinois/NCSA
** Open Source License
**
** Copyright (C) 2011,The Board of Trustees of the University of
** Illinois. All rights reserved.
**
** Developed by:
**
** Research Group of Professor Sam King in the Department of Computer
** Science The University of Illinois at Urbana-Champaign
** http://www.cs.uiuc.edu/homes/kingst/Research.html
**
** Copyright (C) Sam King
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the
** Software), to deal with the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimers.
**
** Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimers in the
** documentation and/or other materials provided with the distribution.
** Neither the names of Sam King or the University of Illinois,
** nor the names of its contributors may be used to endorse or promote
** products derived from this Software without specific prior written
** permission.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR
** ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
**==========================================================
*/
#include "HTTP.h"
#include <iostream>
#include <string>
#include <assert.h>
#include <stdio.h>
using namespace std;
/***************************** HTTP Parser callbacks ************************/
int HTTP::message_begin_cb(http_parser *parser)
{
HTTP *http = (HTTP *) parser->data;
assert(http->getState() == HTTP::INIT);
http->setState(HTTP::HEADER);
return 0;
}
int HTTP::path_cb(http_parser *parser, const char *at, size_t length)
{
HTTP *http = (HTTP *) parser->data;
http->m_path.append(at, length);
return 0;
}
int HTTP::query_string_cb(http_parser *parser, const char *at, size_t length)
{
HTTP *http = (HTTP *) parser->data;
http->m_query.append(at, length);
return 0;
}
int HTTP::url_cb(http_parser *parser, const char *at, size_t length)
{
HTTP *http = (HTTP *) parser->data;
http->appendUrl(at, length);
return 0;
}
int HTTP::fragment_cb(http_parser */*parser*/, const char */*at*/, size_t /*length*/)
{
cout << "fragment" << endl;
assert(false);
return 0;
}
int HTTP::header_field_cb(http_parser *parser, const char *at, size_t length)
{
HTTP *http = (HTTP *) parser->data;
if(http->getState() == HTTP::FIELD) {
http->appendHeaderField(at, length);
} else if((http->getState() == HTTP::VALUE) ||
(http->getState() == HTTP::HEADER)) {
http->newHeaderField(at, length);
http->setState(HTTP::FIELD);
} else {
assert(false);
}
return 0;
}
int HTTP::header_value_cb(http_parser *parser, const char *at, size_t length)
{
HTTP *http = (HTTP *) parser->data;
if(http->getState() == HTTP::FIELD) {
http->setState(HTTP::VALUE);
}
assert(http->getState() == HTTP::VALUE);
http->appendHeaderValue(at, length);
return 0;
}
int HTTP::headers_complete_cb(http_parser *parser)
{
HTTP *http = (HTTP *) parser->data;
http->addHeaderField();
http->m_headerDone = true;
if(http->m_httpType == HTTP_RESPONSE) {
char buf[64];
sprintf(buf, "HTTP/%u.%u %u ", parser->http_major, parser->http_minor, parser->status_code);
http->m_statusStr = buf;
if(parser->status_code == 200) {
http->m_statusStr += "OK";
} else if(parser->status_code == 204) {
http->m_statusStr += "No Content";
} else if(parser->status_code == 301) {
http->m_statusStr += "Moved Permanently";
} else if(parser->status_code == 302) {
http->m_statusStr += "Moved Temporarily";
} else if(parser->status_code == 304) {
http->m_statusStr += "Not Modified";
} else if(parser->status_code == 403) {
http->m_statusStr += "Forbidden";
} else if(parser->status_code == 404) {
http->m_statusStr += "Not Found";
} else if(parser->status_code == 408) {
http->m_statusStr += "Request Timeout";
} else if(parser->status_code == 500) {
http->m_statusStr += "Internal Server Error";
} else if(parser->status_code == 503) {
http->m_statusStr += "Service Unavailable";
} else {
assert(false);
}
http->m_extraParsedBytes = 1;
return -1;
}
return 0;
}
int HTTP::body_cb(http_parser *parser, const char *at, size_t length)
{
HTTP *http = (HTTP *) parser->data;
http->m_body.append(at, length);
return 0;
}
int HTTP::message_complete_cb(http_parser *parser)
{
HTTP *http = (HTTP *) parser->data;
assert((http->getState() == HTTP::VALUE) ||
(http->getState() == HTTP::BODY));
http->setState(HTTP::DONE);
http->messageComplete(parser->method);
return 0;
}
/****************************************************************************/
/*************************** Public Functions *******************************/
HTTP::HTTP(http_parser_type httpType)
{
m_state = INIT;
http_parser_init(&m_parser, httpType);
m_doneParsing = false;
m_httpType = httpType;
m_headerDone = false;
m_settings.on_message_begin = message_begin_cb;
m_settings.on_path = path_cb;
m_settings.on_query_string = query_string_cb;
m_settings.on_url = url_cb;
m_settings.on_fragment = fragment_cb;
m_settings.on_header_field = header_field_cb;
m_settings.on_header_value = header_value_cb;
m_settings.on_headers_complete = headers_complete_cb;
m_settings.on_body = body_cb;
m_settings.on_message_complete = message_complete_cb;
m_parser.data = this;
m_field = NULL;
m_value = NULL;
m_extraParsedBytes = 0;
}
HTTP::~HTTP()
{
if(m_field != NULL) {
delete m_field;
}
if(m_value != NULL) {
delete m_value;
}
for(unsigned int idx = 0; idx < m_headers.size(); idx++) {
delete m_headers[idx].first;
delete m_headers[idx].second;
}
}
int HTTP::addData(const unsigned char *data, int len)
{
if(m_doneParsing) {
assert(false);
}
int ret = http_parser_execute(&m_parser, &m_settings, (const char *) data, len);
ret += m_extraParsedBytes;
m_extraParsedBytes = 0;
return ret;
}
string HTTP::getBody()
{
return m_body;
}
string HTTP::getUrl()
{
return m_url;
}
string HTTP::getHost()
{
string host = (m_method == HTTP_CONNECT) ? m_url : m_host;
if(host.find(':') == string::npos) {
host += ":80";
}
return host;
}
bool HTTP::isHeaderDone()
{
return m_headerDone;
}
bool HTTP::isDone()
{
return m_doneParsing;
}
string HTTP::getReplyHeader()
{
string reply;
assert(m_httpType == HTTP_RESPONSE);
assert(m_statusStr.size() > 0);
reply = m_statusStr + "\r\n";
bool foundConn = false;
for(unsigned int idx = 0; idx < m_headers.size(); idx++) {
string field = *(m_headers[idx].first);
string value = *(m_headers[idx].second);
if(field == "Connection") {
value = "close";
foundConn = true;
}
reply += field + string(": ") + value + string("\r\n");
}
if(!foundConn) {
reply += "Connection: close\r\n";
}
reply += "\r\n";
return reply;
}
string HTTP::getProxyRequest(const char *userAgent)
{
string reply;
string urlPathQuery;
assert(m_httpType == HTTP_REQUEST);
if((m_method == HTTP_GET) || (m_method == HTTP_POST) || (m_method == HTTP_HEAD)) {
if(m_path.size() == 0) {
urlPathQuery = "/";
} else {
urlPathQuery = m_path;
}
if(m_query.size() > 0) {
urlPathQuery += "?" + m_query;
}
if(m_url.find(urlPathQuery) == string::npos) {
// this is a hack to get around buggy HTML from taobao
assert(m_query.size() > 0);
urlPathQuery = m_path + "??" + m_query;
if(m_url.find(urlPathQuery) == string::npos) {
cout << "url path mismatch " << m_url << endl << urlPathQuery << endl;
}
}
}
if(m_method == HTTP_GET) {
reply = "GET " + urlPathQuery + " HTTP/1.1\r\n";
} else if(m_method == HTTP_CONNECT) {
reply = "CONNECT " + m_url + " HTTP/1.1\r\n";
} else if(m_method == HTTP_POST) {
reply = "POST " + urlPathQuery + " HTTP/1.1\r\n";
} else if(m_method == HTTP_HEAD) {
reply = "HEAD " + urlPathQuery + " HTTP/1.1\r\n";
} else {
assert(false);
}
for(unsigned int idx = 0; idx < m_headers.size(); idx++) {
string field = *(m_headers[idx].first);
string value = *(m_headers[idx].second);
if((userAgent != NULL) && (field == "User-Agent")) {
value = string(userAgent);
}
if(field == "Proxy-Connection") {
field = string("Connection");
value = string("close");
//value = string("keep-alive");
}
if(field != "Keep-Alive") {
reply += field + string(": ") + value + string("\r\n");
}
}
reply += string("\r\n");
if(m_body.size() > 0) {
reply += m_body;
}
if(m_method == HTTP_HEAD) {
cout << reply;
}
return reply;
}
/****************************************************************************/
/************************** Private Functions *******************************/
HTTP::HttpState HTTP::getState()
{
return m_state;
}
void HTTP::setState(HttpState newState)
{
m_state = newState;
}
void HTTP::appendUrl(const char *at, size_t len)
{
m_url.append(at, len);
}
void HTTP::addHeaderField()
{
if(m_field != NULL) {
assert(m_value != NULL);
if(*m_field == "Host") {
m_host = *m_value;
}
if(*m_field == "Eoh") {
cout << "got the Eoh header" << endl;
}
m_headers.insert(m_headers.end(), pair<string *, string *>(m_field, m_value));
m_field = NULL;
m_value = NULL;
} else {
assert(m_value == NULL);
}
}
void HTTP::newHeaderField(const char *at, size_t len)
{
addHeaderField();
m_field = new string(at, len);
m_value = new string();
}
void HTTP::appendHeaderField(const char *at, size_t len)
{
assert(m_field != NULL);
m_field->append(at, len);
}
void HTTP::appendHeaderValue(const char *at, size_t len)
{
m_value->append(at, len);
}
void HTTP::messageComplete(unsigned char method)
{
if(m_httpType == HTTP_REQUEST) {
assert((method == HTTP_GET) || (method == HTTP_CONNECT) || (method == HTTP_POST) || (method == HTTP_HEAD));
m_method = method;
}
m_doneParsing = true;
}
/****************************************************************************/