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);
#define PT_WAIT_THREAD(pt, thread)
Block and wait until a child protothread completes.
#define PSOCK_READTO(psock, c)
Read data up to a specified character.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
#define PSOCK_CLOSE_EXIT(psock)
Close a protosocket and exit the protosocket's protothread.
int cfs_open(const char *name, int flags)
Open a file.
#define PSOCK_BEGIN(psock)
Start the protosocket protothread in a function.
Representation of a uIP TCP connection.
#define uip_aborted()
Has the connection been aborted by the other end?
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
#define NULL
The null pointer.
#define PT_INIT(pt)
Initialize a protothread.
#define uip_poll()
Is the connection being polled by uIP?
#define CFS_READ
Specify that cfs_open() should open a file for reading.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
#define PT_THREAD(name_args)
Declaration of a protothread.
#define uip_connected()
Has the connection just been connected?
#define uip_abort()
Abort the current connection.
PETSCII/ASCII conversion functions.
#define PSOCK_INIT(psock, buffer, buffersize)
Initialize a protosocket.
#define MEMB(name, structure, num)
Declare a memory block.
#define PSOCK_CLOSE(psock)
Close a protosocket.
#define PSOCK_SEND(psock, data, datalen)
Send data.
#define PSOCK_END(psock)
Declare the end of a protosocket's protothread.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
#define uip_timedout()
Has the connection timed out?
#define PT_END(pt)
Declare the end of a protothread.
CCIF void tcp_listen(uint16_t port)
Open a TCP port.
#define PSOCK_DATALEN(psock)
The length of the data that was previously read.
#define uip_closed()
Has the connection been closed by the other end?
uip_ipaddr_t ripaddr
The IP address of the remote host.
int timer_expired(struct timer *t)
Check if a timer has expired.
#define PT_EXIT(pt)
Exit the protothread.
void cfs_close(int fd)
Close an open file.
void timer_reset(struct timer *t)
Reset the timer with the same interval.
#define CLOCK_SECOND
A second, measured in system clock time.