7 * \defgroup httpd Web server
\r
9 * The uIP web server is a very simplistic implementation of an HTTP
\r
10 * server. It can serve web pages and files from a read-only ROM
\r
11 * filesystem, and provides a very small scripting language.
\r
19 * Adam Dunkels <adam@sics.se>
\r
23 * Copyright (c) 2004, Adam Dunkels.
\r
24 * All rights reserved.
\r
26 * Redistribution and use in source and binary forms, with or without
\r
27 * modification, are permitted provided that the following conditions
\r
29 * 1. Redistributions of source code must retain the above copyright
\r
30 * notice, this list of conditions and the following disclaimer.
\r
31 * 2. Redistributions in binary form must reproduce the above copyright
\r
32 * notice, this list of conditions and the following disclaimer in the
\r
33 * documentation and/or other materials provided with the distribution.
\r
34 * 3. Neither the name of the Institute nor the names of its contributors
\r
35 * may be used to endorse or promote products derived from this software
\r
36 * without specific prior written permission.
\r
38 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
\r
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
\r
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
\r
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
\r
50 * This file is part of the uIP TCP/IP stack.
\r
52 * Author: Adam Dunkels <adam@sics.se>
\r
54 * $Id: httpd.c,v 1.2 2006/06/11 21:46:38 adam Exp $
\r
56 #include "net/uip.h"
\r
57 #include "apps/httpd/httpd.h"
\r
58 #include "apps/httpd/httpd-fs.h"
\r
59 #include "apps/httpd/httpd-cgi.h"
\r
60 #include "apps/httpd/http-strings.h"
\r
64 #define STATE_WAITING 0
\r
65 #define STATE_OUTPUT 1
\r
68 #define ISO_space 0x20
\r
69 #define ISO_bang 0x21
\r
70 #define ISO_percent 0x25
\r
71 #define ISO_period 0x2e
\r
72 #define ISO_slash 0x2f
\r
73 #define ISO_colon 0x3a
\r
75 /*---------------------------------------------------------------------------*/
\r
76 static unsigned short generate_part_of_file( void *state )
\r
78 struct httpd_state *s = ( struct httpd_state * ) state;
\r
80 if( s->file.len > uip_mss() )
\r
86 s->len = s->file.len;
\r
89 memcpy( uip_appdata, s->file.data, s->len );
\r
94 /*---------------------------------------------------------------------------*/
\r
95 static PT_THREAD( send_file ( struct httpd_state *s ) )
\r
97 PSOCK_BEGIN( &s->sout );
\r
99 ( void ) PT_YIELD_FLAG;
\r
103 PSOCK_GENERATOR_SEND( &s->sout, generate_part_of_file, s );
\r
104 s->file.len -= s->len;
\r
105 s->file.data += s->len;
\r
106 } while( s->file.len > 0 );
\r
108 PSOCK_END( &s->sout );
\r
111 /*---------------------------------------------------------------------------*/
\r
112 static PT_THREAD( send_part_of_file ( struct httpd_state *s ) )
\r
114 PSOCK_BEGIN( &s->sout );
\r
115 ( void ) PT_YIELD_FLAG;
\r
117 PSOCK_SEND( &s->sout, s->file.data, s->len );
\r
119 PSOCK_END( &s->sout );
\r
122 /*---------------------------------------------------------------------------*/
\r
123 static void next_scriptstate( struct httpd_state *s )
\r
126 p = strchr( s->scriptptr, ISO_nl ) + 1;
\r
127 s->scriptlen -= ( unsigned short ) ( p - s->scriptptr );
\r
131 /*---------------------------------------------------------------------------*/
\r
132 static PT_THREAD( handle_script ( struct httpd_state *s ) )
\r
136 PT_BEGIN( &s->scriptpt );
\r
137 ( void ) PT_YIELD_FLAG;
\r
138 while( s->file.len > 0 )
\r
140 /* Check if we should start executing a script. */
\r
141 if( *s->file.data == ISO_percent && *(s->file.data + 1) == ISO_bang )
\r
143 s->scriptptr = s->file.data + 3;
\r
144 s->scriptlen = s->file.len - 3;
\r
145 if( *(s->scriptptr - 1) == ISO_colon )
\r
147 httpd_fs_open( s->scriptptr + 1, &s->file );
\r
148 PT_WAIT_THREAD( &s->scriptpt, send_file(s) );
\r
152 PT_WAIT_THREAD( &s->scriptpt, httpd_cgi(s->scriptptr) (s, s->scriptptr) );
\r
155 next_scriptstate( s );
\r
157 /* The script is over, so we reset the pointers and continue
\r
158 sending the rest of the file. */
\r
159 s->file.data = s->scriptptr;
\r
160 s->file.len = s->scriptlen;
\r
164 /* See if we find the start of script marker in the block of HTML
\r
166 if( s->file.len > uip_mss() )
\r
168 s->len = uip_mss();
\r
172 s->len = s->file.len;
\r
175 if( *s->file.data == ISO_percent )
\r
177 ptr = strchr( s->file.data + 1, ISO_percent );
\r
181 ptr = strchr( s->file.data, ISO_percent );
\r
184 if( ptr != NULL && ptr != s->file.data )
\r
186 s->len = ( int ) ( ptr - s->file.data );
\r
187 if( s->len >= uip_mss() )
\r
189 s->len = uip_mss();
\r
193 PT_WAIT_THREAD( &s->scriptpt, send_part_of_file(s) );
\r
194 s->file.data += s->len;
\r
195 s->file.len -= s->len;
\r
199 PT_END( &s->scriptpt );
\r
202 /*---------------------------------------------------------------------------*/
\r
203 static PT_THREAD( send_headers ( struct httpd_state *s, const char *statushdr ) )
\r
207 PSOCK_BEGIN( &s->sout );
\r
208 ( void ) PT_YIELD_FLAG;
\r
209 PSOCK_SEND_STR( &s->sout, statushdr );
\r
211 ptr = strrchr( s->filename, ISO_period );
\r
214 PSOCK_SEND_STR( &s->sout, http_content_type_binary );
\r
216 else if( strncmp(http_html, ptr, 5) == 0 || strncmp(http_shtml, ptr, 6) == 0 )
\r
218 PSOCK_SEND_STR( &s->sout, http_content_type_html );
\r
220 else if( strncmp(http_css, ptr, 4) == 0 )
\r
222 PSOCK_SEND_STR( &s->sout, http_content_type_css );
\r
224 else if( strncmp(http_png, ptr, 4) == 0 )
\r
226 PSOCK_SEND_STR( &s->sout, http_content_type_png );
\r
228 else if( strncmp(http_gif, ptr, 4) == 0 )
\r
230 PSOCK_SEND_STR( &s->sout, http_content_type_gif );
\r
232 else if( strncmp(http_jpg, ptr, 4) == 0 )
\r
234 PSOCK_SEND_STR( &s->sout, http_content_type_jpg );
\r
238 PSOCK_SEND_STR( &s->sout, http_content_type_plain );
\r
241 PSOCK_END( &s->sout );
\r
244 /*---------------------------------------------------------------------------*/
\r
245 static PT_THREAD( handle_output ( struct httpd_state *s ) )
\r
249 PT_BEGIN( &s->outputpt );
\r
250 ( void ) PT_YIELD_FLAG;
\r
251 if( !httpd_fs_open(s->filename, &s->file) )
\r
253 httpd_fs_open( http_404_html, &s->file );
\r
254 strcpy( s->filename, http_404_html );
\r
255 PT_WAIT_THREAD( &s->outputpt, send_headers(s, http_header_404) );
\r
256 PT_WAIT_THREAD( &s->outputpt, send_file(s) );
\r
260 PT_WAIT_THREAD( &s->outputpt, send_headers(s, http_header_200) );
\r
261 ptr = strchr( s->filename, ISO_period );
\r
262 if( ptr != NULL && strncmp(ptr, http_shtml, 6) == 0 )
\r
264 PT_INIT( &s->scriptpt );
\r
265 PT_WAIT_THREAD( &s->outputpt, handle_script(s) );
\r
269 PT_WAIT_THREAD( &s->outputpt, send_file(s) );
\r
273 PSOCK_CLOSE( &s->sout );
\r
274 PT_END( &s->outputpt );
\r
277 /*---------------------------------------------------------------------------*/
\r
278 static PT_THREAD( handle_input ( struct httpd_state *s ) )
\r
280 PSOCK_BEGIN( &s->sin );
\r
281 ( void ) PT_YIELD_FLAG;
\r
282 PSOCK_READTO( &s->sin, ISO_space );
\r
284 if( strncmp(s->inputbuf, http_get, 4) != 0 )
\r
286 PSOCK_CLOSE_EXIT( &s->sin );
\r
289 PSOCK_READTO( &s->sin, ISO_space );
\r
291 if( s->inputbuf[0] != ISO_slash )
\r
293 PSOCK_CLOSE_EXIT( &s->sin );
\r
296 if( s->inputbuf[1] == ISO_space )
\r
298 strncpy( s->filename, http_index_html, sizeof(s->filename) );
\r
302 s->inputbuf[PSOCK_DATALEN( &s->sin ) - 1] = 0;
\r
304 /* Process any form input being sent to the server. */
\r
305 #if UIP_CONF_PROCESS_HTTPD_FORMS == 1
\r
307 extern void vApplicationProcessFormInput( char *pcInputString );
\r
308 vApplicationProcessFormInput( s->inputbuf );
\r
312 strncpy( s->filename, &s->inputbuf[0], sizeof(s->filename) );
\r
315 /* httpd_log_file(uip_conn->ripaddr, s->filename);*/
\r
316 s->state = STATE_OUTPUT;
\r
320 PSOCK_READTO( &s->sin, ISO_nl );
\r
322 if( strncmp(s->inputbuf, http_referer, 8) == 0 )
\r
324 s->inputbuf[PSOCK_DATALEN( &s->sin ) - 2] = 0;
\r
326 /* httpd_log(&s->inputbuf[9]);*/
\r
330 PSOCK_END( &s->sin );
\r
333 /*---------------------------------------------------------------------------*/
\r
334 static void handle_connection( struct httpd_state *s )
\r
337 if( s->state == STATE_OUTPUT )
\r
339 handle_output( s );
\r
343 /*---------------------------------------------------------------------------*/
\r
344 void httpd_appcall( void )
\r
346 struct httpd_state *s = ( struct httpd_state * ) &( uip_conn->appstate );
\r
348 if( uip_closed() || uip_aborted() || uip_timedout() )
\r
351 else if( uip_connected() )
\r
353 PSOCK_INIT( &s->sin, s->inputbuf, sizeof(s->inputbuf) - 1 );
\r
354 PSOCK_INIT( &s->sout, s->inputbuf, sizeof(s->inputbuf) - 1 );
\r
355 PT_INIT( &s->outputpt );
\r
356 s->state = STATE_WAITING;
\r
358 /* timer_set(&s->timer, CLOCK_SECOND * 100);*/
\r
360 handle_connection( s );
\r
362 else if( s != NULL )
\r
367 if( s->timer >= 20 )
\r
377 handle_connection( s );
\r
385 /*---------------------------------------------------------------------------*/
\r
388 * \brief Initialize the web server
\r
390 * This function initializes the web server and should be
\r
391 * called at system boot-up.
\r
393 void httpd_init( void )
\r
395 uip_listen( HTONS(80) );
\r
398 /*---------------------------------------------------------------------------*/
\r