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.6 2010/12/20 20:06:06 dak664 Exp $
34  */
35 
36 #include <string.h>
37 
38 #include "contiki-net.h"
39 
40 #include "webserver.h"
41 #include "httpd-fs.h"
42 #include "httpd-cgi.h"
43 #include "httpd.h"
44 #include "raven-lcd.h"
45 
46 //#include "http-strings.h"
47 #if COFFEE_FILES
48 #include "cfs-coffee-arch.h"
49 #endif /* COFFEE_FILES */
50 
51 /* DEBUGLOGIC is a convenient way to debug in a simulator without a tcp/ip connection.
52  * Break the program in the process loop and step from the entry in httpd_appcall.
53  * The input file is forced to /index.html and the output directed to TCPBUF.
54  * If cgi's are invoked define it in httpd-cgi.c as well!
55  * psock_generator_send in /core/net/psock.c must also be modified as follows:
56  * ...
57  * // Wait until all data is sent and acknowledged.
58  * if (!s->sendlen) break; //<---add this line
59  * PT_YIELD_UNTIL(&s->psockpt, uip_acked() || uip_rexmit());
60  * ...
61  */
62 #define DEBUGLOGIC 0
63 #define DEBUG 0
64 #if DEBUGLOGIC
65 struct httpd_state *sg;
66 #define uip_mss(...) 512
67 #define uip_appdata TCPBUF
68 char TCPBUF[512];
69 #endif
70 #if DEBUG
71 #include <stdio.h>
72 #if HTTPD_STRING_TYPE==PROGMEM_TYPE
73 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
74 #else
75 #define PRINTF printf
76 #endif
77 #else
78 #define PRINTF(...)
79 #endif
80 
81 #ifndef WEBSERVER_CONF_CGI_CONNS
82 #define CONNS 4
83 #else
84 #define CONNS WEBSERVER_CONF_CGI_CONNS
85 #endif /* WEBSERVER_CONF_CGI_CONNS */
86 
87 #define STATE_WAITING 0
88 #define STATE_OUTPUT 1
89 
90 //#define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, (unsigned int)strlen(str))
91 MEMB(conns, struct httpd_state, CONNS);
92 
93 /* MAX_SCRIPT_NAME_LENGTH should be at least be the maximum file name length+2 for %!: includes */
94 #define MAX_SCRIPT_NAME_LENGTH 20
95 #define ISO_tab 0x09
96 #define ISO_nl 0x0a
97 #define ISO_cr 0x0d
98 #define ISO_space 0x20
99 #define ISO_bang 0x21
100 #define ISO_percent 0x25
101 #define ISO_period 0x2e
102 #define ISO_slash 0x2f
103 #define ISO_colon 0x3a
104 
105 /*---------------------------------------------------------------------------*/
106 static unsigned short
107 generate(void *state)
108 {
109  struct httpd_state *s = (struct httpd_state *)state;
110 
111  if(s->file.len > uip_mss()) {
112  s->len = uip_mss();
113  } else {
114  s->len = s->file.len;
115  }
116  httpd_fs_cpy(uip_appdata, s->file.data, s->len);
117 #if DEBUGLOGIC
118  return 0;
119 #else
120  return s->len;
121 #endif
122 }
123 /*---------------------------------------------------------------------------*/
124 static
125 PT_THREAD(send_file(struct httpd_state *s))
126 {
127  PSOCK_BEGIN(&s->sout);
128 
129  do {
130  PSOCK_GENERATOR_SEND(&s->sout, generate, s);
131  s->file.len -= s->len;
132  s->file.data += s->len;
133  } while(s->file.len > 0);
134 
135  PSOCK_END(&s->sout);
136 }
137 /*---------------------------------------------------------------------------*/
138 static
139 PT_THREAD(send_part_of_file(struct httpd_state *s))
140 {
141  PSOCK_BEGIN(&s->sout);
142 
143  static int oldfilelen, oldlen;
144  static char * olddata;
145 
146  //Store stuff that gets clobbered...
147  oldfilelen = s->file.len;
148  oldlen = s->len;
149  olddata = s->file.data;
150 
151  //How much to send
152  s->file.len = s->len;
153 
154  do {
155  PSOCK_GENERATOR_SEND(&s->sout, generate, s);
156  s->file.len -= s->len;
157  s->file.data += s->len;
158  } while(s->file.len > 0);
159 
160  s->len = oldlen;
161  s->file.len = oldfilelen;
162  s->file.data = olddata;
163 
164  PSOCK_END(&s->sout);
165 }
166 /*---------------------------------------------------------------------------*/
167 static void
168 next_scriptstate(struct httpd_state *s)
169 {
170  char *p;
171 /* Skip over any script parameters to the beginning of the next line */
172  if((p = (char *)httpd_fs_strchr(s->scriptptr, ISO_nl)) != NULL) {
173  p += 1;
174  s->scriptlen -= (unsigned short)(p - s->scriptptr);
175  s->scriptptr = p;
176  } else {
177  s->scriptlen = 0;
178  }
179 
180  /* char *p;
181  p = strchr(s->scriptptr, ISO_nl) + 1;
182  s->scriptlen -= (unsigned short)(p - s->scriptptr);
183  s->scriptptr = p;*/
184 }
185 
186 /*---------------------------------------------------------------------------*/
187 char *
188 get_scriptname(char *dest, char *fromfile)
189 {
190  uint8_t i=0,skip=1;
191  /* Extract a file or cgi name, trim leading spaces and replace termination with zero */
192  /* Returns number of characters processed up to the next non-tab or space */
193  do {
194  dest[i]=httpd_fs_getchar(fromfile++);
195  if (dest[i]==ISO_colon) {if (!skip) break;} //allow leading colons
196  else if (dest[i]==ISO_tab ) {if (skip) continue;else break;}//skip leading tabs
197  else if (dest[i]==ISO_space) {if (skip) continue;else break;}//skip leading spaces
198  else if (dest[i]==ISO_nl ) break; //nl is preferred delimiter
199  else if (dest[i]==ISO_cr ) break; //some editors insert cr
200  else if (dest[i]==0 ) break; //files are terminated with null
201  else skip=0;
202  i++;
203  } while (i<(MAX_SCRIPT_NAME_LENGTH+1));
204  fromfile--;
205  while ((dest[i]==ISO_space) || (dest[i]==ISO_tab)) dest[i]=httpd_fs_getchar(++fromfile);
206  dest[i]=0;
207  return (fromfile);
208 }
209 /*---------------------------------------------------------------------------*/
210 
211 static
212 PT_THREAD(handle_script(struct httpd_state *s))
213 {
214  /* Note script includes will attach a leading : to the filename and a trailing zero */
215  static char scriptname[MAX_SCRIPT_NAME_LENGTH+1],*pptr;
216  static uint16_t filelength;
217 
218  PT_BEGIN(&s->scriptpt);
219 
220  filelength=s->file.len;
221  while(s->file.len > 0) {
222  /* Sanity check */
223  if (s->file.len > filelength) break;
224 
225  /* Check if we should start executing a script, flagged by %! */
226  if(httpd_fs_getchar(s->file.data) == ISO_percent &&
227  httpd_fs_getchar(s->file.data + 1) == ISO_bang) {
228 
229  /* Extract name, if starts with colon include file else call cgi */
230  s->scriptptr=get_scriptname(scriptname,s->file.data+2);
231  s->scriptlen=s->file.len-(s->scriptptr-s->file.data);
232  PRINTF("httpd: Handle script named %s\n",scriptname);
233  if(scriptname[0] == ISO_colon) {
234  if (httpd_fs_open(&scriptname[1], &s->file)) {
235  PT_WAIT_THREAD(&s->scriptpt, send_file(s));
236  }
237  } else {
238  PT_WAIT_THREAD(&s->scriptpt,httpd_cgi(scriptname)(s, s->scriptptr));
239  }
240  next_scriptstate(s);
241 
242  /* Reset the pointers and continue sending the current file. */
243  s->file.data = s->scriptptr;
244  s->file.len = s->scriptlen;
245  } else {
246 
247  /* Send file up to the next potential script */
248  if(s->file.len > uip_mss()) {
249  s->len = uip_mss();
250  } else {
251  s->len = s->file.len;
252  }
253 
254  if(httpd_fs_getchar(s->file.data) == ISO_percent) {
255  pptr = (char *) httpd_fs_strchr(s->file.data + 1, ISO_percent);
256  } else {
257  pptr = (char *) httpd_fs_strchr(s->file.data, ISO_percent);
258  }
259 
260  if(pptr != NULL && pptr != s->file.data) {
261  s->len = (int)(pptr - s->file.data);
262  if(s->len >= uip_mss()) {
263  s->len = uip_mss();
264  }
265  }
266  PRINTF("httpd: Sending %u bytes from 0x%04x\n",s->file.len,(unsigned int)s->file.data);
267  PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s));
268  s->file.data += s->len;
269  s->file.len -= s->len;
270  }
271  }
272 
273  PT_END(&s->scriptpt);
274 }
275 /*---------------------------------------------------------------------------*/
276 const char httpd_http[] HTTPD_STRING_ATTR = "HTTP/1.0 ";
277 const char httpd_server[] HTTPD_STRING_ATTR = "\r\nServer: Contiki/2.0 http://www.sics.se/contiki/\r\nConnection: close\r\n";
278 static unsigned short
279 generate_status(void *sstr)
280 {
281  uint8_t slen=httpd_strlen((char *)sstr);
282  httpd_memcpy(uip_appdata, httpd_http, sizeof(httpd_http)-1);
283  httpd_memcpy(uip_appdata+sizeof(httpd_http)-1, (char *)sstr, slen);
284  slen+=sizeof(httpd_http)-1;
285  httpd_memcpy(uip_appdata+slen, httpd_server, sizeof(httpd_server)-1);
286 #if DEBUGLOGIC
287  return 0;
288 #else
289  return slen+sizeof(httpd_server)-1;
290 #endif
291 }
292 /*---------------------------------------------------------------------------*/
293 const char httpd_content[] HTTPD_STRING_ATTR = "Content-type: ";
294 const char httpd_crlf[] HTTPD_STRING_ATTR = "\r\n\r\n";
295 static unsigned short
296 generate_header(void *hstr)
297 {
298  uint8_t slen=httpd_strlen((char *)hstr);
299  httpd_memcpy(uip_appdata,httpd_content,sizeof(httpd_content)-1);
300  httpd_memcpy(uip_appdata+sizeof(httpd_content)-1, (char *)hstr, slen);
301  slen+=sizeof(httpd_content)-1;
302  httpd_memcpy(uip_appdata+slen,httpd_crlf,sizeof(httpd_crlf)-1);
303 #if DEBUGLOGIC
304  return 0;
305 #else
306  return slen+4;
307 #endif
308 }
309 /*---------------------------------------------------------------------------*/
310 char http_htm[10] PROGMEM ="text/html";
311 char http_css[ 9] PROGMEM ="text/css";
312 const char httpd_mime_htm[] HTTPD_STRING_ATTR = "text/html";
313 const char httpd_mime_css[] HTTPD_STRING_ATTR = "text/css";
314 const char httpd_mime_png[] HTTPD_STRING_ATTR = "image/png";
315 const char httpd_mime_gif[] HTTPD_STRING_ATTR = "image/gif";
316 const char httpd_mime_jpg[] HTTPD_STRING_ATTR = "image/jpeg";
317 const char httpd_mime_txt[] HTTPD_STRING_ATTR = "text/plain";
318 const char httpd_mime_bin[] HTTPD_STRING_ATTR = "application/octet-stream";
319 const char httpd_jpg [] HTTPD_STRING_ATTR = "jpg";
320 const char httpd_shtml [] HTTPD_STRING_ATTR = ".shtml";
321 
322 static
323 PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
324 {
325  char *ptr;
326  PSOCK_BEGIN(&s->sout);
327 
328  PSOCK_GENERATOR_SEND(&s->sout, generate_status, (char *)statushdr);
329 
330  ptr = strrchr(s->filename, ISO_period);
331  if (httpd_strncmp("4", statushdr, 1)==0) {
332  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm );
333  } else if(ptr == NULL) {
334  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_bin );
335  } else {
336  ptr++;
337  if(httpd_strncmp(ptr, &httpd_mime_htm[5],3)== 0 ||httpd_strncmp(ptr, &httpd_shtml[1], 4) == 0) {
338  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm );
339  } else if(httpd_strcmp(ptr, &httpd_mime_css[5]) == 0) {
340  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_css );
341  } else if(httpd_strcmp(ptr, &httpd_mime_png[6]) == 0) {
342  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_png );
343  } else if(httpd_strcmp(ptr, &httpd_mime_gif[6])== 0) {
344  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_gif );
345  } else if(httpd_strcmp(ptr, httpd_mime_jpg) == 0) {
346  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_jpg );
347  } else {
348  PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_txt);
349  }
350  }
351  PSOCK_END(&s->sout);
352 }
353 /*---------------------------------------------------------------------------*/
354 const char httpd_indexfn [] HTTPD_STRING_ATTR = "/index.html";
355 const char httpd_404fn [] HTTPD_STRING_ATTR = "/404.html";
356 const char httpd_404notf [] HTTPD_STRING_ATTR = "404 Not found";
357 const char httpd_200ok [] HTTPD_STRING_ATTR = "200 OK";
358 static
359 PT_THREAD(handle_output(struct httpd_state *s))
360 {
361  char *ptr;
362 
363  PT_BEGIN(&s->outputpt);
364 #if DEBUGLOGIC
365  httpd_strcpy(s->filename,httpd_indexfn);
366 #endif
367  if(!httpd_fs_open(s->filename, &s->file)) {
368  httpd_strcpy(s->filename, httpd_404fn);
369  httpd_fs_open(s->filename, &s->file);
370  PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_404notf));
371  PT_WAIT_THREAD(&s->outputpt, send_file(s));
372  } else {
373  PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_200ok));
374  ptr = strchr(s->filename, ISO_period);
375  if((ptr != NULL && httpd_strncmp(ptr, httpd_shtml, 6) == 0) || httpd_strcmp(s->filename,httpd_indexfn)==0) {
376  PT_INIT(&s->scriptpt);
377  PT_WAIT_THREAD(&s->outputpt, handle_script(s));
378  } else {
379  PT_WAIT_THREAD(&s->outputpt, send_file(s));
380  }
381  }
382  PSOCK_CLOSE(&s->sout);
383  PT_END(&s->outputpt);
384 }
385 /*---------------------------------------------------------------------------*/
386 const char httpd_get[] HTTPD_STRING_ATTR = "GET ";
387 const char httpd_ref[] HTTPD_STRING_ATTR = "Referer:";
388 static
389 PT_THREAD(handle_input(struct httpd_state *s))
390 {
391 
392  PSOCK_BEGIN(&s->sin);
393 
394  PSOCK_READTO(&s->sin, ISO_space);
395 
396  if(httpd_strncmp(s->inputbuf, httpd_get, 4) != 0) {
397  PSOCK_CLOSE_EXIT(&s->sin);
398  }
399  PSOCK_READTO(&s->sin, ISO_space);
400 
401  if(s->inputbuf[0] != ISO_slash) {
402  PSOCK_CLOSE_EXIT(&s->sin);
403  }
404 
405  if(s->inputbuf[1] == ISO_space) {
406  httpd_strcpy(s->filename, httpd_indexfn);
407  } else {
408  s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
409  strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename));
410 {
411  /* Look for ?, if found strip file name and send any following text to the LCD */
412  uint8_t i;
413  for (i=0;i<sizeof(s->inputbuf);i++) {
414  if (s->inputbuf[i]=='?') {
415  raven_lcd_show_text(&s->inputbuf[i]);
416  if (i<sizeof(s->filename)) s->filename[i]=0;
417  // s->inputbuf[i]=0; //allow multiple beeps with multiple ?'s
418  }
419  if (s->inputbuf[i]==0) break;
420  }
421 }
422  }
423 
424  webserver_log_file(&uip_conn->ripaddr, s->filename);
425 
426  s->state = STATE_OUTPUT;
427 
428  while(1) {
429  PSOCK_READTO(&s->sin, ISO_nl);
430 
431  if(httpd_strncmp(s->inputbuf, httpd_ref, 8) == 0) {
432  s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
433  petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2);
434  webserver_log(s->inputbuf);
435  }
436  }
437  PSOCK_END(&s->sin);
438 }
439 /*---------------------------------------------------------------------------*/
440 static void
441 handle_connection(struct httpd_state *s)
442 {
443 #if DEBUGLOGIC
444  handle_output(s);
445 #else
446  handle_input(s);
447  if(s->state == STATE_OUTPUT) {
448  handle_output(s);
449  }
450 #endif
451 }
452 /*---------------------------------------------------------------------------*/
453 void
454 httpd_appcall(void *state)
455 {
456 #if DEBUGLOGIC
457  struct httpd_state *s; //Enter here for debugging with output directed to TCPBUF
458  s = sg = (struct httpd_state *)memb_alloc(&conns);
459  if (1) {
460 #else
461  struct httpd_state *s = (struct httpd_state *)state;
462  if(uip_closed() || uip_aborted() || uip_timedout()) {
463  if(s != NULL) {
464  memb_free(&conns, s);
465  }
466  } else if(uip_connected()) {
467  s = (struct httpd_state *)memb_alloc(&conns);
468  if(s == NULL) {
469  uip_abort();
470  return;
471  }
472 #endif
473  tcp_markconn(uip_conn, s);
474  PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
475  PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
476  PT_INIT(&s->outputpt);
477  s->state = STATE_WAITING;
478  /* timer_set(&s->timer, CLOCK_SECOND * 100);*/
479  s->timer = 0;
480  handle_connection(s);
481  } else if(s != NULL) {
482  if(uip_poll()) {
483  ++s->timer;
484  if(s->timer >= 20) {
485  uip_abort();
486  memb_free(&conns, s);
487  }
488  } else {
489  s->timer = 0;
490  }
491  handle_connection(s);
492  } else {
493  uip_abort();
494  }
495 }
496 /*---------------------------------------------------------------------------*/
497 void
498 httpd_init(void)
499 {
500  tcp_listen(UIP_HTONS(80));
501  memb_init(&conns);
502  httpd_cgi_init();
503 }
504 /*---------------------------------------------------------------------------*/