source: uKadecot/trunk/uip/apps/webserver/httpd.c@ 154

Last change on this file since 154 was 154, checked in by coas-nagasima, 8 years ago

SDカードの中身を/~/でアクセスできるよう変更

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-chdr; charset=SHIFT_JIS
File size: 14.9 KB
Line 
1/**
2 * \addtogroup apps
3 * @{
4 */
5
6/**
7 * \defgroup httpd Web server
8 * @{
9 * The uIP web server is a very simplistic implementation of an HTTP
10 * server. It can serve web pages and files from a read-only ROM
11 * filesystem, and provides a very small scripting language.
12
13 */
14
15/**
16 * \file
17 * Web server
18 * \author
19 * Adam Dunkels <adam@sics.se>
20 */
21
22
23/*
24 * Copyright (c) 2004, Adam Dunkels.
25 * All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of the Institute nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 *
51 * This file is part of the uIP TCP/IP stack.
52 *
53 * Author: Adam Dunkels <adam@sics.se>
54 *
55 * $Id: httpd.c 154 2016-02-02 12:54:35Z coas-nagasima $
56 */
57
58#include "uip.h"
59#include "httpd.h"
60#include "httpd-fs.h"
61#include "http-strings.h"
62#include "base64.h"
63#include "sha1.h"
64#include "kadecot_names.h"
65#include "uip_adpt.h"
66
67#include <string.h>
68
69#define STATE_WAITING 0
70#define STATE_OUTPUT 1
71#define STATE_WS_OUTPUT 2
72
73#define ISO_nl 0x0a
74#define ISO_space 0x20
75#define ISO_bang 0x21
76#define ISO_percent 0x25
77#define ISO_period 0x2e
78#define ISO_slash 0x2f
79#define ISO_colon 0x3a
80
81#ifndef _MSC_VER
82#ifndef strcpy_s
83#define strcpy_s(s1, s1m, s2) strcpy(s1, s2)
84#endif
85
86#ifndef strncpy_s
87#define strncpy_s(dst, dsz, src, sz) strncpy(dst, src, sz)
88#endif
89#endif
90
91#define MAX(a, b) ((a > b) ? a : b)
92
93union temp_type_t {
94 char binary[sizeof(http_content_type_binary)];
95 char html[sizeof(http_content_type_html)];
96 char css[sizeof(http_content_type_css)];
97 char js[sizeof(http_content_type_js)];
98 char json[sizeof(http_content_type_json)];
99 char png[sizeof(http_content_type_png)];
100 char gif[sizeof(http_content_type_gif)];
101 char jpg[sizeof(http_content_type_jpg)];
102 char svg[sizeof(http_content_type_svg)];
103 char plain[sizeof(http_content_type_plain)];
104};
105
106struct temp_buf_t{
107 char headers[MAX(sizeof(http_header_404), sizeof(http_header_200))
108 + sizeof(http_content_encoding_gzip)
109 + sizeof(union temp_type_t)
110 ];
111 char ws_headers[sizeof(http_header_101)
112 + sizeof(http_upgrade) + sizeof(((struct httpd_state *)0)->message.upgrade) + sizeof(http_crnl)
113 + sizeof(http_connection) + sizeof(((struct httpd_state *)0)->message.connection) + sizeof(http_crnl)
114 + sizeof(http_sec_websocket_accept) + sizeof(((struct httpd_state *)0)->message.response_key) + sizeof(http_crnl)
115 + sizeof(http_sec_websocket_protocol) + sizeof(((struct httpd_state *)0)->message.sec_websocket_protocol) + sizeof(http_crnl)
116 + sizeof(http_crnl)
117 ];
118};
119static char temp_buf[sizeof(struct temp_buf_t)];
120
121int httpd_strnicmp(const char *s1, const char *s2, size_t n)
122{
123 int i;
124 char c1, c2;
125
126 for(i = 0; i < n; i++, s1++, s2++){
127 c1 = *s1;
128 c2 = *s2;
129 if(c1 == '\0' && c2 == '\0')
130 return 0;
131
132 if(c1 >= 'a' && c1 <= 'z')
133 c1 += 'A' - 'a';
134
135 if(c2 >= 'a' && c2 <= 'z')
136 c2 += 'A' - 'a';
137
138 if(c1 < c2)
139 return -1;
140
141 if(c1 > c2)
142 return 1;
143 }
144
145 return 0;
146}
147
148struct websocket *websocket_getws(ID wbsid)
149{
150 struct uip_conn *conn = uip_getconn(wbsid);
151 if (conn == NULL)
152 return NULL;
153
154 return &conn->appstate.websocket;
155}
156
157/*---------------------------------------------------------------------------*/
158static unsigned short
159generate_part_of_file(void *state)
160{
161 struct httpd_state *s = (struct httpd_state *)state;
162 int len;
163
164 if (s->file.len > uip_mss()) {
165 len = uip_mss();
166 } else {
167 len = s->file.len;
168 }
169 s->len = httpd_fs_read(&s->file, uip_appdata, len);
170
171 return s->len;
172}
173/*---------------------------------------------------------------------------*/
174static
175PT_THREAD(send_file(struct httpd_state *s))
176{
177 PSOCK_BEGIN(&s->sout);
178
179 do {
180 PSOCK_GENERATOR_SEND(&s->sout, generate_part_of_file, s);
181 s->file.len -= s->len;
182 s->file.pos += s->len;
183 } while (s->file.len > 0);
184
185 PSOCK_END(&s->sout);
186}
187/*---------------------------------------------------------------------------*/
188static
189PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
190{
191 char *pos = temp_buf;
192 int len;
193 char *ptr;
194
195 PSOCK_BEGIN(&s->sout);
196
197 len = strlen(statushdr);
198 memcpy(pos, statushdr, len); pos += len;
199
200 if (s->drv == 0) {
201 len = strlen(http_content_encoding_gzip);
202 memcpy(pos, http_content_encoding_gzip, len); pos += len;
203 }
204
205 ptr = strrchr(s->message.request_url, ISO_period);
206 if (ptr == NULL) {
207 len = strlen(http_content_type_binary);
208 memcpy(pos, http_content_type_binary, len); pos += len;
209 } else if (strncmp(http_html, ptr, 5) == 0 ||
210 strncmp(http_shtml, ptr, 6) == 0) {
211 len = strlen(http_content_type_html);
212 memcpy(pos, http_content_type_html, len); pos += len;
213 } else if (strncmp(http_css, ptr, 4) == 0) {
214 len = strlen(http_content_type_css);
215 memcpy(pos, http_content_type_css, len); pos += len;
216 } else if (strncmp(http_js, ptr, 3) == 0) {
217 len = strlen(http_content_type_js);
218 memcpy(pos, http_content_type_js, len); pos += len;
219 } else if (strncmp(http_json, ptr, 5) == 0) {
220 len = strlen(http_content_type_json);
221 memcpy(pos, http_content_type_json, len); pos += len;
222 } else if (strncmp(http_png, ptr, 4) == 0) {
223 len = strlen(http_content_type_png);
224 memcpy(pos, http_content_type_png, len); pos += len;
225 } else if (strncmp(http_gif, ptr, 4) == 0) {
226 len = strlen(http_content_type_gif);
227 memcpy(pos, http_content_type_gif, len); pos += len;
228 } else if (strncmp(http_jpg, ptr, 4) == 0) {
229 len = strlen(http_content_type_jpg);
230 memcpy(pos, http_content_type_jpg, len); pos += len;
231 } else if (strncmp(http_svg, ptr, 4) == 0) {
232 len = strlen(http_content_type_svg);
233 memcpy(pos, http_content_type_svg, len); pos += len;
234 } else {
235 len = strlen(http_content_type_plain);
236 memcpy(pos, http_content_type_plain, len); pos += len;
237 }
238 *pos = '\0';
239
240 PSOCK_SEND(&s->sout, temp_buf, pos - temp_buf);
241
242 PSOCK_END(&s->sout);
243}
244/*---------------------------------------------------------------------------*/
245static
246PT_THREAD(handle_output(struct httpd_state *s))
247{
248 PT_BEGIN(&s->outputpt);
249
250 s->file.fs = ((char *)&s[1]) - 64 - 512;
251
252 if (!httpd_fs_open(s->drv, s->filename, &s->file)) {
253 s->drv = 0;
254 httpd_fs_open(s->drv, http_404_html, &s->file);
255 strcpy_s(s->message.request_url, sizeof(s->message.request_url), http_404_html);
256 PT_WAIT_THREAD(&s->outputpt,
257 send_headers(s,
258 http_header_404));
259 PT_WAIT_THREAD(&s->outputpt, send_file(s));
260 } else {
261 PT_WAIT_THREAD(&s->outputpt,
262 send_headers(s,
263 http_header_200));
264 PT_WAIT_THREAD(&s->outputpt, send_file(s));
265 }
266
267 PSOCK_CLOSE(&s->sout);
268 PT_END(&s->outputpt);
269}
270/*---------------------------------------------------------------------------*/
271static
272PT_THREAD(send_ws_headers(struct httpd_state *s, const char *statushdr))
273{
274 char *pos = temp_buf;
275 int len;
276
277 PSOCK_BEGIN(&s->sout);
278
279 len = strlen(statushdr);
280 memcpy(pos, statushdr, len); pos += len;
281
282 len = strlen(http_upgrade);
283 memcpy(pos, http_upgrade, len); pos += len;
284 len = strlen(s->message.upgrade);
285 memcpy(pos, s->message.upgrade, len); pos += len;
286 len = strlen(http_crnl);
287 memcpy(pos, http_crnl, len); pos += len;
288
289 len = strlen(http_connection);
290 memcpy(pos, http_connection, len); pos += len;
291 len = strlen(s->message.connection);
292 memcpy(pos, s->message.connection, len); pos += len;
293 len = strlen(http_crnl);
294 memcpy(pos, http_crnl, len); pos += len;
295
296 len = strlen(http_sec_websocket_accept);
297 memcpy(pos, http_sec_websocket_accept, len); pos += len;
298 len = strlen(s->message.response_key);
299 memcpy(pos, s->message.response_key, len); pos += len;
300 len = strlen(http_crnl);
301 memcpy(pos, http_crnl, len); pos += len;
302
303 len = strlen(http_sec_websocket_protocol);
304 memcpy(pos, http_sec_websocket_protocol, len); pos += len;
305 len = strlen(s->message.sec_websocket_protocol);
306 memcpy(pos, s->message.sec_websocket_protocol, len); pos += len;
307 len = strlen(http_crnl);
308 memcpy(pos, http_crnl, len); pos += len;
309
310 len = strlen(http_crnl);
311 memcpy(pos, http_crnl, len); pos += len;
312 *pos = '\0';
313
314 PSOCK_SEND(&s->sout, temp_buf, pos - temp_buf);
315
316 PSOCK_END(&s->sout);
317}
318/*---------------------------------------------------------------------------*/
319static unsigned short
320generate_part_of_ws_data(void *state)
321{
322 struct httpd_state *s = (struct httpd_state *)state;
323
324 s->len = websocket_output(&s->websocket, uip_appdata, uip_mss());
325
326 return s->len;
327}
328/*---------------------------------------------------------------------------*/
329static
330PT_THREAD(send_ws_data(struct httpd_state *s))
331{
332 PSOCK_BEGIN(&s->sout);
333
334 PSOCK_GENERATOR_SEND(&s->sout, generate_part_of_ws_data, s);
335
336 PSOCK_END(&s->sout);
337}
338/*---------------------------------------------------------------------------*/
339static
340PT_THREAD(handle_ws_output(struct httpd_state *s))
341{
342 char shaHash[20];
343 SHA_CTX sha1;
344 int len;
345
346 PT_BEGIN(&s->outputpt);
347
348 strlncat(s->message.response_key, sizeof(s->message.response_key),
349 s->message.sec_websocket_key, sizeof(s->message.sec_websocket_key));
350 len = strlncat(s->message.response_key, sizeof(s->message.response_key),
351 http_websocket_guid, sizeof(http_websocket_guid));
352 memset(shaHash, 0, sizeof(shaHash));
353 SHA1_Init(&sha1);
354 SHA1_Update(&sha1, (sha1_byte *)s->message.response_key, len);
355 SHA1_Final((sha1_byte *)shaHash, &sha1);
356 base64_encode((unsigned char *)s->message.response_key,
357 sizeof(s->message.response_key), (unsigned char *)shaHash, sizeof(shaHash));
358
359 PT_WAIT_THREAD(&s->outputpt, send_ws_headers(s, http_header_101));
360
361 s->message.response_key[0] = '\0';
362
363 do {
364 while(!websocket_newdata(&s->websocket))
365 PT_YIELD(&s->outputpt);
366
367 PT_WAIT_THREAD(&s->outputpt, send_ws_data(s));
368 } while ((s->state == STATE_WS_OUTPUT) && (!s->close_req));
369 s->state = STATE_WAITING;
370 websocket_destroy(&s->websocket);
371 s->close_req = 0;
372
373 PSOCK_CLOSE(&s->sout);
374
375 PT_END(&s->outputpt);
376}
377/*---------------------------------------------------------------------------*/
378static
379PT_THREAD(handle_input(struct httpd_state *s))
380{
381 size_t done;
382 const char *data;
383 PSOCK_BEGIN(&s->sin);
384
385 memset(&s->message, 0, sizeof(s->message));
386 http_parser_init(&s->parser, HTTP_REQUEST);
387
388 for (;;) {
389 PSOCK_WAIT_UNTIL(&s->sin, psock_newdata(&s->sin));
390
391 s->parse_pos = 0;
392 s->parse_len = uip_datalen();
393 data = &((const char *)uip_appdata)[s->parse_pos];
394 done = http_parser_execute(&s->parser, &websvr_settings, data, s->parse_len);
395 if (done != 0) {
396 s->parse_pos += done;
397 s->parse_len -= done;
398 break;
399 }
400 }
401
402 if (!s->message.message_complete_cb_called
403 && s->message.method != HTTP_GET) {
404 PSOCK_CLOSE_EXIT(&s->sin);
405 }
406
407 /* ""か"/"なら"index.html"に変更 */
408 if ((s->message.request_url[0] == '\0') || ((s->message.request_url[0] == '/') && (s->message.request_url[1] == '\0'))) {
409 strncpy_s(s->message.request_url, sizeof(s->message.request_url), http_index_html, sizeof(s->message.request_url));
410 s->filename = s->message.request_url;
411 }
412 /* "/~/"ならSDカードから読み込み */
413 else if ((s->message.request_url[0] == '/') && (s->message.request_url[1] == '~') && (s->message.request_url[2] == '/')) {
414 s->drv = 1;
415 s->filename = &s->message.request_url[2];
416 }
417 else {
418 s->drv = 0;
419 s->filename = s->message.request_url;
420 }
421
422 /* httpd_log_file(uip_conn->ripaddr, s->message.request_url);*/
423 /* httpd_log(s->message.referer);*/
424
425 if (httpd_strnicmp(http_websocket, s->message.upgrade, sizeof(s->message.upgrade)) == 0) {
426 s->state = STATE_WS_OUTPUT;
427
428 websocket_init(&s->websocket, uip_getid((struct uip_conn *)((intptr_t)s - offsetof(struct uip_conn, appstate))));
429 for (;;) {
430 if(s->parse_len <= 0){
431 PT_YIELD(&s->sin.pt);
432 PSOCK_WAIT_UNTIL(&s->sin, psock_newdata(&s->sin));
433
434 s->parse_pos = 0;
435 s->parse_len = uip_datalen();
436 }
437 data = &((const char *)uip_appdata)[s->parse_pos];
438 done = websocket_input(&s->websocket, (void *)data, s->parse_len);
439 if ((done != 0) || (s->websocket.rstate.opecode == connection_close)) {
440 s->close_req = 1;
441 PSOCK_CLOSE_EXIT(&s->sin);
442 break;
443 }
444 s->parse_pos = s->parse_len;
445 s->parse_len = 0;
446 }
447 }
448 else {
449 s->state = STATE_OUTPUT;
450
451 while (1) {
452 PSOCK_READTO(&s->sin, ISO_nl);
453 }
454 }
455
456 PSOCK_END(&s->sin);
457}
458/*---------------------------------------------------------------------------*/
459static void
460handle_connection(struct httpd_state *s)
461{
462 handle_input(s);
463 switch (s->state) {
464 case STATE_OUTPUT:
465 handle_output(s);
466 break;
467 case STATE_WS_OUTPUT:
468 handle_ws_output(s);
469 break;
470 }
471}
472/*---------------------------------------------------------------------------*/
473void
474httpd_appcall(void)
475{
476 struct httpd_state *s = (struct httpd_state *)&(uip_conn->appstate);
477
478 if (uip_closed() || uip_aborted() || uip_timedout()) {
479 } else if (uip_connected()) {
480 if(s->state != STATE_WS_OUTPUT){
481 PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
482 PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
483 PT_INIT(&s->outputpt);
484 s->state = STATE_WAITING;
485 /* timer_set(&s->timer, CLOCK_SECOND * 100);*/
486 s->timer = 0;
487 handle_connection(s);
488 }
489 else {
490 handle_connection(s);
491 }
492 } else if (s != NULL) {
493 if(s->state != STATE_WS_OUTPUT){
494 if (uip_poll()) {
495 ++s->timer;
496 if (s->timer >= 20) {
497 uip_abort();
498 }
499 } else {
500 s->timer = 0;
501 }
502 }
503 handle_connection(s);
504 } else {
505 uip_abort();
506 }
507}
508/*---------------------------------------------------------------------------*/
509/**
510 * \brief Initialize the web server
511 *
512 * This function initializes the web server and should be
513 * called at system boot-up.
514 */
515void
516httpd_init(void)
517{
518 uip_listen(HTONS(41314));
519
520 httpd_fs_init();
521 kadecot_names_init();
522}
523/*---------------------------------------------------------------------------*/
524/** @} */
Note: See TracBrowser for help on using the repository browser.