osd-contiki/platform/avr-raven/apps/raven-webserver/httpd.c

480 lines
14 KiB
C
Raw Normal View History

2008-10-14 12:14:13 +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>
*
* $Id: httpd.c,v 1.3 2009/07/23 16:16:07 dak664 Exp $
2008-10-14 12:14:13 +02:00
*/
#include <string.h>
#include "contiki-net.h"
#include "webserver.h"
#include "httpd-fs.h"
#include "httpd-cgi.h"
#include "httpd.h"
//#include "lib/petsciiconv.h"
#define petsciiconv_topetscii(...)
//#include "http-strings.h"
#if COFFEE_FILES
#include "cfs-coffee-arch.h"
#endif /* COFFEE_FILES */
/* DEBUGLOGIC is a convenient way to debug in a simulator without a tcp/ip connection.
* Break the program in the process loop and step from the entry in httpd_appcall.
* The input file is forced to /index.html and the output directed to TCPBUF.
* If cgi's are invoked define it in httpd-cgi.c as well.
* psock_generator_send in /core/net/psock.c must also be modified as follows:
* ...
* // Wait until all data is sent and acknowledged.
* if (!s->sendlen) break; //<---add this line
* PT_YIELD_UNTIL(&s->psockpt, uip_acked() || uip_rexmit());
* ...
*/
#define DEBUGLOGIC 0
#define DEBUG 0
#if DEBUGLOGIC
struct httpd_state *sg;
#define uip_mss(...) 512
#define uip_appdata TCPBUF
char TCPBUF[512];
#endif
#if DEBUG
#include <stdio.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else
#define PRINTF(...)
#endif
2008-10-14 12:14:13 +02:00
#ifndef WEBSERVER_CONF_CGI_CONNS
#define CONNS 4
#else
2008-10-14 12:14:13 +02:00
#define CONNS WEBSERVER_CONF_CGI_CONNS
#endif /* WEBSERVER_CONF_CGI_CONNS */
#define STATE_WAITING 0
#define STATE_OUTPUT 1
//#define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, (unsigned int)strlen(str))
2008-10-14 12:14:13 +02:00
MEMB(conns, struct httpd_state, CONNS);
/* MAX_SCRIPT_NAME_LENGTH should be at least be the maximum file name length+2 for %!: includes */
#define MAX_SCRIPT_NAME_LENGTH 20
#define ISO_tab 0x09
2008-10-14 12:14:13 +02:00
#define ISO_nl 0x0a
#define ISO_cr 0x0d
2008-10-14 12:14:13 +02:00
#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;
}
httpd_fs_cpy(uip_appdata, s->file.data, s->len);
#if DEBUGLOGIC
return 0;
#else
2008-10-14 12:14:13 +02:00
return s->len;
#endif
2008-10-14 12:14:13 +02:00
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(send_file(struct httpd_state *s))
{
PSOCK_BEGIN(&s->sout);
do {
2008-10-14 12:14:13 +02:00
PSOCK_GENERATOR_SEND(&s->sout, generate, s);
s->file.len -= s->len;
2008-10-14 12:14:13 +02:00
s->file.data += s->len;
} while(s->file.len > 0);
2008-10-14 12:14:13 +02:00
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(send_part_of_file(struct httpd_state *s))
{
PSOCK_BEGIN(&s->sout);
static int oldfilelen, oldlen;
2008-10-14 12:14:13 +02:00
static char * olddata;
2008-10-14 12:14:13 +02:00
//Store stuff that gets clobbered...
oldfilelen = s->file.len;
oldlen = s->len;
2008-10-14 12:14:13 +02:00
olddata = s->file.data;
2008-10-14 12:14:13 +02:00
//How much to send
s->file.len = s->len;
do {
PSOCK_GENERATOR_SEND(&s->sout, generate, s);
s->file.len -= s->len;
s->file.data += s->len;
} while(s->file.len > 0);
2008-10-14 12:14:13 +02:00
s->len = oldlen;
s->file.len = oldfilelen;
2008-10-14 12:14:13 +02:00
s->file.data = olddata;
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static void
next_scriptstate(struct httpd_state *s)
{
char *p;
/* Skip over any script parameters to the beginning of the next line */
2008-10-14 12:14:13 +02:00
if((p = (char *)httpd_fs_strchr(s->scriptptr, ISO_nl)) != NULL) {
p += 1;
s->scriptlen -= (unsigned short)(p - s->scriptptr);
s->scriptptr = p;
2008-10-14 12:14:13 +02:00
} else {
s->scriptlen = 0;
}
2008-10-14 12:14:13 +02:00
/* char *p;
p = strchr(s->scriptptr, ISO_nl) + 1;
s->scriptlen -= (unsigned short)(p - s->scriptptr);
s->scriptptr = p;*/
}
/*---------------------------------------------------------------------------*/
char *
get_scriptname(char *dest, char *fromfile)
{
uint8_t i=0,skip=1;
/* Extract a file or cgi name, trim leading spaces and replace termination with zero */
/* Returns number of characters processed up to the next non-tab or space */
do {
dest[i]=httpd_fs_getchar(fromfile++);
if (dest[i]==ISO_colon) {if (!skip) break;} //allow leading colons
else if (dest[i]==ISO_tab ) {if (skip) continue;else break;}//skip leading tabs
else if (dest[i]==ISO_space) {if (skip) continue;else break;}//skip leading spaces
else if (dest[i]==ISO_nl ) break; //nl is preferred delimiter
else if (dest[i]==ISO_cr ) break; //some editors insert cr
else if (dest[i]==0 ) break; //files are terminated with null
else skip=0;
i++;
} while (i<(MAX_SCRIPT_NAME_LENGTH+1));
fromfile--;
while ((dest[i]==ISO_space) || (dest[i]==ISO_tab)) dest[i]=httpd_fs_getchar(++fromfile);
dest[i]=0;
return (fromfile);
}
2008-10-14 12:14:13 +02:00
/*---------------------------------------------------------------------------*/
2008-10-14 12:14:13 +02:00
static
PT_THREAD(handle_script(struct httpd_state *s))
{
/* Note script includes will attach a leading : to the filename and a trailing zero */
static char scriptname[MAX_SCRIPT_NAME_LENGTH+1],*pptr;
static uint16_t filelength;
2008-10-14 12:14:13 +02:00
PT_BEGIN(&s->scriptpt);
filelength=s->file.len;
2008-10-14 12:14:13 +02:00
while(s->file.len > 0) {
/* Sanity check */
if (s->file.len > filelength) break;
2008-10-14 12:14:13 +02:00
/* Check if we should start executing a script, flagged by %! */
2008-10-14 12:14:13 +02:00
if(httpd_fs_getchar(s->file.data) == ISO_percent &&
httpd_fs_getchar(s->file.data + 1) == ISO_bang) {
/* Extract name, if starts with colon include file else call cgi */
s->scriptptr=get_scriptname(scriptname,s->file.data+2);
s->scriptlen=s->file.len-(s->scriptptr-s->file.data);
PRINTF("httpd: Handle script named %s\n",scriptname);
if(scriptname[0] == ISO_colon) {
if (httpd_fs_open(&scriptname[1], &s->file)) {
PT_WAIT_THREAD(&s->scriptpt, send_file(s));
}
} else {
PT_WAIT_THREAD(&s->scriptpt,httpd_cgi(scriptname)(s, s->scriptptr));
}
next_scriptstate(s);
2008-10-14 12:14:13 +02:00
/* Reset the pointers and continue sending the current file. */
2008-10-14 12:14:13 +02:00
s->file.data = s->scriptptr;
s->file.len = s->scriptlen;
2008-10-14 12:14:13 +02:00
} else {
/* Send file up to the next potential script */
2008-10-14 12:14:13 +02:00
if(s->file.len > uip_mss()) {
s->len = uip_mss();
2008-10-14 12:14:13 +02:00
} else {
s->len = s->file.len;
}
if(httpd_fs_getchar(s->file.data) == ISO_percent) {
pptr = (char *) httpd_fs_strchr(s->file.data + 1, ISO_percent);
2008-10-14 12:14:13 +02:00
} else {
pptr = (char *) httpd_fs_strchr(s->file.data, ISO_percent);
2008-10-14 12:14:13 +02:00
}
if(pptr != NULL && pptr != s->file.data) {
s->len = (int)(pptr - s->file.data);
if(s->len >= uip_mss()) {
s->len = uip_mss();
}
2008-10-14 12:14:13 +02:00
}
PRINTF("httpd: Sending %u bytes from 0x%04x\n",s->file.len,s->file.data);
2008-10-14 12:14:13 +02:00
PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s));
s->file.data += s->len;
s->file.len -= s->len;
2008-10-14 12:14:13 +02:00
}
}
2008-10-14 12:14:13 +02:00
PT_END(&s->scriptpt);
}
/*---------------------------------------------------------------------------*/
static unsigned short
generate_status_P(void *pstr)
{
uint8_t slen=strlen_P(pstr);
memcpy_P(uip_appdata, PSTR("HTTP/1.0 "), 9);
memcpy_P(uip_appdata+9, pstr, slen);
slen+=9;
memcpy_P(uip_appdata+slen, PSTR("\r\nServer: Contiki/2.0 http://www.sics.se/contiki/\r\nConnection: close\r\n"), 70);
#if DEBUGLOGIC
return 0;
#else
return slen+70;
#endif
}
/*---------------------------------------------------------------------------*/
static unsigned short
generate_header_P(void *pstr)
{
uint8_t slen=strlen_P(pstr);
memcpy_P(uip_appdata,PSTR("Content-type: "),14);
memcpy_P(uip_appdata+14, pstr, slen);
slen+=14;
memcpy_P(uip_appdata+slen,PSTR("\r\n\r\n"),4);
#if DEBUGLOGIC
return 0;
#else
return slen+4;
#endif
}
/*---------------------------------------------------------------------------*/
char http_htm[10] PROGMEM ="text/html";
char http_css[ 9] PROGMEM ="text/css";
char http_png[10] PROGMEM ="image/png";
char http_gif[10] PROGMEM ="image/gif";
char http_jpg[11] PROGMEM ="image/jpeg";
char http_txt[11] PROGMEM ="text/plain";
char http_shtml[ 6] PROGMEM =".shtml";
char index_html[12] PROGMEM ="/index.html";
2008-10-14 12:14:13 +02:00
static
PT_THREAD(send_headers(struct httpd_state *s, char *statushdr))
2008-10-14 12:14:13 +02:00
{
char *ptr;
PSOCK_BEGIN(&s->sout);
PSOCK_GENERATOR_SEND(&s->sout,generate_status_P,statushdr);
2008-10-14 12:14:13 +02:00
ptr = strrchr(s->filename, ISO_period);
if (pgm_read_byte_near(statushdr)=='4') {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P, http_htm);
} else if(ptr == NULL) {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P,PSTR("application/octet-stream"));
2008-10-14 12:14:13 +02:00
} else {
ptr++;
if(strncmp_P(ptr, &http_htm[5],3)== 0 ||strncmp_P(ptr, &http_shtml[1], 4) == 0) {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P, http_htm );
} else if(strcmp_P(ptr, &http_css[5]) == 0) {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P, http_css );
} else if(strcmp_P(ptr, &http_png[6]) == 0) {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P, http_png );
} else if(strcmp_P(ptr, &http_gif[6])== 0) {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P, http_gif );
} else if(strcmp_P(ptr, PSTR("jpg")) == 0) {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P, http_jpg );
} else {
PSOCK_GENERATOR_SEND(&s->sout, generate_header_P, http_txt);
}
2008-10-14 12:14:13 +02:00
}
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(struct httpd_state *s))
{
char *ptr;
PT_BEGIN(&s->outputpt);
#if DEBUGLOGIC
strcpy(s->filename,"/index.html");
#endif
2008-10-14 12:14:13 +02:00
if(!httpd_fs_open(s->filename, &s->file)) {
strcpy_P(s->filename, PSTR("/404.html"));
httpd_fs_open(s->filename, &s->file);
PT_WAIT_THREAD(&s->outputpt, send_headers(s, PSTR("404 Not found")));
PT_WAIT_THREAD(&s->outputpt, send_file(s));
2008-10-14 12:14:13 +02:00
} else {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, PSTR("200 OK")));
2008-10-14 12:14:13 +02:00
ptr = strchr(s->filename, ISO_period);
if((ptr != NULL && strncmp_P(ptr, http_shtml, 6) == 0) || strcmp_P(s->filename,index_html)==0) {
2008-10-14 12:14:13 +02:00
PT_INIT(&s->scriptpt);
PT_WAIT_THREAD(&s->outputpt, handle_script(s));
} else {
PT_WAIT_THREAD(&s->outputpt, send_file(s));
2008-10-14 12:14:13 +02:00
}
}
PSOCK_CLOSE(&s->sout);
PT_END(&s->outputpt);
}
/*---------------------------------------------------------------------------*/
char http_get[4] PROGMEM ="GET ";
char http_ref[8] PROGMEM ="Referer:";
2008-10-14 12:14:13 +02:00
static
PT_THREAD(handle_input(struct httpd_state *s))
{
2008-10-14 12:14:13 +02:00
PSOCK_BEGIN(&s->sin);
PSOCK_READTO(&s->sin, ISO_space);
if(strncmp_P(s->inputbuf, http_get, 4) != 0) {
2008-10-14 12:14:13 +02:00
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) {
strcpy_P(s->filename, index_html);
2008-10-14 12:14:13 +02:00
} else {
s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
strncpy(s->filename, &s->inputbuf[0], sizeof(s->filename));
}
webserver_log_file(&uip_conn->ripaddr, s->filename);
2008-10-14 12:14:13 +02:00
s->state = STATE_OUTPUT;
while(1) {
PSOCK_READTO(&s->sin, ISO_nl);
if(strncmp_P(s->inputbuf, http_ref, 8) == 0) {
2008-10-14 12:14:13 +02:00
s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2);
2008-10-14 12:14:13 +02:00
webserver_log(s->inputbuf);
}
}
PSOCK_END(&s->sin);
}
/*---------------------------------------------------------------------------*/
static void
handle_connection(struct httpd_state *s)
{
#if DEBUGLOGIC
handle_output(s);
#else
2008-10-14 12:14:13 +02:00
handle_input(s);
if(s->state == STATE_OUTPUT) {
handle_output(s);
}
#endif
2008-10-14 12:14:13 +02:00
}
/*---------------------------------------------------------------------------*/
void
httpd_appcall(void *state)
{
#if DEBUGLOGIC
struct httpd_state *s; //Enter here for debugging with output directed to TCPBUF
s = sg = (struct httpd_state *)memb_alloc(&conns);
if (1) {
#else
2008-10-14 12:14:13 +02:00
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;
}
#endif
2008-10-14 12:14:13 +02:00
tcp_markconn(uip_conn, s);
PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
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);
2008-10-14 12:14:13 +02:00
}
} else {
s->timer = 0;
}
handle_connection(s);
} else {
uip_abort();
}
}
/*---------------------------------------------------------------------------*/
void
httpd_init(void)
{
tcp_listen(HTONS(80));
memb_init(&conns);
httpd_cgi_init();
}
/*---------------------------------------------------------------------------*/