Contiki 2.5
httpd-cfs.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-cfs.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.h"
41 #include "cfs/cfs.h"
42 #include "lib/petsciiconv.h"
43 #include "http-strings.h"
44 
45 #include "httpd-cfs.h"
46 
47 #ifndef WEBSERVER_CONF_CFS_CONNS
48 #define CONNS 4
49 #else /* WEBSERVER_CONF_CFS_CONNS */
50 #define CONNS WEBSERVER_CONF_CFS_CONNS
51 #endif /* WEBSERVER_CONF_CFS_CONNS */
52 
53 #define STATE_WAITING 0
54 #define STATE_OUTPUT 1
55 
56 #define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, strlen(str))
57 MEMB(conns, struct httpd_state, CONNS);
58 
59 #define ISO_nl 0x0a
60 #define ISO_space 0x20
61 #define ISO_period 0x2e
62 #define ISO_slash 0x2f
63 
64 /*---------------------------------------------------------------------------*/
65 static
66 PT_THREAD(send_file(struct httpd_state *s))
67 {
68  PSOCK_BEGIN(&s->sout);
69 
70  do {
71  /* Read data from file system into buffer */
72  s->len = cfs_read(s->fd, s->outputbuf, sizeof(s->outputbuf));
73 
74  /* If there is data in the buffer, send it */
75  if(s->len > 0) {
76  PSOCK_SEND(&s->sout, (uint8_t *)s->outputbuf, s->len);
77  } else {
78  break;
79  }
80  } while(s->len > 0);
81 
82  PSOCK_END(&s->sout);
83 }
84 /*---------------------------------------------------------------------------*/
85 static
86 PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
87 {
88  char *ptr;
89 
90  PSOCK_BEGIN(&s->sout);
91 
92  SEND_STRING(&s->sout, statushdr);
93 
94  ptr = strrchr(s->filename, ISO_period);
95  if(ptr == NULL) {
96  SEND_STRING(&s->sout, http_content_type_plain);
97  } else if(strncmp(http_html, ptr, 5) == 0) {
98  SEND_STRING(&s->sout, http_content_type_html);
99  } else if(strncmp(http_css, ptr, 4) == 0) {
100  SEND_STRING(&s->sout, http_content_type_css);
101  } else if(strncmp(http_png, ptr, 4) == 0) {
102  SEND_STRING(&s->sout, http_content_type_png);
103  } else if(strncmp(http_jpg, ptr, 4) == 0) {
104  SEND_STRING(&s->sout, http_content_type_jpg);
105  } else {
106  SEND_STRING(&s->sout, http_content_type_binary);
107  }
108  PSOCK_END(&s->sout);
109 }
110 /*---------------------------------------------------------------------------*/
111 static
112 PT_THREAD(handle_output(struct httpd_state *s))
113 {
114  PT_BEGIN(&s->outputpt);
115 
116  petsciiconv_topetscii(s->filename, sizeof(s->filename));
117  s->fd = cfs_open(s->filename, CFS_READ);
118  petsciiconv_toascii(s->filename, sizeof(s->filename));
119  if(s->fd < 0) {
120  s->fd = cfs_open("notfound.html", CFS_READ);
121  if(s->fd < 0) {
122  uip_abort();
123  memb_free(&conns, s);
124  webserver_log_file(&uip_conn->ripaddr, "reset (no notfound.html)");
125  PT_EXIT(&s->outputpt);
126  }
127  PT_WAIT_THREAD(&s->outputpt,
128  send_headers(s, http_header_404));
129  } else {
130  PT_WAIT_THREAD(&s->outputpt,
131  send_headers(s, http_header_200));
132  }
133  PT_WAIT_THREAD(&s->outputpt, send_file(s));
134  cfs_close(s->fd);
135  s->fd = -1;
136  PSOCK_CLOSE(&s->sout);
137  PT_END(&s->outputpt);
138 }
139 /*---------------------------------------------------------------------------*/
140 static
141 PT_THREAD(handle_input(struct httpd_state *s))
142 {
143  PSOCK_BEGIN(&s->sin);
144 
145  PSOCK_READTO(&s->sin, ISO_space);
146 
147  if(strncmp(s->inputbuf, http_get, 4) != 0) {
148  PSOCK_CLOSE_EXIT(&s->sin);
149  }
150  PSOCK_READTO(&s->sin, ISO_space);
151 
152  if(s->inputbuf[0] != ISO_slash) {
153  PSOCK_CLOSE_EXIT(&s->sin);
154  }
155 
156  if(s->inputbuf[1] == ISO_space) {
157  strncpy(s->filename, &http_index_html[1], sizeof(s->filename));
158  } else {
159  s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
160  strncpy(s->filename, &s->inputbuf[1], sizeof(s->filename));
161  }
162 
163  petsciiconv_topetscii(s->filename, sizeof(s->filename));
164  webserver_log_file(&uip_conn->ripaddr, s->filename);
165  petsciiconv_toascii(s->filename, sizeof(s->filename));
166  s->state = STATE_OUTPUT;
167 
168  while(1) {
169  PSOCK_READTO(&s->sin, ISO_nl);
170 
171  if(strncmp(s->inputbuf, http_referer, 8) == 0) {
172  s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
173  petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2);
174  webserver_log(s->inputbuf);
175  }
176  }
177 
178  PSOCK_END(&s->sin);
179 }
180 /*---------------------------------------------------------------------------*/
181 static void
182 handle_connection(struct httpd_state *s)
183 {
184  handle_input(s);
185  if(s->state == STATE_OUTPUT) {
186  handle_output(s);
187  }
188 }
189 /*---------------------------------------------------------------------------*/
190 void
191 httpd_appcall(void *state)
192 {
193  struct httpd_state *s = (struct httpd_state *)state;
194 
195  if(uip_closed() || uip_aborted() || uip_timedout()) {
196  if(s != NULL) {
197  if(s->fd >= 0) {
198  cfs_close(s->fd);
199  s->fd = -1;
200  }
201  memb_free(&conns, s);
202  }
203  } else if(uip_connected()) {
204  s = (struct httpd_state *)memb_alloc(&conns);
205  if(s == NULL) {
206  uip_abort();
207  webserver_log_file(&uip_conn->ripaddr, "reset (no memory block)");
208  return;
209  }
210  tcp_markconn(uip_conn, s);
211  PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
212  PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
213  PT_INIT(&s->outputpt);
214  s->fd = -1;
215  s->state = STATE_WAITING;
216  timer_set(&s->timer, CLOCK_SECOND * 10);
217  handle_connection(s);
218  } else if(s != NULL) {
219  if(uip_poll()) {
220  if(timer_expired(&s->timer)) {
221  uip_abort();
222  if(s->fd >= 0) {
223  cfs_close(s->fd);
224  s->fd = -1;
225  }
226  memb_free(&conns, s);
227  webserver_log_file(&uip_conn->ripaddr, "reset (timeout)");
228  }
229  } else {
230  timer_reset(&s->timer);
231  }
232  handle_connection(s);
233  } else {
234  uip_abort();
235  }
236 }
237 /*---------------------------------------------------------------------------*/
238 void
239 httpd_init(void)
240 {
241  tcp_listen(UIP_HTONS(80));
242  memb_init(&conns);
243 }
244 /*---------------------------------------------------------------------------*/