ban4jp -
/
uIP-1-0_webserver
uIP 1.0 based webserver for LPC1114 + ENC28J60
apps/webserver/httpd.c@0:685224d2f66d, 2014-06-14 (annotated)
- Committer:
- ban4jp
- Date:
- Sat Jun 14 16:02:21 2014 +0000
- Revision:
- 0:685224d2f66d
initial commit.
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 | 0:685224d2f66d | 58 | #include "uip.h" |
ban4jp | 0:685224d2f66d | 59 | #include "httpd.h" |
ban4jp | 0:685224d2f66d | 60 | #include "httpd-fs.h" |
ban4jp | 0:685224d2f66d | 61 | #include "httpd-cgi.h" |
ban4jp | 0:685224d2f66d | 62 | #include "http-strings.h" |
ban4jp | 0:685224d2f66d | 63 | |
ban4jp | 0:685224d2f66d | 64 | #include <string.h> |
ban4jp | 0:685224d2f66d | 65 | |
ban4jp | 0:685224d2f66d | 66 | #define STATE_WAITING 0 |
ban4jp | 0:685224d2f66d | 67 | #define STATE_OUTPUT 1 |
ban4jp | 0:685224d2f66d | 68 | |
ban4jp | 0:685224d2f66d | 69 | #define ISO_nl 0x0a |
ban4jp | 0:685224d2f66d | 70 | #define ISO_space 0x20 |
ban4jp | 0:685224d2f66d | 71 | #define ISO_bang 0x21 |
ban4jp | 0:685224d2f66d | 72 | #define ISO_percent 0x25 |
ban4jp | 0:685224d2f66d | 73 | #define ISO_period 0x2e |
ban4jp | 0:685224d2f66d | 74 | #define ISO_slash 0x2f |
ban4jp | 0:685224d2f66d | 75 | #define ISO_colon 0x3a |
ban4jp | 0:685224d2f66d | 76 | |
ban4jp | 0:685224d2f66d | 77 | |
ban4jp | 0:685224d2f66d | 78 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 79 | static unsigned short |
ban4jp | 0:685224d2f66d | 80 | generate_part_of_file(void *state) |
ban4jp | 0:685224d2f66d | 81 | { |
ban4jp | 0:685224d2f66d | 82 | struct httpd_state *s = (struct httpd_state *)state; |
ban4jp | 0:685224d2f66d | 83 | |
ban4jp | 0:685224d2f66d | 84 | if(s->file.len > uip_mss()) { |
ban4jp | 0:685224d2f66d | 85 | s->len = uip_mss(); |
ban4jp | 0:685224d2f66d | 86 | } else { |
ban4jp | 0:685224d2f66d | 87 | s->len = s->file.len; |
ban4jp | 0:685224d2f66d | 88 | } |
ban4jp | 0:685224d2f66d | 89 | memcpy(uip_appdata, s->file.data, s->len); |
ban4jp | 0:685224d2f66d | 90 | |
ban4jp | 0:685224d2f66d | 91 | return s->len; |
ban4jp | 0:685224d2f66d | 92 | } |
ban4jp | 0:685224d2f66d | 93 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 94 | static |
ban4jp | 0:685224d2f66d | 95 | PT_THREAD(send_file(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 96 | { |
ban4jp | 0:685224d2f66d | 97 | PSOCK_BEGIN(&s->sout); |
ban4jp | 0:685224d2f66d | 98 | |
ban4jp | 0:685224d2f66d | 99 | do { |
ban4jp | 0:685224d2f66d | 100 | PSOCK_GENERATOR_SEND(&s->sout, generate_part_of_file, s); |
ban4jp | 0:685224d2f66d | 101 | s->file.len -= s->len; |
ban4jp | 0:685224d2f66d | 102 | s->file.data += s->len; |
ban4jp | 0:685224d2f66d | 103 | } while(s->file.len > 0); |
ban4jp | 0:685224d2f66d | 104 | |
ban4jp | 0:685224d2f66d | 105 | PSOCK_END(&s->sout); |
ban4jp | 0:685224d2f66d | 106 | } |
ban4jp | 0:685224d2f66d | 107 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 108 | static |
ban4jp | 0:685224d2f66d | 109 | PT_THREAD(send_part_of_file(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 110 | { |
ban4jp | 0:685224d2f66d | 111 | PSOCK_BEGIN(&s->sout); |
ban4jp | 0:685224d2f66d | 112 | |
ban4jp | 0:685224d2f66d | 113 | PSOCK_SEND(&s->sout, s->file.data, s->len); |
ban4jp | 0:685224d2f66d | 114 | |
ban4jp | 0:685224d2f66d | 115 | PSOCK_END(&s->sout); |
ban4jp | 0:685224d2f66d | 116 | } |
ban4jp | 0:685224d2f66d | 117 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 118 | static void |
ban4jp | 0:685224d2f66d | 119 | next_scriptstate(struct httpd_state *s) |
ban4jp | 0:685224d2f66d | 120 | { |
ban4jp | 0:685224d2f66d | 121 | char *p; |
ban4jp | 0:685224d2f66d | 122 | p = strchr(s->scriptptr, ISO_nl) + 1; |
ban4jp | 0:685224d2f66d | 123 | s->scriptlen -= (unsigned short)(p - s->scriptptr); |
ban4jp | 0:685224d2f66d | 124 | s->scriptptr = p; |
ban4jp | 0:685224d2f66d | 125 | } |
ban4jp | 0:685224d2f66d | 126 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 127 | static |
ban4jp | 0:685224d2f66d | 128 | PT_THREAD(handle_script(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 129 | { |
ban4jp | 0:685224d2f66d | 130 | char *ptr; |
ban4jp | 0:685224d2f66d | 131 | |
ban4jp | 0:685224d2f66d | 132 | PT_BEGIN(&s->scriptpt); |
ban4jp | 0:685224d2f66d | 133 | |
ban4jp | 0:685224d2f66d | 134 | |
ban4jp | 0:685224d2f66d | 135 | while(s->file.len > 0) { |
ban4jp | 0:685224d2f66d | 136 | |
ban4jp | 0:685224d2f66d | 137 | /* Check if we should start executing a script. */ |
ban4jp | 0:685224d2f66d | 138 | if(*s->file.data == ISO_percent && |
ban4jp | 0:685224d2f66d | 139 | *(s->file.data + 1) == ISO_bang) { |
ban4jp | 0:685224d2f66d | 140 | s->scriptptr = s->file.data + 3; |
ban4jp | 0:685224d2f66d | 141 | s->scriptlen = s->file.len - 3; |
ban4jp | 0:685224d2f66d | 142 | if(*(s->scriptptr - 1) == ISO_colon) { |
ban4jp | 0:685224d2f66d | 143 | httpd_fs_open(s->scriptptr + 1, &s->file); |
ban4jp | 0:685224d2f66d | 144 | PT_WAIT_THREAD(&s->scriptpt, send_file(s)); |
ban4jp | 0:685224d2f66d | 145 | } else { |
ban4jp | 0:685224d2f66d | 146 | PT_WAIT_THREAD(&s->scriptpt, |
ban4jp | 0:685224d2f66d | 147 | httpd_cgi(s->scriptptr)(s, s->scriptptr)); |
ban4jp | 0:685224d2f66d | 148 | } |
ban4jp | 0:685224d2f66d | 149 | next_scriptstate(s); |
ban4jp | 0:685224d2f66d | 150 | |
ban4jp | 0:685224d2f66d | 151 | /* The script is over, so we reset the pointers and continue |
ban4jp | 0:685224d2f66d | 152 | sending the rest of the file. */ |
ban4jp | 0:685224d2f66d | 153 | s->file.data = s->scriptptr; |
ban4jp | 0:685224d2f66d | 154 | s->file.len = s->scriptlen; |
ban4jp | 0:685224d2f66d | 155 | } else { |
ban4jp | 0:685224d2f66d | 156 | /* See if we find the start of script marker in the block of HTML |
ban4jp | 0:685224d2f66d | 157 | to be sent. */ |
ban4jp | 0:685224d2f66d | 158 | |
ban4jp | 0:685224d2f66d | 159 | if(s->file.len > uip_mss()) { |
ban4jp | 0:685224d2f66d | 160 | s->len = uip_mss(); |
ban4jp | 0:685224d2f66d | 161 | } else { |
ban4jp | 0:685224d2f66d | 162 | s->len = s->file.len; |
ban4jp | 0:685224d2f66d | 163 | } |
ban4jp | 0:685224d2f66d | 164 | |
ban4jp | 0:685224d2f66d | 165 | if(*s->file.data == ISO_percent) { |
ban4jp | 0:685224d2f66d | 166 | ptr = strchr(s->file.data + 1, ISO_percent); |
ban4jp | 0:685224d2f66d | 167 | } else { |
ban4jp | 0:685224d2f66d | 168 | ptr = strchr(s->file.data, ISO_percent); |
ban4jp | 0:685224d2f66d | 169 | } |
ban4jp | 0:685224d2f66d | 170 | if(ptr != NULL && |
ban4jp | 0:685224d2f66d | 171 | ptr != s->file.data) { |
ban4jp | 0:685224d2f66d | 172 | s->len = (int)(ptr - s->file.data); |
ban4jp | 0:685224d2f66d | 173 | if(s->len >= uip_mss()) { |
ban4jp | 0:685224d2f66d | 174 | s->len = uip_mss(); |
ban4jp | 0:685224d2f66d | 175 | } |
ban4jp | 0:685224d2f66d | 176 | } |
ban4jp | 0:685224d2f66d | 177 | PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s)); |
ban4jp | 0:685224d2f66d | 178 | s->file.data += s->len; |
ban4jp | 0:685224d2f66d | 179 | s->file.len -= s->len; |
ban4jp | 0:685224d2f66d | 180 | |
ban4jp | 0:685224d2f66d | 181 | } |
ban4jp | 0:685224d2f66d | 182 | } |
ban4jp | 0:685224d2f66d | 183 | |
ban4jp | 0:685224d2f66d | 184 | PT_END(&s->scriptpt); |
ban4jp | 0:685224d2f66d | 185 | } |
ban4jp | 0:685224d2f66d | 186 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 187 | static |
ban4jp | 0:685224d2f66d | 188 | PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) |
ban4jp | 0:685224d2f66d | 189 | { |
ban4jp | 0:685224d2f66d | 190 | char *ptr; |
ban4jp | 0:685224d2f66d | 191 | |
ban4jp | 0:685224d2f66d | 192 | PSOCK_BEGIN(&s->sout); |
ban4jp | 0:685224d2f66d | 193 | |
ban4jp | 0:685224d2f66d | 194 | PSOCK_SEND_STR(&s->sout, statushdr); |
ban4jp | 0:685224d2f66d | 195 | |
ban4jp | 0:685224d2f66d | 196 | ptr = strrchr(s->filename, ISO_period); |
ban4jp | 0:685224d2f66d | 197 | if(ptr == NULL) { |
ban4jp | 0:685224d2f66d | 198 | PSOCK_SEND_STR(&s->sout, http_content_type_binary); |
ban4jp | 0:685224d2f66d | 199 | } else if(strncmp(http_html, ptr, 5) == 0 || |
ban4jp | 0:685224d2f66d | 200 | strncmp(http_shtml, ptr, 6) == 0) { |
ban4jp | 0:685224d2f66d | 201 | PSOCK_SEND_STR(&s->sout, http_content_type_html); |
ban4jp | 0:685224d2f66d | 202 | } else if(strncmp(http_css, ptr, 4) == 0) { |
ban4jp | 0:685224d2f66d | 203 | PSOCK_SEND_STR(&s->sout, http_content_type_css); |
ban4jp | 0:685224d2f66d | 204 | } else if(strncmp(http_png, ptr, 4) == 0) { |
ban4jp | 0:685224d2f66d | 205 | PSOCK_SEND_STR(&s->sout, http_content_type_png); |
ban4jp | 0:685224d2f66d | 206 | } else if(strncmp(http_gif, ptr, 4) == 0) { |
ban4jp | 0:685224d2f66d | 207 | PSOCK_SEND_STR(&s->sout, http_content_type_gif); |
ban4jp | 0:685224d2f66d | 208 | } else if(strncmp(http_jpg, ptr, 4) == 0) { |
ban4jp | 0:685224d2f66d | 209 | PSOCK_SEND_STR(&s->sout, http_content_type_jpg); |
ban4jp | 0:685224d2f66d | 210 | } else { |
ban4jp | 0:685224d2f66d | 211 | PSOCK_SEND_STR(&s->sout, http_content_type_plain); |
ban4jp | 0:685224d2f66d | 212 | } |
ban4jp | 0:685224d2f66d | 213 | PSOCK_END(&s->sout); |
ban4jp | 0:685224d2f66d | 214 | } |
ban4jp | 0:685224d2f66d | 215 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 216 | static |
ban4jp | 0:685224d2f66d | 217 | PT_THREAD(handle_output(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 218 | { |
ban4jp | 0:685224d2f66d | 219 | char *ptr; |
ban4jp | 0:685224d2f66d | 220 | |
ban4jp | 0:685224d2f66d | 221 | PT_BEGIN(&s->outputpt); |
ban4jp | 0:685224d2f66d | 222 | |
ban4jp | 0:685224d2f66d | 223 | if(!httpd_fs_open(s->filename, &s->file)) { |
ban4jp | 0:685224d2f66d | 224 | httpd_fs_open(http_404_html, &s->file); |
ban4jp | 0:685224d2f66d | 225 | strcpy(s->filename, http_404_html); |
ban4jp | 0:685224d2f66d | 226 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 0:685224d2f66d | 227 | send_headers(s, |
ban4jp | 0:685224d2f66d | 228 | http_header_404)); |
ban4jp | 0:685224d2f66d | 229 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 0:685224d2f66d | 230 | send_file(s)); |
ban4jp | 0:685224d2f66d | 231 | } else { |
ban4jp | 0:685224d2f66d | 232 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 0:685224d2f66d | 233 | send_headers(s, |
ban4jp | 0:685224d2f66d | 234 | http_header_200)); |
ban4jp | 0:685224d2f66d | 235 | ptr = strchr(s->filename, ISO_period); |
ban4jp | 0:685224d2f66d | 236 | if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0) { |
ban4jp | 0:685224d2f66d | 237 | PT_INIT(&s->scriptpt); |
ban4jp | 0:685224d2f66d | 238 | PT_WAIT_THREAD(&s->outputpt, handle_script(s)); |
ban4jp | 0:685224d2f66d | 239 | } else { |
ban4jp | 0:685224d2f66d | 240 | PT_WAIT_THREAD(&s->outputpt, |
ban4jp | 0:685224d2f66d | 241 | send_file(s)); |
ban4jp | 0:685224d2f66d | 242 | } |
ban4jp | 0:685224d2f66d | 243 | } |
ban4jp | 0:685224d2f66d | 244 | PSOCK_CLOSE(&s->sout); |
ban4jp | 0:685224d2f66d | 245 | PT_END(&s->outputpt); |
ban4jp | 0:685224d2f66d | 246 | } |
ban4jp | 0:685224d2f66d | 247 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 248 | static |
ban4jp | 0:685224d2f66d | 249 | PT_THREAD(handle_input(struct httpd_state *s)) |
ban4jp | 0:685224d2f66d | 250 | { |
ban4jp | 0:685224d2f66d | 251 | PSOCK_BEGIN(&s->sin); |
ban4jp | 0:685224d2f66d | 252 | |
ban4jp | 0:685224d2f66d | 253 | PSOCK_READTO(&s->sin, ISO_space); |
ban4jp | 0:685224d2f66d | 254 | |
ban4jp | 0:685224d2f66d | 255 | |
ban4jp | 0:685224d2f66d | 256 | if(strncmp(s->inputbuf, http_get, 4) != 0) { |
ban4jp | 0:685224d2f66d | 257 | PSOCK_CLOSE_EXIT(&s->sin); |
ban4jp | 0:685224d2f66d | 258 | } |
ban4jp | 0:685224d2f66d | 259 | PSOCK_READTO(&s->sin, ISO_space); |
ban4jp | 0:685224d2f66d | 260 | |
ban4jp | 0:685224d2f66d | 261 | if(s->inputbuf[0] != ISO_slash) { |
ban4jp | 0:685224d2f66d | 262 | PSOCK_CLOSE_EXIT(&s->sin); |
ban4jp | 0:685224d2f66d | 263 | } |
ban4jp | 0:685224d2f66d | 264 | |
ban4jp | 0:685224d2f66d | 265 | if(s->inputbuf[1] == ISO_space) { |
ban4jp | 0:685224d2f66d | 266 | strncpy(s->filename, http_index_html, sizeof(s->filename)); |
ban4jp | 0:685224d2f66d | 267 | } else { |
ban4jp | 0:685224d2f66d | 268 | s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0; |
ban4jp | 0:685224d2f66d | 269 | strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename)); |
ban4jp | 0:685224d2f66d | 270 | } |
ban4jp | 0:685224d2f66d | 271 | |
ban4jp | 0:685224d2f66d | 272 | /* httpd_log_file(uip_conn->ripaddr, s->filename);*/ |
ban4jp | 0:685224d2f66d | 273 | |
ban4jp | 0:685224d2f66d | 274 | s->state = STATE_OUTPUT; |
ban4jp | 0:685224d2f66d | 275 | |
ban4jp | 0:685224d2f66d | 276 | while(1) { |
ban4jp | 0:685224d2f66d | 277 | PSOCK_READTO(&s->sin, ISO_nl); |
ban4jp | 0:685224d2f66d | 278 | |
ban4jp | 0:685224d2f66d | 279 | if(strncmp(s->inputbuf, http_referer, 8) == 0) { |
ban4jp | 0:685224d2f66d | 280 | s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0; |
ban4jp | 0:685224d2f66d | 281 | /* httpd_log(&s->inputbuf[9]);*/ |
ban4jp | 0:685224d2f66d | 282 | } |
ban4jp | 0:685224d2f66d | 283 | } |
ban4jp | 0:685224d2f66d | 284 | |
ban4jp | 0:685224d2f66d | 285 | PSOCK_END(&s->sin); |
ban4jp | 0:685224d2f66d | 286 | } |
ban4jp | 0:685224d2f66d | 287 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 288 | static void |
ban4jp | 0:685224d2f66d | 289 | handle_connection(struct httpd_state *s) |
ban4jp | 0:685224d2f66d | 290 | { |
ban4jp | 0:685224d2f66d | 291 | handle_input(s); |
ban4jp | 0:685224d2f66d | 292 | if(s->state == STATE_OUTPUT) { |
ban4jp | 0:685224d2f66d | 293 | handle_output(s); |
ban4jp | 0:685224d2f66d | 294 | } |
ban4jp | 0:685224d2f66d | 295 | } |
ban4jp | 0:685224d2f66d | 296 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 297 | void |
ban4jp | 0:685224d2f66d | 298 | httpd_appcall(void) |
ban4jp | 0:685224d2f66d | 299 | { |
ban4jp | 0:685224d2f66d | 300 | struct httpd_state *s = (struct httpd_state *)&(uip_conn->appstate); |
ban4jp | 0:685224d2f66d | 301 | |
ban4jp | 0:685224d2f66d | 302 | if(uip_closed() || uip_aborted() || uip_timedout()) { |
ban4jp | 0:685224d2f66d | 303 | } else if(uip_connected()) { |
ban4jp | 0:685224d2f66d | 304 | PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1); |
ban4jp | 0:685224d2f66d | 305 | PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1); |
ban4jp | 0:685224d2f66d | 306 | PT_INIT(&s->outputpt); |
ban4jp | 0:685224d2f66d | 307 | s->state = STATE_WAITING; |
ban4jp | 0:685224d2f66d | 308 | /* timer_set(&s->timer, CLOCK_SECOND * 100);*/ |
ban4jp | 0:685224d2f66d | 309 | s->timer = 0; |
ban4jp | 0:685224d2f66d | 310 | handle_connection(s); |
ban4jp | 0:685224d2f66d | 311 | } else if(s != NULL) { |
ban4jp | 0:685224d2f66d | 312 | if(uip_poll()) { |
ban4jp | 0:685224d2f66d | 313 | ++s->timer; |
ban4jp | 0:685224d2f66d | 314 | if(s->timer >= 20) { |
ban4jp | 0:685224d2f66d | 315 | uip_abort(); |
ban4jp | 0:685224d2f66d | 316 | } |
ban4jp | 0:685224d2f66d | 317 | } else { |
ban4jp | 0:685224d2f66d | 318 | s->timer = 0; |
ban4jp | 0:685224d2f66d | 319 | } |
ban4jp | 0:685224d2f66d | 320 | handle_connection(s); |
ban4jp | 0:685224d2f66d | 321 | } else { |
ban4jp | 0:685224d2f66d | 322 | uip_abort(); |
ban4jp | 0:685224d2f66d | 323 | } |
ban4jp | 0:685224d2f66d | 324 | } |
ban4jp | 0:685224d2f66d | 325 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 326 | /** |
ban4jp | 0:685224d2f66d | 327 | * \brief Initialize the web server |
ban4jp | 0:685224d2f66d | 328 | * |
ban4jp | 0:685224d2f66d | 329 | * This function initializes the web server and should be |
ban4jp | 0:685224d2f66d | 330 | * called at system boot-up. |
ban4jp | 0:685224d2f66d | 331 | */ |
ban4jp | 0:685224d2f66d | 332 | void |
ban4jp | 0:685224d2f66d | 333 | httpd_init(void) |
ban4jp | 0:685224d2f66d | 334 | { |
ban4jp | 0:685224d2f66d | 335 | uip_listen(HTONS(80)); |
ban4jp | 0:685224d2f66d | 336 | } |
ban4jp | 0:685224d2f66d | 337 | /*---------------------------------------------------------------------------*/ |
ban4jp | 0:685224d2f66d | 338 | /** @} */ |