-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtools.cpp
442 lines (346 loc) · 7.91 KB
/
tools.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
437
438
439
440
441
442
#include <stdio.h>
#include <string.h>
#include "tools.h"
#include <winsock2.h>
// 检查域名中是否含有端口号,有则去掉
void checkAddr(char* addr, int addr_len)
{
char* pCurrent;
int count;
pCurrent = addr;
count = 0;
while(*pCurrent != ':')
{
count++;
*pCurrent++;
}
count = addr_len - count;
memset(pCurrent, '\0', addr_len - count + 1);
}
void fprintHTTPMsg(FILE* file, char* msg,int msg_len)
{
int count = 0;
char* p = msg;
while(true)
{
if(count >= msg_len)
break;
if( (*p >= 32 && *p <=126) || *p == 10 || *p == 13)
fprintf(file, "%c", *p);
else
fprintf(file, ".");
count++;
p++;
}
fprintf(file, "\n");
}
int readLine(char* line, char* msg)
{
char *p = msg;
int counter = 0;
while(*p != '\n')
{
p++;
counter++;
}
counter++;
memcpy(line, msg, counter);
return counter;
}
int getHTTPMethod(char* http_request, char* method)
{
char *p = http_request;
int counter = 0;
while( *p != ' ')
{
p++;
counter++;
}
memcpy(method, http_request, counter);
return counter;
}
int getHTTPURL(char* http_request, char* url)
{
char *p = NULL;
char *url_start = NULL;
int counter = 0;
if( (p = strstr(http_request, "http://")) != NULL)
{
p += 7;
while( *p != '/')
{
p++;
}
url_start = p;
}
else
{
p = strstr(http_request, " ");
p++;
url_start = p;
}
while( *p != ' ')
{
p++;
counter++;
}
memcpy(url, url_start, counter);
return counter;
}
int getHTTPVersion(char* http_request, char* version)
{
char *p = http_request;
char *version_start = NULL;
int counter = 0;
while( *p != ' ') p++;
p++;
while( *p != ' ') p++;
p++;
version_start = p;
while( *p != '\r')
{
p++;
counter++;
}
memcpy(version, version_start, counter);
return counter;
}
void parseHTTPRequest(char* http_request, int http_request_len, ParseResult* result)
{
char new_http_request[TENKB_SIZE];
int new_http_request_len = 0;
char *p = NULL;
char line[KB_SIZE];
int line_len = 0;
int http_request_line_header_len = 0;
// 获取HTTP请求中的Host字段的值
char host[KB_SIZE];
int hostlen;
hostlen = getHTTPHost(http_request, host);
printf("%s\n", host);
// Host字段是IP地址
if(atoi(host))
{
getIPAddr(host, hostlen);
result->serverIPAddr = inet_addr(host);
}
else{
// 获取服务器地址
getIPAddr(host, hostlen);
hostent* hostEnt = gethostbyname(host);
if(hostEnt == NULL)
{
printf("Error at gethostbyname()\n");
}
result->serverIPAddr = (*(in_addr*)(hostEnt->h_addr_list[0])).s_addr;
}
/* 读取method */
char *method = NULL;
int method_len = 0;
if( (method = (char*)malloc(sizeof(char) * KB_SIZE)) != NULL)
{
method_len = getHTTPMethod(http_request, method);
}
result->isMethodEqConnect = false;
if(method != NULL && strncmp( method, "CONNECT", 7) == 0)
{
result->isMethodEqConnect = true;
return;
}
memcpy(new_http_request + new_http_request_len, method, method_len);
new_http_request_len += method_len;
if(method != NULL)
{
free(method);
method = NULL;
}
memcpy(new_http_request + new_http_request_len, " ", 1);
new_http_request_len++;
/* 读取url */
char *url = NULL;
int url_len = 0;
if( (url = (char*)malloc(sizeof(char) * KB_SIZE)) != NULL)
{
url_len = getHTTPURL(http_request, url);
}
memcpy(new_http_request + new_http_request_len, url, url_len);
new_http_request_len += url_len;
memcpy(new_http_request + new_http_request_len, " ", 1);
new_http_request_len++;
if(url != NULL)
{
free(url);
url = NULL;
}
/* 读取版本号 */
char* version = NULL;
int version_len = 0;
if( (version = (char*)malloc(sizeof(char) * KB_SIZE)) != NULL)
{
version_len = getHTTPVersion(http_request, version);
}
memcpy(new_http_request + new_http_request_len, version, version_len);
new_http_request_len += version_len;
if(version != NULL)
{
free(version);
version = NULL;
}
memcpy(new_http_request + new_http_request_len, "\r\n", 2);
new_http_request_len += 2;
/* 计算请求行的长度 */
p = http_request;
while( *p != '\n')
{
p++;
http_request_line_header_len++;
}
http_request_line_header_len++;
/* 读取请求首部 */
p = strstr(http_request, "\r\n");
p += 2;
while( *p != '\r' && *(p+1) != '\n')
{
line_len = readLine(line, p);
p += line_len;
http_request_line_header_len += line_len;
/* 读取Proxy-Connection,修改其为Connection*/
if( strstr(line, "Proxy-Connection: ") != NULL)
{
char *value = strstr(line, " ");
value++;
char *pTmp = NULL;
pTmp = value;
int counter = 0;
while( *pTmp != '\n')
{
pTmp++;
counter++;
}
memcpy(new_http_request + new_http_request_len, "Connection: ", strlen("Connection: "));
new_http_request_len += strlen("Connection: ");
memcpy(new_http_request + new_http_request_len, value, counter);
new_http_request_len += counter;
memcpy(new_http_request + new_http_request_len, "\r\n", 2);
new_http_request_len += 2;
continue;
}
memcpy(new_http_request + new_http_request_len, line, line_len);
new_http_request_len += line_len;
}
memcpy(new_http_request + new_http_request_len, "\r\n", 2);
new_http_request_len += 2;
/* p指向实体部分 */
p += 2;
http_request_line_header_len += 2;
if(p != NULL && http_request_len - http_request_line_header_len != 0)
{
memcpy(new_http_request + new_http_request_len, p, http_request_len - http_request_line_header_len);
new_http_request_len += http_request_len - http_request_line_header_len;
}
/* 将新的HTTP请求写入result */
memcpy(result->new_http_request, new_http_request, new_http_request_len);
result->new_http_request_len = new_http_request_len;
// printHTTPMsg(new_http_request, new_http_request_len, "new_http_request");
}
void printHTTPMsg(char* msg, int msglen, char* str)
{
int count = 0;
char* pMsg = NULL;
if( (pMsg = (char*)malloc(msglen)) != NULL)
{
memcpy(pMsg, msg, msglen);
}
char* p = pMsg;
printf("\n--------------%s (%d Bytes)----------------\n", str, msglen);
while(count < msglen)
{
if( 0<= *p && *p <= 255)
{
if( (32 <= *p && *p <=126) || *p == 10 || *p == 13)
printf("%c", *p);
else
printf("%c", '.');
}
count++;
p++;
}
printf("\n------------------------------------------------\n");
if(pMsg != NULL)
{
free(pMsg);
pMsg = NULL;
}
}
// 从IP:Port中获取IP地址
void getIPAddr(char* addr, int addr_len)
{
char* pCurrent;
int count;
bool findFlag = true;
pCurrent = addr;
count = 0;
while(*pCurrent != ':')
{
count++;
*pCurrent++;
if(count > addr_len)
{
findFlag = false;
break;
}
}
if(findFlag)
{
//count = addr_len - count;
memset(pCurrent, '\0', addr_len - count + 1);
}
}
int getHTTPHost(char* msg, char* host)
{
char* pCurrent;
char* pHostStart;
char* pHostEnd;
int host_len;
bool findFlag;
findFlag = false;
pCurrent = msg;
while(true)
{
// 跳过一行
while(*pCurrent != '\n')
{
// 查找字段名为Host对应的值
if(strncmp("Host: ", pCurrent, 6) == 0)
{
pCurrent += 6;
pHostStart = pCurrent;
while(*pCurrent != '\r')
pCurrent++;
pHostEnd = pCurrent;
findFlag = true;
break;
}
pCurrent++;
}
pCurrent++;
if(findFlag == true)
break;
}
host_len = pHostEnd - pHostStart;
memcpy(host, pHostStart, host_len);
host[host_len] = '\0';
return host_len;
}
// 返回response的长度,检查http报文中是否含有connect方法,若有则构造一个回复报文
int handleSSLConnect(char* httpmsg, char* response)
{
char *p;
p = strstr(httpmsg, "CONNECT");
if(p != NULL)
{
strcpy(response, "HTTP/1.1 200 Connection Established\r\n\r\n");
return strlen(response);
}
return 0;
}