-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttpd.c
More file actions
362 lines (330 loc) · 9.65 KB
/
Copy pathhttpd.c
File metadata and controls
362 lines (330 loc) · 9.65 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) 2009 Braiden Kindt
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in 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:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* 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 AUTHORS 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 IN THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <string.h>
#include "httpd.h"
#include "socket.h"
#include "uart.h"
#include "w5100.h"
#include "romfs.h"
#include "debug.h"
#include "pff.h"
uint8_t _listen_sock;
uint8_t _clients[HTTPD_MAX_CLIENTS];
uint8_t _connected_clients;
uint8_t _fdset;
void httpd_init()
{
// create listening socket
_listen_sock = sock_listen(HTTPD_PORT, HTTPD_MAX_CLIENTS);
// no connected clients
_connected_clients = 0;
_fdset = 0x00;
// add listening socket to our FD set (used by select)
FD_SET(_fdset, _listen_sock);
}
void httpd_write_response(uint8_t sockfd, uint8_t status)
{
sock_write_P(sockfd, PSTR("HTTP/1.1 "), 9);
if (status == HTTPD_OK) {
sock_write_P(sockfd, PSTR("200 OK\r\n"), 8);
log("200\n");
} else {
if (status == HTTPD_NOTFOUND) {
sock_write_P(sockfd, PSTR("404 NOT FOUND\r\n"), 15);
log("404\n");
} else {
sock_write_P(sockfd, PSTR("500 SERVER ERROR\r\n"), 11);
log("500\n");
}
sock_write_P(sockfd, PSTR("Content-Type: text/html\r\n\r\n"), 27);
if (status == HTTPD_NOTFOUND) {
sock_write_P(sockfd, PSTR("Not Found"), 9);
} else {
sock_write_P(sockfd, PSTR("Server Error"), 12);
}
}
}
static uint8_t _httpd_read_method(uint8_t sock)
{
char buffer[2];
if (sock_read(sock, buffer, 2)) {
#ifdef DEBUG
uint8_t n;
for (n=0; n<4; n++) {
log_int(w5100_mem_read(W5100_SOCKET_REG+sock, W5100_Sn_DIPR0+n),1,10);
if (n<3) log_char('.');
}
#endif
if (!strncmp_P(buffer, PSTR("GE"), 2)) {
buffer[0] = HTTPD_GET;
buffer[1] = 3;
log(" GET\n");
} else if (!strncmp_P(buffer, PSTR("HE"), 2)) {
buffer[0] = HTTPD_HEAD;
buffer[1] = 4;
log(" HEAD\n");
} else if (!strncmp_P(buffer, PSTR("PO"), 2)) {
buffer[0] = HTTPD_POST;
buffer[1] = 4;
log(" POST\n");
} else {
buffer[0] = HTTPD_INVALID_METHOD;
buffer[1] = 0;
log(" ???\n");
}
sock_read(sock, NULL, buffer[1]);
return buffer[0];
}
return HTTPD_INVALID_METHOD;
}
uint8_t httpd_read_path(uint8_t sock, char *buffer)
{
uint8_t maxlen = HTTPD_FILENAME_MAX_LENGTH;
char c;
// read from stream until connection gone, buffer full, or end of token
while (sock_read(sock, &c, 1)
&& c != '/' && c != ' ' && c != '\n' && c != '\r'
&& c != '?' && c != '&' && c != '='
&& maxlen--) {
*buffer++ = c;
log_char(c);
}
*buffer = 0;
log("\n");
if (c == ' ' || c == '\n' || c == '\r') {
// no more tokens
return HTTPD_READ_DONE;
} else if (c =='/') {
// more tokens
return HTTPD_READ_PATH;
} else if (c == '?') {
// no more tokens, but query string exists
return HTTPD_READ_QUERY_STRING;
} else if (c == '&') {
// start of query string param
return HTTPD_READ_QUERY_PARAM;
} else if (c == '=') {
// query string value
return HTTPD_READ_QUERY_VALUE;
} else if (maxlen == 0) {
// url is too long, won't continue parsing
return HTTPD_READ_DONE;
} else {
// loop exitted for another reason, client gone?
return HTTPD_READ_DONE;
}
}
static void _httpd_write_content_type(uint8_t sock, char *filename)
{
PGM_P type = NULL;
sock_write_P(sock, PSTR("Content-Type: "), 14);
if (!*filename) {
// no filname in url, that mean a direcotry was openned with
// no file, and the deafult (empty) filename was picked up
// assumit it is .html
type = PSTR("text/html");
} else {
// FIXME, need to position after last '.'
// otherwise mmc fails to get mime type
// when . in path.
// position pointer at first char after .
while (*filename && *filename++ != '.');
if (strcmp_P(filename, PSTR("html")) == 0) {
type = PSTR("text/html");
} else if (strcmp_P(filename, PSTR("htm")) == 0) {
type = PSTR("text/html");
} else if (strcmp_P(filename, PSTR("txt")) == 0) {
type = PSTR("text/plain");
} else if (strcmp_P(filename, PSTR("jpg")) == 0) {
type = PSTR("image/jpeg");
} else if (strcmp_P(filename, PSTR("gif")) == 0) {
type = PSTR("image/gif");
} else if (strcmp_P(filename, PSTR("css")) == 0) {
type = PSTR("text/css");
} else if (strcmp_P(filename, PSTR("js")) == 0) {
type = PSTR("application/x-javascript");
} else {
type = PSTR("application/octet-stream");
}
}
sock_write_P(sock, type, strlen_P(type));
sock_write_P(sock, PSTR("\r\n"), 2);
}
#ifdef SUPPORT_MMC
// sock buffer is pointed to start of URI, attempt to open resource using FATFS
static void _httpd_handle_uri_mmc(uint8_t sock, uint8_t method, httpd_cgi_handler_t cgi_handler)
{
char buffer[256];
char c;
char *ptr = buffer;
uint8_t len = 255;
while (sock_read(sock, &c, 1)
&& c != ' ' && c != '\n' && c != '\r' && c != '?'
&& len--) {
*ptr++ = c;
}
*ptr = 0;
log_str(buffer);
log("\n");
if (pf_open(buffer) == FR_OK) {
// file was openned, write headers
httpd_write_response(sock, HTTPD_OK);
_httpd_write_content_type(sock, buffer);
sock_write_P(sock, PSTR("\r\n"), 2);
// if request wan't head, write file contents
if (method != HTTPD_HEAD) {
WORD offset = 0;
WORD read = 0;
do {
pf_read(buffer, 255, &read);
sock_write(sock, buffer, read);
offset += 255;
} while (read == 255);
}
} else {
httpd_write_response(sock, HTTPD_NOTFOUND);
}
}
#endif
// sock buffer is pointed to start of URI, attempt to open resource using PROGMEM
static void _httpd_handle_uri(uint8_t sock, uint8_t method, httpd_cgi_handler_t cgi_handler)
{
uint8_t status;
// open the root node of filesystem
romfs_node_t dirnode, filenode;
romfs_root(&dirnode);
char buffer[HTTPD_FILENAME_MAX_LENGTH + 3];
while ((status = httpd_read_path(sock, buffer))) {
if (status == HTTPD_READ_PATH) {
// special directory 'cgi-bin' delegates control to external function
if (strcmp_P(buffer, PSTR("cgi-bin")) == 0) {
(*cgi_handler)(sock, method, buffer);
return;
#ifdef SUPPORT_MMC
// speial link jumping uri to mmc
} else if (strcmp_P(buffer, PSTR("mmc")) == 0) {
_httpd_handle_uri_mmc(sock, method, cgi_handler);
return;
#endif
// try to open the path as a directory
} else if (romfs_open(&dirnode, &filenode, buffer)) {
if (!ROMFS_IS_DIRECTORY(filenode)) {
// the url requested a directory, but a file
// with that name exists, resource not found
break;
}
// use the new node as our directory
dirnode = filenode;
} else {
// diretory could not be openned
break;
}
} else if (status == HTTPD_READ_QUERY_STRING) {
// stop processing tokens at query string
status = HTTPD_READ_DONE;
break;
}
}
// status == 0, we stopped tokenizing the url and buffer
// contains a filename, try to open it
if (!status) {
// try to open file
status = romfs_open(&dirnode, &filenode, buffer);
if (status) {
// file was found, write headers
httpd_write_response(sock, HTTPD_OK);
_httpd_write_content_type(sock, buffer);
sock_write_P(sock, PSTR("\r\n"), 2);
} else {
// try to open the .gz version of the file
strcat_P(buffer, PSTR(".gz"));
log("Trying .gz\n");
status = romfs_open(&dirnode, &filenode, buffer);
buffer[strlen(buffer)-3] = 0x00;
if (status) {
// found a gzip'd version
httpd_write_response(sock, HTTPD_OK);
_httpd_write_content_type(sock, buffer);
sock_write_P(sock, PSTR("Content-Encoding: gzip\r\n\r\n"), 26);
} else {
// 404
httpd_write_response(sock, HTTPD_NOTFOUND);
}
}
if (status) {
// we were able to open the file, send it
// it not head, write the data
if (method != HTTPD_HEAD) {
uint16_t len = filenode.data_len;
PGM_VOID_P data = filenode.data;
// sock_write can only do 255 bytes at a time
while (len) {
if (len > 255) {
sock_write_P(sock, data, 255);
data += 255;
len -= 255;
} else {
sock_write_P(sock, data, len);
len = 0;
}
}
}
}
} else {
httpd_write_response(sock, HTTPD_NOTFOUND);
}
}
void httpd_loop(httpd_cgi_handler_t cgi_handler)
{
// wait for something to happen
uint8_t ready = sock_select(_fdset, 0x7F);
// new client is connecting
if (FD_ISSET(ready, _listen_sock)) {
uint8_t new_client = sock_accept(_listen_sock);
_clients[_connected_clients++] = new_client;
FD_SET(_fdset, new_client);
}
uint8_t n = 0;
for (n = 0; n < _connected_clients; n++) {
uint8_t fd = _clients[n];
// a connected client is readable
if (FD_ISSET(ready, fd)) {
uint8_t method = _httpd_read_method(fd);
if (method != HTTPD_INVALID_METHOD) {
// invoke the handler, fd's next read will read first char of url
_httpd_handle_uri(fd, method, cgi_handler);
} else {
httpd_write_response(fd, HTTPD_ERR);
}
// we always close connection
sock_close(fd);
FD_CLEAR(_fdset, fd);
_clients[n] = _clients[_connected_clients--];
n--;
}
}
}