-
Notifications
You must be signed in to change notification settings - Fork 0
/
Http_Request.cc
187 lines (175 loc) · 3.66 KB
/
Http_Request.cc
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
#include"Http_Request.h"
using namespace std;
Http_Request::Http_Request() {
method = NULL;
url = NULL;
abs_url = NULL;
first_read = true;
get_head = false;
request_line = true;
}
// This function analysis and repair the
// HTTP request header and recompose
// HTTP request.
status Http_Request::handle_request(char *buffer, char *&url, char *&transfer) {
//cout << "buffer = " << buffer << endl;
int counter = 0;
bool host;
char *header = new char[strlen(buffer) * 2];
transfer = new char[strlen(buffer) * 2];
char *tmp = buffer;
int count = 0;
int num_spac = 0;
int flag = 0;
bool relative = false;
// Check the header of both absolute and relative
// URL and recompose the header for the request
while (*tmp != '\0') {
if (flag < 2) {
if (*tmp == 'g' && num_spac == 0)
*tmp = 'G';
if (*tmp == 'e' && num_spac == 0)
*tmp = 'E';
if (*tmp == 't' && num_spac == 0)
*tmp = 'T';
if (*tmp != ' ' && num_spac > 1)
num_spac = 1;
if (*tmp == ' ' && flag == 0)
num_spac++;
if (num_spac > 1) {
tmp++;
continue;
}
if (*tmp == '/' && num_spac == 1) {
while (*tmp != ' ') {
header[count++] = *tmp;
tmp++;
}
relative = true;
flag = 1;
}
if (!relative) {
if (*tmp == 'H')
*tmp = 'h';
if (*tmp == 'T' && num_spac > 0)
*tmp = 't';
if (*tmp == 'P' || *tmp == 'p') {
*tmp = 'p';
flag = 1;
}
if (*tmp == ' ' && flag == 1) {
flag = 2;
}
header[count++] = *tmp;
}
if (*tmp == ' ' && flag == 1 && relative) {
flag = 2;
header[count++] = *tmp;
tmp++;
}
}
if (flag == 2) {
while (*tmp != '\r' && *tmp != '\n') {
if (*tmp == 'h') {
*tmp = 'H';
}
if (*tmp == 't') {
*tmp = 'T';
}
if (*tmp == 'p' || *tmp == 'P') {
*tmp = 'P';
flag = 3;
}
if (*tmp != ' ')
header[count++] = *tmp;
tmp++;
}
header[count - 1] = '0';
header[count] = *tmp;
tmp++;
if (*tmp != '\n')//if request line not terminate with '\n'
header[count++] = '\n';
else {
header[count++] = *tmp;
tmp++;
}
}
if (flag == 3) {
header[count] = '\0';
strcat(header, tmp);
break;
}
tmp++;
}
cout << "header = " << header << endl;
char *p;
strcpy(transfer, header);
first_read = false;
if (request_line) {
p = strtok(header, " \r\n");
while (p != NULL) {
if (strlen(p) == 0)
continue;
switch (counter) {
case 0:
if (handle_method(p) < 0) {
printf("invalid method\n");
return method_not_found;
}
counter++;
break;
case 1:
counter++;
break;
case 2:
if (handle_version(p) < 0)
error("invalid version\n");
counter++;
request_line = false;
break;
case 3:
counter++;
break;
case 4:
url = handle_host(p);
counter++;
break;
default:
break;
}
p = strtok(NULL, " \r\n");
}
}
return ok;
}
// Get the absolute url
char *Http_Request::get_abs_url() {
return this->abs_url;
}
// Handle the method
int Http_Request::handle_method(char *method) {
char GET[] = "GET";
if (strcmp(method, GET) == 0) {
this->method = new char[strlen(method) + 1];
memcpy(this->method, method, strlen(method));
return 1;
} else
return -1;
}
// parste url from host part.
char* Http_Request::handle_host(char *&url) {
char *p = url;
char *result = new char[strlen(p) + 1];
strcpy(result, url);
return result;
}
// check if the version is 1.0 if not change to 1.0
int Http_Request::handle_version(char *version) {
if (strlen(version) > 10)
return -1;
if (version[8] == '1')
version[8] = 0;
this->version = new char[strlen(version) + 1];
memcpy(this->version, version, strlen(version));
return 1;
}