-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp.c
More file actions
341 lines (263 loc) · 6.9 KB
/
http.c
File metadata and controls
341 lines (263 loc) · 6.9 KB
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "http.h"
#include "tcp.h"
struct http_client *http_client_table;
const char * const mime_types[] = {
"htm", "text/html",
"txt", "text/plain",
"css", "text/css",
"js", "text/javascript",
"jsn", "application/json",
"xml", "text/xml",
"jpg", "image/jpeg",
"png", "image/png",
"gif", "image/gif",
"ico", "image/x-icon",
"svg", "image/svg+xml",
NULL
};
const char * const text_types[] = { "htm", "txt", "css", "js", "jsn", "xml", "svg", NULL };
uint8_t *http_tx_buffer;
const char *http_system_response_fmt = "\
HTTP/1.0 %u %s\r\n\
Content-Type: text/html\r\n\
Content-Length: %u\r\n\
\r\n\
%u %s\r\n";
const char *http_response_fmt = "\
HTTP/1.0 200 OK\r\n\
Content-Type: %s\r\n\
Content-Length: %lu\r\n\
\r\n";
void http_init(void) {
http_client_table = calloc(HTTP_MAX_CLIENTS, sizeof(struct http_client));
http_tx_buffer = malloc(TCP_PACKET_LEN);
}
struct http_client *http_get_client(struct tcp_sock *s) {
uint8_t i;
for (i = 0; i < HTTP_MAX_CLIENTS; i++) {
if (http_client_table[i].s == s) {
return &http_client_table[i];
}
}
return NULL;
}
void http_log(struct http_client *c, uint16_t code) {
printf("%u.%u.%u.%u %s %s %u\n",
c->s->daddr[0], c->s->daddr[1], c->s->daddr[2], c->s->daddr[3],
c->req_method, c->req_file, code);
}
void http_system_response(struct http_client *c, uint16_t code, char *message) {
sprintf((char *)http_tx_buffer, http_system_response_fmt, code, message, 4 + strlen(message) + 2, code, message);
c->tx_cur = strlen((char *)http_tx_buffer);
http_log(c, code);
c->state = HTTP_TX_HDR;
}
char *http_content_type(struct http_client *c) {
char ext[4];
uint8_t i;
uint16_t len = strlen(c->req_file);
if (len < 4) {
return "application/octet-stream";
}
strncpy(ext, &c->req_file[len - 3], 4);
for (i = 0; mime_types[i]; i += 2) {
if (strcasecmp(ext, mime_types[i]) == 0) {
return mime_types[i + 1];
}
}
return "application/octet-stream";
}
uint8_t http_file_mode(struct http_client *c) {
char ext[4];
uint8_t i;
uint16_t len = strlen(c->req_file);
if (len < 4) {
return HTTP_FILE_MODE_BINARY;
}
strncpy(ext, &c->req_file[len - 3], 4);
for (i = 0; text_types[i]; i++) {
if (strcasecmp(ext, text_types[i]) == 0) {
return HTTP_FILE_MODE_TEXT;
}
}
return HTTP_FILE_MODE_BINARY;
}
int16_t http_file_open(struct http_client *c) {
if (c->file_mode == HTTP_FILE_MODE_TEXT) {
return open(&c->req_file[1], O_RDONLY, _IOTEXT);
} else {
return open(&c->req_file[1], O_RDONLY, 0);
}
}
uint32_t http_content_length(struct http_client *c, int16_t fd) {
uint32_t pos;
_fcb[fd].mode = 0;
lseek(fd, 0, SEEK_END);
pos = fdtell(fd);
if (c->file_mode == HTTP_FILE_MODE_TEXT) {
if (pos >= SECSIZE) {
lseek(fd, pos - SECSIZE, SEEK_SET);
}
_fcb[fd].mode = _IOTEXT;
lseek(fd, 0, SEEK_END);
pos = fdtell(fd);
}
return pos;
}
void http_response(struct http_client *c) {
c->file_mode = http_file_mode(c);
c->fd = http_file_open(c);
if (c->fd >= 0) {
c->tx_len = http_content_length(c, c->fd);
sprintf((char *)http_tx_buffer, http_response_fmt, http_content_type(c), c->tx_len);
http_log(c, 200);
// For HEAD requests, close file since we won't send the body
if (strncmp(c->req_method, "HEAD", 4) == 0) {
close(c->fd);
c->fd = -1;
}
c->state = HTTP_TX_HDR;
} else {
http_system_response(c, 404, "Not Found");
}
}
void http_parse_request(struct http_client *c) {
char *req_method;
char *req_file;
if (c->rx_cur < 9) {
return;
}
if (strncmp(&c->rx_buff[c->rx_cur - 4], "\r\n\r\n", 4) != 0) {
return;
}
req_method = strtok(c->rx_buff, " ");
if (!req_method) {
http_system_response(c, 400, "Bad Request");
return;
}
if (strlen(req_method) > 7) {
http_system_response(c, 400, "Bad Request");
return;
}
req_file = strtok(NULL, " ");
if (!req_file) {
http_system_response(c, 400, "Bad Request");
return;
}
if (strlen(req_file) >= 14) {
http_system_response(c, 414, "URI Too Long");
return;
}
if (req_file[0] != '/') {
http_system_response(c, 400, "Bad Request");
return;
}
if (strchr(req_file, ':')) {
http_system_response(c, 400, "Bad Request");
return;
}
strcpy(c->req_method, req_method);
if (strlen(req_file) == 1) {
strcpy(c->req_file, "/INDEX.HTM");
} else {
strcpy(c->req_file, req_file);
}
if (strncmp(c->req_method, "GET", 3) == 0 || strncmp(c->req_method, "HEAD", 4) == 0) {
http_response(c);
} else {
http_system_response(c, 404, "Not Found");
}
}
void http_open(struct tcp_sock *s) {
struct http_client *c = &http_client_table[0];
struct http_client *cc;
uint8_t i;
for (i = 0; i < HTTP_MAX_CLIENTS; i++) {
cc = &http_client_table[i];
if (!cc->s) {
c = cc;
break;
}
if (cc->s->ticks > c->s->ticks) {
c = cc;
}
}
if (c->s) {
printf("Client limit reached: evicting %d.%d.%d.%d:%u ticks=%u\n",
c->s->daddr[0], c->s->daddr[1], c->s->daddr[2], c->s->daddr[3], c->s->dport, c->s->ticks);
tcp_sock_close(c->s);
}
memset(c, 0, sizeof(struct http_client));
c->s = s;
c->state = HTTP_RX_REQ;
c->fd = -1;
}
void http_recv(struct tcp_sock *s, uint8_t *data, uint16_t len) {
struct http_client *c = http_get_client(s);
if (!c) {
return;
}
if (c->state != HTTP_RX_REQ) {
return;
}
if (c->rx_cur + len > HTTP_RX_LEN) {
http_system_response(c, 431, "Request Header Fields Too Large");
return;
}
memcpy(&c->rx_buff[c->rx_cur], data, len);
c->rx_cur += len;
http_parse_request(c);
}
void http_send(struct tcp_sock *s, uint16_t len) {
struct http_client *c = http_get_client(s);
if (!c) {
return;
}
switch (c->state) {
case HTTP_TX_HDR:
if (c->fd < 0) {
tcp_tx_data_fin(c->s, http_tx_buffer, strlen((char *)http_tx_buffer));
} else {
tcp_tx_data(c->s, http_tx_buffer, strlen((char *)http_tx_buffer));
c->state = HTTP_TX_BODY;
c->tx_cur = 0;
}
break;
case HTTP_TX_BODY:
if (len > TCP_PACKET_LEN) {
len = TCP_PACKET_LEN;
}
lseek(c->fd, c->tx_cur, SEEK_SET);
len = read(c->fd, http_tx_buffer, len);
if (len > 0) {
c->tx_cur += len;
if (c->tx_cur >= c->tx_len) {
tcp_tx_data_fin(c->s, http_tx_buffer, len);
close(c->fd);
c->fd = -1;
} else {
tcp_tx_data(c->s, http_tx_buffer, len);
}
} else {
// EOF or read error - abort the connection
close(c->fd);
c->fd = -1;
tcp_tx_rst(c->s);
tcp_sock_close(c->s);
}
break;
}
}
void http_close(struct tcp_sock *s) {
struct http_client *c = http_get_client(s);
if (!c) {
return;
}
if (c->fd >= 0) {
close(c->fd);
}
memset(c, 0, sizeof(struct http_client));
}