37 #include "contiki-net.h"
39 #include "webserver.h"
42 #include "http-strings.h"
44 #include "httpd-cfs.h"
46 #ifndef WEBSERVER_CONF_CFS_CONNS
49 #define CONNS WEBSERVER_CONF_CFS_CONNS
52 #define STATE_WAITING 0
53 #define STATE_OUTPUT 1
55 #define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, strlen(str))
56 MEMB(conns,
struct httpd_state, CONNS);
59 #define ISO_space 0x20
60 #define ISO_period 0x2e
61 #define ISO_slash 0x2f
65 PT_THREAD(send_file(
struct httpd_state *s))
71 s->len = cfs_read(s->fd, s->outputbuf,
sizeof(s->outputbuf));
75 PSOCK_SEND(&s->sout, (uint8_t *)s->outputbuf, s->len);
85 PT_THREAD(send_headers(
struct httpd_state *s,
const char *statushdr))
91 SEND_STRING(&s->sout, statushdr);
93 ptr = strrchr(s->filename, ISO_period);
95 SEND_STRING(&s->sout, http_content_type_plain);
96 }
else if(strncmp(http_html, ptr, 5) == 0) {
97 SEND_STRING(&s->sout, http_content_type_html);
98 }
else if(strncmp(http_css, ptr, 4) == 0) {
99 SEND_STRING(&s->sout, http_content_type_css);
100 }
else if(strncmp(http_png, ptr, 4) == 0) {
101 SEND_STRING(&s->sout, http_content_type_png);
102 }
else if(strncmp(http_jpg, ptr, 4) == 0) {
103 SEND_STRING(&s->sout, http_content_type_jpg);
105 SEND_STRING(&s->sout, http_content_type_binary);
111 PT_THREAD(handle_output(
struct httpd_state *s))
115 petsciiconv_topetscii(s->filename,
sizeof(s->filename));
117 petsciiconv_toascii(s->filename,
sizeof(s->filename));
127 send_headers(s, http_header_404));
130 send_headers(s, http_header_200));
140 PT_THREAD(handle_input(
struct httpd_state *s))
146 if(strncmp(s->inputbuf, http_get, 4) != 0) {
151 if(s->inputbuf[0] != ISO_slash) {
155 if(s->inputbuf[1] == ISO_space) {
156 strncpy(s->filename, &http_index_html[1],
sizeof(s->filename));
159 strncpy(s->filename, &s->inputbuf[1],
sizeof(s->filename));
162 petsciiconv_topetscii(s->filename,
sizeof(s->filename));
164 petsciiconv_toascii(s->filename,
sizeof(s->filename));
165 s->state = STATE_OUTPUT;
170 if(strncmp(s->inputbuf, http_referer, 8) == 0) {
172 petsciiconv_topetscii(s->inputbuf,
PSOCK_DATALEN(&s->sin) - 2);
173 webserver_log(s->inputbuf);
181 handle_connection(
struct httpd_state *s)
184 if(s->state == STATE_OUTPUT) {
190 httpd_appcall(
void *state)
192 struct httpd_state *s = (
struct httpd_state *)state;
210 PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf,
sizeof(s->inputbuf) - 1);
211 PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf,
sizeof(s->inputbuf) - 1);
214 s->state = STATE_WAITING;
216 handle_connection(s);
217 }
else if(s !=
NULL) {
231 handle_connection(s);