2009-07-12 01:40:17 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* netutils/thttpd/thttpd.c
|
|
|
|
|
* Tiny HTTP Server
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
|
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
|
*
|
|
|
|
|
* Derived from the file of the same name in the original THTTPD package:
|
|
|
|
|
*
|
|
|
|
|
* Copyright <EFBFBD> 1995,1998,1999,2000,2001 by Jef Poskanzer <jef@mail.acme.com>.
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
|
|
#include <nuttx/compiler.h>
|
2009-07-12 20:46:11 +02:00
|
|
|
|
#include <nuttx/symtab.h>
|
|
|
|
|
#include <net/uip/thttpd.h>
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-07-12 18:45:27 +02:00
|
|
|
|
#include "config.h"
|
2009-07-12 01:40:17 +02:00
|
|
|
|
#include "fdwatch.h"
|
|
|
|
|
#include "libhttpd.h"
|
|
|
|
|
#include "timers.h"
|
|
|
|
|
|
2009-07-12 18:45:27 +02:00
|
|
|
|
#ifdef CONFIG_THTTPD
|
|
|
|
|
|
2009-07-12 01:40:17 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
|
# define MAXPATHLEN 64
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* The connection states */
|
|
|
|
|
|
|
|
|
|
#define CNST_FREE 0
|
|
|
|
|
#define CNST_READING 1
|
|
|
|
|
#define CNST_SENDING 2
|
2009-08-16 21:24:18 +02:00
|
|
|
|
#define CNST_LINGERING 3
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
#define SPARE_FDS 2
|
|
|
|
|
#define AVAILABLE_FDS (CONFIG_NSOCKET_DESCRIPTORS - SPARE_FDS)
|
|
|
|
|
|
2009-07-12 01:40:17 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Types
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
struct connect_s
|
|
|
|
|
{
|
|
|
|
|
int conn_state;
|
|
|
|
|
int next_free_connect;
|
|
|
|
|
httpd_conn *hc;
|
|
|
|
|
time_t active_at;
|
|
|
|
|
Timer *wakeup_timer;
|
|
|
|
|
Timer *linger_timer;
|
|
|
|
|
off_t end_offset; /* The final offset+1 of the file to send */
|
|
|
|
|
off_t offset; /* The current offset into the file to send */
|
|
|
|
|
boolean eof; /* Set TRUE when length==0 read from file */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Data
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static httpd_server *hs = NULL;
|
|
|
|
|
static struct connect_s *connects;
|
|
|
|
|
static int num_connects;
|
|
|
|
|
static int first_free_connect;
|
|
|
|
|
static int httpd_conn_count;
|
2009-07-18 20:04:05 +02:00
|
|
|
|
static struct fdwatch_s *fw;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Data
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int terminate = 0;
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
time_t start_time;
|
|
|
|
|
time_t stats_time;
|
|
|
|
|
long stats_connections;
|
|
|
|
|
off_t stats_bytes;
|
|
|
|
|
int stats_simultaneous;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Function Prototypes
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static void shut_down(void);
|
|
|
|
|
static int handle_newconnect(struct timeval *tv, int listen_fd);
|
|
|
|
|
static void handle_read(struct connect_s *conn, struct timeval *tv);
|
|
|
|
|
static void handle_send(struct connect_s *conn, struct timeval *tv);
|
|
|
|
|
static void handle_linger(struct connect_s *conn, struct timeval *tv);
|
|
|
|
|
static void finish_connection(struct connect_s *conn, struct timeval *tv);
|
|
|
|
|
static void clear_connection(struct connect_s *conn, struct timeval *tv);
|
|
|
|
|
static void really_clear_connection(struct connect_s *conn, struct timeval *tv);
|
|
|
|
|
static void idle(ClientData client_data, struct timeval *nowP);
|
|
|
|
|
static void linger_clear_connection(ClientData client_data, struct timeval *nowP);
|
|
|
|
|
static void occasional(ClientData client_data, struct timeval *nowP);
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
static void logstats(struct timeval *nowP);
|
|
|
|
|
static void thttpd_logstats(long secs);
|
|
|
|
|
#else
|
|
|
|
|
# define logstats(nowP)
|
|
|
|
|
# define thttpd_logstats(secs)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static void shut_down(void)
|
|
|
|
|
{
|
|
|
|
|
int cnum;
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
(void)gettimeofday(&tv, NULL);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
logstats(&tv);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
for (cnum = 0; cnum < AVAILABLE_FDS; ++cnum)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
if (connects[cnum].conn_state != CNST_FREE)
|
|
|
|
|
{
|
|
|
|
|
httpd_close_conn(connects[cnum].hc, &tv);
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (connects[cnum].hc != NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
httpd_destroy_conn(connects[cnum].hc);
|
|
|
|
|
free((void *)connects[cnum].hc);
|
|
|
|
|
--httpd_conn_count;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
connects[cnum].hc = NULL;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 17:08:09 +02:00
|
|
|
|
if (hs)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
httpd_server *ths = hs;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
hs = NULL;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
if (ths->listen_fd != -1)
|
|
|
|
|
{
|
2009-07-18 20:04:05 +02:00
|
|
|
|
fdwatch_del_fd(fw, ths->listen_fd);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
httpd_terminate(ths);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmr_destroy();
|
|
|
|
|
free((void *)connects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int handle_newconnect(struct timeval *tv, int listen_fd)
|
|
|
|
|
{
|
|
|
|
|
struct connect_s *conn;
|
|
|
|
|
ClientData client_data;
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
/* This loops until the accept() fails, trying to start new connections as
|
|
|
|
|
* fast as possible so we don't overrun the listen queue.
|
2009-07-12 01:40:17 +02:00
|
|
|
|
*/
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
nvdbg("New connection(s) on listen_fd %d\n", listen_fd);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
/* Is there room in the connection table? */
|
2009-08-03 01:35:27 +02:00
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
if (num_connects >= AVAILABLE_FDS)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
/* Out of connection slots. Run the timers, then the existing
|
|
|
|
|
* connections, and maybe we'll free up a slot by the time we get
|
2009-08-03 01:35:27 +02:00
|
|
|
|
* back here.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-07-12 01:40:17 +02:00
|
|
|
|
ndbg("too many connections!\n");
|
|
|
|
|
tmr_run(tv);
|
2009-08-03 01:35:27 +02:00
|
|
|
|
return -1;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the first free connection entry off the free list */
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
#ifdef CONFIG_DEBUG
|
2009-07-12 01:40:17 +02:00
|
|
|
|
if (first_free_connect == -1 ||
|
|
|
|
|
connects[first_free_connect].conn_state != CNST_FREE)
|
|
|
|
|
{
|
|
|
|
|
ndbg("the connects free list is messed up\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2009-08-03 01:35:27 +02:00
|
|
|
|
#endif
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn = &connects[first_free_connect];
|
|
|
|
|
|
|
|
|
|
/* Make the httpd_conn if necessary */
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
if (!conn->hc)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
conn->hc = NEW(httpd_conn, 1);
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (conn->hc == NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
ndbg("out of memory allocating an httpd_conn\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2009-08-03 01:35:27 +02:00
|
|
|
|
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn->hc->initialized = 0;
|
2009-08-03 01:35:27 +02:00
|
|
|
|
httpd_conn_count++;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the connection */
|
|
|
|
|
|
|
|
|
|
switch (httpd_get_conn(hs, listen_fd, conn->hc))
|
|
|
|
|
{
|
|
|
|
|
/* Some error happened. Run the timers, then the existing
|
|
|
|
|
* connections. Maybe the error will clear.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
case GC_FAIL:
|
|
|
|
|
tmr_run(tv);
|
2009-08-03 01:35:27 +02:00
|
|
|
|
return -1;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
|
|
|
|
/* No more connections to accept for now */
|
|
|
|
|
|
|
|
|
|
case GC_NO_MORE:
|
2009-08-03 01:35:27 +02:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
nvdbg("New connection fd %d\n", conn->hc->conn_fd);
|
|
|
|
|
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn->conn_state = CNST_READING;
|
|
|
|
|
|
|
|
|
|
/* Pop it off the free list */
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
first_free_connect = conn->next_free_connect;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn->next_free_connect = -1;
|
2009-08-03 01:35:27 +02:00
|
|
|
|
num_connects++;
|
|
|
|
|
client_data.p = conn;
|
|
|
|
|
conn->active_at = tv->tv_sec;
|
|
|
|
|
conn->wakeup_timer = NULL;
|
|
|
|
|
conn->linger_timer = NULL;
|
|
|
|
|
conn->offset = 0;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
|
|
|
|
/* Set the connection file descriptor to no-delay mode */
|
|
|
|
|
|
|
|
|
|
httpd_set_ndelay(conn->hc->conn_fd);
|
2009-08-16 17:26:52 +02:00
|
|
|
|
fdwatch_add_fd(fw, conn->hc->conn_fd, conn);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
++stats_connections;
|
|
|
|
|
if (num_connects > stats_simultaneous)
|
2009-08-03 01:35:27 +02:00
|
|
|
|
{
|
|
|
|
|
stats_simultaneous = num_connects;
|
|
|
|
|
}
|
2009-07-12 01:40:17 +02:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handle_read(struct connect_s *conn, struct timeval *tv)
|
|
|
|
|
{
|
|
|
|
|
ClientData client_data;
|
|
|
|
|
httpd_conn *hc = conn->hc;
|
|
|
|
|
off_t actual;
|
|
|
|
|
int sz;
|
|
|
|
|
|
|
|
|
|
/* Is there room in our buffer to read more bytes? */
|
|
|
|
|
|
|
|
|
|
if (hc->read_idx >= hc->read_size)
|
|
|
|
|
{
|
2009-08-03 01:35:27 +02:00
|
|
|
|
if (hc->read_size > CONFIG_THTTPD_MAXREALLOC)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-15 21:36:06 +02:00
|
|
|
|
BADREQUEST("MAXREALLOC");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
goto errout_with_400;
|
|
|
|
|
}
|
2009-08-03 01:35:27 +02:00
|
|
|
|
httpd_realloc_str(&hc->read_buf, &hc->read_size, hc->read_size + CONFIG_THTTPD_REALLOCINCR);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read some more bytes */
|
|
|
|
|
|
|
|
|
|
sz = read(hc->conn_fd, &(hc->read_buf[hc->read_idx]), hc->read_size - hc->read_idx);
|
|
|
|
|
if (sz == 0)
|
|
|
|
|
{
|
2009-08-15 21:36:06 +02:00
|
|
|
|
BADREQUEST("EOF");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
goto errout_with_400;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sz < 0)
|
|
|
|
|
{
|
2009-08-15 21:36:06 +02:00
|
|
|
|
/* Ignore EINTR and EAGAIN. Also ignore EWOULDBLOCK. At first glance
|
2009-07-12 01:40:17 +02:00
|
|
|
|
* you would think that connections returned by fdwatch as readable
|
|
|
|
|
* should never give an EWOULDBLOCK; however, this apparently can
|
|
|
|
|
* happen if a packet gets garbled.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-08-15 21:36:06 +02:00
|
|
|
|
|
|
|
|
|
ndbg("read(fd=%d) failed: %d\n", hc->conn_fd, errno);
|
|
|
|
|
BADREQUEST("read");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
goto errout_with_400;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hc->read_idx += sz;
|
|
|
|
|
conn->active_at = tv->tv_sec;
|
|
|
|
|
|
|
|
|
|
/* Do we have a complete request yet? */
|
|
|
|
|
|
|
|
|
|
switch (httpd_got_request(hc))
|
|
|
|
|
{
|
|
|
|
|
case GR_NO_REQUEST:
|
|
|
|
|
return;
|
|
|
|
|
case GR_BAD_REQUEST:
|
2009-08-15 21:36:06 +02:00
|
|
|
|
BADREQUEST("httpd_got_request");
|
|
|
|
|
goto errout_with_400;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Yes. Try parsing and resolving it */
|
|
|
|
|
|
|
|
|
|
if (httpd_parse_request(hc) < 0)
|
|
|
|
|
{
|
|
|
|
|
goto errout_with_connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Start the connection going */
|
|
|
|
|
|
|
|
|
|
if (httpd_start_request(hc, tv) < 0)
|
|
|
|
|
{
|
|
|
|
|
/* Something went wrong. Close down the connection */
|
|
|
|
|
|
|
|
|
|
goto errout_with_connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set up the file offsets to read */
|
|
|
|
|
|
|
|
|
|
conn->eof = FALSE;
|
|
|
|
|
if (hc->got_range)
|
|
|
|
|
{
|
|
|
|
|
conn->offset = hc->range_start;
|
|
|
|
|
conn->end_offset = hc->range_end + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
conn->offset = 0;
|
|
|
|
|
if (hc->bytes_to_send < 0)
|
|
|
|
|
{
|
|
|
|
|
conn->end_offset = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
conn->end_offset = hc->bytes_to_send;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if it's already handled */
|
|
|
|
|
|
|
|
|
|
if (hc->file_fd < 0)
|
|
|
|
|
{
|
|
|
|
|
/* No file descriptor means someone else is handling it */
|
|
|
|
|
|
|
|
|
|
conn->offset = hc->bytes_sent;
|
|
|
|
|
goto errout_with_connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (conn->offset >= conn->end_offset)
|
|
|
|
|
{
|
|
|
|
|
/* There's nothing to send */
|
|
|
|
|
|
|
|
|
|
goto errout_with_connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Seek to the offset of the next byte to send */
|
|
|
|
|
|
|
|
|
|
actual = lseek(hc->file_fd, conn->offset, SEEK_SET);
|
|
|
|
|
if (actual != conn->offset)
|
|
|
|
|
{
|
|
|
|
|
ndbg("fseek to %d failed: offset=%d errno=%d\n", conn->offset, actual, errno);
|
2009-08-15 21:36:06 +02:00
|
|
|
|
BADREQUEST("lseek");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
goto errout_with_400;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We have a valid connection and a file to send to it */
|
|
|
|
|
|
|
|
|
|
conn->conn_state = CNST_SENDING;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
client_data.p = conn;
|
2009-07-18 20:04:05 +02:00
|
|
|
|
fdwatch_del_fd(fw, hc->conn_fd);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
errout_with_400:
|
2009-08-15 21:36:06 +02:00
|
|
|
|
BADREQUEST("errout");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
httpd_send_err(hc, 400, httpd_err400title, "", httpd_err400form, "");
|
|
|
|
|
|
|
|
|
|
errout_with_connection:
|
|
|
|
|
finish_connection(conn, tv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int read_buffer(struct connect_s *conn)
|
|
|
|
|
{
|
|
|
|
|
httpd_conn *hc = conn->hc;
|
|
|
|
|
ssize_t nread = 0;
|
|
|
|
|
|
|
|
|
|
if (hc->buflen < CONFIG_THTTPD_IOBUFFERSIZE && !conn->eof)
|
|
|
|
|
{
|
|
|
|
|
nread = read(hc->file_fd, &hc->buffer[hc->buflen],
|
|
|
|
|
CONFIG_THTTPD_IOBUFFERSIZE - hc->buflen);
|
|
|
|
|
if (nread == 0)
|
|
|
|
|
{
|
|
|
|
|
/* Reading zero bytes means we are at the end of file */
|
|
|
|
|
|
|
|
|
|
conn->end_offset = conn->offset;
|
|
|
|
|
conn->eof = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (nread > 0)
|
|
|
|
|
{
|
|
|
|
|
hc->buflen += nread;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handle_send(struct connect_s *conn, struct timeval *tv)
|
|
|
|
|
{
|
|
|
|
|
httpd_conn *hc = conn->hc;
|
|
|
|
|
int nwritten;
|
|
|
|
|
int nread;
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
/* Read until the entire file is sent -- this could take awhile!! */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
while (conn->offset < conn->end_offset)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-16 17:26:52 +02:00
|
|
|
|
nvdbg("offset: %d end_offset: %d bytes_sent: %d\n",
|
|
|
|
|
conn->offset, conn->end_offset, conn->hc->bytes_sent);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
/* Fill the rest of the response buffer with file data */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
nread = read_buffer(conn);
|
|
|
|
|
if (nread < 0)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-16 17:26:52 +02:00
|
|
|
|
ndbg("File read error: %d\n", errno);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
goto errout_clear_connection;
|
|
|
|
|
}
|
2009-08-16 17:26:52 +02:00
|
|
|
|
nvdbg("Read %d bytes, buflen %d\n", nread, hc->buflen);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
/* Send the buffer */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (hc->buflen > 0)
|
|
|
|
|
{
|
|
|
|
|
/* httpd_write does not return until all bytes have been sent
|
|
|
|
|
* (or an error occurs).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
nwritten = httpd_write(hc->conn_fd, hc->buffer, hc->buflen);
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
|
|
|
|
ndbg("Error sending %s: %d\n", hc->encodedurl, errno);
|
|
|
|
|
goto errout_clear_connection;
|
|
|
|
|
}
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
/* We wrote one full buffer of data (httpd_write does not
|
|
|
|
|
* return until the full buffer is written (or an error occurs).
|
|
|
|
|
*/
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
conn->active_at = tv->tv_sec;
|
|
|
|
|
hc->buflen = 0;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
/* And update how much of the file we wrote */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
conn->offset += nwritten;
|
|
|
|
|
conn->hc->bytes_sent += nwritten;
|
|
|
|
|
nvdbg("Wrote %d bytes\n", nwritten);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-16 16:29:08 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
/* The file transfer is complete -- finish the connection */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
nvdbg("Finish connection\n");
|
|
|
|
|
finish_connection(conn, tv);
|
2009-08-16 16:29:08 +02:00
|
|
|
|
return;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
|
|
|
|
errout_clear_connection:
|
2009-08-16 16:29:08 +02:00
|
|
|
|
ndbg("Clear connection\n");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
clear_connection(conn, tv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handle_linger(struct connect_s *conn, struct timeval *tv)
|
|
|
|
|
{
|
|
|
|
|
char buf[4096];
|
|
|
|
|
int ret;
|
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
/* In lingering-close mode we just read and ignore bytes. An error or EOF
|
2009-07-12 01:40:17 +02:00
|
|
|
|
* ends things, otherwise we go until a timeout
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
ret = read(conn->hc->conn_fd, buf, sizeof(buf));
|
|
|
|
|
if (ret < 0 && (errno == EINTR || errno == EAGAIN))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret <= 0)
|
|
|
|
|
{
|
|
|
|
|
really_clear_connection(conn, tv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void finish_connection(struct connect_s *conn, struct timeval *tv)
|
|
|
|
|
{
|
|
|
|
|
/* If we haven't actually sent the buffered response yet, do so now */
|
|
|
|
|
|
|
|
|
|
httpd_write_response(conn->hc);
|
|
|
|
|
|
|
|
|
|
/* And clear */
|
|
|
|
|
|
|
|
|
|
clear_connection(conn, tv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void clear_connection(struct connect_s *conn, struct timeval *tv)
|
|
|
|
|
{
|
|
|
|
|
ClientData client_data;
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (conn->wakeup_timer != NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
tmr_cancel(conn->wakeup_timer);
|
|
|
|
|
conn->wakeup_timer = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is our version of Apache's lingering_close() routine, which is
|
|
|
|
|
* their version of the often-broken SO_LINGER socket option. For why
|
|
|
|
|
* this is necessary, see http://www.apache.org/docs/misc/fin_wait_2.html
|
|
|
|
|
* What we do is delay the actual closing for a few seconds, while reading
|
|
|
|
|
* any bytes that come over the connection. However, we don't want to do
|
|
|
|
|
* this unless it's necessary, because it ties up a connection slot and
|
|
|
|
|
* file descriptor which means our maximum connection-handling rateis
|
|
|
|
|
* lower. So, elsewhere we set a flag when we detect the few
|
|
|
|
|
* circumstances that make a lingering close necessary. If the flag isn't
|
|
|
|
|
* set we do the real close now.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (conn->conn_state == CNST_LINGERING)
|
|
|
|
|
{
|
|
|
|
|
/* If we were already lingering, shut down for real */
|
|
|
|
|
|
|
|
|
|
tmr_cancel(conn->linger_timer);
|
2009-08-16 21:24:18 +02:00
|
|
|
|
conn->linger_timer = NULL;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn->hc->should_linger = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (conn->hc->should_linger)
|
|
|
|
|
{
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_del_fd(fw, conn->hc->conn_fd);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn->conn_state = CNST_LINGERING;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
fdwatch_add_fd(fw, conn->hc->conn_fd, conn);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
client_data.p = conn;
|
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
conn->linger_timer = tmr_create(tv, linger_clear_connection, client_data,
|
|
|
|
|
CONFIG_THTTPD_LINGER_MSEC, 0);
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (conn->linger_timer != NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-16 21:24:18 +02:00
|
|
|
|
return;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
2009-08-16 21:24:18 +02:00
|
|
|
|
ndbg("tmr_create(linger_clear_connection) failed\n");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
2009-08-16 21:24:18 +02:00
|
|
|
|
|
|
|
|
|
/* Either we are done lingering, we shouldn't linger, or we failed to setup the linger */
|
|
|
|
|
|
|
|
|
|
really_clear_connection(conn, tv);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void really_clear_connection(struct connect_s *conn, struct timeval *tv)
|
|
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
stats_bytes += conn->hc->bytes_sent;
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_del_fd(fw, conn->hc->conn_fd);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
httpd_close_conn(conn->hc, tv);
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (conn->linger_timer != NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
tmr_cancel(conn->linger_timer);
|
|
|
|
|
conn->linger_timer = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
conn->conn_state = CNST_FREE;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn->next_free_connect = first_free_connect;
|
|
|
|
|
first_free_connect = conn - connects; /* division by sizeof is implied */
|
|
|
|
|
num_connects--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void idle(ClientData client_data, struct timeval *nowP)
|
|
|
|
|
{
|
|
|
|
|
int cnum;
|
|
|
|
|
struct connect_s *conn;
|
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
for (cnum = 0; cnum < AVAILABLE_FDS; ++cnum)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
conn = &connects[cnum];
|
|
|
|
|
switch (conn->conn_state)
|
|
|
|
|
{
|
|
|
|
|
case CNST_READING:
|
|
|
|
|
if (nowP->tv_sec - conn->active_at >= CONFIG_THTTPD_IDLE_READ_LIMIT_SEC)
|
|
|
|
|
{
|
|
|
|
|
ndbg("%s connection timed out reading\n", httpd_ntoa(&conn->hc->client_addr));
|
|
|
|
|
httpd_send_err(conn->hc, 408, httpd_err408title, "",
|
|
|
|
|
httpd_err408form, "");
|
|
|
|
|
finish_connection(conn, nowP);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CNST_SENDING:
|
|
|
|
|
if (nowP->tv_sec - conn->active_at >= CONFIG_THTTPD_IDLE_SEND_LIMIT_SEC)
|
|
|
|
|
{
|
|
|
|
|
ndbg("%s connection timed out sending\n", httpd_ntoa(&conn->hc->client_addr));
|
|
|
|
|
clear_connection(conn, nowP);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void linger_clear_connection(ClientData client_data, struct timeval *nowP)
|
|
|
|
|
{
|
|
|
|
|
struct connect_s *conn;
|
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
nvdbg("Clear connection\n");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
conn = (struct connect_s *) client_data.p;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
conn->linger_timer = NULL;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
really_clear_connection(conn, nowP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void occasional(ClientData client_data, struct timeval *nowP)
|
|
|
|
|
{
|
|
|
|
|
tmr_cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Generate debugging statistics ndbg messages for all packages */
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
static void logstats(struct timeval *nowP)
|
|
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
time_t now;
|
|
|
|
|
long up_secs;
|
|
|
|
|
long stats_secs;
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (!nowP)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-16 17:26:52 +02:00
|
|
|
|
(void)gettimeofday(&tv, NULL);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
nowP = &tv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now = nowP->tv_sec;
|
|
|
|
|
up_secs = now - start_time;
|
|
|
|
|
stats_secs = now - stats_time;
|
|
|
|
|
if (stats_secs == 0)
|
|
|
|
|
{
|
|
|
|
|
stats_secs = 1; /* fudge */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats_time = now;
|
|
|
|
|
ndbg("up %ld seconds, stats for %ld seconds\n", up_secs, stats_secs);
|
|
|
|
|
|
|
|
|
|
thttpd_logstats(stats_secs);
|
|
|
|
|
httpd_logstats(stats_secs);
|
2009-07-18 20:04:05 +02:00
|
|
|
|
fdwatch_logstats(fw, stats_secs);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
tmr_logstats(stats_secs);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Generate debugging statistics */
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
static void thttpd_logstats(long secs)
|
|
|
|
|
{
|
|
|
|
|
if (secs > 0)
|
|
|
|
|
{
|
|
|
|
|
ndbg("thttpd - %ld connections (%g/sec), %d max simultaneous, %lld bytes (%g/sec), %d httpd_conns allocated\n",
|
|
|
|
|
stats_connections, (float)stats_connections / secs,
|
|
|
|
|
stats_simultaneous, (sint64) stats_bytes,
|
|
|
|
|
(float)stats_bytes / secs, httpd_conn_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats_connections = 0;
|
|
|
|
|
stats_bytes = 0;
|
|
|
|
|
stats_simultaneous = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2009-07-12 20:46:11 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Function: thttpd_main
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* This function is the entrypoint into the THTTPD server. It does not
|
|
|
|
|
* return. It may be called, the normal mechanism for starting the server
|
|
|
|
|
* is:
|
|
|
|
|
*
|
|
|
|
|
* 1) Set is g_thttpdsymtab and g_thttpdnsymbols. The user is required
|
|
|
|
|
* to provide a symbol table to use for binding CGI programs (if CGI
|
|
|
|
|
* is enabled. See examples/nxflat and examples/thttpd for examples of
|
|
|
|
|
* how such a symbol table may be created.
|
|
|
|
|
* 2) Call task_create() to start thttpd_main()
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2009-07-12 01:40:17 +02:00
|
|
|
|
int thttpd_main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
int num_ready;
|
|
|
|
|
int cnum;
|
2009-07-12 20:46:11 +02:00
|
|
|
|
FAR struct connect_s *conn;
|
|
|
|
|
FAR httpd_conn *hc;
|
2009-08-02 18:08:20 +02:00
|
|
|
|
httpd_sockaddr sa;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
struct timeval tv;
|
2009-07-18 21:47:08 +02:00
|
|
|
|
#ifdef CONFIG_THTTPD_DIR
|
|
|
|
|
int ret;
|
|
|
|
|
#endif
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-02 17:08:09 +02:00
|
|
|
|
nvdbg("THTTPD started\n");
|
|
|
|
|
|
2009-07-12 01:40:17 +02:00
|
|
|
|
/* Setup host address */
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2009-07-12 20:46:11 +02:00
|
|
|
|
# error "IPv6 support not yet implemented"
|
2009-07-12 01:40:17 +02:00
|
|
|
|
#else
|
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
|
sa.sin_port = HTONS(CONFIG_THTTPD_PORT);
|
|
|
|
|
sa.sin_addr.s_addr = HTONL(CONFIG_THTTPD_IPADDR);
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
/* Initialize the fdwatch package to handle all of the configured
|
|
|
|
|
* socket descriptors
|
|
|
|
|
*/
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
fw = fdwatch_initialize(CONFIG_NSOCKET_DESCRIPTORS);
|
|
|
|
|
if (!fw)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
ndbg("fdwatch initialization failure\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Switch directories again if requested */
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_DATADIR
|
|
|
|
|
if (chdir(CONFIG_THTTPD_DATADIR) < 0)
|
|
|
|
|
{
|
|
|
|
|
ndbg("chdir to %s: %d\n", CONFIG_THTTPD_DATADIR, errno);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Initialize the timer package */
|
|
|
|
|
|
|
|
|
|
tmr_init();
|
|
|
|
|
|
|
|
|
|
/* Initialize the HTTP layer */
|
|
|
|
|
|
2009-08-02 17:08:09 +02:00
|
|
|
|
nvdbg("Calling httpd_initialize()\n");
|
2009-08-15 22:25:13 +02:00
|
|
|
|
hs = httpd_initialize(&sa);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
if (!hs)
|
|
|
|
|
{
|
2009-08-02 17:08:09 +02:00
|
|
|
|
ndbg("httpd_initialize() failed\n");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set up the occasional timer */
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (tmr_create(NULL, occasional, JunkClientData, CONFIG_THTTPD_OCCASIONAL_MSEC * 1000L, 1) == NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
ndbg("tmr_create(occasional) failed\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set up the idle timer */
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (tmr_create(NULL, idle, JunkClientData, 5 * 1000L, 1) == NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
ndbg("tmr_create(idle) failed\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
{
|
|
|
|
|
struct timeval ts;
|
|
|
|
|
gettimeofday(&ts, NULL);
|
2009-08-16 17:26:52 +02:00
|
|
|
|
start_time = ts.tv_sec;
|
|
|
|
|
stats_time = ts.tv_sec;
|
|
|
|
|
stats_connections = 0;
|
|
|
|
|
stats_bytes = 0;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
stats_simultaneous = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Initialize our connections table */
|
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
connects = NEW(struct connect_s, AVAILABLE_FDS);
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (connects == NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-16 17:26:52 +02:00
|
|
|
|
ndbg("Out of memory allocating a struct connect_s\n");
|
2009-07-12 01:40:17 +02:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
for (cnum = 0; cnum < AVAILABLE_FDS; ++cnum)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-16 17:26:52 +02:00
|
|
|
|
connects[cnum].conn_state = CNST_FREE;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
connects[cnum].next_free_connect = cnum + 1;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
connects[cnum].hc = NULL;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
connects[AVAILABLE_FDS - 1].next_free_connect = -1; /* end of link list */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
first_free_connect = 0;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
num_connects = 0;
|
|
|
|
|
httpd_conn_count = 0;
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
if (hs != NULL)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
if (hs->listen_fd != -1)
|
2009-08-16 17:26:52 +02:00
|
|
|
|
{
|
|
|
|
|
fdwatch_add_fd(fw, hs->listen_fd, NULL);
|
|
|
|
|
}
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Main loop */
|
|
|
|
|
|
2009-08-02 17:08:09 +02:00
|
|
|
|
nvdbg("Entering the main loop\n");
|
2009-08-16 17:26:52 +02:00
|
|
|
|
(void)gettimeofday(&tv, NULL);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
while ((!terminate) || num_connects > 0)
|
|
|
|
|
{
|
|
|
|
|
/* Do the fd watch */
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
num_ready = fdwatch(fw, tmr_mstimeout(&tv));
|
2009-07-12 01:40:17 +02:00
|
|
|
|
if (num_ready < 0)
|
|
|
|
|
{
|
|
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
2009-08-03 01:35:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* Not errors... try again */
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
ndbg("fdwatch failed: %d\n", errno);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
(void)gettimeofday(&tv, NULL);
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
|
|
|
|
if (num_ready == 0)
|
|
|
|
|
{
|
|
|
|
|
/* No fd's are ready - run the timers */
|
|
|
|
|
|
|
|
|
|
tmr_run(&tv);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Is it a new connection? */
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
if (fdwatch_check_fd(fw, hs->listen_fd))
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-03 01:35:27 +02:00
|
|
|
|
if (!handle_newconnect(&tv, hs->listen_fd))
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
|
|
|
|
/* Go around the loop and do another fdwatch, rather than
|
|
|
|
|
* dropping through and processing existing connections. New
|
|
|
|
|
* connections always get priority.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find the connections that need servicing */
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
while ((conn = (struct connect_s*)fdwatch_get_next_client_data(fw)) != (struct connect_s*)-1)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-03 01:35:27 +02:00
|
|
|
|
if (conn)
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-03 01:35:27 +02:00
|
|
|
|
hc = conn->hc;
|
|
|
|
|
if (!fdwatch_check_fd(fw, hc->conn_fd))
|
|
|
|
|
{
|
|
|
|
|
/* Something went wrong */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
nvdbg("Clearing connection\n");
|
|
|
|
|
clear_connection(conn, &tv);
|
|
|
|
|
}
|
|
|
|
|
else
|
2009-07-12 01:40:17 +02:00
|
|
|
|
{
|
2009-08-03 01:35:27 +02:00
|
|
|
|
nvdbg("Handle conn_state %d\n", conn->conn_state);
|
|
|
|
|
switch (conn->conn_state)
|
|
|
|
|
{
|
|
|
|
|
case CNST_READING:
|
2009-08-16 17:26:52 +02:00
|
|
|
|
{
|
|
|
|
|
handle_read(conn, &tv);
|
|
|
|
|
|
|
|
|
|
/* If a GET request was received and a file is ready to
|
|
|
|
|
* be sent, then fall through to send the file.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (conn->conn_state != CNST_SENDING)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
case CNST_SENDING:
|
2009-08-16 17:26:52 +02:00
|
|
|
|
{
|
|
|
|
|
/* Send a file -- this really should be performed on a
|
|
|
|
|
* separate thread to keep the serve from locking up during
|
|
|
|
|
* the write.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
handle_send(conn, &tv);
|
|
|
|
|
}
|
2009-08-03 01:35:27 +02:00
|
|
|
|
break;
|
2009-08-16 17:26:52 +02:00
|
|
|
|
|
2009-08-03 01:35:27 +02:00
|
|
|
|
case CNST_LINGERING:
|
2009-08-16 17:26:52 +02:00
|
|
|
|
{
|
|
|
|
|
/* Linger close the connection */
|
|
|
|
|
|
|
|
|
|
handle_linger(conn, &tv);
|
|
|
|
|
}
|
2009-08-03 01:35:27 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2009-07-12 01:40:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tmr_run(&tv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The main loop terminated */
|
|
|
|
|
|
|
|
|
|
shut_down();
|
|
|
|
|
ndbg("Exiting\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-12 18:45:27 +02:00
|
|
|
|
#endif /* CONFIG_THTTPD */
|
2009-07-12 01:40:17 +02:00
|
|
|
|
|