2003-02-20 07:01:34 +00:00
|
|
|
/* +----------------------------------------------------------------------- *\
|
|
|
|
| */ static const char pkgname[] = "darkhttpd/0.1"; /*
|
|
|
|
| */ static const char copyright[] = "copyright (c) 2003 Emil Mikulic"; /*
|
|
|
|
+----------------------------------------------------------------------- */
|
|
|
|
|
2003-02-20 04:25:12 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2003-03-01 07:01:56 +00:00
|
|
|
/*
|
|
|
|
* TODO:
|
2003-03-01 10:10:27 +00:00
|
|
|
* x Ignore SIGPIPE.
|
2003-03-01 09:07:56 +00:00
|
|
|
* x Actually serve files.
|
2003-03-01 07:01:56 +00:00
|
|
|
* . Generate directory entries.
|
2003-03-01 10:00:06 +00:00
|
|
|
* x Log to file.
|
2003-03-01 07:01:56 +00:00
|
|
|
* . Partial content.
|
2003-03-01 09:07:56 +00:00
|
|
|
* . If-Modified-Since.
|
2003-03-01 07:01:56 +00:00
|
|
|
* . Keep-alive connections.
|
2003-03-01 07:13:12 +00:00
|
|
|
* . Chroot, set{uid|gid}.
|
|
|
|
* . Port to Win32.
|
2003-03-01 07:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2003-02-20 04:25:12 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2003-03-01 09:07:56 +00:00
|
|
|
#include <sys/stat.h>
|
2003-02-26 11:09:43 +00:00
|
|
|
#include <sys/queue.h>
|
2003-02-20 04:25:12 +00:00
|
|
|
#include <netinet/in.h>
|
2003-02-20 10:31:34 +00:00
|
|
|
#include <arpa/inet.h>
|
2003-03-01 10:00:06 +00:00
|
|
|
#include <assert.h>
|
2003-02-28 11:21:31 +00:00
|
|
|
#include <ctype.h>
|
2003-02-20 04:25:12 +00:00
|
|
|
#include <err.h>
|
2003-03-01 09:07:56 +00:00
|
|
|
#include <errno.h>
|
2003-03-01 10:10:27 +00:00
|
|
|
#include <signal.h>
|
2003-03-01 09:07:56 +00:00
|
|
|
#include <stdarg.h>
|
2003-02-20 07:01:34 +00:00
|
|
|
#include <stdio.h>
|
2003-02-20 04:25:12 +00:00
|
|
|
#include <stdlib.h>
|
2003-02-21 00:53:45 +00:00
|
|
|
#include <string.h>
|
2003-02-27 03:21:36 +00:00
|
|
|
#include <time.h>
|
2003-02-20 04:25:12 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
/* for easy defusal */
|
|
|
|
#define debugf printf
|
|
|
|
|
2003-03-01 09:07:56 +00:00
|
|
|
#ifndef min
|
|
|
|
#define min(a,b) ( ((a)<(b)) ? (a) : (b) )
|
|
|
|
#endif
|
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
LIST_HEAD(conn_list_head, connection) connlist =
|
|
|
|
LIST_HEAD_INITIALIZER(conn_list_head);
|
|
|
|
|
2003-02-21 00:53:45 +00:00
|
|
|
struct connection
|
|
|
|
{
|
2003-02-26 11:09:43 +00:00
|
|
|
LIST_ENTRY(connection) entries;
|
|
|
|
|
2003-02-21 00:53:45 +00:00
|
|
|
int socket;
|
2003-02-27 03:21:36 +00:00
|
|
|
in_addr_t client;
|
|
|
|
time_t last_active;
|
2003-02-21 00:53:45 +00:00
|
|
|
enum {
|
|
|
|
RECV_REQUEST, /* receiving request */
|
|
|
|
SEND_HEADER, /* sending generated header */
|
|
|
|
SEND_REPLY, /* sending reply */
|
|
|
|
DONE /* connection closed, need to remove from queue */
|
|
|
|
} state;
|
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* char request[request_length+1] is null-terminated */
|
2003-02-21 00:53:45 +00:00
|
|
|
char *request;
|
|
|
|
unsigned int request_length;
|
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
char *method, *uri;
|
|
|
|
|
2003-02-21 00:53:45 +00:00
|
|
|
char *header;
|
|
|
|
unsigned int header_sent, header_length;
|
2003-03-01 10:00:06 +00:00
|
|
|
int header_dont_free, header_only, http_code;
|
2003-02-21 00:53:45 +00:00
|
|
|
|
|
|
|
enum { REPLY_GENERATED, REPLY_FROMFILE } reply_type;
|
|
|
|
char *reply;
|
2003-02-27 03:21:36 +00:00
|
|
|
int reply_dont_free;
|
2003-02-21 00:53:45 +00:00
|
|
|
FILE *reply_file;
|
|
|
|
unsigned int reply_sent, reply_length;
|
2003-03-01 10:00:06 +00:00
|
|
|
|
|
|
|
unsigned int total_sent;
|
2003-02-21 00:53:45 +00:00
|
|
|
};
|
2003-02-20 04:25:12 +00:00
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
|
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* If a connection is idle for IDLETIME seconds or more, it gets closed and
|
|
|
|
* removed from the connlist.
|
|
|
|
*/
|
|
|
|
#define IDLETIME 60
|
|
|
|
|
2003-02-27 23:41:44 +00:00
|
|
|
/* To prevent a malformed request from eating up too much memory, die once the
|
|
|
|
* request exceeds this many bytes:
|
|
|
|
*/
|
2003-03-01 09:07:56 +00:00
|
|
|
#define MAX_REQUEST_LENGTH 4000
|
2003-02-27 23:41:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* Defaults can be overridden on the command-line */
|
2003-02-20 10:31:34 +00:00
|
|
|
static in_addr_t bindaddr = INADDR_ANY;
|
2003-02-20 07:01:34 +00:00
|
|
|
static u_int16_t bindport = 80;
|
2003-03-01 07:13:12 +00:00
|
|
|
static int max_connections = -1; /* kern.ipc.somaxconn */
|
2003-03-01 10:00:06 +00:00
|
|
|
static const char *index_name = "index.html";
|
2003-02-20 04:25:12 +00:00
|
|
|
|
2003-03-01 07:13:12 +00:00
|
|
|
static int sockin = -1; /* socket to accept connections from */
|
|
|
|
static char *wwwroot = NULL; /* a path name */
|
2003-02-20 10:31:34 +00:00
|
|
|
static char *logfile_name = NULL; /* NULL = no logging */
|
2003-03-01 10:00:06 +00:00
|
|
|
static FILE *logfile = NULL;
|
2003-02-20 10:31:34 +00:00
|
|
|
static int want_chroot = 0;
|
2003-02-20 04:25:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-20 07:01:34 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Initialize the sockin global. This is the socket that we accept
|
|
|
|
* connections from.
|
|
|
|
*/
|
|
|
|
static void init_sockin(void)
|
2003-02-20 04:25:12 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in addrin;
|
|
|
|
int sockopt;
|
|
|
|
|
|
|
|
/* create incoming socket */
|
|
|
|
sockin = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sockin == -1) err(1, "socket()");
|
|
|
|
|
|
|
|
/* reuse address */
|
|
|
|
sockopt = 1;
|
|
|
|
if (setsockopt(sockin, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&sockopt, sizeof(sockopt)) == -1)
|
|
|
|
err(1, "setsockopt(SO_REUSEADDR)");
|
|
|
|
|
|
|
|
/* bind socket */
|
|
|
|
addrin.sin_family = (u_char)PF_INET;
|
2003-02-20 07:01:34 +00:00
|
|
|
addrin.sin_port = htons(bindport);
|
|
|
|
addrin.sin_addr.s_addr = bindaddr;
|
2003-02-20 04:25:12 +00:00
|
|
|
memset(&(addrin.sin_zero), 0, 8);
|
|
|
|
if (bind(sockin, (struct sockaddr *)&addrin,
|
|
|
|
sizeof(struct sockaddr)) == -1)
|
2003-02-20 07:01:34 +00:00
|
|
|
err(1, "bind(port %u)", bindport);
|
2003-02-20 04:25:12 +00:00
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
debugf("listening on %s:%u\n", inet_ntoa(addrin.sin_addr), bindport);
|
2003-02-20 10:31:34 +00:00
|
|
|
|
2003-02-20 04:25:12 +00:00
|
|
|
/* listen on socket */
|
2003-02-20 07:01:34 +00:00
|
|
|
if (listen(sockin, max_connections) == -1)
|
2003-02-20 04:25:12 +00:00
|
|
|
err(1, "listen()");
|
2003-02-20 07:01:34 +00:00
|
|
|
}
|
|
|
|
|
2003-02-20 04:25:12 +00:00
|
|
|
|
|
|
|
|
2003-02-20 07:01:34 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Prints a usage statement.
|
|
|
|
*/
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("\n usage: darkhttpd /path/to/wwwroot [options]\n\n"
|
2003-02-27 23:41:44 +00:00
|
|
|
"options:\n\n"
|
|
|
|
"\t--port number (default: %u)\n" /* bindport */
|
2003-02-20 07:01:34 +00:00
|
|
|
"\t\tSpecifies which port to listen on for connections.\n"
|
|
|
|
"\n"
|
2003-02-20 10:31:34 +00:00
|
|
|
"\t--addr ip (default: all)\n"
|
|
|
|
"\t\tIf multiple interfaces are present, specifies\n"
|
|
|
|
"\t\twhich one to bind the listening port to.\n"
|
2003-02-20 07:01:34 +00:00
|
|
|
"\n"
|
|
|
|
"\t--maxconn number (default: system maximum)\n"
|
|
|
|
"\t\tSpecifies how many concurrent connections to accept.\n"
|
|
|
|
"\n"
|
|
|
|
"\t--log filename (default: no logging)\n"
|
|
|
|
"\t\tSpecifies which file to log requests to.\n"
|
|
|
|
"\n"
|
|
|
|
"\t--chroot (default: don't chroot)\n"
|
|
|
|
"\t\tLocks server into wwwroot directory for added security.\n"
|
|
|
|
"\n"
|
2003-03-01 07:13:12 +00:00
|
|
|
"\t--index filename (default: %s)\n" /* index_name */
|
|
|
|
"\t\tDefault file to serve when a directory is requested.\n"
|
|
|
|
"\n"
|
2003-02-20 10:31:34 +00:00
|
|
|
/* "\t--uid blah, --gid blah\n" FIXME */
|
2003-03-01 07:13:12 +00:00
|
|
|
, bindport, index_name);
|
2003-02-20 07:01:34 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Parses commandline options.
|
|
|
|
*/
|
|
|
|
static void parse_commandline(const int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc < 2) usage(); /* no wwwroot given */
|
|
|
|
wwwroot = argv[1];
|
|
|
|
|
|
|
|
/* walk through the remainder of the arguments (if any) */
|
|
|
|
for (i=2; i<argc; i++)
|
|
|
|
{
|
2003-02-20 10:31:34 +00:00
|
|
|
if (strcmp(argv[i], "--port") == 0)
|
|
|
|
{
|
|
|
|
if (++i >= argc) errx(1, "missing number after --port");
|
|
|
|
bindport = (u_int16_t)atoi(argv[i]);
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "--addr") == 0)
|
|
|
|
{
|
|
|
|
if (++i >= argc) errx(1, "missing ip after --addr");
|
|
|
|
bindaddr = inet_addr(argv[i]);
|
|
|
|
if (bindaddr == (in_addr_t)INADDR_NONE)
|
|
|
|
errx(1, "malformed --addr argument");
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "--maxconn") == 0)
|
|
|
|
{
|
|
|
|
if (++i >= argc) errx(1, "missing number after --maxconn");
|
|
|
|
max_connections = atoi(argv[i]);
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "--log") == 0)
|
|
|
|
{
|
|
|
|
if (++i >= argc) errx(1, "missing filename after --log");
|
|
|
|
logfile_name = argv[i];
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "--chroot") == 0)
|
|
|
|
{
|
|
|
|
want_chroot = 1;
|
|
|
|
}
|
2003-03-01 07:13:12 +00:00
|
|
|
else if (strcmp(argv[i], "--index") == 0)
|
|
|
|
{
|
|
|
|
if (++i >= argc) errx(1, "missing filename after --index");
|
|
|
|
index_name = argv[i];
|
|
|
|
}
|
2003-02-20 10:31:34 +00:00
|
|
|
else
|
|
|
|
errx(1, "unknown argument `%s'", argv[i]);
|
2003-02-20 07:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* malloc that errx()s if it can't allocate.
|
|
|
|
*/
|
|
|
|
static void *xmalloc(const size_t size)
|
|
|
|
{
|
|
|
|
void *ptr = malloc(size);
|
|
|
|
if (ptr == NULL) errx(1, "can't allocate %u bytes", size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-21 00:53:45 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
2003-02-27 03:21:36 +00:00
|
|
|
* Allocate and initialize an empty connection.
|
2003-02-21 00:53:45 +00:00
|
|
|
*/
|
2003-02-27 03:21:36 +00:00
|
|
|
static struct connection *new_connection(void)
|
2003-02-21 00:53:45 +00:00
|
|
|
{
|
2003-02-27 03:21:36 +00:00
|
|
|
struct connection *conn = (struct connection *)
|
|
|
|
xmalloc(sizeof(struct connection));
|
2003-02-26 11:09:43 +00:00
|
|
|
|
|
|
|
conn->socket = -1;
|
2003-02-27 03:21:36 +00:00
|
|
|
conn->client = INADDR_ANY;
|
|
|
|
conn->last_active = time(NULL);
|
2003-02-26 11:09:43 +00:00
|
|
|
conn->request = NULL;
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->method = conn->uri = NULL;
|
2003-02-26 11:09:43 +00:00
|
|
|
conn->request_length = 0;
|
|
|
|
conn->header = NULL;
|
|
|
|
conn->header_sent = conn->header_length = 0;
|
2003-03-01 09:07:56 +00:00
|
|
|
conn->header_dont_free = conn->header_only = 0;
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->http_code = 0;
|
2003-02-26 11:09:43 +00:00
|
|
|
conn->reply = NULL;
|
2003-03-01 09:07:56 +00:00
|
|
|
conn->reply_dont_free = 0;
|
2003-02-26 11:09:43 +00:00
|
|
|
conn->reply_file = NULL;
|
2003-03-01 07:44:51 +00:00
|
|
|
conn->reply_sent = conn->reply_length = 0;
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->total_sent = 0;
|
2003-02-26 11:09:43 +00:00
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* Make it harmless so it gets garbage-collected if it should, for some
|
|
|
|
* reason, fail to be correctly filled out.
|
|
|
|
*/
|
|
|
|
conn->state = DONE;
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Accept a connection from sockin and add it to the connection queue.
|
|
|
|
*/
|
|
|
|
static void accept_connection(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addrin;
|
|
|
|
socklen_t sin_size;
|
|
|
|
struct connection *conn;
|
|
|
|
|
|
|
|
/* allocate and initialise struct connection */
|
|
|
|
conn = new_connection();
|
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
sin_size = (socklen_t)sizeof(struct sockaddr);
|
|
|
|
conn->socket = accept(sockin, (struct sockaddr *)&addrin,
|
|
|
|
&sin_size);
|
|
|
|
if (conn->socket == -1) err(1, "accept()");
|
|
|
|
|
|
|
|
conn->state = RECV_REQUEST;
|
2003-02-27 03:21:36 +00:00
|
|
|
conn->client = addrin.sin_addr.s_addr;
|
2003-02-26 11:09:43 +00:00
|
|
|
LIST_INSERT_HEAD(&connlist, conn, entries);
|
|
|
|
|
|
|
|
debugf("accepted connection from %s:%u\n",
|
|
|
|
inet_ntoa(addrin.sin_addr),
|
|
|
|
ntohs(addrin.sin_port) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Cleanly deallocate the internals of a struct connection
|
|
|
|
*/
|
|
|
|
static void free_connection(struct connection *conn)
|
|
|
|
{
|
|
|
|
if (conn->socket != -1) close(conn->socket);
|
|
|
|
if (conn->request != NULL) free(conn->request);
|
2003-03-01 10:00:06 +00:00
|
|
|
if (conn->method != NULL) free(conn->method);
|
|
|
|
if (conn->uri != NULL) free(conn->uri);
|
2003-02-27 03:21:36 +00:00
|
|
|
if (conn->header != NULL && !conn->header_dont_free) free(conn->header);
|
|
|
|
if (conn->reply != NULL && !conn->reply_dont_free) free(conn->reply);
|
2003-02-26 11:09:43 +00:00
|
|
|
if (conn->reply_file != NULL) fclose(conn->reply_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* realloc that errx()s if it can't allocate.
|
|
|
|
*/
|
|
|
|
static void *xrealloc(void *original, const size_t size)
|
|
|
|
{
|
|
|
|
void *ptr = realloc(original, size);
|
|
|
|
if (ptr == NULL) errx(1, "can't reallocate %u bytes", size);
|
|
|
|
return ptr;
|
2003-02-21 00:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-28 11:21:31 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Uppercasify all characters in a string of given length.
|
|
|
|
*/
|
|
|
|
static void strntoupper(char *str, const int length)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<length; i++)
|
|
|
|
str[i] = toupper(str[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* If a connection has been idle for more than IDLETIME seconds, it will be
|
|
|
|
* marked as DONE and killed off in httpd_poll()
|
|
|
|
*/
|
|
|
|
static void poll_check_timeout(struct connection *conn)
|
|
|
|
{
|
|
|
|
if (time(NULL) - conn->last_active >= IDLETIME)
|
|
|
|
conn->state = DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-28 00:09:41 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Build an RFC1123 date in the static buffer _date[] and return it.
|
|
|
|
*/
|
|
|
|
#define MAX_DATE_LENGTH 29 /* strlen("Fri, 28 Feb 2003 00:02:08 GMT") */
|
|
|
|
static char _date[MAX_DATE_LENGTH + 1];
|
2003-02-28 11:21:31 +00:00
|
|
|
static char *rfc1123_date(const time_t when)
|
2003-02-28 00:09:41 +00:00
|
|
|
{
|
2003-02-28 11:21:31 +00:00
|
|
|
time_t now = when;
|
2003-02-28 00:09:41 +00:00
|
|
|
strftime(_date, MAX_DATE_LENGTH,
|
|
|
|
"%a, %d %b %Y %H:%M:%S %Z", gmtime(&now) );
|
|
|
|
return _date;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-01 07:44:51 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Decode URL by converting %XX (where XX are hexadecimal digits) to the
|
|
|
|
* character it represents. Don't forget to free the return value.
|
|
|
|
*/
|
|
|
|
static char *urldecode(const char *url)
|
|
|
|
{
|
|
|
|
int len = strlen(url);
|
|
|
|
char *out = (char*)xmalloc(len+1);
|
|
|
|
int i, pos;
|
|
|
|
|
|
|
|
for (i=0, pos=0; i<len; i++)
|
|
|
|
{
|
|
|
|
if (url[i] == '%' && i+2 < len &&
|
|
|
|
isxdigit(url[i+1]) && isxdigit(url[i+2]))
|
|
|
|
{
|
|
|
|
/* decode %XX */
|
|
|
|
#define HEX_TO_DIGIT(hex) ( \
|
|
|
|
((hex) >= 'A' && (hex) <= 'F') ? ((hex)-'A'+10): \
|
|
|
|
((hex) >= 'a' && (hex) <= 'f') ? ((hex)-'a'+10): \
|
|
|
|
((hex)-'0') )
|
|
|
|
|
|
|
|
out[pos++] = HEX_TO_DIGIT(url[i+1]) * 16 +
|
|
|
|
HEX_TO_DIGIT(url[i+2]);
|
|
|
|
i += 2;
|
|
|
|
|
|
|
|
#undef HEX_TO_DIGIT
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* straight copy */
|
|
|
|
out[pos++] = url[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out[pos] = 0;
|
|
|
|
|
2003-03-01 09:07:56 +00:00
|
|
|
out = xrealloc(out, strlen(out)+1); /* dealloc what we don't need */
|
2003-03-01 07:44:51 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
2003-03-01 07:01:56 +00:00
|
|
|
* A default reply for any (erroneous) occasion.
|
2003-02-27 03:21:36 +00:00
|
|
|
*/
|
|
|
|
static void default_reply(struct connection *conn,
|
2003-03-01 09:07:56 +00:00
|
|
|
const int errcode, const char *errname, const char *format, ...)
|
2003-02-27 03:21:36 +00:00
|
|
|
{
|
2003-03-01 09:07:56 +00:00
|
|
|
char *reason;
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
vasprintf(&reason, format, va);
|
|
|
|
va_end(va);
|
2003-03-01 10:00:06 +00:00
|
|
|
if (reason == NULL) errx(1, "out of memory in vasprintf()");
|
2003-03-01 09:07:56 +00:00
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
conn->reply_length = asprintf(&(conn->reply),
|
|
|
|
"<html><head><title>%d %s</title></head><body>\n"
|
2003-02-28 11:21:31 +00:00
|
|
|
"<h1>%s</h1>\n" /* errname */
|
|
|
|
"%s\n" /* reason */
|
|
|
|
"<hr>\n"
|
|
|
|
"Generated by %s on %s\n"
|
2003-02-27 03:21:36 +00:00
|
|
|
"</body></html>\n",
|
2003-02-28 11:21:31 +00:00
|
|
|
errcode, errname, errname, reason, pkgname, rfc1123_date(time(NULL)));
|
2003-03-01 09:07:56 +00:00
|
|
|
free(reason);
|
2003-02-27 03:21:36 +00:00
|
|
|
if (conn->reply == NULL) errx(1, "out of memory in asprintf()");
|
|
|
|
|
|
|
|
conn->header_length = asprintf(&(conn->header),
|
|
|
|
"HTTP/1.1 %d %s\r\n"
|
2003-02-28 00:09:41 +00:00
|
|
|
"Date: %s\r\n"
|
2003-02-27 03:21:36 +00:00
|
|
|
"Server: %s\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Content-Length: %d\r\n"
|
|
|
|
"Content-Type: text/html\r\n"
|
|
|
|
"\r\n",
|
2003-02-28 11:21:31 +00:00
|
|
|
errcode, errname, rfc1123_date(time(NULL)), pkgname, conn->reply_length);
|
2003-02-27 03:21:36 +00:00
|
|
|
if (conn->header == NULL) errx(1, "out of memory in asprintf()");
|
2003-03-01 10:00:06 +00:00
|
|
|
|
2003-02-27 03:58:42 +00:00
|
|
|
conn->reply_type = REPLY_GENERATED;
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->http_code = errcode;
|
2003-02-27 03:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-28 00:09:41 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
2003-02-28 10:39:43 +00:00
|
|
|
* Parse an HTTP request like "GET / HTTP/1.1" to get the method (GET) and the
|
|
|
|
* url (/). Remember to deallocate the method and url buffers.
|
2003-02-28 00:09:41 +00:00
|
|
|
*/
|
2003-03-01 10:00:06 +00:00
|
|
|
static void parse_request(struct connection *conn)
|
2003-02-28 00:09:41 +00:00
|
|
|
{
|
2003-03-01 10:00:06 +00:00
|
|
|
unsigned int bound1, bound2;
|
|
|
|
|
|
|
|
assert(conn->request_length == strlen(conn->request));
|
2003-02-28 10:39:43 +00:00
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
for (bound1 = 0; bound1 < conn->request_length &&
|
|
|
|
conn->request[bound1] != ' '; bound1++);
|
2003-02-28 10:39:43 +00:00
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->method = (char*)xmalloc(bound1 + 1);
|
|
|
|
memcpy(conn->method, conn->request, bound1);
|
|
|
|
conn->method[bound1] = 0;
|
|
|
|
strntoupper(conn->method, bound1);
|
2003-02-28 10:39:43 +00:00
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
for (bound2=bound1+1; bound2 < conn->request_length &&
|
|
|
|
conn->request[bound2] != ' ' &&
|
|
|
|
conn->request[bound2] != '\r'; bound2++);
|
2003-02-28 10:39:43 +00:00
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->uri = (char*)xmalloc(bound2 - bound1);
|
|
|
|
memcpy(conn->uri, conn->request + bound1 + 1, bound2 - bound1 - 1);
|
|
|
|
conn->uri[bound2 - bound1 - 1] = 0;
|
2003-02-28 11:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Process a GET/HEAD request
|
|
|
|
*/
|
2003-03-01 10:00:06 +00:00
|
|
|
static void process_get(struct connection *conn)
|
2003-02-28 11:21:31 +00:00
|
|
|
{
|
2003-03-01 09:07:56 +00:00
|
|
|
char *decoded_url, *target, *tmp;
|
|
|
|
struct stat filestat;
|
2003-03-01 07:44:51 +00:00
|
|
|
|
2003-03-01 09:07:56 +00:00
|
|
|
/* work out path of file being requested */
|
2003-03-01 10:00:06 +00:00
|
|
|
decoded_url = urldecode(conn->uri);
|
2003-03-01 09:07:56 +00:00
|
|
|
/* FIXME: ensure this is a safe URL, i.e. no '../' */
|
2003-03-01 07:44:51 +00:00
|
|
|
if (decoded_url[strlen(decoded_url)-1] == '/')
|
|
|
|
{
|
|
|
|
asprintf(&target, "%s%s%s", wwwroot, decoded_url, index_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asprintf(&target, "%s%s", wwwroot, decoded_url);
|
|
|
|
}
|
|
|
|
free(decoded_url);
|
|
|
|
|
|
|
|
debugf(">>>%s<<<\n", target);
|
2003-03-01 09:07:56 +00:00
|
|
|
|
|
|
|
conn->reply_file = fopen(target, "rb");
|
2003-03-01 07:44:51 +00:00
|
|
|
free(target);
|
2003-03-01 09:07:56 +00:00
|
|
|
if (conn->reply_file == NULL)
|
|
|
|
{
|
|
|
|
/* fopen() failed */
|
|
|
|
if (errno == ENOENT)
|
|
|
|
default_reply(conn, 404, "Not Found",
|
2003-03-01 10:00:06 +00:00
|
|
|
"The URI you requested (%s) was not found.", conn->uri);
|
2003-03-01 09:07:56 +00:00
|
|
|
else
|
|
|
|
default_reply(conn, 403, "Forbidden",
|
|
|
|
"The URI you requested (%s) cannot be returned.<br>\n"
|
|
|
|
"%s.", /* reason why */
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->uri, strerror(errno));
|
2003-03-01 09:07:56 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2003-03-01 07:44:51 +00:00
|
|
|
|
2003-03-01 09:07:56 +00:00
|
|
|
/* get information on the file */
|
|
|
|
if (fstat(fileno(conn->reply_file), &filestat) == -1)
|
|
|
|
{
|
|
|
|
default_reply(conn, 500, "Internal Server Error",
|
|
|
|
"fstat() failed: %s.", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->reply_type = REPLY_FROMFILE;
|
|
|
|
conn->reply_length = filestat.st_size;
|
|
|
|
|
|
|
|
asprintf(&tmp,
|
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
"Date: %s\r\n"
|
|
|
|
"Server: %s\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Content-Length: %d\r\n"
|
|
|
|
"Content-Type: text/plain\r\n" /* FIXME */
|
|
|
|
, rfc1123_date(time(NULL)), pkgname, conn->reply_length
|
|
|
|
);
|
2003-03-01 10:00:06 +00:00
|
|
|
if (tmp == NULL) errx(1, "out of memory in asprintf()");
|
2003-03-01 09:07:56 +00:00
|
|
|
|
|
|
|
conn->header_length = asprintf(&(conn->header),
|
|
|
|
"%s"
|
|
|
|
"Last-Modified: %s\r\n"
|
|
|
|
"\r\n"
|
|
|
|
, tmp, rfc1123_date(filestat.st_mtime)
|
|
|
|
);
|
|
|
|
free(tmp);
|
2003-03-01 10:00:06 +00:00
|
|
|
if (conn->header == NULL) errx(1, "out of memory in asprintf()");
|
|
|
|
conn->http_code = 200;
|
2003-02-28 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Process a request: build the header and reply, advance state.
|
|
|
|
*/
|
|
|
|
static void process_request(struct connection *conn)
|
|
|
|
{
|
2003-03-01 10:00:06 +00:00
|
|
|
parse_request(conn);
|
|
|
|
debugf("method=``%s'', url=``%s''\n", conn->method, conn->uri);
|
2003-02-27 03:21:36 +00:00
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
if (strcmp(conn->method, "GET") == 0)
|
|
|
|
process_get(conn);
|
|
|
|
else if (strcmp(conn->method, "HEAD") == 0)
|
2003-03-01 09:07:56 +00:00
|
|
|
{
|
2003-03-01 10:00:06 +00:00
|
|
|
process_get(conn);
|
2003-03-01 09:07:56 +00:00
|
|
|
conn->header_only = 1;
|
|
|
|
}
|
2003-03-01 10:00:06 +00:00
|
|
|
else if (strcmp(conn->method, "OPTIONS") == 0 ||
|
|
|
|
strcmp(conn->method, "POST") == 0 ||
|
|
|
|
strcmp(conn->method, "PUT") == 0 ||
|
|
|
|
strcmp(conn->method, "DELETE") == 0 ||
|
|
|
|
strcmp(conn->method, "TRACE") == 0 ||
|
|
|
|
strcmp(conn->method, "CONNECT") == 0)
|
2003-02-28 11:21:31 +00:00
|
|
|
default_reply(conn, 501, "Not Implemented",
|
2003-03-01 10:00:06 +00:00
|
|
|
"The method you specified (%s) is not implemented.",
|
|
|
|
conn->method);
|
2003-02-28 11:21:31 +00:00
|
|
|
else
|
|
|
|
default_reply(conn, 400, "Bad Request",
|
2003-03-01 10:00:06 +00:00
|
|
|
"%s is not a valid HTTP/1.1 method.", conn->method);
|
2003-02-28 11:21:31 +00:00
|
|
|
|
|
|
|
/* advance state */
|
2003-02-27 03:58:42 +00:00
|
|
|
conn->state = SEND_HEADER;
|
2003-02-27 03:21:36 +00:00
|
|
|
|
2003-02-28 11:21:31 +00:00
|
|
|
/* request, method, url not needed anymore */
|
|
|
|
debugf("%s", conn->request);
|
2003-02-27 03:58:42 +00:00
|
|
|
free(conn->request);
|
|
|
|
conn->request = NULL;
|
|
|
|
debugf("%s-=-\n", conn->header);
|
2003-02-27 03:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Receiving request.
|
|
|
|
*/
|
|
|
|
static void poll_recv_request(struct connection *conn)
|
|
|
|
{
|
|
|
|
#define BUFSIZE 65536
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
ssize_t recvd;
|
|
|
|
|
|
|
|
recvd = recv(conn->socket, buf, BUFSIZE, 0);
|
|
|
|
if (recvd == -1) err(1, "recv()");
|
|
|
|
if (recvd == 0)
|
|
|
|
{
|
|
|
|
/* socket closed on us */
|
|
|
|
conn->state = DONE;
|
|
|
|
return;
|
|
|
|
}
|
2003-02-27 03:21:36 +00:00
|
|
|
conn->last_active = time(NULL);
|
|
|
|
#undef BUFSIZE
|
2003-02-26 11:09:43 +00:00
|
|
|
|
|
|
|
/* append to conn->request */
|
2003-03-01 09:07:56 +00:00
|
|
|
conn->request = xrealloc(conn->request, conn->request_length+recvd+1);
|
2003-02-26 11:09:43 +00:00
|
|
|
memcpy(conn->request+conn->request_length, buf, recvd);
|
|
|
|
conn->request_length += recvd;
|
2003-02-27 03:21:36 +00:00
|
|
|
conn->request[conn->request_length] = 0;
|
2003-02-26 11:09:43 +00:00
|
|
|
|
2003-02-27 03:21:36 +00:00
|
|
|
/* process request if we have all of it */
|
|
|
|
if (conn->request_length > 4 &&
|
|
|
|
memcmp(conn->request+conn->request_length-4, "\r\n\r\n", 4) == 0)
|
|
|
|
process_request(conn);
|
2003-02-27 23:41:44 +00:00
|
|
|
|
|
|
|
/* die if it's too long */
|
|
|
|
if (conn->request_length > MAX_REQUEST_LENGTH)
|
|
|
|
{
|
2003-02-28 11:21:31 +00:00
|
|
|
default_reply(conn, 413, "Request Entity Too Large",
|
|
|
|
"Your request was dropped because it was too long.");
|
2003-02-27 23:41:44 +00:00
|
|
|
conn->state = SEND_HEADER;
|
|
|
|
}
|
2003-02-26 11:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-27 03:58:42 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
2003-03-01 09:07:56 +00:00
|
|
|
* Sending header. Assumes conn->header is not NULL.
|
2003-02-27 03:58:42 +00:00
|
|
|
*/
|
2003-02-26 11:09:43 +00:00
|
|
|
static void poll_send_header(struct connection *conn)
|
2003-02-27 03:58:42 +00:00
|
|
|
{
|
2003-03-01 10:00:06 +00:00
|
|
|
ssize_t sent;
|
|
|
|
|
|
|
|
assert(conn->header_length == strlen(conn->header));
|
|
|
|
|
|
|
|
sent = send(conn->socket, conn->header + conn->header_sent,
|
2003-02-27 03:58:42 +00:00
|
|
|
conn->header_length - conn->header_sent, 0);
|
|
|
|
if (sent == -1) err(1, "send()");
|
|
|
|
if (sent == 0)
|
|
|
|
{
|
|
|
|
conn->state = DONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
conn->header_sent += (unsigned int)sent;
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->total_sent += (unsigned int)sent;
|
2003-02-27 03:58:42 +00:00
|
|
|
|
|
|
|
/* check if we're done sending */
|
|
|
|
if (conn->header_sent == conn->header_length)
|
|
|
|
{
|
|
|
|
if (!conn->header_dont_free) free(conn->header);
|
|
|
|
conn->header = NULL;
|
2003-03-01 09:07:56 +00:00
|
|
|
|
|
|
|
if (conn->header_only)
|
|
|
|
conn->state = DONE;
|
|
|
|
else
|
|
|
|
conn->state = SEND_REPLY;
|
2003-02-27 03:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
|
2003-02-27 03:58:42 +00:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------
|
2003-03-01 09:07:56 +00:00
|
|
|
* Sending reply.
|
2003-02-27 03:58:42 +00:00
|
|
|
*/
|
2003-02-26 11:09:43 +00:00
|
|
|
static void poll_send_reply(struct connection *conn)
|
2003-02-27 03:58:42 +00:00
|
|
|
{
|
2003-03-01 09:07:56 +00:00
|
|
|
ssize_t sent;
|
2003-03-01 10:00:06 +00:00
|
|
|
|
|
|
|
assert( (conn->reply_type == REPLY_GENERATED &&
|
|
|
|
conn->reply_length == strlen(conn->reply)) ||
|
|
|
|
conn->reply_type == REPLY_FROMFILE);
|
|
|
|
|
2003-03-01 09:07:56 +00:00
|
|
|
if (conn->reply_type == REPLY_GENERATED)
|
|
|
|
{
|
|
|
|
sent = send(conn->socket, conn->reply + conn->reply_sent,
|
|
|
|
conn->reply_length - conn->reply_sent, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* from file! */
|
|
|
|
#define BUFSIZE 65000
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
int amount = min(BUFSIZE, conn->reply_length - conn->reply_sent);
|
|
|
|
#undef BUFSIZE
|
|
|
|
|
|
|
|
if (fseek(conn->reply_file, conn->reply_sent, SEEK_SET) == -1)
|
|
|
|
err(1, "fseek(%d)", conn->reply_sent);
|
|
|
|
|
|
|
|
if (fread(buf, amount, 1, conn->reply_file) != 1)
|
|
|
|
err(1, "fread()");
|
|
|
|
|
|
|
|
sent = send(conn->socket, buf, amount, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle any errors in send() */
|
2003-02-27 03:58:42 +00:00
|
|
|
if (sent == -1) err(1, "send()");
|
|
|
|
if (sent == 0)
|
|
|
|
{
|
|
|
|
conn->state = DONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
conn->reply_sent += (unsigned int)sent;
|
2003-03-01 10:00:06 +00:00
|
|
|
conn->total_sent += (unsigned int)sent;
|
2003-02-27 03:58:42 +00:00
|
|
|
|
|
|
|
/* check if we're done sending */
|
|
|
|
if (conn->reply_sent == conn->reply_length)
|
|
|
|
{
|
2003-03-01 09:07:56 +00:00
|
|
|
if (!conn->reply_dont_free && conn->reply != NULL)
|
|
|
|
{
|
|
|
|
free(conn->reply);
|
|
|
|
conn->reply = NULL;
|
|
|
|
}
|
|
|
|
if (conn->reply_file != NULL) fclose(conn->reply_file);
|
2003-02-27 03:58:42 +00:00
|
|
|
conn->state = DONE;
|
|
|
|
}
|
|
|
|
}
|
2003-02-26 11:09:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Add a connection's details to the logfile.
|
|
|
|
*/
|
|
|
|
static void log_connection(const struct connection *conn)
|
|
|
|
{
|
|
|
|
struct in_addr inaddr;
|
|
|
|
|
|
|
|
assert(conn->http_code != 0);
|
|
|
|
if (logfile == NULL) return;
|
|
|
|
|
|
|
|
/* Separated by tabs:
|
|
|
|
* time client method uri http_code bytes_sent
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FIXME: Referer, User-Agent */
|
|
|
|
|
|
|
|
inaddr.s_addr = conn->client;
|
|
|
|
|
|
|
|
fprintf(logfile, "%lu\t%s\t%s\t%s\t%d\t%u\n",
|
|
|
|
time(NULL), inet_ntoa(inaddr), conn->method, conn->uri,
|
|
|
|
conn->http_code, conn->total_sent);
|
|
|
|
fflush(logfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-21 00:53:45 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Main loop of the httpd - a select() and then delegation to accept
|
2003-03-01 10:00:06 +00:00
|
|
|
* connections, handle receiving of requests, and sending of replies.
|
2003-02-21 00:53:45 +00:00
|
|
|
*/
|
|
|
|
static void httpd_poll(void)
|
|
|
|
{
|
|
|
|
fd_set recv_set, send_set;
|
2003-02-26 11:09:43 +00:00
|
|
|
int max_fd, select_ret;
|
|
|
|
struct connection *conn;
|
2003-03-01 10:10:27 +00:00
|
|
|
struct timeval timeout = { IDLETIME, 0 };
|
|
|
|
int bother_with_timeout = 0;
|
2003-02-21 00:53:45 +00:00
|
|
|
|
|
|
|
FD_ZERO(&recv_set);
|
|
|
|
FD_ZERO(&send_set);
|
2003-02-26 11:09:43 +00:00
|
|
|
max_fd = 0;
|
|
|
|
|
|
|
|
/* set recv/send fd_sets */
|
|
|
|
#define MAX_FD_SET(sock, fdset) FD_SET(sock,fdset), \
|
|
|
|
max_fd = (max_fd<sock) ? sock : max_fd
|
2003-02-21 00:53:45 +00:00
|
|
|
|
|
|
|
MAX_FD_SET(sockin, &recv_set);
|
2003-02-26 11:09:43 +00:00
|
|
|
|
|
|
|
LIST_FOREACH(conn, &connlist, entries)
|
|
|
|
{
|
2003-02-27 03:21:36 +00:00
|
|
|
poll_check_timeout(conn);
|
|
|
|
switch (conn->state)
|
|
|
|
{
|
|
|
|
case RECV_REQUEST:
|
|
|
|
MAX_FD_SET(conn->socket, &recv_set);
|
2003-03-01 10:10:27 +00:00
|
|
|
bother_with_timeout = 1;
|
2003-02-27 03:21:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SEND_HEADER:
|
|
|
|
case SEND_REPLY:
|
|
|
|
MAX_FD_SET(conn->socket, &send_set);
|
2003-03-01 10:10:27 +00:00
|
|
|
bother_with_timeout = 1;
|
2003-02-27 03:21:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DONE:
|
|
|
|
/* clean out stale connections while we're at it */
|
|
|
|
LIST_REMOVE(conn, entries);
|
2003-03-01 10:00:06 +00:00
|
|
|
log_connection(conn);
|
2003-02-27 03:21:36 +00:00
|
|
|
free_connection(conn);
|
|
|
|
free(conn);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: errx(1, "invalid state");
|
|
|
|
}
|
2003-02-26 11:09:43 +00:00
|
|
|
}
|
2003-02-21 00:53:45 +00:00
|
|
|
#undef MAX_FD_SET
|
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
debugf("select("), fflush(stdout);
|
2003-03-01 10:10:27 +00:00
|
|
|
select_ret = select(max_fd + 1, &recv_set, &send_set, NULL,
|
|
|
|
(bother_with_timeout) ? &timeout : NULL);
|
|
|
|
if (select_ret == 0)
|
|
|
|
{
|
|
|
|
if (!bother_with_timeout)
|
|
|
|
errx(1, "select() timed out");
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2003-02-21 00:53:45 +00:00
|
|
|
if (select_ret == -1) err(1, "select()");
|
2003-02-26 11:09:43 +00:00
|
|
|
debugf(")\n");
|
2003-02-21 00:53:45 +00:00
|
|
|
|
2003-02-26 11:09:43 +00:00
|
|
|
/* poll connections that select() says need attention */
|
2003-02-21 00:53:45 +00:00
|
|
|
if (FD_ISSET(sockin, &recv_set)) accept_connection();
|
2003-02-26 11:09:43 +00:00
|
|
|
|
|
|
|
LIST_FOREACH(conn, &connlist, entries)
|
|
|
|
switch (conn->state)
|
|
|
|
{
|
|
|
|
case RECV_REQUEST:
|
|
|
|
if (FD_ISSET(conn->socket, &recv_set))
|
|
|
|
poll_recv_request(conn);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEND_HEADER:
|
|
|
|
if (FD_ISSET(conn->socket, &send_set))
|
|
|
|
poll_send_header(conn);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEND_REPLY:
|
|
|
|
if (FD_ISSET(conn->socket, &send_set))
|
|
|
|
poll_send_reply(conn);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: errx(1, "invalid state");
|
|
|
|
}
|
2003-02-21 00:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-01 10:10:27 +00:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Ignore SIGPIPE
|
|
|
|
*/
|
|
|
|
static void ignore_signal(int signum)
|
|
|
|
{
|
|
|
|
if (signum == signum) { /* do nothing */ }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-20 07:01:34 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
printf("%s, %s.\n", pkgname, copyright);
|
|
|
|
parse_commandline(argc, argv);
|
|
|
|
init_sockin();
|
2003-02-21 00:53:45 +00:00
|
|
|
|
2003-03-01 10:00:06 +00:00
|
|
|
/* open logfile */
|
|
|
|
if (logfile_name != NULL)
|
|
|
|
{
|
|
|
|
logfile = fopen(logfile_name, "wb");
|
|
|
|
if (logfile == NULL) err(1, "fopen(\"%s\")", logfile_name);
|
|
|
|
}
|
|
|
|
|
2003-03-01 10:10:27 +00:00
|
|
|
signal(SIGPIPE, ignore_signal);
|
|
|
|
|
2003-02-21 00:53:45 +00:00
|
|
|
for (;;) httpd_poll();
|
|
|
|
|
|
|
|
(void) close(sockin); /* unreachable =/ fix later */
|
2003-02-20 04:25:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */
|