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

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

URLがフォルダを指している場合、/index.htmlにリダイレクトするよう変更。

  • 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: 15.3 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 155 2016-02-04 14:11:09Z 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 if (s->file.redirect) {
206 len = strlen(http_location);
207 memcpy(pos, http_location, len); pos += len;
208 if (s->drv == 1) {
209 len = 2;
210 memcpy(pos, "/~", len); pos += len;
211 }
212 len = strlen(s->filename);
213 memcpy(pos, s->filename, len); pos += len;
214 len = 2;
215 memcpy(pos, "\r\n", len); pos += len;
216 }
217
218 ptr = strrchr(s->message.request_url, ISO_period);
219 if (ptr == NULL) {
220 len = strlen(http_content_type_binary);
221 memcpy(pos, http_content_type_binary, len); pos += len;
222 } else if (strncmp(http_html, ptr, 5) == 0 ||
223 strncmp(http_shtml, ptr, 6) == 0) {
224 len = strlen(http_content_type_html);
225 memcpy(pos, http_content_type_html, len); pos += len;
226 } else if (strncmp(http_css, ptr, 4) == 0) {
227 len = strlen(http_content_type_css);
228 memcpy(pos, http_content_type_css, len); pos += len;
229 } else if (strncmp(http_js, ptr, 3) == 0) {
230 len = strlen(http_content_type_js);
231 memcpy(pos, http_content_type_js, len); pos += len;
232 } else if (strncmp(http_json, ptr, 5) == 0) {
233 len = strlen(http_content_type_json);
234 memcpy(pos, http_content_type_json, len); pos += len;
235 } else if (strncmp(http_png, ptr, 4) == 0) {
236 len = strlen(http_content_type_png);
237 memcpy(pos, http_content_type_png, len); pos += len;
238 } else if (strncmp(http_gif, ptr, 4) == 0) {
239 len = strlen(http_content_type_gif);
240 memcpy(pos, http_content_type_gif, len); pos += len;
241 } else if (strncmp(http_jpg, ptr, 4) == 0) {
242 len = strlen(http_content_type_jpg);
243 memcpy(pos, http_content_type_jpg, len); pos += len;
244 } else if (strncmp(http_svg, ptr, 4) == 0) {
245 len = strlen(http_content_type_svg);
246 memcpy(pos, http_content_type_svg, len); pos += len;
247 } else {
248 len = strlen(http_content_type_plain);
249 memcpy(pos, http_content_type_plain, len); pos += len;
250 }
251 *pos = '\0';
252
253 PSOCK_SEND(&s->sout, temp_buf, pos - temp_buf);
254
255 PSOCK_END(&s->sout);
256}
257/*---------------------------------------------------------------------------*/
258static
259PT_THREAD(handle_output(struct httpd_state *s))
260{
261 PT_BEGIN(&s->outputpt);
262
263 if (!httpd_fs_open(s->drv, s->filename, sizeof(s->filename), &s->file)) {
264 s->drv = 0;
265 strcpy_s(s->filename, sizeof(s->filename), http_404_html);
266 httpd_fs_open(s->drv, s->filename, sizeof(s->filename), &s->file);
267 PT_WAIT_THREAD(&s->outputpt,
268 send_headers(s,
269 http_header_404));
270 PT_WAIT_THREAD(&s->outputpt, send_file(s));
271 } else {
272 PT_WAIT_THREAD(&s->outputpt,
273 send_headers(s,
274 s->file.redirect ? http_header_301 : http_header_200));
275 PT_WAIT_THREAD(&s->outputpt, send_file(s));
276 }
277
278 PSOCK_CLOSE(&s->sout);
279 PT_END(&s->outputpt);
280}
281/*---------------------------------------------------------------------------*/
282static
283PT_THREAD(send_ws_headers(struct httpd_state *s, const char *statushdr))
284{
285 char *pos = temp_buf;
286 int len;
287
288 PSOCK_BEGIN(&s->sout);
289
290 len = strlen(statushdr);
291 memcpy(pos, statushdr, len); pos += len;
292
293 len = strlen(http_upgrade);
294 memcpy(pos, http_upgrade, len); pos += len;
295 len = strlen(s->message.upgrade);
296 memcpy(pos, s->message.upgrade, len); pos += len;
297 len = strlen(http_crnl);
298 memcpy(pos, http_crnl, len); pos += len;
299
300 len = strlen(http_connection);
301 memcpy(pos, http_connection, len); pos += len;
302 len = strlen(s->message.connection);
303 memcpy(pos, s->message.connection, len); pos += len;
304 len = strlen(http_crnl);
305 memcpy(pos, http_crnl, len); pos += len;
306
307 len = strlen(http_sec_websocket_accept);
308 memcpy(pos, http_sec_websocket_accept, len); pos += len;
309 len = strlen(s->message.response_key);
310 memcpy(pos, s->message.response_key, len); pos += len;
311 len = strlen(http_crnl);
312 memcpy(pos, http_crnl, len); pos += len;
313
314 len = strlen(http_sec_websocket_protocol);
315 memcpy(pos, http_sec_websocket_protocol, len); pos += len;
316 len = strlen(s->message.sec_websocket_protocol);
317 memcpy(pos, s->message.sec_websocket_protocol, len); pos += len;
318 len = strlen(http_crnl);
319 memcpy(pos, http_crnl, len); pos += len;
320
321 len = strlen(http_crnl);
322 memcpy(pos, http_crnl, len); pos += len;
323 *pos = '\0';
324
325 PSOCK_SEND(&s->sout, temp_buf, pos - temp_buf);
326
327 PSOCK_END(&s->sout);
328}
329/*---------------------------------------------------------------------------*/
330static unsigned short
331generate_part_of_ws_data(void *state)
332{
333 struct httpd_state *s = (struct httpd_state *)state;
334
335 s->len = websocket_output(&s->websocket, uip_appdata, uip_mss());
336
337 return s->len;
338}
339/*---------------------------------------------------------------------------*/
340static
341PT_THREAD(send_ws_data(struct httpd_state *s))
342{
343 PSOCK_BEGIN(&s->sout);
344
345 PSOCK_GENERATOR_SEND(&s->sout, generate_part_of_ws_data, s);
346
347 PSOCK_END(&s->sout);
348}
349/*---------------------------------------------------------------------------*/
350static
351PT_THREAD(handle_ws_output(struct httpd_state *s))
352{
353 char shaHash[20];
354 SHA_CTX sha1;
355 int len;
356
357 PT_BEGIN(&s->outputpt);
358
359 strlncat(s->message.response_key, sizeof(s->message.response_key),
360 s->message.sec_websocket_key, sizeof(s->message.sec_websocket_key));
361 len = strlncat(s->message.response_key, sizeof(s->message.response_key),
362 http_websocket_guid, sizeof(http_websocket_guid));
363 memset(shaHash, 0, sizeof(shaHash));
364 SHA1_Init(&sha1);
365 SHA1_Update(&sha1, (sha1_byte *)s->message.response_key, len);
366 SHA1_Final((sha1_byte *)shaHash, &sha1);
367 base64_encode((unsigned char *)s->message.response_key,
368 sizeof(s->message.response_key), (unsigned char *)shaHash, sizeof(shaHash));
369
370 PT_WAIT_THREAD(&s->outputpt, send_ws_headers(s, http_header_101));
371
372 s->message.response_key[0] = '\0';
373
374 do {
375 while(!websocket_newdata(&s->websocket))
376 PT_YIELD(&s->outputpt);
377
378 PT_WAIT_THREAD(&s->outputpt, send_ws_data(s));
379 } while ((s->state == STATE_WS_OUTPUT) && (!s->close_req));
380 s->state = STATE_WAITING;
381 websocket_destroy(&s->websocket);
382 s->close_req = 0;
383
384 PSOCK_CLOSE(&s->sout);
385
386 PT_END(&s->outputpt);
387}
388/*---------------------------------------------------------------------------*/
389static
390PT_THREAD(handle_input(struct httpd_state *s))
391{
392 size_t done;
393 const char *data;
394 char *ptr;
395
396 PSOCK_BEGIN(&s->sin);
397
398 memset(&s->message, 0, sizeof(s->message));
399 http_parser_init(&s->parser, HTTP_REQUEST);
400
401 for (;;) {
402 PSOCK_WAIT_UNTIL(&s->sin, psock_newdata(&s->sin));
403
404 s->parse_pos = 0;
405 s->parse_len = uip_datalen();
406 data = &((const char *)uip_appdata)[s->parse_pos];
407 done = http_parser_execute(&s->parser, &websvr_settings, data, s->parse_len);
408 if (done != 0) {
409 s->parse_pos += done;
410 s->parse_len -= done;
411 break;
412 }
413 }
414
415 if (!s->message.message_complete_cb_called
416 && s->message.method != HTTP_GET) {
417 PSOCK_CLOSE_EXIT(&s->sin);
418 }
419
420 /* ""か"/"なら"index.html"に変更 */
421 if ((s->message.request_url[0] == '\0') || ((s->message.request_url[0] == '/') && (s->message.request_url[1] == '\0'))) {
422 s->drv = 0;
423 strcpy_s(s->filename, sizeof(s->filename), http_index_html);
424 }
425 /* "/~/"ならSDカードから読み込み */
426 else if ((s->message.request_url[0] == '/') && (s->message.request_url[1] == '~') && (s->message.request_url[2] == '/')) {
427 s->drv = 1;
428 strcpy_s(s->filename, sizeof(s->filename), &s->message.request_url[2]);
429 }
430 else {
431 s->drv = 0;
432 strcpy_s(s->filename, sizeof(s->filename), s->message.request_url);
433 }
434
435 ptr = strrchr(s->filename, '?');
436 if (ptr != NULL)
437 ptr[0] = '\0';
438
439 /* httpd_log_file(uip_conn->ripaddr, s->message.request_url);*/
440 /* httpd_log(s->message.referer);*/
441
442 if (httpd_strnicmp(http_websocket, s->message.upgrade, sizeof(s->message.upgrade)) == 0) {
443 s->state = STATE_WS_OUTPUT;
444
445 s->close_req = 0;
446 websocket_init(&s->websocket, uip_getid((struct uip_conn *)((intptr_t)s - offsetof(struct uip_conn, appstate))));
447 for (;;) {
448 if(s->parse_len <= 0){
449 PT_YIELD(&s->sin.pt);
450 PSOCK_WAIT_UNTIL(&s->sin, psock_newdata(&s->sin));
451
452 s->parse_pos = 0;
453 s->parse_len = uip_datalen();
454 }
455 data = &((const char *)uip_appdata)[s->parse_pos];
456 done = websocket_input(&s->websocket, (void *)data, s->parse_len);
457 if ((done != 0) || (s->websocket.rstate.opecode == connection_close)) {
458 s->close_req = 1;
459 PSOCK_CLOSE_EXIT(&s->sin);
460 break;
461 }
462 s->parse_pos = s->parse_len;
463 s->parse_len = 0;
464 }
465 }
466 else {
467 s->state = STATE_OUTPUT;
468
469 while (1) {
470 PSOCK_READTO(&s->sin, ISO_nl);
471 }
472 }
473
474 PSOCK_END(&s->sin);
475}
476/*---------------------------------------------------------------------------*/
477static void
478handle_connection(struct httpd_state *s)
479{
480 handle_input(s);
481 switch (s->state) {
482 case STATE_OUTPUT:
483 handle_output(s);
484 break;
485 case STATE_WS_OUTPUT:
486 handle_ws_output(s);
487 break;
488 }
489}
490/*---------------------------------------------------------------------------*/
491void
492httpd_appcall(void)
493{
494 struct httpd_state *s = (struct httpd_state *)&(uip_conn->appstate);
495
496 if (uip_closed() || uip_aborted() || uip_timedout()) {
497 } else if (uip_connected()) {
498 if(s->state != STATE_WS_OUTPUT){
499 PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
500 PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
501 PT_INIT(&s->outputpt);
502 s->state = STATE_WAITING;
503 /* timer_set(&s->timer, CLOCK_SECOND * 100);*/
504 s->timer = 0;
505 handle_connection(s);
506 }
507 else {
508 handle_connection(s);
509 }
510 } else if (s != NULL) {
511 if(s->state != STATE_WS_OUTPUT){
512 if (uip_poll()) {
513 ++s->timer;
514 if (s->timer >= 20) {
515 uip_abort();
516 }
517 } else {
518 s->timer = 0;
519 }
520 }
521 handle_connection(s);
522 } else {
523 uip_abort();
524 }
525}
526/*---------------------------------------------------------------------------*/
527/**
528 * \brief Initialize the web server
529 *
530 * This function initializes the web server and should be
531 * called at system boot-up.
532 */
533void
534httpd_init(void)
535{
536 uip_listen(HTONS(41314));
537
538 httpd_fs_init();
539 kadecot_names_init();
540}
541/*---------------------------------------------------------------------------*/
542/** @} */
Note: See TracBrowser for help on using the repository browser.