2 * Copyright (c) 2004, Adam Dunkels.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file is part of the Contiki operating system.
31 * Author: Adam Dunkels <adam@sics.se>
33 * $Id: httpd.c,v 1.15 2010/04/06 20:16:25 oliverschmidt Exp $
39 #include "contiki-net.h"
41 #include "webserver.h"
43 #include "httpd-cgi.h"
44 #include "lib/petsciiconv.h"
45 #include "http-strings.h"
49 #ifndef WEBSERVER_CONF_CGI_CONNS
51 #else /* WEBSERVER_CONF_CGI_CONNS */
52 #define CONNS WEBSERVER_CONF_CGI_CONNS
53 #endif /* WEBSERVER_CONF_CGI_CONNS */
55 #define STATE_WAITING 0
56 #define STATE_OUTPUT 1
58 #define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, (unsigned int)strlen(str))
59 MEMB(conns
, struct httpd_state
, CONNS
);
62 #define ISO_space 0x20
64 #define ISO_percent 0x25
65 #define ISO_period 0x2e
66 #define ISO_slash 0x2f
67 #define ISO_colon 0x3a
69 /*---------------------------------------------------------------------------*/
73 struct httpd_state
*s
= (struct httpd_state
*)state
;
75 if(s
->file
.len
> uip_mss()) {
80 memcpy(uip_appdata
, s
->file
.data
, s
->len
);
84 /*---------------------------------------------------------------------------*/
86 PT_THREAD(send_file(struct httpd_state
*s
))
88 PSOCK_BEGIN(&s
->sout
);
91 PSOCK_GENERATOR_SEND(&s
->sout
, generate
, s
);
92 s
->file
.len
-= s
->len
;
93 s
->file
.data
+= s
->len
;
94 } while(s
->file
.len
> 0);
98 /*---------------------------------------------------------------------------*/
100 PT_THREAD(send_part_of_file(struct httpd_state
*s
))
102 PSOCK_BEGIN(&s
->sout
);
104 PSOCK_SEND(&s
->sout
, (uint8_t *)s
->file
.data
, s
->len
);
108 /*---------------------------------------------------------------------------*/
110 next_scriptstate(struct httpd_state
*s
)
114 if((p
= strchr(s
->scriptptr
, ISO_nl
)) != NULL
) {
116 s
->scriptlen
-= (unsigned short)(p
- s
->scriptptr
);
122 p = strchr(s->scriptptr, ISO_nl) + 1;
123 s->scriptlen -= (unsigned short)(p - s->scriptptr);
126 /*---------------------------------------------------------------------------*/
128 PT_THREAD(handle_script(struct httpd_state
*s
))
132 PT_BEGIN(&s
->scriptpt
);
134 while(s
->file
.len
> 0) {
136 /* Check if we should start executing a script. */
137 if(*s
->file
.data
== ISO_percent
&&
138 *(s
->file
.data
+ 1) == ISO_bang
) {
139 s
->scriptptr
= s
->file
.data
+ 3;
140 s
->scriptlen
= s
->file
.len
- 3;
141 if(*(s
->scriptptr
- 1) == ISO_colon
) {
142 httpd_fs_open(s
->scriptptr
+ 1, &s
->file
);
143 PT_WAIT_THREAD(&s
->scriptpt
, send_file(s
));
145 PT_WAIT_THREAD(&s
->scriptpt
,
146 httpd_cgi(s
->scriptptr
)(s
, s
->scriptptr
));
150 /* The script is over, so we reset the pointers and continue
151 sending the rest of the file. */
152 s
->file
.data
= s
->scriptptr
;
153 s
->file
.len
= s
->scriptlen
;
155 /* See if we find the start of script marker in the block of HTML
158 if(s
->file
.len
> uip_mss()) {
161 s
->len
= s
->file
.len
;
164 if(*s
->file
.data
== ISO_percent
) {
165 ptr
= strchr(s
->file
.data
+ 1, ISO_percent
);
167 ptr
= strchr(s
->file
.data
, ISO_percent
);
170 ptr
!= s
->file
.data
) {
171 s
->len
= (int)(ptr
- s
->file
.data
);
172 if(s
->len
>= uip_mss()) {
176 PT_WAIT_THREAD(&s
->scriptpt
, send_part_of_file(s
));
177 s
->file
.data
+= s
->len
;
178 s
->file
.len
-= s
->len
;
182 PT_END(&s
->scriptpt
);
184 /*---------------------------------------------------------------------------*/
186 PT_THREAD(send_headers(struct httpd_state
*s
, const char *statushdr
))
190 PSOCK_BEGIN(&s
->sout
);
192 SEND_STRING(&s
->sout
, statushdr
);
194 ptr
= strrchr(s
->filename
, ISO_period
);
196 ptr
= http_content_type_binary
;
197 } else if(strncmp(http_html
, ptr
, 5) == 0 ||
198 strncmp(http_shtml
, ptr
, 6) == 0) {
199 ptr
= http_content_type_html
;
200 } else if(strncmp(http_css
, ptr
, 4) == 0) {
201 ptr
= http_content_type_css
;
202 } else if(strncmp(http_png
, ptr
, 4) == 0) {
203 ptr
= http_content_type_png
;
204 } else if(strncmp(http_gif
, ptr
, 4) == 0) {
205 ptr
= http_content_type_gif
;
206 } else if(strncmp(http_jpg
, ptr
, 4) == 0) {
207 ptr
= http_content_type_jpg
;
209 ptr
= http_content_type_plain
;
211 SEND_STRING(&s
->sout
, ptr
);
214 /*---------------------------------------------------------------------------*/
216 PT_THREAD(handle_output(struct httpd_state
*s
))
220 PT_BEGIN(&s
->outputpt
);
222 if(!httpd_fs_open(s
->filename
, &s
->file
)) {
223 strcpy(s
->filename
, http_404_html
);
224 httpd_fs_open(s
->filename
, &s
->file
);
225 PT_WAIT_THREAD(&s
->outputpt
,
228 PT_WAIT_THREAD(&s
->outputpt
,
231 PT_WAIT_THREAD(&s
->outputpt
,
234 ptr
= strrchr(s
->filename
, ISO_period
);
235 if(ptr
!= NULL
&& strncmp(ptr
, http_shtml
, 6) == 0) {
236 PT_INIT(&s
->scriptpt
);
237 PT_WAIT_THREAD(&s
->outputpt
, handle_script(s
));
239 PT_WAIT_THREAD(&s
->outputpt
,
243 PSOCK_CLOSE(&s
->sout
);
244 PT_END(&s
->outputpt
);
246 /*---------------------------------------------------------------------------*/
248 PT_THREAD(handle_input(struct httpd_state
*s
))
250 PSOCK_BEGIN(&s
->sin
);
252 PSOCK_READTO(&s
->sin
, ISO_space
);
254 if(strncmp(s
->inputbuf
, http_get
, 4) != 0) {
255 PSOCK_CLOSE_EXIT(&s
->sin
);
257 PSOCK_READTO(&s
->sin
, ISO_space
);
259 if(s
->inputbuf
[0] != ISO_slash
) {
260 PSOCK_CLOSE_EXIT(&s
->sin
);
263 if(s
->inputbuf
[1] == ISO_space
) {
264 strncpy(s
->filename
, http_index_html
, sizeof(s
->filename
));
266 s
->inputbuf
[PSOCK_DATALEN(&s
->sin
) - 1] = 0;
267 strncpy(s
->filename
, &s
->inputbuf
[0], sizeof(s
->filename
));
270 webserver_log_file(&uip_conn
->ripaddr
, s
->filename
);
272 s
->state
= STATE_OUTPUT
;
275 PSOCK_READTO(&s
->sin
, ISO_nl
);
277 if(strncmp(s
->inputbuf
, http_referer
, 8) == 0) {
278 s
->inputbuf
[PSOCK_DATALEN(&s
->sin
) - 2] = 0;
279 petsciiconv_topetscii(s
->inputbuf
, PSOCK_DATALEN(&s
->sin
) - 2);
280 webserver_log(s
->inputbuf
);
286 /*---------------------------------------------------------------------------*/
288 handle_connection(struct httpd_state
*s
)
291 if(s
->state
== STATE_OUTPUT
) {
295 /*---------------------------------------------------------------------------*/
297 httpd_appcall(void *state
)
299 struct httpd_state
*s
= (struct httpd_state
*)state
;
301 if(uip_closed() || uip_aborted() || uip_timedout()) {
303 memb_free(&conns
, s
);
305 } else if(uip_connected()) {
306 s
= (struct httpd_state
*)memb_alloc(&conns
);
311 tcp_markconn(uip_conn
, s
);
312 PSOCK_INIT(&s
->sin
, (uint8_t *)s
->inputbuf
, sizeof(s
->inputbuf
) - 1);
313 PSOCK_INIT(&s
->sout
, (uint8_t *)s
->inputbuf
, sizeof(s
->inputbuf
) - 1);
314 PT_INIT(&s
->outputpt
);
315 s
->state
= STATE_WAITING
;
316 /* timer_set(&s->timer, CLOCK_SECOND * 100);*/
318 handle_connection(s
);
319 } else if(s
!= NULL
) {
324 memb_free(&conns
, s
);
329 handle_connection(s
);
334 /*---------------------------------------------------------------------------*/
338 tcp_listen(HTONS(80));
343 /*---------------------------------------------------------------------------*/
345 httpd_sprint_ip6(uip_ip6addr_t addr
, char * result
)
348 unsigned char zerocnt
= 0;
349 unsigned char numprinted
= 0;
350 char * starting
= result
;
353 while (numprinted
< 8) {
354 if ((addr
.u16
[i
] == 0) && (zerocnt
== 0)) {
355 while(addr
.u16
[zerocnt
+ i
] == 0) zerocnt
++;
362 numprinted
+= zerocnt
;
364 result
+= sprintf(result
, "%x", (unsigned int)(ntohs(addr
.u16
[i
])));
368 if (numprinted
!= 8) *result
++ = ':';
372 return (result
- starting
);
374 #endif /* UIP_CONF_IPV6 */
375 /*---------------------------------------------------------------------------*/