forked from wangchuan3533/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
212 lines (196 loc) · 6.44 KB
/
http.c
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
#include "define.h"
#include "http.h"
http_request_t *http_request_create()
{
http_request_t *h = (http_request_t *)malloc(sizeof(http_request_t));
if (NULL == h) {
err_quit("malloc");
}
memset(h, 0, sizeof(http_request_t));
return h;
}
void http_request_destroy(http_request_t *h)
{
int i;
if (h) {
for (i = 0; i < h->count; i++) {
if (h->buffers[i])
free(h->buffers[i]);
}
free(h);
}
}
// TODO filter invalid request
http_request_state_t http_request_parse(struct evbuffer *b, http_request_t *h)
{
char *line, *tmp;
size_t n;
while ((line = evbuffer_readln(b, &n, EVBUFFER_EOL_CRLF)) != NULL) {
switch (h->state) {
case HTTP_REQUEST_STATE_STARTED:
// if no first line, goto error
if (n == 0) {
goto error;
}
// parse first line
// method
h->method = line;
tmp = strchr(line, ' ');
if (tmp == NULL) {
goto error;
}
*tmp++ = '\0';
// request_uri
h->request_uri = tmp;
tmp = strrchr(tmp, ' ');
if (tmp == NULL) {
goto error;
}
// http_version
*tmp++ = '\0';
h->http_version = tmp;
// store the pointer for free()
h->buffers[h->count++] = line;
// first line parse finished
h->state = HTTP_REQUEST_STATE_PARSED_FIRST_LINE;
break;
case HTTP_REQUEST_STATE_PARSED_FIRST_LINE:
// skip to next state
h->state = HTTP_REQUEST_STATE_HEADERS_PARSING;
case HTTP_REQUEST_STATE_HEADERS_PARSING:
if (n == 0) {
goto finished;
}
// parse request
if (!h->host && strncasecmp(HTTP_HEADER_HOST, line, strlen(HTTP_HEADER_HOST)) == 0) {
tmp = strchr(line, ' ');
h->host = tmp + 1;
h->buffers[h->count++] = line;
} else if (!h->user_agent && strncasecmp(HTTP_HEADER_USER_AGENT, line, strlen(HTTP_HEADER_USER_AGENT)) == 0) {
tmp = strchr(line, ' ');
h->user_agent = tmp + 1;
h->buffers[h->count++] = line;
} else if (!h->connection && strncasecmp(HTTP_HEADER_CONNECTION, line, strlen(HTTP_HEADER_CONNECTION)) == 0) {
tmp = strchr(line, ' ');
h->connection = tmp + 1;
h->buffers[h->count++] = line;
} else if (!h->cookie && strncasecmp(HTTP_HEADER_COOKIE, line, strlen(HTTP_HEADER_COOKIE)) == 0) {
tmp = strchr(line, ' ');
h->cookie = tmp + 1;
h->buffers[h->count++] = line;
} else if (!h->upgrade && strncasecmp(HTTP_HEADER_UPGRADE, line, strlen(HTTP_HEADER_UPGRADE)) == 0) {
tmp = strchr(line, ' ');
h->upgrade = tmp + 1;
h->buffers[h->count++] = line;
} else if (!h->sec_websocket_key && strncasecmp(HTTP_HEADER_SEC_WEBSOCKET_KEY, line, strlen(HTTP_HEADER_SEC_WEBSOCKET_KEY)) == 0) {
tmp = strchr(line, ' ');
h->sec_websocket_key = tmp + 1;
h->buffers[h->count++] = line;
} else if (!h->sec_websocket_protocol && strncasecmp(HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL, line, strlen(HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL)) == 0) {
tmp = strchr(line, ' ');
h->sec_websocket_protocol = tmp + 1;
h->buffers[h->count++] = line;
} else if (!h->sec_websocket_version && strncasecmp(HTTP_HEADER_SEC_WEBSOCKET_VERSION, line, strlen(HTTP_HEADER_SEC_WEBSOCKET_VERSION)) == 0) {
tmp = strchr(line, ' ');
h->sec_websocket_version = tmp + 1;
h->buffers[h->count++] = line;
} else {
// ignore
free(line);
}
break;
case HTTP_REQUEST_STATE_FINISHED:
// impossible
goto finished;
case HTTP_REQUEST_STATE_ERROR:
// impossible
goto error;
}
}
return h->state;
error:
free(line);
h->state = HTTP_REQUEST_STATE_ERROR;
return h->state;
finished:
free(line);
h->state = HTTP_REQUEST_STATE_FINISHED;
return h->state;
}
void print_http_request(http_request_t *h)
{
int i;
printf("\n");
printf("state:%d\n", h->state);
if (h->method)
printf("[method]:%s\n", h->method);
else
printf("[method]:NULL\n");
if (h->request_uri)
printf("[request_uri]:%s\n", h->request_uri);
else
printf("[request_uri]:NULL\n");
if (h->http_version)
printf("[http_version]:%s\n", h->http_version);
else
printf("[http_version]:NULL\n");
if (h->host)
printf("[host]:%s\n", h->host);
else
printf("[host]:NULL\n");
if (h->user_agent)
printf("[user_agent]:%s\n", h->user_agent);
else
printf("[user_agent]:NULL\n");
if (h->cookie)
printf("[cookie]:%s\n", h->cookie);
else
printf("[cookie]:NULL\n");
if (h->connection)
printf("[connection]:%s\n", h->connection);
else
printf("[connection]:NULL\n");
if (h->upgrade)
printf("[upgrade]:%s\n", h->upgrade);
else
printf("[upgrade]:NULL\n");
if (h->sec_websocket_key)
printf("[sec_websocket_key]:%s\n", h->sec_websocket_key);
else
printf("[sec_websocket_key]:NULL\n");
if (h->sec_websocket_protocol)
printf("[sec_websocket_protocol]:%s\n", h->sec_websocket_protocol);
else
printf("[sec_websocket_protocol]:NULL\n");
if (h->sec_websocket_version)
printf("[sec_websocket_version]:%s\n", h->sec_websocket_version);
else
printf("[sec_websocket_version]:NULL\n");
printf("\nDUMP LINES:\n");
for (i = 0; i < h->count; i++) {
printf("[line %d]: %s\n", i, h->buffers[i]);
}
printf("\n");
}
int send_http_response(struct evbuffer *b, int response)
{
const char *resp;
switch (response) {
case 200:
resp = HTTP_RESPONSE_200;
break;
case 400:
resp = HTTP_RESPONSE_400;
break;
case 403:
resp = HTTP_RESPONSE_403;
break;
case 404:
default:
resp = HTTP_RESPONSE_404;
}
if (evbuffer_add(b, resp, strlen(resp)) != 0) {
return -1;
}
return 0;
}