2006-06-18 00:41:10 +02: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>
|
|
|
|
*
|
2010-04-06 13:49:47 +02:00
|
|
|
* $Id: httpd.c,v 1.13 2010/04/06 11:49:47 oliverschmidt Exp $
|
2006-06-18 00:41:10 +02:00
|
|
|
*/
|
2009-08-12 20:23:37 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2007-04-23 23:19:55 +02:00
|
|
|
#include <string.h>
|
2008-10-14 13:07:57 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "contiki-net.h"
|
2007-04-23 23:19:55 +02:00
|
|
|
|
|
|
|
#include "webserver.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "httpd-fs.h"
|
|
|
|
#include "httpd-cgi.h"
|
2007-11-26 22:36:35 +01:00
|
|
|
#include "lib/petsciiconv.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "http-strings.h"
|
|
|
|
|
2007-04-23 23:19:55 +02:00
|
|
|
#include "httpd.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2007-11-26 22:36:35 +01:00
|
|
|
#ifndef WEBSERVER_CONF_CGI_CONNS
|
|
|
|
#define CONNS 4
|
|
|
|
#else /* WEBSERVER_CONF_CGI_CONNS */
|
|
|
|
#define CONNS WEBSERVER_CONF_CGI_CONNS
|
|
|
|
#endif /* WEBSERVER_CONF_CGI_CONNS */
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
#define STATE_WAITING 0
|
|
|
|
#define STATE_OUTPUT 1
|
|
|
|
|
2007-11-17 21:44:07 +01:00
|
|
|
#define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, (unsigned int)strlen(str))
|
2007-11-26 22:36:35 +01:00
|
|
|
MEMB(conns, struct httpd_state, CONNS);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
#define ISO_nl 0x0a
|
|
|
|
#define ISO_space 0x20
|
|
|
|
#define ISO_bang 0x21
|
|
|
|
#define ISO_percent 0x25
|
|
|
|
#define ISO_period 0x2e
|
|
|
|
#define ISO_slash 0x2f
|
|
|
|
#define ISO_colon 0x3a
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static unsigned short
|
|
|
|
generate(void *state)
|
|
|
|
{
|
|
|
|
struct httpd_state *s = (struct httpd_state *)state;
|
|
|
|
|
|
|
|
if(s->file.len > uip_mss()) {
|
|
|
|
s->len = uip_mss();
|
|
|
|
} else {
|
|
|
|
s->len = s->file.len;
|
|
|
|
}
|
2008-10-14 13:07:57 +02:00
|
|
|
memcpy(uip_appdata, s->file.data, s->len);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
return s->len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(send_file(struct httpd_state *s))
|
|
|
|
{
|
|
|
|
PSOCK_BEGIN(&s->sout);
|
|
|
|
|
2008-10-14 13:07:57 +02:00
|
|
|
do {
|
2006-06-18 00:41:10 +02:00
|
|
|
PSOCK_GENERATOR_SEND(&s->sout, generate, s);
|
|
|
|
s->file.len -= s->len;
|
|
|
|
s->file.data += s->len;
|
|
|
|
} while(s->file.len > 0);
|
|
|
|
|
|
|
|
PSOCK_END(&s->sout);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(send_part_of_file(struct httpd_state *s))
|
|
|
|
{
|
|
|
|
PSOCK_BEGIN(&s->sout);
|
2008-10-14 13:07:57 +02:00
|
|
|
|
|
|
|
PSOCK_SEND(&s->sout, (uint8_t *)s->file.data, s->len);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
PSOCK_END(&s->sout);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
next_scriptstate(struct httpd_state *s)
|
|
|
|
{
|
|
|
|
char *p;
|
2006-09-20 21:18:56 +02:00
|
|
|
|
2008-10-14 13:07:57 +02:00
|
|
|
if((p = strchr(s->scriptptr, ISO_nl)) != NULL) {
|
2006-09-20 21:18:56 +02:00
|
|
|
p += 1;
|
|
|
|
s->scriptlen -= (unsigned short)(p - s->scriptptr);
|
|
|
|
s->scriptptr = p;
|
|
|
|
} else {
|
|
|
|
s->scriptlen = 0;
|
|
|
|
}
|
|
|
|
/* char *p;
|
2006-06-18 00:41:10 +02:00
|
|
|
p = strchr(s->scriptptr, ISO_nl) + 1;
|
|
|
|
s->scriptlen -= (unsigned short)(p - s->scriptptr);
|
2006-09-20 21:18:56 +02:00
|
|
|
s->scriptptr = p;*/
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(handle_script(struct httpd_state *s))
|
|
|
|
{
|
2008-10-14 13:07:57 +02:00
|
|
|
char *ptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
PT_BEGIN(&s->scriptpt);
|
|
|
|
|
|
|
|
while(s->file.len > 0) {
|
|
|
|
|
|
|
|
/* Check if we should start executing a script. */
|
2008-10-14 13:07:57 +02:00
|
|
|
if(*s->file.data == ISO_percent &&
|
|
|
|
*(s->file.data + 1) == ISO_bang) {
|
2006-06-18 00:41:10 +02:00
|
|
|
s->scriptptr = s->file.data + 3;
|
2008-10-14 13:07:57 +02:00
|
|
|
s->scriptlen = s->file.len - 3;
|
|
|
|
if(*(s->scriptptr - 1) == ISO_colon) {
|
|
|
|
httpd_fs_open(s->scriptptr + 1, &s->file);
|
|
|
|
PT_WAIT_THREAD(&s->scriptpt, send_file(s));
|
2006-06-18 00:41:10 +02:00
|
|
|
} else {
|
2008-10-14 13:07:57 +02:00
|
|
|
PT_WAIT_THREAD(&s->scriptpt,
|
|
|
|
httpd_cgi(s->scriptptr)(s, s->scriptptr));
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
next_scriptstate(s);
|
|
|
|
|
|
|
|
/* The script is over, so we reset the pointers and continue
|
|
|
|
sending the rest of the file. */
|
|
|
|
s->file.data = s->scriptptr;
|
|
|
|
s->file.len = s->scriptlen;
|
|
|
|
} else {
|
|
|
|
/* See if we find the start of script marker in the block of HTML
|
|
|
|
to be sent. */
|
|
|
|
|
|
|
|
if(s->file.len > uip_mss()) {
|
|
|
|
s->len = uip_mss();
|
|
|
|
} else {
|
2008-10-14 13:07:57 +02:00
|
|
|
s->len = s->file.len;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-14 13:07:57 +02:00
|
|
|
if(*s->file.data == ISO_percent) {
|
|
|
|
ptr = strchr(s->file.data + 1, ISO_percent);
|
2006-06-18 00:41:10 +02:00
|
|
|
} else {
|
2008-10-14 13:07:57 +02:00
|
|
|
ptr = strchr(s->file.data, ISO_percent);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
if(ptr != NULL &&
|
2008-10-14 13:07:57 +02:00
|
|
|
ptr != s->file.data) {
|
2006-06-18 00:41:10 +02:00
|
|
|
s->len = (int)(ptr - s->file.data);
|
|
|
|
if(s->len >= uip_mss()) {
|
|
|
|
s->len = uip_mss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s));
|
|
|
|
s->file.data += s->len;
|
|
|
|
s->file.len -= s->len;
|
|
|
|
}
|
|
|
|
}
|
2008-10-14 13:07:57 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
PT_END(&s->scriptpt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr))
|
|
|
|
{
|
2010-04-06 13:49:47 +02:00
|
|
|
const char *ptr;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
PSOCK_BEGIN(&s->sout);
|
|
|
|
|
|
|
|
SEND_STRING(&s->sout, statushdr);
|
|
|
|
|
|
|
|
ptr = strrchr(s->filename, ISO_period);
|
|
|
|
if(ptr == NULL) {
|
2010-04-06 13:49:47 +02:00
|
|
|
ptr = http_content_type_binary;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else if(strncmp(http_html, ptr, 5) == 0 ||
|
|
|
|
strncmp(http_shtml, ptr, 6) == 0) {
|
2010-04-06 13:49:47 +02:00
|
|
|
ptr = http_content_type_html;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else if(strncmp(http_css, ptr, 4) == 0) {
|
2010-04-06 13:49:47 +02:00
|
|
|
ptr = http_content_type_css;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else if(strncmp(http_png, ptr, 4) == 0) {
|
2010-04-06 13:49:47 +02:00
|
|
|
ptr = http_content_type_png;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else if(strncmp(http_gif, ptr, 4) == 0) {
|
2010-04-06 13:49:47 +02:00
|
|
|
ptr = http_content_type_gif;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else if(strncmp(http_jpg, ptr, 4) == 0) {
|
2010-04-06 13:49:47 +02:00
|
|
|
ptr = http_content_type_jpg;
|
2006-06-18 00:41:10 +02:00
|
|
|
} else {
|
2010-04-06 13:49:47 +02:00
|
|
|
ptr = http_content_type_plain;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2010-04-06 13:49:47 +02:00
|
|
|
SEND_STRING(&s->sout, ptr);
|
2006-06-18 00:41:10 +02:00
|
|
|
PSOCK_END(&s->sout);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(handle_output(struct httpd_state *s))
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
PT_BEGIN(&s->outputpt);
|
|
|
|
|
|
|
|
if(!httpd_fs_open(s->filename, &s->file)) {
|
|
|
|
httpd_fs_open(http_404_html, &s->file);
|
|
|
|
PT_WAIT_THREAD(&s->outputpt,
|
|
|
|
send_headers(s,
|
|
|
|
http_header_404));
|
|
|
|
PT_WAIT_THREAD(&s->outputpt,
|
|
|
|
send_file(s));
|
|
|
|
} else {
|
|
|
|
PT_WAIT_THREAD(&s->outputpt,
|
|
|
|
send_headers(s,
|
|
|
|
http_header_200));
|
|
|
|
ptr = strchr(s->filename, ISO_period);
|
|
|
|
if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0) {
|
|
|
|
PT_INIT(&s->scriptpt);
|
|
|
|
PT_WAIT_THREAD(&s->outputpt, handle_script(s));
|
|
|
|
} else {
|
|
|
|
PT_WAIT_THREAD(&s->outputpt,
|
|
|
|
send_file(s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PSOCK_CLOSE(&s->sout);
|
|
|
|
PT_END(&s->outputpt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(handle_input(struct httpd_state *s))
|
2008-10-14 13:07:57 +02:00
|
|
|
{
|
|
|
|
PSOCK_BEGIN(&s->sin);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
PSOCK_READTO(&s->sin, ISO_space);
|
|
|
|
|
|
|
|
if(strncmp(s->inputbuf, http_get, 4) != 0) {
|
|
|
|
PSOCK_CLOSE_EXIT(&s->sin);
|
|
|
|
}
|
|
|
|
PSOCK_READTO(&s->sin, ISO_space);
|
|
|
|
|
|
|
|
if(s->inputbuf[0] != ISO_slash) {
|
|
|
|
PSOCK_CLOSE_EXIT(&s->sin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->inputbuf[1] == ISO_space) {
|
|
|
|
strncpy(s->filename, http_index_html, sizeof(s->filename));
|
|
|
|
} else {
|
|
|
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
|
|
|
|
strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename));
|
|
|
|
}
|
|
|
|
|
2007-04-23 23:19:55 +02:00
|
|
|
webserver_log_file(&uip_conn->ripaddr, s->filename);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
s->state = STATE_OUTPUT;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PSOCK_READTO(&s->sin, ISO_nl);
|
|
|
|
|
|
|
|
if(strncmp(s->inputbuf, http_referer, 8) == 0) {
|
|
|
|
s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
|
2007-11-26 00:31:11 +01:00
|
|
|
petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2);
|
2007-11-24 14:16:30 +01:00
|
|
|
webserver_log(s->inputbuf);
|
2006-06-18 00:41:10 +02: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();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tcp_markconn(uip_conn, s);
|
2007-11-17 21:44:07 +01: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-18 00:41:10 +02:00
|
|
|
PT_INIT(&s->outputpt);
|
|
|
|
s->state = STATE_WAITING;
|
|
|
|
/* timer_set(&s->timer, CLOCK_SECOND * 100);*/
|
|
|
|
s->timer = 0;
|
|
|
|
handle_connection(s);
|
|
|
|
} else if(s != NULL) {
|
|
|
|
if(uip_poll()) {
|
|
|
|
++s->timer;
|
|
|
|
if(s->timer >= 20) {
|
|
|
|
uip_abort();
|
|
|
|
memb_free(&conns, s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s->timer = 0;
|
|
|
|
}
|
|
|
|
handle_connection(s);
|
|
|
|
} else {
|
|
|
|
uip_abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
httpd_init(void)
|
|
|
|
{
|
|
|
|
tcp_listen(HTONS(80));
|
|
|
|
memb_init(&conns);
|
|
|
|
httpd_cgi_init();
|
|
|
|
}
|
2009-08-12 20:23:37 +02:00
|
|
|
#if UIP_CONF_IPV6
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uint8_t
|
|
|
|
httpd_sprint_ip6(uip_ip6addr_t addr, char * result)
|
|
|
|
{
|
|
|
|
unsigned char i = 0;
|
|
|
|
unsigned char zerocnt = 0;
|
|
|
|
unsigned char numprinted = 0;
|
|
|
|
char * starting = result;
|
|
|
|
|
|
|
|
*result++='[';
|
|
|
|
while (numprinted < 8) {
|
|
|
|
if ((addr.u16[i] == 0) && (zerocnt == 0)) {
|
|
|
|
while(addr.u16[zerocnt + i] == 0) zerocnt++;
|
|
|
|
if (zerocnt == 1) {
|
|
|
|
*result++ = '0';
|
|
|
|
numprinted++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += zerocnt;
|
|
|
|
numprinted += zerocnt;
|
|
|
|
} else {
|
|
|
|
result += sprintf(result, "%x", (unsigned int)(ntohs(addr.u16[i])));
|
|
|
|
i++;
|
|
|
|
numprinted++;
|
|
|
|
}
|
|
|
|
if (numprinted != 8) *result++ = ':';
|
|
|
|
}
|
|
|
|
*result++=']';
|
|
|
|
*result=0;
|
|
|
|
return (result - starting);
|
|
|
|
}
|
|
|
|
#endif /* UIP_CONF_IPV6 */
|
2006-06-18 00:41:10 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|