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 #include "cfs/cfs.h"
40 
41 #include "webserver.h"
42 #include "libconio_arch-small.h"
43 
44 #include "httpd-cfs.h"
45 
46 #define STATE_WAITING 0
47 #define STATE_OUTPUT 1
48 
49 #define SEND_STRING(s, str) PSOCK_SEND(s, str, strlen(str))
50 MEMB(conns, struct httpd_state, 2);
51 
52 /*---------------------------------------------------------------------------*/
53 static
54 PT_THREAD(send_file(struct httpd_state *s))
55 {
56  PSOCK_BEGIN(&s->sout);
57 
58  do {
59  /* Read data from file system into buffer */
60  s->len = cfs_read(s->fd, s->outputbuf, sizeof(s->outputbuf));
61 
62  /* If there is data in the buffer, send it */
63  if(s->len > 0) {
64  PSOCK_SEND(&s->sout, s->outputbuf, s->len);
65  } else {
66  break;
67  }
68  } while(s->len > 0);
69 
70  PSOCK_END(&s->sout);
71 }
72 /*---------------------------------------------------------------------------*/
73 static
74 PT_THREAD(send_headers(struct httpd_state *s, char *statushdr))
75 {
76  char *ptr;
77 
78  PSOCK_BEGIN(&s->sout);
79 
80  SEND_STRING(&s->sout, statushdr);
81  SEND_STRING(&s->sout, "Server: " CONTIKI_VERSION_STRING "\r\n");
82 
83  ptr = strrchr(s->filename, '.');
84  if(ptr == NULL) {
85  SEND_STRING(&s->sout, "Content-type: text/plain\r\n\r\n");
86  } else if(strncmp(".html", ptr, 5) == 0) {
87  SEND_STRING(&s->sout, "Content-type: text/html\r\n\r\n");
88  } else if(strncmp(".css", ptr, 4) == 0) {
89  SEND_STRING(&s->sout, "Content-type: text/css\r\n\r\n");
90  } else if(strncmp(".png", ptr, 4) == 0) {
91  SEND_STRING(&s->sout, "Content-type: image/png\r\n\r\n");
92  } else if(strncmp(".jpg", ptr, 4) == 0) {
93  SEND_STRING(&s->sout, "Content-type: image/jpeg\r\n\r\n");
94  } else {
95  SEND_STRING(&s->sout, "Content-type: application/octet-stream\r\n\r\n");
96  }
97  PSOCK_END(&s->sout);
98 }
99 /*---------------------------------------------------------------------------*/
100 static
101 PT_THREAD(handle_output(struct httpd_state *s))
102 {
103  PT_BEGIN(&s->outputpt);
104 
105  s->fd = cfs_open(s->filename, CFS_READ);
106  if(s->fd < 0) {
107  s->fd = cfs_open("404.html", CFS_READ);
108  if(s->fd < 0) {
109  uip_abort();
110  PT_EXIT(&s->outputpt);
111  }
112  PT_WAIT_THREAD(&s->outputpt,
113  send_headers(s, "HTTP/1.0 404 Not found\r\n"));
114  PT_WAIT_THREAD(&s->outputpt,
115  send_file(s));
116  } else {
117  PT_WAIT_THREAD(&s->outputpt,
118  send_headers(s, "HTTP/1.0 200 OK\r\n"));
119  PT_WAIT_THREAD(&s->outputpt,
120  send_file(s));
121  cfs_close(s->fd);
122  }
123  PSOCK_CLOSE(&s->sout);
124  PT_END(&s->outputpt);
125 }
126 /*---------------------------------------------------------------------------*/
127 static
128 PT_THREAD(handle_input(struct httpd_state *s))
129 {
130  PSOCK_BEGIN(&s->sin);
131 
132  PSOCK_READTO(&s->sin, ' ');
133 
134  if(strncmp(s->inputbuf, "GET ", 4) != 0) {
135  PSOCK_CLOSE_EXIT(&s->sin);
136  }
137  PSOCK_READTO(&s->sin, ' ');
138 
139  if(s->inputbuf[0] != '/') {
140  PSOCK_CLOSE_EXIT(&s->sin);
141  }
142 
143  if(s->inputbuf[1] == ' ') {
144  strncpy(s->filename, "index.html", sizeof(s->filename));
145  } else {
146  s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
147  strncpy(s->filename, &s->inputbuf[1], sizeof(s->filename));
148  }
149 
150  // webserver_log_file(&uip_conn->ripaddr, s->filename);
151  libputs_arch(s->filename);
152  s->state = STATE_OUTPUT;
153 
154  while(1) {
155  PSOCK_READTO(&s->sin, '\n');
156 
157  if(strncmp(s->inputbuf, "Referer:", 8) == 0) {
158  s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
159  libputs_arch(&s->inputbuf[9]);
160  // webserver_log(&s->inputbuf[9]);
161  }
162  }
163 
164  PSOCK_END(&s->sin);
165 }
166 /*---------------------------------------------------------------------------*/
167 static void
168 handle_connection(struct httpd_state *s)
169 {
170  handle_input(s);
171  if(s->state == STATE_OUTPUT) {
172  handle_output(s);
173  }
174 }
175 /*---------------------------------------------------------------------------*/
176 void
177 httpd_appcall(void *state)
178 {
179  struct httpd_state *s = (struct httpd_state *)state;
180 
181  if(uip_closed() || uip_aborted() || uip_timedout()) {
182  if(s != NULL) {
183  memb_free(&conns, s);
184  }
185  } else if(uip_connected()) {
186  s = (struct httpd_state *)memb_alloc(&conns);
187  if(s == NULL) {
188  uip_abort();
189  return;
190  }
191  tcp_markconn(uip_conn, s);
192  PSOCK_INIT(&s->sin, s->inputbuf, sizeof(s->inputbuf) - 1);
193  PSOCK_INIT(&s->sout, s->inputbuf, sizeof(s->inputbuf) - 1);
194  PT_INIT(&s->outputpt);
195  s->state = STATE_WAITING;
196  timer_set(&s->timer, CLOCK_SECOND * 10);
197  handle_connection(s);
198  } else if(s != NULL) {
199  if(uip_poll()) {
200  if(timer_expired(&s->timer)) {
201  uip_abort();
202  }
203  } else {
204  timer_reset(&s->timer);
205  }
206  handle_connection(s);
207  } else {
208  uip_abort();
209  }
210 }
211 /*---------------------------------------------------------------------------*/
212 void
213 httpd_init(void)
214 {
215  tcp_listen(UIP_HTONS(80));
216  memb_init(&conns);
217 }
218 /*---------------------------------------------------------------------------*/