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