Contiki 2.5
httpd.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  * $Id: httpd.c,v 1.2 2010/10/19 18:29:05 adamdunkels Exp $
34  */
35 
36 #include <string.h>
37 
38 #include "contiki-net.h"
39 
40 #include "webserver-nogui.h"
41 #include "httpd-fs.h"
42 #include "httpd-cgi.h"
43 #include "http-strings.h"
44 
45 #include "httpd.h"
46 #include "ctk/libconio_arch-small.h"
47 
48 #define STATE_WAITING 0
49 #define STATE_OUTPUT 1
50 
51 #define SEND_STRING(s, str) PSOCK_SEND(s, str, (unsigned int)strlen(str))
52 MEMB(conns, struct httpd_state, 4);
53 
54 #define ISO_nl 0x0a
55 #define ISO_space 0x20
56 #define ISO_bang 0x21
57 #define ISO_percent 0x25
58 #define ISO_period 0x2e
59 #define ISO_slash 0x2f
60 #define ISO_colon 0x3a
61 
62 /*---------------------------------------------------------------------------*/
63 static unsigned short
64 generate(void *state)
65 {
66  struct httpd_state *s = (struct httpd_state *)state;
67 
68  if(s->file.len > uip_mss()) {
69  s->len = uip_mss();
70  } else {
71  s->len = s->file.len;
72  }
73  memcpy(uip_appdata, s->file.data, s->len);
74 
75  return s->len;
76 }
77 /*---------------------------------------------------------------------------*/
78 static
79 PT_THREAD(send_file(struct httpd_state *s))
80 {
81  PSOCK_BEGIN(&s->sout);
82 
83  do {
84  PSOCK_GENERATOR_SEND(&s->sout, generate, s);
85  s->file.len -= s->len;
86  s->file.data += s->len;
87  } while(s->file.len > 0);
88 
89  PSOCK_END(&s->sout);
90 }
91 /*---------------------------------------------------------------------------*/
92 static
93 PT_THREAD(send_part_of_file(struct httpd_state *s))
94 {
95  PSOCK_BEGIN(&s->sout);
96 
97  PSOCK_SEND(&s->sout, s->file.data, s->len);
98 
99  PSOCK_END(&s->sout);
100 }
101 /*---------------------------------------------------------------------------*/
102 #if HTTPD_CONF_SCRIPT
103 static void
104 next_scriptstate(struct httpd_state *s)
105 {
106  char *p;
107 
108  if((p = strchr(s->scriptptr, ISO_nl)) != NULL) {
109  p += 1;
110  s->scriptlen -= (unsigned short)(p - s->scriptptr);
111  s->scriptptr = p;
112  } else {
113  s->scriptlen = 0;
114  }
115  /* char *p;
116  p = strchr(s->scriptptr, ISO_nl) + 1;
117  s->scriptlen -= (unsigned short)(p - s->scriptptr);
118  s->scriptptr = p;*/
119 }
120 /*---------------------------------------------------------------------------*/
121 static
122 PT_THREAD(handle_script(struct httpd_state *s))
123 {
124  char *ptr;
125 
126  PT_BEGIN(&s->scriptpt);
127 
128  while(s->file.len > 0) {
129 
130  /* Check if we should start executing a script. */
131  if(*s->file.data == ISO_percent &&
132  *(s->file.data + 1) == ISO_bang) {
133  s->scriptptr = s->file.data + 3;
134  s->scriptlen = s->file.len - 3;
135  if(*(s->scriptptr - 1) == ISO_colon) {
136  httpd_fs_open(s->scriptptr + 1, &s->file);
137  PT_WAIT_THREAD(&s->scriptpt, send_file(s));
138 #if HTTPD_CONF_CGI
139  } else {
140  PT_WAIT_THREAD(&s->scriptpt,
141  httpd_cgi(s->scriptptr)(s, s->scriptptr));
142 #endif /* HTTPD_CONF_CGI */
143  }
144  next_scriptstate(s);
145 
146  /* The script is over, so we reset the pointers and continue
147  sending the rest of the file. */
148  s->file.data = s->scriptptr;
149  s->file.len = s->scriptlen;
150  } else {
151  /* See if we find the start of script marker in the block of HTML
152  to be sent. */
153 
154  if(s->file.len > uip_mss()) {
155  s->len = uip_mss();
156  } else {
157  s->len = s->file.len;
158  }
159 
160  if(*s->file.data == ISO_percent) {
161  ptr = strchr(s->file.data + 1, ISO_percent);
162  } else {
163  ptr = strchr(s->file.data, ISO_percent);
164  }
165  if(ptr != NULL &&
166  ptr != s->file.data) {
167  s->len = (int)(ptr - s->file.data);
168  if(s->len >= uip_mss()) {
169  s->len = uip_mss();
170  }
171  }
172  PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s));
173  s->file.data += s->len;
174  s->file.len -= s->len;
175  }
176  }
177 
178  PT_END(&s->scriptpt);
179 }
180 #endif /* HTTPD_CONF_SCRIPT */
181 /*---------------------------------------------------------------------------*/
182 static
183 PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
184 {
185  char *ptr;
186 
187  PSOCK_BEGIN(&s->sout);
188 
189  SEND_STRING(&s->sout, statushdr);
190 
191  ptr = strrchr(s->filename, ISO_period);
192  if(ptr == NULL) {
193  SEND_STRING(&s->sout, http_content_type_binary);
194  } else if(strncmp(http_html, ptr, 5) == 0 ||
195  strncmp(http_shtml, ptr, 6) == 0) {
196  SEND_STRING(&s->sout, http_content_type_html);
197 #if 0
198  } else if(strncmp(http_css, ptr, 4) == 0) {
199  SEND_STRING(&s->sout, http_content_type_css);
200  } else if(strncmp(http_png, ptr, 4) == 0) {
201  SEND_STRING(&s->sout, http_content_type_png);
202  } else if(strncmp(http_gif, ptr, 4) == 0) {
203  SEND_STRING(&s->sout, http_content_type_gif);
204  } else if(strncmp(http_jpg, ptr, 4) == 0) {
205  SEND_STRING(&s->sout, http_content_type_jpg);
206 #endif
207  } else {
208  SEND_STRING(&s->sout, http_content_type_plain);
209  }
210  PSOCK_END(&s->sout);
211 }
212 /*---------------------------------------------------------------------------*/
213 static
214 PT_THREAD(handle_output(struct httpd_state *s))
215 {
216  char *ptr;
217 
218  PT_BEGIN(&s->outputpt);
219 
220  if(!httpd_fs_open(s->filename, &s->file)) {
221  httpd_fs_open(http_404_html, &s->file);
222  PT_WAIT_THREAD(&s->outputpt,
223  send_headers(s,
224  http_header_404));
225  PT_WAIT_THREAD(&s->outputpt,
226  send_file(s));
227  } else {
228  PT_WAIT_THREAD(&s->outputpt,
229  send_headers(s,
230  http_header_200));
231  ptr = strchr(s->filename, ISO_period);
232 #if HTTPD_CONF_SCRIPT
233  if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0) {
234  PT_INIT(&s->scriptpt);
235  PT_WAIT_THREAD(&s->outputpt, handle_script(s));
236  } else {
237  PT_WAIT_THREAD(&s->outputpt,
238  send_file(s));
239  }
240 #else /* HTTPD_CONF_SCRIPT */
241  PT_WAIT_THREAD(&s->outputpt,
242  send_file(s));
243 #endif /* HTTPD_CONF_SCRIPT */
244  }
245  PSOCK_CLOSE(&s->sout);
246  PT_END(&s->outputpt);
247 }
248 /*---------------------------------------------------------------------------*/
249 static
250 PT_THREAD(handle_input(struct httpd_state *s))
251 {
252  PSOCK_BEGIN(&s->sin);
253 
254  PSOCK_READTO(&s->sin, ISO_space);
255 
256  if(strncmp(s->inputbuf, http_get, 4) != 0) {
257  PSOCK_CLOSE_EXIT(&s->sin);
258  }
259  PSOCK_READTO(&s->sin, ISO_space);
260 
261  if(s->inputbuf[0] != ISO_slash) {
262  PSOCK_CLOSE_EXIT(&s->sin);
263  }
264 
265  if(s->inputbuf[1] == ISO_space) {
266  strncpy(s->filename, http_index_html, sizeof(s->filename));
267  } else {
268  s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
269  strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename));
270  }
271 
272  libputs_arch(s->filename);
273 
274  s->state = STATE_OUTPUT;
275 
276  while(1) {
277  PSOCK_READTO(&s->sin, ISO_nl);
278 
279  if(strncmp(s->inputbuf, http_referer, 8) == 0) {
280  s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
281  }
282  }
283 
284  PSOCK_END(&s->sin);
285 }
286 /*---------------------------------------------------------------------------*/
287 static void
288 handle_connection(struct httpd_state *s)
289 {
290  handle_input(s);
291  if(s->state == STATE_OUTPUT) {
292  handle_output(s);
293  }
294 }
295 /*---------------------------------------------------------------------------*/
296 void
297 httpd_appcall(void *state)
298 {
299  struct httpd_state *s = (struct httpd_state *)state;
300 
301  if(uip_closed() || uip_aborted() || uip_timedout()) {
302  if(s != NULL) {
303  memb_free(&conns, s);
304  }
305  } else if(uip_connected()) {
306  s = (struct httpd_state *)memb_alloc(&conns);
307  if(s == NULL) {
308  uip_abort();
309  return;
310  }
311  tcp_markconn(uip_conn, s);
312  PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
313  PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
314  PT_INIT(&s->outputpt);
315  s->state = STATE_WAITING;
316  /* timer_set(&s->timer, CLOCK_SECOND * 100);*/
317  s->timer = 0;
318  handle_connection(s);
319  } else if(s != NULL) {
320  if(uip_poll()) {
321  ++s->timer;
322  if(s->timer >= 20) {
323  uip_abort();
324  memb_free(&conns, s);
325  }
326  } else {
327  s->timer = 0;
328  }
329  handle_connection(s);
330  } else {
331  uip_abort();
332  }
333 }
334 /*---------------------------------------------------------------------------*/
335 void
336 httpd_init(void)
337 {
338  tcp_listen(UIP_HTONS(80));
339  memb_init(&conns);
340 #if HTTPD_CONF_CGI
341  httpd_cgi_init();
342 #endif /* HTTPD_CONF_CGI */
343 }
344 /*---------------------------------------------------------------------------*/