38 #include "contiki-net.h"
41 #include "webserver.h"
42 #include "libconio_arch-small.h"
44 #include "httpd-cfs.h"
46 #define STATE_WAITING 0
47 #define STATE_OUTPUT 1
49 #define SEND_STRING(s, str) PSOCK_SEND(s, str, strlen(str))
50 MEMB(conns,
struct httpd_state, 2);
54 PT_THREAD(send_file(
struct httpd_state *s))
60 s->len = cfs_read(s->fd, s->outputbuf,
sizeof(s->outputbuf));
74 PT_THREAD(send_headers(
struct httpd_state *s,
char *statushdr))
80 SEND_STRING(&s->sout, statushdr);
81 SEND_STRING(&s->sout,
"Server: " CONTIKI_VERSION_STRING
"\r\n");
83 ptr = strrchr(s->filename,
'.');
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");
95 SEND_STRING(&s->sout,
"Content-type: application/octet-stream\r\n\r\n");
101 PT_THREAD(handle_output(
struct httpd_state *s))
113 send_headers(s,
"HTTP/1.0 404 Not found\r\n"));
118 send_headers(s,
"HTTP/1.0 200 OK\r\n"));
128 PT_THREAD(handle_input(
struct httpd_state *s))
134 if(strncmp(s->inputbuf,
"GET ", 4) != 0) {
139 if(s->inputbuf[0] !=
'/') {
143 if(s->inputbuf[1] ==
' ') {
144 strncpy(s->filename,
"index.html",
sizeof(s->filename));
147 strncpy(s->filename, &s->inputbuf[1],
sizeof(s->filename));
151 libputs_arch(s->filename);
152 s->state = STATE_OUTPUT;
157 if(strncmp(s->inputbuf,
"Referer:", 8) == 0) {
159 libputs_arch(&s->inputbuf[9]);
168 handle_connection(
struct httpd_state *s)
171 if(s->state == STATE_OUTPUT) {
177 httpd_appcall(
void *state)
179 struct httpd_state *s = (
struct httpd_state *)state;
192 PSOCK_INIT(&s->sin, s->inputbuf,
sizeof(s->inputbuf) - 1);
193 PSOCK_INIT(&s->sout, s->inputbuf,
sizeof(s->inputbuf) - 1);
195 s->state = STATE_WAITING;
197 handle_connection(s);
198 }
else if(s !=
NULL) {
206 handle_connection(s);