ban4jp -
/
uIP-1-0_webserver
uIP 1.0 based webserver for LPC1114 + ENC28J60
apps/webserver/httpd.cpp@2:4da9ed411bdc, 2014-06-21 (annotated)
- Committer:
- ban4jp
- Date:
- Sat Jun 21 11:54:24 2014 +0000
- Revision:
- 2:4da9ed411bdc
- Parent:
- apps/webserver/httpd.c@0:685224d2f66d
- Child:
- 3:a2715e9c7737
"/app/webserver" convert *.c to *.cpp.; added TMP102 Temperature sensor page.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ban4jp | 0:685224d2f66d | 1 | /** |
ban4jp | 0:685224d2f66d | 2 | * \addtogroup apps |
ban4jp | 0:685224d2f66d | 3 | * @{ |
ban4jp | 0:685224d2f66d | 4 | */ |
ban4jp | 0:685224d2f66d | 5 | |
ban4jp | 0:685224d2f66d | 6 | /** |
ban4jp | 0:685224d2f66d | 7 | * \defgroup httpd Web server |
ban4jp | 0:685224d2f66d | 8 | * @{ |
ban4jp | 0:685224d2f66d | 9 | * The uIP web server is a very simplistic implementation of an HTTP |
ban4jp | 0:685224d2f66d | 10 | * server. It can serve web pages and files from a read-only ROM |
ban4jp | 0:685224d2f66d | 11 | * filesystem, and provides a very small scripting language. |
ban4jp | 0:685224d2f66d | 12 | |
ban4jp | 0:685224d2f66d | 13 | */ |
ban4jp | 0:685224d2f66d | 14 | |
ban4jp | 0:685224d2f66d | 15 | /** |
ban4jp | 0:685224d2f66d | 16 | * \file |
ban4jp | 0:685224d2f66d | 17 | * Web server |
ban4jp | 0:685224d2f66d | 18 | * \author |
ban4jp | 0:685224d2f66d | 19 | * Adam Dunkels <adam@sics.se> |
ban4jp | 0:685224d2f66d | 20 | */ |
ban4jp | 0:685224d2f66d | 21 | |
ban4jp | 0:685224d2f66d | 22 | |
ban4jp | 0:685224d2f66d | 23 | /* |
ban4jp | 0:685224d2f66d | 24 | * Copyright (c) 2004, Adam Dunkels. |
ban4jp | 0:685224d2f66d | 25 | * All rights reserved. |
ban4jp | 0:685224d2f66d | 26 | * |
ban4jp | 0:685224d2f66d | 27 | * Redistribution and use in source and binary forms, with or without |
ban4jp | 0:685224d2f66d | 28 | * modification, are permitted provided that the following conditions |
ban4jp | 0:685224d2f66d | 29 | * are met: |
ban4jp | 0:685224d2f66d | 30 | * 1. Redistributions of source code must retain the above copyright |
ban4jp | 0:685224d2f66d | 31 | * notice, this list of conditions and the following disclaimer. |
ban4jp | 0:685224d2f66d | 32 | * 2. Redistributions in binary form must reproduce the above copyright |
ban4jp | 0:685224d2f66d | 33 | * notice, this list of conditions and the following disclaimer in the |
ban4jp | 0:685224d2f66d | 34 | * documentation and/or other materials provided with the distribution. |
ban4jp | 0:685224d2f66d | 35 | * 3. Neither the name of the Institute nor the names of its contributors |
ban4jp | 0:685224d2f66d | 36 | * may be used to endorse or promote products derived from this software |
ban4jp | 0:685224d2f66d | 37 | * without specific prior written permission. |
ban4jp | 0:685224d2f66d | 38 | * |
ban4jp | 0:685224d2f66d | 39 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
ban4jp | 0:685224d2f66d | 40 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
ban4jp | 0:685224d2f66d | 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
ban4jp | 0:685224d2f66d | 42 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
ban4jp | 0:685224d2f66d | 43 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
ban4jp | 0:685224d2f66d | 44 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
ban4jp | 0:685224d2f66d | 45 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ban4jp | 0:685224d2f66d | 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
ban4jp | 0:685224d2f66d | 47 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
ban4jp | 0:685224d2f66d | 48 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
ban4jp | 0:685224d2f66d | 49 | * SUCH DAMAGE. |
ban4jp | 0:685224d2f66d | 50 | * |
ban4jp | 0:685224d2f66d | 51 | * This file is part of the uIP TCP/IP stack. |
ban4jp | 0:685224d2f66d | 52 | * |
ban4jp | 0:685224d2f66d | 53 | * Author: Adam Dunkels <adam@sics.se> |
ban4jp | 0:685224d2f66d | 54 | * |
ban4jp | 0:685224d2f66d | 55 | * $Id: httpd.c,v 1.2 2006/06/11 21:46:38 adam Exp $ |
ban4jp | 0:685224d2f66d | 56 | */ |
ban4jp | 0:685224d2f66d | 57 | |
ban4jp | 2:4da9ed411bdc | 58 | extern "C" { |
ban4jp | 2:4da9ed411bdc | 59 | #include "uip.h" |
ban4jp | 2:4da9ed411bdc | 60 | #include "httpd.h" |
ban4jp | 2:4da9ed411bdc | 61 | } |
ban4jp | 2:4da9ed411bdc | 62 | //#include "httpd-fs.h" |
ban4jp | 0:685224d2f66d | 63 | #include "httpd-cgi.h" |
ban4jp | 0:685224d2f66d | 64 | #include "http-strings.h" |
ban4jp | 0:685224d2f66d | 65 | |
ban4jp | 0:685224d2f66d | 66 | #include <string.h> |
ban4jp | 0:685224d2f66d | 67 | |
ban4jp | 0:685224d2f66d | 68 | #define STATE_WAITING 0 |
ban4jp | 0:685224d2f66d | 69 | #define STATE_OUTPUT 1 |
ban4jp | 0:685224d2f66d | 70 | |
ban4jp | 0:685224d2f66d | 71 | #define ISO_nl 0x0a |
ban4jp | 0:685224d2f66d | 72 | #define ISO_space 0x20 |
ban4jp | 0:685224d2f66d | 73 | #define ISO_bang 0x21 |
ban4jp | 0:685224d2f66d | 74 | #define ISO_percent 0x25 |
ban4jp | 0:685224d2f66d | 75 | #define ISO_period 0x2e |
ban4jp | 0:685224d2f66d | 76 | #define ISO_slash 0x2f |
ban4jp | 0:685224d2f66d | 77 | #define ISO_colon 0x3a |
ban4jp | 0:685224d2f66d | 78 | |
ban4jp | 0:685224d2f66d | 79 | |
ban4jp | 0:685224d2f66d | 80 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 81 | static unsigned short |
ban4jp | 0:685224d2f66d | 82 | generate_part_of_file(void *state) |
ban4jp | 0:685224d2f66d | 83 | { |
ban4jp | 0:685224d2f66d | 84 | struct httpd_state *s = (struct httpd_state *)state; |
ban4jp | 0:685224d2f66d | 85 | |
ban4jp | 0:685224d2f66d | 86 | if(s->file.len > uip_mss()) { |
ban4jp | 0:685224d2f66d | 87 | s->len = uip_mss(); |
ban4jp | 0:685224d2f66d | 88 | } else { |
ban4jp | 0:685224d2f66d | 89 | s->len = s->file.len; |
ban4jp | 0:685224d2f66d | 90 | } |
ban4jp | 0:685224d2f66d | 91 | memcpy(uip_appdata, s->file.data, s->len); |
ban4jp | 0:685224d2f66d | 92 | |
ban4jp | 0:685224d2f66d | 93 | return s->len; |
ban4jp | 0:685224d2f66d | 94 | } |
ban4jp | 0:685224d2f66d | 95 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 96 | static |
ban4jp | 0:685224d2f66d | 97 | PT_THREAD(send_file(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 98 | { |
ban4jp | 0:685224d2f66d | 99 | PSOCK_BEGIN(&s->sout); |
ban4jp | 0:685224d2f66d | 100 | |
ban4jp | 0:685224d2f66d | 101 | do { |
ban4jp | 0:685224d2f66d | 102 | PSOCK_GENERATOR_SEND(&s->sout, generate_part_of_file, s); |
ban4jp | 0:685224d2f66d | 103 | s->file.len -= s->len; |
ban4jp | 0:685224d2f66d | 104 | s->file.data += s->len; |
ban4jp | 0:685224d2f66d | 105 | } while(s->file.len > 0); |
ban4jp | 0:685224d2f66d | 106 | |
ban4jp | 0:685224d2f66d | 107 | PSOCK_END(&s->sout); |
ban4jp | 0:685224d2f66d | 108 | } |
ban4jp | 0:685224d2f66d | 109 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 110 | static |
ban4jp | 0:685224d2f66d | 111 | PT_THREAD(send_part_of_file(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 112 | { |
ban4jp | 0:685224d2f66d | 113 | PSOCK_BEGIN(&s->sout); |
ban4jp | 0:685224d2f66d | 114 | |
ban4jp | 0:685224d2f66d | 115 | PSOCK_SEND(&s->sout, s->file.data, s->len); |
ban4jp | 0:685224d2f66d | 116 | |
ban4jp | 0:685224d2f66d | 117 | PSOCK_END(&s->sout); |
ban4jp | 0:685224d2f66d | 118 | } |
ban4jp | 0:685224d2f66d | 119 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 120 | static void |
ban4jp | 0:685224d2f66d | 121 | next_scriptstate(struct httpd_state *s) |
ban4jp | 0:685224d2f66d | 122 | { |
ban4jp | 0:685224d2f66d | 123 | char *p; |
ban4jp | 0:685224d2f66d | 124 | p = strchr(s->scriptptr, ISO_nl) + 1; |
ban4jp | 0:685224d2f66d | 125 | s->scriptlen -= (unsigned short)(p - s->scriptptr); |
ban4jp | 0:685224d2f66d | 126 | s->scriptptr = p; |
ban4jp | 0:685224d2f66d | 127 | } |
ban4jp | 0:685224d2f66d | 128 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 129 | static |
ban4jp | 0:685224d2f66d | 130 | PT_THREAD(handle_script(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 131 | { |
ban4jp | 0:685224d2f66d | 132 | char *ptr; |
ban4jp | 0:685224d2f66d | 133 | |
ban4jp | 0:685224d2f66d | 134 | PT_BEGIN(&s->scriptpt); |
ban4jp | 0:685224d2f66d | 135 | |
ban4jp | 0:685224d2f66d | 136 | |
ban4jp | 0:685224d2f66d | 137 | while(s->file.len > 0) { |
ban4jp | 0:685224d2f66d | 138 | |
ban4jp | 0:685224d2f66d | 139 | /* Check if we should start executing a script. */ |
ban4jp | 0:685224d2f66d | 140 | if(*s->file.data == ISO_percent && |
ban4jp | 0:685224d2f66d | 141 | *(s->file.data + 1) == ISO_bang) { |
ban4jp | 0:685224d2f66d | 142 | s->scriptptr = s->file.data + 3; |
ban4jp | 0:685224d2f66d | 143 | s->scriptlen = s->file.len - 3; |
ban4jp | 0:685224d2f66d | 144 | if(*(s->scriptptr - 1) == ISO_colon) { |
ban4jp | 2:4da9ed411bdc | 145 | httpd_fs_open(s->scriptptr + 1, &s->file); |
ban4jp | 2:4da9ed411bdc | 146 | PT_WAIT_THREAD(&s->scriptpt, send_file(s)); |
ban4jp | 0:685224d2f66d | 147 | } else { |
ban4jp | 2:4da9ed411bdc | 148 | PT_WAIT_THREAD(&s->scriptpt, |
ban4jp | 2:4da9ed411bdc | 149 | httpd_cgi(s->scriptptr)(s, s->scriptptr)); |
ban4jp | 0:685224d2f66d | 150 | } |
ban4jp | 0:685224d2f66d | 151 | next_scriptstate(s); |
ban4jp | 0:685224d2f66d | 152 | |
ban4jp | 0:685224d2f66d | 153 | /* The script is over, so we reset the pointers and continue |
ban4jp | 2:4da9ed411bdc | 154 | sending the rest of the file. */ |
ban4jp | 0:685224d2f66d | 155 | s->file.data = s->scriptptr; |
ban4jp | 0:685224d2f66d | 156 | s->file.len = s->scriptlen; |
ban4jp | 0:685224d2f66d | 157 | } else { |
ban4jp | 0:685224d2f66d | 158 | /* See if we find the start of script marker in the block of HTML |
ban4jp | 2:4da9ed411bdc | 159 | to be sent. */ |
ban4jp | 0:685224d2f66d | 160 | |
ban4jp | 0:685224d2f66d | 161 | if(s->file.len > uip_mss()) { |
ban4jp | 2:4da9ed411bdc | 162 | s->len = uip_mss(); |
ban4jp | 0:685224d2f66d | 163 | } else { |
ban4jp | 2:4da9ed411bdc | 164 | s->len = s->file.len; |
ban4jp | 0:685224d2f66d | 165 | } |
ban4jp | 0:685224d2f66d | 166 | |
ban4jp | 0:685224d2f66d | 167 | if(*s->file.data == ISO_percent) { |
ban4jp | 2:4da9ed411bdc | 168 | ptr = strchr(s->file.data + 1, ISO_percent); |
ban4jp | 0:685224d2f66d | 169 | } else { |
ban4jp | 2:4da9ed411bdc | 170 | ptr = strchr(s->file.data, ISO_percent); |
ban4jp | 0:685224d2f66d | 171 | } |
ban4jp | 0:685224d2f66d | 172 | if(ptr != NULL && |
ban4jp | 2:4da9ed411bdc | 173 | ptr != s->file.data) { |
ban4jp | 2:4da9ed411bdc | 174 | s->len = (int)(ptr - s->file.data); |
ban4jp | 2:4da9ed411bdc | 175 | if(s->len >= uip_mss()) { |
ban4jp | 2:4da9ed411bdc | 176 | s->len = uip_mss(); |
ban4jp | 2:4da9ed411bdc | 177 | } |
ban4jp | 0:685224d2f66d | 178 | } |
ban4jp | 0:685224d2f66d | 179 | PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s)); |
ban4jp | 0:685224d2f66d | 180 | s->file.data += s->len; |
ban4jp | 0:685224d2f66d | 181 | s->file.len -= s->len; |
ban4jp | 0:685224d2f66d | 182 | |
ban4jp | 0:685224d2f66d | 183 | } |
ban4jp | 0:685224d2f66d | 184 | } |
ban4jp | 0:685224d2f66d | 185 | |
ban4jp | 0:685224d2f66d | 186 | PT_END(&s->scriptpt); |
ban4jp | 0:685224d2f66d | 187 | } |
ban4jp | 0:685224d2f66d | 188 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 189 | static |
ban4jp | 0:685224d2f66d | 190 | PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) |
ban4jp | 0:685224d2f66d | 191 | { |
ban4jp | 0:685224d2f66d | 192 | char *ptr; |
ban4jp | 0:685224d2f66d | 193 | |
ban4jp | 0:685224d2f66d | 194 | PSOCK_BEGIN(&s->sout); |
ban4jp | 0:685224d2f66d | 195 | |
ban4jp | 0:685224d2f66d | 196 | PSOCK_SEND_STR(&s->sout, statushdr); |
ban4jp | 0:685224d2f66d | 197 | |
ban4jp | 0:685224d2f66d | 198 | ptr = strrchr(s->filename, ISO_period); |
ban4jp | 0:685224d2f66d | 199 | if(ptr == NULL) { |
ban4jp | 0:685224d2f66d | 200 | PSOCK_SEND_STR(&s->sout, http_content_type_binary); |
ban4jp | 2:4da9ed411bdc | 201 | } else if(strncmp(http_html, ptr, sizeof(http_html)-1) == 0 || |
ban4jp | 2:4da9ed411bdc | 202 | strncmp(http_shtml, ptr, sizeof(http_shtml)-1) == 0) { |
ban4jp | 0:685224d2f66d | 203 | PSOCK_SEND_STR(&s->sout, http_content_type_html); |
ban4jp | 2:4da9ed411bdc | 204 | } else if(strncmp(http_json, ptr, sizeof(http_json)-1) == 0) { |
ban4jp | 2:4da9ed411bdc | 205 | PSOCK_SEND_STR(&s->sout, http_content_type_json); |
ban4jp | 2:4da9ed411bdc | 206 | } else if(strncmp(http_xml, ptr, sizeof(http_xml)-1) == 0) { |
ban4jp | 2:4da9ed411bdc | 207 | PSOCK_SEND_STR(&s->sout, http_content_type_xml); |
ban4jp | 2:4da9ed411bdc | 208 | } else if(strncmp(http_css, ptr, sizeof(http_css)-1) == 0) { |
ban4jp | 0:685224d2f66d | 209 | PSOCK_SEND_STR(&s->sout, http_content_type_css); |
ban4jp | 2:4da9ed411bdc | 210 | } else if(strncmp(http_png, ptr, sizeof(http_png)-1) == 0) { |
ban4jp | 0:685224d2f66d | 211 | PSOCK_SEND_STR(&s->sout, http_content_type_png); |
ban4jp | 2:4da9ed411bdc | 212 | } else if(strncmp(http_gif, ptr, sizeof(http_gif)-1) == 0) { |
ban4jp | 0:685224d2f66d | 213 | PSOCK_SEND_STR(&s->sout, http_content_type_gif); |
ban4jp | 2:4da9ed411bdc | 214 | } else if(strncmp(http_jpg, ptr, sizeof(http_jpg)-1) == 0) { |
ban4jp | 0:685224d2f66d | 215 | PSOCK_SEND_STR(&s->sout, http_content_type_jpg); |
ban4jp | 0:685224d2f66d | 216 | } else { |
ban4jp | 0:685224d2f66d | 217 | PSOCK_SEND_STR(&s->sout, http_content_type_plain); |
ban4jp | 0:685224d2f66d | 218 | } |
ban4jp | 0:685224d2f66d | 219 | PSOCK_END(&s->sout); |
ban4jp | 0:685224d2f66d | 220 | } |
ban4jp | 0:685224d2f66d | 221 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 222 | static |
ban4jp | 0:685224d2f66d | 223 | PT_THREAD(handle_output(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 224 | { |
ban4jp | 0:685224d2f66d | 225 | char *ptr; |
ban4jp | 0:685224d2f66d | 226 | |
ban4jp | 0:685224d2f66d | 227 | PT_BEGIN(&s->outputpt); |
ban4jp | 0:685224d2f66d | 228 | |
ban4jp | 0:685224d2f66d | 229 | if(!httpd_fs_open(s->filename, &s->file)) { |
ban4jp | 0:685224d2f66d | 230 | httpd_fs_open(http_404_html, &s->file); |
ban4jp | 0:685224d2f66d | 231 | strcpy(s->filename, http_404_html); |
ban4jp | 0:685224d2f66d | 232 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 2:4da9ed411bdc | 233 | send_headers(s, |
ban4jp | 2:4da9ed411bdc | 234 | http_header_404)); |
ban4jp | 0:685224d2f66d | 235 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 2:4da9ed411bdc | 236 | send_file(s)); |
ban4jp | 0:685224d2f66d | 237 | } else { |
ban4jp | 0:685224d2f66d | 238 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 2:4da9ed411bdc | 239 | send_headers(s, |
ban4jp | 2:4da9ed411bdc | 240 | http_header_200)); |
ban4jp | 0:685224d2f66d | 241 | ptr = strchr(s->filename, ISO_period); |
ban4jp | 2:4da9ed411bdc | 242 | if(ptr != NULL && (strncmp(ptr, http_shtml, sizeof(http_shtml)-1) == 0 || |
ban4jp | 2:4da9ed411bdc | 243 | strncmp(ptr, http_json, sizeof(http_json)-1) == 0 || |
ban4jp | 2:4da9ed411bdc | 244 | strncmp(ptr, http_xml, sizeof(http_xml)-1) == 0)) { |
ban4jp | 0:685224d2f66d | 245 | PT_INIT(&s->scriptpt); |
ban4jp | 0:685224d2f66d | 246 | PT_WAIT_THREAD(&s->outputpt, handle_script(s)); |
ban4jp | 0:685224d2f66d | 247 | } else { |
ban4jp | 0:685224d2f66d | 248 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 2:4da9ed411bdc | 249 | send_file(s)); |
ban4jp | 0:685224d2f66d | 250 | } |
ban4jp | 0:685224d2f66d | 251 | } |
ban4jp | 0:685224d2f66d | 252 | PSOCK_CLOSE(&s->sout); |
ban4jp | 0:685224d2f66d | 253 | PT_END(&s->outputpt); |
ban4jp | 0:685224d2f66d | 254 | } |
ban4jp | 0:685224d2f66d | 255 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 256 | static |
ban4jp | 0:685224d2f66d | 257 | PT_THREAD(handle_input(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 258 | { |
ban4jp | 0:685224d2f66d | 259 | PSOCK_BEGIN(&s->sin); |
ban4jp | 0:685224d2f66d | 260 | |
ban4jp | 0:685224d2f66d | 261 | PSOCK_READTO(&s->sin, ISO_space); |
ban4jp | 0:685224d2f66d | 262 | |
ban4jp | 0:685224d2f66d | 263 | |
ban4jp | 2:4da9ed411bdc | 264 | if(strncmp(s->inputbuf, http_get, sizeof(http_get)-1) != 0) { |
ban4jp | 0:685224d2f66d | 265 | PSOCK_CLOSE_EXIT(&s->sin); |
ban4jp | 0:685224d2f66d | 266 | } |
ban4jp | 0:685224d2f66d | 267 | PSOCK_READTO(&s->sin, ISO_space); |
ban4jp | 0:685224d2f66d | 268 | |
ban4jp | 0:685224d2f66d | 269 | if(s->inputbuf[0] != ISO_slash) { |
ban4jp | 0:685224d2f66d | 270 | PSOCK_CLOSE_EXIT(&s->sin); |
ban4jp | 0:685224d2f66d | 271 | } |
ban4jp | 0:685224d2f66d | 272 | |
ban4jp | 0:685224d2f66d | 273 | if(s->inputbuf[1] == ISO_space) { |
ban4jp | 0:685224d2f66d | 274 | strncpy(s->filename, http_index_html, sizeof(s->filename)); |
ban4jp | 0:685224d2f66d | 275 | } else { |
ban4jp | 0:685224d2f66d | 276 | s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0; |
ban4jp | 0:685224d2f66d | 277 | strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename)); |
ban4jp | 0:685224d2f66d | 278 | } |
ban4jp | 0:685224d2f66d | 279 | |
ban4jp | 0:685224d2f66d | 280 | /* httpd_log_file(uip_conn->ripaddr, s->filename);*/ |
ban4jp | 0:685224d2f66d | 281 | |
ban4jp | 0:685224d2f66d | 282 | s->state = STATE_OUTPUT; |
ban4jp | 0:685224d2f66d | 283 | |
ban4jp | 0:685224d2f66d | 284 | while(1) { |
ban4jp | 0:685224d2f66d | 285 | PSOCK_READTO(&s->sin, ISO_nl); |
ban4jp | 0:685224d2f66d | 286 | |
ban4jp | 2:4da9ed411bdc | 287 | if(strncmp(s->inputbuf, http_referer, sizeof(http_referer)-1) == 0) { |
ban4jp | 0:685224d2f66d | 288 | s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0; |
ban4jp | 0:685224d2f66d | 289 | /* httpd_log(&s->inputbuf[9]);*/ |
ban4jp | 0:685224d2f66d | 290 | } |
ban4jp | 0:685224d2f66d | 291 | } |
ban4jp | 0:685224d2f66d | 292 | |
ban4jp | 0:685224d2f66d | 293 | PSOCK_END(&s->sin); |
ban4jp | 0:685224d2f66d | 294 | } |
ban4jp | 0:685224d2f66d | 295 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 296 | static void |
ban4jp | 0:685224d2f66d | 297 | handle_connection(struct httpd_state *s) |
ban4jp | 0:685224d2f66d | 298 | { |
ban4jp | 0:685224d2f66d | 299 | handle_input(s); |
ban4jp | 0:685224d2f66d | 300 | if(s->state == STATE_OUTPUT) { |
ban4jp | 0:685224d2f66d | 301 | handle_output(s); |
ban4jp | 0:685224d2f66d | 302 | } |
ban4jp | 0:685224d2f66d | 303 | } |
ban4jp | 0:685224d2f66d | 304 | /*---------------------------------------------------------------------------*/ |
ban4jp | 2:4da9ed411bdc | 305 | extern "C" void |
ban4jp | 0:685224d2f66d | 306 | httpd_appcall(void) |
ban4jp | 0:685224d2f66d | 307 | { |
ban4jp | 0:685224d2f66d | 308 | struct httpd_state *s = (struct httpd_state *)&(uip_conn->appstate); |
ban4jp | 0:685224d2f66d | 309 | |
ban4jp | 0:685224d2f66d | 310 | if(uip_closed() || uip_aborted() || uip_timedout()) { |
ban4jp | 0:685224d2f66d | 311 | } else if(uip_connected()) { |
ban4jp | 0:685224d2f66d | 312 | PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1); |
ban4jp | 0:685224d2f66d | 313 | PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1); |
ban4jp | 0:685224d2f66d | 314 | PT_INIT(&s->outputpt); |
ban4jp | 0:685224d2f66d | 315 | s->state = STATE_WAITING; |
ban4jp | 0:685224d2f66d | 316 | /* timer_set(&s->timer, CLOCK_SECOND * 100);*/ |
ban4jp | 0:685224d2f66d | 317 | s->timer = 0; |
ban4jp | 0:685224d2f66d | 318 | handle_connection(s); |
ban4jp | 0:685224d2f66d | 319 | } else if(s != NULL) { |
ban4jp | 0:685224d2f66d | 320 | if(uip_poll()) { |
ban4jp | 0:685224d2f66d | 321 | ++s->timer; |
ban4jp | 0:685224d2f66d | 322 | if(s->timer >= 20) { |
ban4jp | 2:4da9ed411bdc | 323 | uip_abort(); |
ban4jp | 0:685224d2f66d | 324 | } |
ban4jp | 0:685224d2f66d | 325 | } else { |
ban4jp | 0:685224d2f66d | 326 | s->timer = 0; |
ban4jp | 0:685224d2f66d | 327 | } |
ban4jp | 0:685224d2f66d | 328 | handle_connection(s); |
ban4jp | 0:685224d2f66d | 329 | } else { |
ban4jp | 0:685224d2f66d | 330 | uip_abort(); |
ban4jp | 0:685224d2f66d | 331 | } |
ban4jp | 0:685224d2f66d | 332 | } |
ban4jp | 0:685224d2f66d | 333 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 334 | /** |
ban4jp | 0:685224d2f66d | 335 | * \brief Initialize the web server |
ban4jp | 0:685224d2f66d | 336 | * |
ban4jp | 0:685224d2f66d | 337 | * This function initializes the web server and should be |
ban4jp | 0:685224d2f66d | 338 | * called at system boot-up. |
ban4jp | 0:685224d2f66d | 339 | */ |
ban4jp | 2:4da9ed411bdc | 340 | extern "C" void |
ban4jp | 0:685224d2f66d | 341 | httpd_init(void) |
ban4jp | 0:685224d2f66d | 342 | { |
ban4jp | 0:685224d2f66d | 343 | uip_listen(HTONS(80)); |
ban4jp | 0:685224d2f66d | 344 | } |
ban4jp | 0:685224d2f66d | 345 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 346 | /** @} */ |