38 #include "contiki-net.h"
40 #include "webserver-nogui.h"
42 #include "httpd-cgi.h"
43 #include "http-strings.h"
46 #include "ctk/libconio_arch-small.h"
48 #define STATE_WAITING 0
49 #define STATE_OUTPUT 1
51 #define SEND_STRING(s, str) PSOCK_SEND(s, str, (unsigned int)strlen(str))
52 MEMB(conns,
struct httpd_state, 4);
55 #define ISO_space 0x20
57 #define ISO_percent 0x25
58 #define ISO_period 0x2e
59 #define ISO_slash 0x2f
60 #define ISO_colon 0x3a
66 struct httpd_state *s = (
struct httpd_state *)state;
79 PT_THREAD(send_file(
struct httpd_state *s))
85 s->file.len -= s->len;
86 s->file.data += s->len;
87 }
while(s->file.len > 0);
93 PT_THREAD(send_part_of_file(
struct httpd_state *s))
102 #if HTTPD_CONF_SCRIPT
104 next_scriptstate(
struct httpd_state *s)
108 if((p = strchr(s->scriptptr, ISO_nl)) !=
NULL) {
110 s->scriptlen -= (
unsigned short)(p - s->scriptptr);
122 PT_THREAD(handle_script(
struct httpd_state *s))
128 while(s->file.len > 0) {
131 if(*s->file.data == ISO_percent &&
132 *(s->file.data + 1) == ISO_bang) {
133 s->scriptptr = s->file.data + 3;
134 s->scriptlen = s->file.len - 3;
135 if(*(s->scriptptr - 1) == ISO_colon) {
136 httpd_fs_open(s->scriptptr + 1, &s->file);
141 httpd_cgi(s->scriptptr)(s, s->scriptptr));
148 s->file.data = s->scriptptr;
149 s->file.len = s->scriptlen;
157 s->len = s->file.len;
160 if(*s->file.data == ISO_percent) {
161 ptr = strchr(s->file.data + 1, ISO_percent);
163 ptr = strchr(s->file.data, ISO_percent);
166 ptr != s->file.data) {
167 s->len = (int)(ptr - s->file.data);
173 s->file.data += s->len;
174 s->file.len -= s->len;
183 PT_THREAD(send_headers(
struct httpd_state *s,
const char *statushdr))
189 SEND_STRING(&s->sout, statushdr);
191 ptr = strrchr(s->filename, ISO_period);
193 SEND_STRING(&s->sout, http_content_type_binary);
194 }
else if(strncmp(http_html, ptr, 5) == 0 ||
195 strncmp(http_shtml, ptr, 6) == 0) {
196 SEND_STRING(&s->sout, http_content_type_html);
198 }
else if(strncmp(http_css, ptr, 4) == 0) {
199 SEND_STRING(&s->sout, http_content_type_css);
200 }
else if(strncmp(http_png, ptr, 4) == 0) {
201 SEND_STRING(&s->sout, http_content_type_png);
202 }
else if(strncmp(http_gif, ptr, 4) == 0) {
203 SEND_STRING(&s->sout, http_content_type_gif);
204 }
else if(strncmp(http_jpg, ptr, 4) == 0) {
205 SEND_STRING(&s->sout, http_content_type_jpg);
208 SEND_STRING(&s->sout, http_content_type_plain);
214 PT_THREAD(handle_output(
struct httpd_state *s))
220 if(!httpd_fs_open(s->filename, &s->file)) {
221 httpd_fs_open(http_404_html, &s->file);
231 ptr = strchr(s->filename, ISO_period);
232 #if HTTPD_CONF_SCRIPT
233 if(ptr !=
NULL && strncmp(ptr, http_shtml, 6) == 0) {
250 PT_THREAD(handle_input(
struct httpd_state *s))
256 if(strncmp(s->inputbuf, http_get, 4) != 0) {
261 if(s->inputbuf[0] != ISO_slash) {
265 if(s->inputbuf[1] == ISO_space) {
266 strncpy(s->filename, http_index_html,
sizeof(s->filename));
269 strncpy(s->filename, &s->inputbuf[0],
sizeof(s->filename));
272 libputs_arch(s->filename);
274 s->state = STATE_OUTPUT;
279 if(strncmp(s->inputbuf, http_referer, 8) == 0) {
288 handle_connection(
struct httpd_state *s)
291 if(s->state == STATE_OUTPUT) {
297 httpd_appcall(
void *state)
299 struct httpd_state *s = (
struct httpd_state *)state;
312 PSOCK_INIT(&s->sin, s->inputbuf,
sizeof(s->inputbuf) - 1);
313 PSOCK_INIT(&s->sout, s->inputbuf,
sizeof(s->inputbuf) - 1);
315 s->state = STATE_WAITING;
318 handle_connection(s);
319 }
else if(s !=
NULL) {
329 handle_connection(s);