2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
2007-11-27 23:36:07 +00:00
|
|
|
* $Id: httpd-cfs.c,v 1.7 2007/11/27 23:36:07 oliverschmidt Exp $
|
2006-06-17 22:41:10 +00:00
|
|
|
*/
|
|
|
|
|
2007-04-23 23:08:44 +00:00
|
|
|
#include <string.h>
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-04-23 23:08:44 +00:00
|
|
|
#include "contiki-net.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-04-23 23:08:44 +00:00
|
|
|
#include "webserver.h"
|
2007-11-26 20:53:00 +00:00
|
|
|
#include "cfs/cfs.h"
|
|
|
|
#include "lib/petsciiconv.h"
|
|
|
|
#include "http-strings.h"
|
2007-04-23 23:08:44 +00:00
|
|
|
|
|
|
|
#include "httpd-cfs.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-11-26 21:50:46 +00:00
|
|
|
#ifndef WEBSERVER_CONF_CFS_CONNS
|
|
|
|
#define CONNS 4
|
|
|
|
#else /* WEBSERVER_CONF_CFS_CONNS */
|
|
|
|
#define CONNS WEBSERVER_CONF_CFS_CONNS
|
|
|
|
#endif /* WEBSERVER_CONF_CFS_CONNS */
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
#define STATE_WAITING 0
|
|
|
|
#define STATE_OUTPUT 1
|
|
|
|
|
2007-11-18 02:54:45 +00:00
|
|
|
#define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, strlen(str))
|
2007-11-26 21:50:46 +00:00
|
|
|
MEMB(conns, struct httpd_state, CONNS);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
#define ISO_nl 0x0a
|
|
|
|
#define ISO_space 0x20
|
|
|
|
#define ISO_period 0x2e
|
|
|
|
#define ISO_slash 0x2f
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(send_file(struct httpd_state *s))
|
|
|
|
{
|
|
|
|
PSOCK_BEGIN(&s->sout);
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Read data from file system into buffer */
|
2007-04-23 23:08:44 +00:00
|
|
|
s->len = cfs_read(s->fd, s->outputbuf, sizeof(s->outputbuf));
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/* If there is data in the buffer, send it */
|
|
|
|
if(s->len > 0) {
|
2007-11-18 02:54:45 +00:00
|
|
|
PSOCK_SEND(&s->sout, (uint8_t *)s->outputbuf, s->len);
|
2006-06-17 22:41:10 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(s->len > 0);
|
|
|
|
|
|
|
|
PSOCK_END(&s->sout);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
2007-11-26 20:53:00 +00:00
|
|
|
PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
PSOCK_BEGIN(&s->sout);
|
|
|
|
|
|
|
|
SEND_STRING(&s->sout, statushdr);
|
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
ptr = strrchr(s->filename, ISO_period);
|
2006-06-17 22:41:10 +00:00
|
|
|
if(ptr == NULL) {
|
2007-11-26 20:53:00 +00:00
|
|
|
SEND_STRING(&s->sout, http_content_type_plain);
|
|
|
|
} else if(strncmp(http_html, ptr, 5) == 0) {
|
|
|
|
SEND_STRING(&s->sout, http_content_type_html);
|
|
|
|
} else if(strncmp(http_css, ptr, 4) == 0) {
|
|
|
|
SEND_STRING(&s->sout, http_content_type_css);
|
|
|
|
} else if(strncmp(http_png, ptr, 4) == 0) {
|
|
|
|
SEND_STRING(&s->sout, http_content_type_png);
|
|
|
|
} else if(strncmp(http_jpg, ptr, 4) == 0) {
|
|
|
|
SEND_STRING(&s->sout, http_content_type_jpg);
|
2006-06-17 22:41:10 +00:00
|
|
|
} else {
|
2007-11-26 20:53:00 +00:00
|
|
|
SEND_STRING(&s->sout, http_content_type_binary);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
PSOCK_END(&s->sout);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(handle_output(struct httpd_state *s))
|
|
|
|
{
|
|
|
|
PT_BEGIN(&s->outputpt);
|
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
petsciiconv_topetscii(s->filename, sizeof(s->filename));
|
2006-06-17 22:41:10 +00:00
|
|
|
s->fd = cfs_open(s->filename, CFS_READ);
|
|
|
|
if(s->fd < 0) {
|
|
|
|
s->fd = cfs_open("404.html", CFS_READ);
|
|
|
|
if(s->fd < 0) {
|
|
|
|
uip_abort();
|
2007-11-27 23:36:07 +00:00
|
|
|
memb_free(&conns, s);
|
2007-11-22 11:25:08 +00:00
|
|
|
webserver_log_file(&uip_conn->ripaddr, "reset (no 404.html)");
|
2006-06-17 22:41:10 +00:00
|
|
|
PT_EXIT(&s->outputpt);
|
|
|
|
}
|
|
|
|
PT_WAIT_THREAD(&s->outputpt,
|
2007-11-26 20:53:00 +00:00
|
|
|
send_headers(s, http_header_404));
|
2006-06-17 22:41:10 +00:00
|
|
|
} else {
|
|
|
|
PT_WAIT_THREAD(&s->outputpt,
|
2007-11-26 20:53:00 +00:00
|
|
|
send_headers(s, http_header_200));
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2007-11-22 11:25:08 +00:00
|
|
|
PT_WAIT_THREAD(&s->outputpt, send_file(s));
|
|
|
|
cfs_close(s->fd);
|
2006-06-17 22:41:10 +00:00
|
|
|
PSOCK_CLOSE(&s->sout);
|
|
|
|
PT_END(&s->outputpt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(handle_input(struct httpd_state *s))
|
|
|
|
{
|
|
|
|
PSOCK_BEGIN(&s->sin);
|
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
PSOCK_READTO(&s->sin, ISO_space);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
if(strncmp(s->inputbuf, http_get, 4) != 0) {
|
2006-06-17 22:41:10 +00:00
|
|
|
PSOCK_CLOSE_EXIT(&s->sin);
|
|
|
|
}
|
2007-11-26 20:53:00 +00:00
|
|
|
PSOCK_READTO(&s->sin, ISO_space);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
if(s->inputbuf[0] != ISO_slash) {
|
2006-06-17 22:41:10 +00:00
|
|
|
PSOCK_CLOSE_EXIT(&s->sin);
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
if(s->inputbuf[1] == ISO_space) {
|
2007-11-27 23:36:07 +00:00
|
|
|
strncpy(s->filename, &http_index_html[1], sizeof(s->filename));
|
2006-06-17 22:41:10 +00:00
|
|
|
} else {
|
|
|
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
|
|
|
|
strncpy(s->filename, &s->inputbuf[1], sizeof(s->filename));
|
|
|
|
}
|
|
|
|
|
2007-04-23 23:08:44 +00:00
|
|
|
webserver_log_file(&uip_conn->ripaddr, s->filename);
|
2006-06-17 22:41:10 +00:00
|
|
|
s->state = STATE_OUTPUT;
|
|
|
|
|
|
|
|
while(1) {
|
2007-11-26 20:53:00 +00:00
|
|
|
PSOCK_READTO(&s->sin, ISO_nl);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-11-26 20:53:00 +00:00
|
|
|
if(strncmp(s->inputbuf, http_referer, 8) == 0) {
|
2006-06-17 22:41:10 +00:00
|
|
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
|
2007-11-26 20:53:00 +00:00
|
|
|
petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2);
|
2007-11-22 11:25:08 +00:00
|
|
|
webserver_log(s->inputbuf);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PSOCK_END(&s->sin);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_connection(struct httpd_state *s)
|
|
|
|
{
|
|
|
|
handle_input(s);
|
|
|
|
if(s->state == STATE_OUTPUT) {
|
|
|
|
handle_output(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
httpd_appcall(void *state)
|
|
|
|
{
|
|
|
|
struct httpd_state *s = (struct httpd_state *)state;
|
|
|
|
|
|
|
|
if(uip_closed() || uip_aborted() || uip_timedout()) {
|
|
|
|
if(s != NULL) {
|
|
|
|
memb_free(&conns, s);
|
|
|
|
}
|
|
|
|
} else if(uip_connected()) {
|
|
|
|
s = (struct httpd_state *)memb_alloc(&conns);
|
|
|
|
if(s == NULL) {
|
|
|
|
uip_abort();
|
2007-11-22 11:25:08 +00:00
|
|
|
webserver_log_file(&uip_conn->ripaddr, "reset (no memory block)");
|
2006-06-17 22:41:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tcp_markconn(uip_conn, s);
|
2007-11-18 02:54:45 +00:00
|
|
|
PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
|
|
|
|
PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
|
2006-06-17 22:41:10 +00:00
|
|
|
PT_INIT(&s->outputpt);
|
|
|
|
s->state = STATE_WAITING;
|
|
|
|
timer_set(&s->timer, CLOCK_SECOND * 10);
|
|
|
|
handle_connection(s);
|
|
|
|
} else if(s != NULL) {
|
|
|
|
if(uip_poll()) {
|
|
|
|
if(timer_expired(&s->timer)) {
|
|
|
|
uip_abort();
|
2007-11-27 23:36:07 +00:00
|
|
|
memb_free(&conns, s);
|
2007-11-22 11:25:08 +00:00
|
|
|
webserver_log_file(&uip_conn->ripaddr, "reset (timeout)");
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timer_reset(&s->timer);
|
|
|
|
}
|
|
|
|
handle_connection(s);
|
|
|
|
} else {
|
|
|
|
uip_abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
httpd_init(void)
|
|
|
|
{
|
|
|
|
tcp_listen(HTONS(80));
|
|
|
|
memb_init(&conns);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|