38 #include "contiki-net.h"
40 #include "webserver.h"
43 #include "http-strings.h"
45 #include "httpd-cfs.h"
47 #ifndef WEBSERVER_CONF_CFS_CONNS
50 #define CONNS WEBSERVER_CONF_CFS_CONNS
53 #define STATE_WAITING 0
54 #define STATE_OUTPUT 1
56 #define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, strlen(str))
57 MEMB(conns,
struct httpd_state, CONNS);
60 #define ISO_space 0x20
61 #define ISO_period 0x2e
62 #define ISO_slash 0x2f
66 PT_THREAD(send_file(
struct httpd_state *s))
72 s->len = cfs_read(s->fd, s->outputbuf,
sizeof(s->outputbuf));
76 PSOCK_SEND(&s->sout, (uint8_t *)s->outputbuf, s->len);
86 PT_THREAD(send_headers(
struct httpd_state *s,
const char *statushdr))
92 SEND_STRING(&s->sout, statushdr);
94 ptr = strrchr(s->filename, ISO_period);
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);
106 SEND_STRING(&s->sout, http_content_type_binary);
112 PT_THREAD(handle_output(
struct httpd_state *s))
116 petsciiconv_topetscii(s->filename,
sizeof(s->filename));
118 petsciiconv_toascii(s->filename,
sizeof(s->filename));
128 send_headers(s, http_header_404));
131 send_headers(s, http_header_200));
141 PT_THREAD(handle_input(
struct httpd_state *s))
147 if(strncmp(s->inputbuf, http_get, 4) != 0) {
152 if(s->inputbuf[0] != ISO_slash) {
156 if(s->inputbuf[1] == ISO_space) {
157 strncpy(s->filename, &http_index_html[1],
sizeof(s->filename));
160 strncpy(s->filename, &s->inputbuf[1],
sizeof(s->filename));
163 petsciiconv_topetscii(s->filename,
sizeof(s->filename));
165 petsciiconv_toascii(s->filename,
sizeof(s->filename));
166 s->state = STATE_OUTPUT;
171 if(strncmp(s->inputbuf, http_referer, 8) == 0) {
173 petsciiconv_topetscii(s->inputbuf,
PSOCK_DATALEN(&s->sin) - 2);
174 webserver_log(s->inputbuf);
182 handle_connection(
struct httpd_state *s)
185 if(s->state == STATE_OUTPUT) {
191 httpd_appcall(
void *state)
193 struct httpd_state *s = (
struct httpd_state *)state;
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);
215 s->state = STATE_WAITING;
217 handle_connection(s);
218 }
else if(s !=
NULL) {
232 handle_connection(s);