2007-09-23 22:45:30 +02:00
|
|
|
/****************************************************************************
|
2007-11-25 17:50:16 +01:00
|
|
|
* netutils/webserver/httpd.c
|
2007-08-27 01:12:17 +02:00
|
|
|
* httpd Web server
|
|
|
|
*
|
2008-02-01 22:23:19 +01:00
|
|
|
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
|
2007-09-23 22:45:30 +02:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-09-23 22:45:30 +02:00
|
|
|
* This is a leverage of similar logic from uIP:
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
* Copyright (c) 2004, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The uIP web server is a very simplistic implementation of an HTTP
|
|
|
|
* server. It can serve web pages and files from a read-only ROM
|
|
|
|
* filesystem, and provides a very small scripting language.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2007-09-23 22:45:30 +02:00
|
|
|
*
|
2007-08-27 01:12:17 +02:00
|
|
|
* 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.
|
2007-09-23 22:45:30 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
#include <sys/socket.h>
|
2007-11-19 19:17:23 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-11-20 00:09:39 +01:00
|
|
|
#include <unistd.h>
|
2007-09-23 22:45:30 +02:00
|
|
|
#include <string.h>
|
2007-11-20 00:09:39 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
2007-11-19 19:17:23 +01:00
|
|
|
#include <debug.h>
|
2007-09-09 19:20:56 +02:00
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <net/uip/uip.h>
|
2007-11-19 19:17:23 +01:00
|
|
|
#include <net/uip/uip-lib.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <net/uip/httpd.h>
|
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
#include "httpd.h"
|
2007-08-27 01:12:17 +02:00
|
|
|
#include "httpd-cgi.h"
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
2007-08-27 01:12:17 +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
|
|
|
|
|
2007-11-28 16:25:09 +01:00
|
|
|
#undef CONFIG_NETUTILS_HTTPD_DUMPBUFFER
|
|
|
|
#undef CONFIG_NETUTILS_HTTPD_DUMPPSTATE
|
2007-11-20 21:32:33 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char g_httpcontenttypebinary[] = "Content-type: application/octet-stream\r\n\r\n";
|
|
|
|
static const char g_httpcontenttypecss[] = "Content-type: text/css\r\n\r\n";
|
|
|
|
static const char g_httpcontenttypegif[] = "Content-type: image/gif\r\n\r\n";
|
|
|
|
static const char g_httpcontenttypehtml[] = "Content-type: text/html\r\n\r\n";
|
|
|
|
static const char g_httpcontenttypejpg[] = "Content-type: image/jpeg\r\n\r\n";
|
|
|
|
static const char g_httpcontenttypeplain[] = "Content-type: text/plain\r\n\r\n";
|
|
|
|
static const char g_httpcontenttypepng[] = "Content-type: image/png\r\n\r\n";
|
|
|
|
|
|
|
|
static const char g_httpextensionhtml[] = ".html";
|
|
|
|
static const char g_httpextensionshtml[] = ".shtml";
|
|
|
|
static const char g_httpextensioncss[] = ".css";
|
|
|
|
static const char g_httpextensionpng[] = ".png";
|
|
|
|
static const char g_httpextensiongif[] = ".gif";
|
|
|
|
static const char g_httpextensionjpg[] = ".jpg";
|
|
|
|
|
|
|
|
static const char g_http404path[] = "/404.html";
|
|
|
|
static const char g_httpindexpath[] = "/index.html";
|
|
|
|
|
|
|
|
static const char g_httpcmdget[] = "GET ";
|
|
|
|
|
|
|
|
static const char g_httpheader200[] =
|
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
"Server: uIP/1.0 http://www.sics.se/~adam/uip/\r\n"
|
|
|
|
"Connection: close\r\n";
|
|
|
|
|
|
|
|
static const char g_httpheader404[] =
|
|
|
|
"HTTP/1.0 404 Not found\r\n"
|
|
|
|
"Server: uIP/1.0 http://www.sics.se/~adam/uip/\r\n"
|
|
|
|
"Connection: close\r\n";
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_DUMPBUFFER
|
2007-11-20 21:32:33 +01:00
|
|
|
static void httpd_dumpbuffer(const char *buffer, ssize_t nbytes)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-19 19:17:23 +01:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
char line[128];
|
|
|
|
int ch;
|
|
|
|
int i;
|
|
|
|
int j;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
for (i = 0; i < nbytes; i += 16)
|
|
|
|
{
|
|
|
|
sprintf(line, "%04x: ", i);
|
|
|
|
for ( j = 0; j < 16; j++)
|
|
|
|
{
|
|
|
|
if (i + j < nbytes)
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
sprintf(&line[strlen(line)], "%02x ", buffer[i+j] );
|
2007-11-19 19:17:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(&line[strlen(line)], " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( j = 0; j < 16; j++)
|
|
|
|
{
|
|
|
|
if (i + j < nbytes)
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ch = buffer[i+j];
|
2007-11-19 19:17:23 +01:00
|
|
|
sprintf(&line[strlen(line)], "%c", ch >= 0x20 && ch <= 0x7e ? ch : '.');
|
|
|
|
}
|
|
|
|
}
|
2007-11-20 00:09:39 +01:00
|
|
|
dbg("%s\n", line);
|
2007-11-19 19:17:23 +01:00
|
|
|
}
|
|
|
|
#endif
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-19 19:17:23 +01:00
|
|
|
#else
|
2007-11-20 21:32:33 +01:00
|
|
|
# define httpd_dumpbuffer(buffer,nbytes)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_DUMPPSTATE
|
|
|
|
static void httpd_dumppstate(struct httpd_state *pstate, const char *msg)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
dbg("[%d] pstate(%p): [%s]\n", pstate->ht_sockfd, pstate, msg);
|
|
|
|
dbg(" filename: [%s]\n", pstate->ht_filename);
|
|
|
|
dbg(" htfile len: %d\n", pstate->ht_file.len);
|
|
|
|
dbg(" sockfd: %d\n", pstate->ht_sockfd);
|
|
|
|
dbg(" scriptptr: %p\n", pstate->ht_scriptptr);
|
|
|
|
dbg(" scriptlen: %d\n", pstate->ht_scriptlen);
|
|
|
|
dbg(" sndlen: %d\n", pstate->ht_sndlen);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define httpd_dumppstate(pstate, msg)
|
2007-11-19 19:17:23 +01:00
|
|
|
#endif
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
static void next_scriptstate(struct httpd_state *pstate)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
char *p;
|
2007-11-20 21:32:33 +01:00
|
|
|
p = strchr(pstate->ht_scriptptr, ISO_nl) + 1;
|
|
|
|
pstate->ht_scriptlen -= (unsigned short)(p - pstate->ht_scriptptr);
|
|
|
|
pstate->ht_scriptptr = p;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
static int handle_script(struct httpd_state *pstate)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
int len;
|
2007-08-27 01:12:17 +02:00
|
|
|
char *ptr;
|
2007-09-09 19:20:56 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
while(pstate->ht_file.len > 0)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
|
|
|
/* Check if we should start executing a script */
|
2007-11-19 19:17:23 +01:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
if (*pstate->ht_file.data == ISO_percent && *(pstate->ht_file.data + 1) == ISO_bang)
|
2007-11-19 19:17:23 +01:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
pstate->ht_scriptptr = pstate->ht_file.data + 3;
|
|
|
|
pstate->ht_scriptlen = pstate->ht_file.len - 3;
|
|
|
|
if (*(pstate->ht_scriptptr - 1) == ISO_colon)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
httpd_fs_open(pstate->ht_scriptptr + 1, &pstate->ht_file);
|
|
|
|
send(pstate->ht_sockfd, pstate->ht_file.data, pstate->ht_file.len, 0);
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
httpd_cgi(pstate->ht_scriptptr)(pstate, pstate->ht_scriptptr);
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
next_scriptstate(pstate);
|
|
|
|
|
|
|
|
/* The script is over, so we reset the pointers and continue
|
|
|
|
* sending the rest of the file
|
|
|
|
*/
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
pstate->ht_file.data = pstate->ht_scriptptr;
|
|
|
|
pstate->ht_file.len = pstate->ht_scriptlen;
|
2007-11-19 19:17:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-20 00:09:39 +01:00
|
|
|
/* See if we find the start of script marker in the block of HTML
|
|
|
|
* to be sent
|
|
|
|
*/
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
if (pstate->ht_file.len > HTTPD_IOBUFFER_SIZE)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
len = HTTPD_IOBUFFER_SIZE;
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
len = pstate->ht_file.len;
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
if (*pstate->ht_file.data == ISO_percent)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ptr = strchr(pstate->ht_file.data + 1, ISO_percent);
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ptr = strchr(pstate->ht_file.data, ISO_percent);
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
if (ptr != NULL && ptr != pstate->ht_file.data)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
len = (int)(ptr - pstate->ht_file.data);
|
|
|
|
if (len >= HTTPD_IOBUFFER_SIZE)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
len = HTTPD_IOBUFFER_SIZE;
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
send(pstate->ht_sockfd, pstate->ht_file.data, len, 0);
|
|
|
|
pstate->ht_file.data += len;
|
|
|
|
pstate->ht_file.len -= len;
|
2007-11-19 19:17:23 +01:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
return OK;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
static int httpd_addchunk(struct httpd_state *pstate, const char *buffer, int len)
|
|
|
|
{
|
|
|
|
int newlen;
|
|
|
|
int chunklen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Determine the size of the next chunk so that it fits into the buffer */
|
|
|
|
|
|
|
|
newlen = pstate->ht_sndlen + len;
|
|
|
|
if (newlen > HTTPD_IOBUFFER_SIZE)
|
|
|
|
{
|
|
|
|
newlen = HTTPD_IOBUFFER_SIZE;
|
|
|
|
chunklen = HTTPD_IOBUFFER_SIZE - pstate->ht_sndlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunklen = len;
|
|
|
|
}
|
|
|
|
vdbg("[%d] sndlen=%d len=%d newlen=%d chunklen=%d\n",
|
|
|
|
pstate->ht_sockfd, pstate->ht_sndlen, len, newlen, chunklen);
|
|
|
|
|
|
|
|
/* Copy that chunk into the send buffer */
|
|
|
|
|
|
|
|
memcpy(&pstate->ht_buffer[pstate->ht_sndlen], buffer, chunklen);
|
|
|
|
|
|
|
|
if (newlen >= HTTPD_IOBUFFER_SIZE)
|
|
|
|
{
|
|
|
|
/* The buffer is full.. Send what we have and reset to send again */
|
|
|
|
|
|
|
|
httpd_dumpbuffer(pstate->ht_buffer, newlen);
|
|
|
|
ret = send(pstate->ht_sockfd, pstate->ht_buffer, newlen, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
newlen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pstate->ht_sndlen = newlen;
|
|
|
|
len -= chunklen;
|
|
|
|
buffer += chunklen;
|
|
|
|
}
|
|
|
|
while (len > 0);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_headers(struct httpd_state *pstate, const char *statushdr, int len)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
char *ptr;
|
2007-09-09 19:20:56 +02:00
|
|
|
int ret;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, statushdr, len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2007-09-09 19:20:56 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
ptr = strrchr(pstate->ht_filename, ISO_period);
|
2007-09-09 19:20:56 +02:00
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, g_httpcontenttypebinary, strlen(g_httpcontenttypebinary));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
else if (strncmp(g_httpextensionhtml, ptr, strlen(g_httpextensionhtml)) == 0 ||
|
|
|
|
strncmp(g_httpextensionshtml, ptr, strlen(g_httpextensionshtml)) == 0)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, g_httpcontenttypehtml, strlen(g_httpcontenttypehtml));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
else if (strncmp(g_httpextensioncss, ptr, strlen(g_httpextensioncss)) == 0)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, g_httpcontenttypecss, strlen(g_httpcontenttypecss));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
else if (strncmp(g_httpextensionpng, ptr, strlen(g_httpextensionpng)) == 0)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, g_httpcontenttypepng, strlen(g_httpcontenttypepng));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
else if (strncmp(g_httpextensiongif, ptr, strlen(g_httpextensiongif)) == 0)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, g_httpcontenttypegif, strlen(g_httpcontenttypegif));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
else if (strncmp(g_httpextensionjpg, ptr, strlen(g_httpextensionjpg)) == 0)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, g_httpcontenttypejpg, strlen(g_httpcontenttypejpg));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = httpd_addchunk(pstate, g_httpcontenttypeplain, strlen(g_httpcontenttypeplain));
|
2007-09-09 19:20:56 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
|
2007-09-09 19:20:56 +02:00
|
|
|
return ret;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
static int httpd_sendfile(struct httpd_state *pstate)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
|
|
|
char *ptr;
|
2007-11-20 21:32:33 +01:00
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
pstate->ht_sndlen = 0;
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
if (!httpd_fs_open(pstate->ht_filename, &pstate->ht_file))
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
memcpy(pstate->ht_filename, g_http404path, strlen(g_http404path));
|
|
|
|
httpd_fs_open(g_http404path, &pstate->ht_file);
|
|
|
|
if (send_headers(pstate, g_httpheader404, strlen(g_httpheader404)) == OK)
|
|
|
|
{
|
|
|
|
ret = httpd_addchunk(pstate, pstate->ht_file.data, pstate->ht_file.len);
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
if (send_headers(pstate, g_httpheader200, strlen(g_httpheader200)) == OK)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ptr = strchr(pstate->ht_filename, ISO_period);
|
|
|
|
if (ptr != NULL &&
|
|
|
|
strncmp(ptr, g_httpextensionshtml, strlen(g_httpextensionshtml)) == 0)
|
|
|
|
{
|
|
|
|
ret = handle_script(pstate);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = httpd_addchunk(pstate, pstate->ht_file.data, pstate->ht_file.len);
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send anything remaining in the buffer */
|
|
|
|
|
|
|
|
if (ret == OK && pstate->ht_sndlen > 0)
|
|
|
|
{
|
|
|
|
httpd_dumpbuffer(pstate->ht_buffer, pstate->ht_sndlen);
|
|
|
|
if (send(pstate->ht_sockfd, pstate->ht_buffer, pstate->ht_sndlen, 0) < 0)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
ret = ERROR;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
|
|
|
|
return ret;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
static inline int httpd_cmd(struct httpd_state *pstate)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-09-09 19:20:56 +02:00
|
|
|
ssize_t recvlen;
|
2007-11-19 19:17:23 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get the next HTTP command. We will handle only GET */
|
2007-09-09 19:20:56 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
recvlen = recv(pstate->ht_sockfd, pstate->ht_buffer, HTTPD_IOBUFFER_SIZE, 0);
|
2007-11-19 19:17:23 +01:00
|
|
|
if (recvlen < 0)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
dbg("[%d] recv failed: %d\n", pstate->ht_sockfd, errno);
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
httpd_dumpbuffer(pstate->ht_buffer, recvlen);
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
/* We will handle only GET */
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
if (strncmp(pstate->ht_buffer, g_httpcmdget, strlen(g_httpcmdget)) != 0)
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
dbg("[%d] Unsupported command\n", pstate->ht_sockfd);
|
2007-09-09 19:20:56 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
/* Get the name of the file to provide */
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
if (pstate->ht_buffer[4] != ISO_slash)
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
dbg("[%d] Missing path\n", pstate->ht_sockfd);
|
2007-11-19 19:17:23 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else if (pstate->ht_buffer[5] == ISO_space)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
strncpy(pstate->ht_filename, g_httpindexpath, strlen(g_httpindexpath));
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-20 21:32:33 +01:00
|
|
|
for (i = 0;
|
2007-11-22 00:28:00 +01:00
|
|
|
i < (HTTPD_MAX_FILENAME-1) && pstate->ht_buffer[i+4] != ISO_space;
|
2007-11-20 21:32:33 +01:00
|
|
|
i++)
|
2007-11-19 19:17:23 +01:00
|
|
|
{
|
2007-11-22 00:28:00 +01:00
|
|
|
pstate->ht_filename[i] = pstate->ht_buffer[i+4];
|
2007-11-19 19:17:23 +01:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
pstate->ht_filename[i]='\0';
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-20 21:32:33 +01:00
|
|
|
dbg("[%d] Filename: %s\n", pstate->ht_sockfd, pstate->ht_filename);
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
/* Then send the file */
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
return httpd_sendfile(pstate);
|
2007-11-19 19:17:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: httpd_handler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Each time a new connection to port 80 is made, a new thread is created
|
|
|
|
* that begins at this entry point. There should be exactly one argument
|
|
|
|
* and it should be the socket descriptor (+1).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
static void *httpd_handler(void *arg)
|
2007-11-19 19:17:23 +01:00
|
|
|
{
|
|
|
|
struct httpd_state *pstate = (struct httpd_state *)malloc(sizeof(struct httpd_state));
|
2007-11-20 00:09:39 +01:00
|
|
|
int sockfd = (int)arg;
|
2007-11-19 19:17:23 +01:00
|
|
|
int ret = ERROR;
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
dbg("[%d] Started\n", sockfd);
|
2007-11-20 00:09:39 +01:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
/* Verify that the state structure was successfully allocated */
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
if (pstate)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-19 19:17:23 +01:00
|
|
|
/* Loop processing each HTTP command */
|
2007-11-20 22:55:06 +01:00
|
|
|
// do
|
2007-09-09 19:20:56 +02:00
|
|
|
{
|
2007-11-19 19:17:23 +01:00
|
|
|
/* Re-initialize the thread state structure */
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
memset(pstate, 0, sizeof(struct httpd_state));
|
2007-11-20 21:32:33 +01:00
|
|
|
pstate->ht_sockfd = sockfd;
|
2007-11-19 19:17:23 +01:00
|
|
|
|
|
|
|
/* Then handle the next httpd command */
|
|
|
|
|
|
|
|
ret = httpd_cmd(pstate);
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-20 22:55:06 +01:00
|
|
|
// while (ret == OK);
|
2007-09-09 19:20:56 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
/* End of command processing -- Clean up and exit */
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
free(pstate);
|
|
|
|
}
|
2007-11-19 19:17:23 +01:00
|
|
|
|
|
|
|
/* Exit the task */
|
|
|
|
|
2007-11-20 21:32:33 +01:00
|
|
|
dbg("[%d] Exitting\n", sockfd);
|
2007-11-20 00:09:39 +01:00
|
|
|
close(sockfd);
|
2008-12-12 19:01:25 +01:00
|
|
|
return NULL;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: httpd_listen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the main processing thread for the webserver. It never returns
|
|
|
|
* unless an error occurs
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int httpd_listen(void)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-19 19:17:23 +01:00
|
|
|
/* Execute httpd_handler on each connection to port 80 */
|
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
uip_server(HTONS(80), httpd_handler, CONFIG_NETUTILS_HTTPDSTACKSIZE);
|
2007-11-19 19:17:23 +01:00
|
|
|
|
|
|
|
/* uip_server only returns on errors */
|
|
|
|
|
|
|
|
return ERROR;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: httpd_init
|
|
|
|
*
|
|
|
|
* Description:
|
2007-11-19 19:17:23 +01:00
|
|
|
* This function initializes the web server and should be called at system
|
|
|
|
* boot-up.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-09-23 22:45:30 +02:00
|
|
|
****************************************************************************/
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
void httpd_init(void)
|
|
|
|
{
|
|
|
|
}
|