httpd.c revision 2f99b54e8ec8e908ea894bc808d1c18a5f51a850
/*
* Copyright (C) 2006-2008 Internet Systems Consortium, Inc. ("ISC")
*
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: httpd.c,v 1.14 2008/01/17 23:47:00 tbox Exp $ */
/*! \file */
#include <string.h>
/*%
* TODO:
*
* o Put in better checks to make certain things are passed in correctly.
* This includes a magic number for externally-visible structures,
* checking for NULL-ness before dereferencing, etc.
* o Make the URL processing external functions which will fill-in a buffer
* structure we provide, or return an error and we will render a generic
* page and close the client.
*/
#ifdef DEBUG_HTTPD
#else
#define ENTER(x) do { } while(0)
#define EXIT(x) do { } while(0)
#define NOTICE(x) do { } while(0)
#endif
#define HTTP_RECVLEN 1024
#define HTTP_SENDGROW 1024
#define HTTP_SEND_MAXLEN 10240
/*%
* HTTP urls. These are the URLs we manage, and the function to call to
* provide the data for it. We pass in the base url (so the same function
* can handle multiple requests), and a structure to fill in to return a
* result to the client. We also pass in a pointer to be filled in for
* the data cleanup function.
*/
struct isc_httpdurl {
char *url;
void *action_arg;
};
/*% http client */
struct isc_httpd {
unsigned int state;
/*%
* Received data state.
*/
unsigned int method;
char *url;
char *querystring;
char *protocol;
/*
* Flags on the httpd client.
*/
int flags;
/*%
* Transmit data state.
*
* This is the data buffer we will transmit.
*
* This free function pointer is filled in by the rendering function
* we call. The free function is called after the data is transmitted
* to the client.
*
* The bufflist is the list of buffers we are currently transmitting.
* The headerdata is where we render our headers to. If we run out of
* space when rendering a header, we will change the size of our
* buffer. We will not free it until we are finished, and will
* allocate an additional HTTP_SENDGROW bytes per header space grow.
*
* We currently use two buffers total, one for the headers (which
* we manage) and another for the client to fill in (which it manages,
* it provides the space for it, etc) -- we will pass that buffer
* structure back to the caller, who is responsible for managing the
* space it may have allocated as backing store for it. This second
* buffer is bodybuffer, and we only allocate the buffer itself, not
* the backing store.
*/
char *headerdata; /*%< send header buf */
unsigned int headerlen; /*%< current header buffer size */
const char *mimetype;
unsigned int retcode;
const char *retmsg;
void *freecb_arg;
};
/*% lightweight socket manager for httpd output */
struct isc_httpdmgr {
void *cb_arg; /*%< argument for the above */
unsigned int flags;
};
/*%
* HTTP methods.
*/
#define ISC_HTTPD_METHODUNKNOWN 0
#define ISC_HTTPD_METHODGET 1
#define ISC_HTTPD_METHODPOST 2
/*%
* Client states.
*
* _IDLE The client is not doing anything at all. This state should
* only occur just after creation, and just before being
* destroyed.
*
* _RECV The client is waiting for data after issuing a socket recv().
*
* _RECVDONE Data has been received, and is being processed.
*
* _SEND All data for a response has completed, and a reply was
* sent via a socket send() call.
*
* _SENDDONE Send is completed.
*
* Badly formatted state table:
*
* IDLE -> RECV when client has a recv() queued.
*
* RECV -> RECVDONE when recvdone event received.
*
* RECVDONE -> SEND if the data for a reply is at hand.
*
* SEND -> RECV when a senddone event was received.
*
* At any time -> RECV on error. If RECV fails, the client will
* self-destroy, closing the socket and freeing memory.
*/
#define ISC_HTTPD_STATEIDLE 0
#define ISC_HTTPD_STATERECV 1
#define ISC_HTTPD_STATERECVDONE 2
#define ISC_HTTPD_STATESEND 3
#define ISC_HTTPD_STATESENDDONE 4
/*%
* Overall magic test that means we're not idle.
*/
static void destroy_client(isc_httpd_t **);
static void httpdmgr_destroy(isc_httpdmgr_t *);
static isc_result_t render_404(const char *, const char *,
void *,
unsigned int *, const char **,
const char **, isc_buffer_t *,
isc_httpdfree_t **, void **);
static void
{
}
{
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS) {
return (result);
}
/* XXXMLG ignore errors on isc_socket_listen() */
if (result != ISC_R_SUCCESS) {
"isc_socket_listen() failed: %s",
goto cleanup;
}
if (result != ISC_R_SUCCESS)
goto cleanup;
return (ISC_R_SUCCESS);
return (result);
}
static void
{
ENTER("httpdmgr_destroy");
if (!MSHUTTINGDOWN(httpdmgr)) {
NOTICE("httpdmgr_destroy not shutting down yet");
return;
}
/*
* If all clients are not shut down, don't do anything yet.
*/
NOTICE("httpdmgr_destroy clients still active");
return;
}
NOTICE("httpdmgr_destroy detaching socket, task, and timermgr");
/*
* Clear out the list of all actions we know about. Just free the
* memory.
*/
}
EXIT("httpdmgr_destroy");
}
static isc_result_t
{
char *s;
char *p;
int delim;
ENTER("request");
/*
* If we don't find a blank line in our buffer, return that we need
* more data.
*/
delim = 1;
if (s == NULL) {
delim = 2;
}
if (s == NULL)
return (ISC_R_NOTFOUND);
/*
* Determine if this is a POST or GET method. Any other values will
* cause an error to be returned.
*/
} else {
return (ISC_R_RANGE);
}
/*
* From now on, p is the start of our buffer.
*/
/*
* Extract the URL.
*/
s = p;
(*s != '\n' && *s != '\r' && *s != '\0' && *s != ' '))
s++;
if (!LENGTHOK(s))
return (ISC_R_NOTFOUND);
if (!BUFLENOK(s))
return (ISC_R_NOMEMORY);
*s = 0;
/*
* Make the URL relative.
*/
/* Skip first / */
while (*p != '/' && *p != 0)
p++;
if (*p == 0)
return (ISC_R_RANGE);
p++;
/* Skip second / */
while (*p != '/' && *p != 0)
p++;
if (*p == 0)
return (ISC_R_RANGE);
p++;
/* Find third / */
while (*p != '/' && *p != 0)
p++;
if (*p == 0) {
p--;
*p = '/';
}
}
p = s + delim;
s = p;
/*
* Now, see if there is a ? mark in the URL. If so, this is
* part of the query string, and we will split it from the URL.
*/
*(httpd->querystring) = 0;
httpd->querystring++;
}
/*
* Extract the HTTP/1.X protocol. We will bounce on anything but
* HTTP/1.1 for now.
*/
(*s != '\n' && *s != '\r' && *s != '\0'))
s++;
if (!LENGTHOK(s))
return (ISC_R_NOTFOUND);
if (!BUFLENOK(s))
return (ISC_R_NOMEMORY);
*s = 0;
return (ISC_R_RANGE);
p = s + 1;
s = p;
/*
* Standards compliance hooks here.
*/
return (ISC_R_RANGE);
EXIT("request");
return (ISC_R_SUCCESS);
}
static void
{
isc_region_t r;
ENTER("accept");
if (MSHUTTINGDOWN(httpdmgr)) {
NOTICE("accept shutting down, goto out");
goto out;
}
NOTICE("accept canceled, goto out");
goto out;
}
/* XXXMLG log failure */
NOTICE("accept returned failure, goto requeue");
goto requeue;
}
goto requeue;
}
/* XXXMLG log failure */
NOTICE("accept failed to allocate memory, goto requeue");
goto requeue;
}
/*
* Initialize the buffer for our headers.
*/
goto requeue;
}
httpd);
NOTICE("accept queued recv on socket");
httpdmgr);
if (result != ISC_R_SUCCESS) {
/* XXXMLG what to do? Log failure... */
NOTICE("accept could not reaccept due to failure");
}
out:
isc_event_free(&ev);
EXIT("accept");
}
static isc_result_t
void *arg,
const char **mimetype, isc_buffer_t *b,
{
static char msg[] = "No such URL.";
*retcode = 404;
*retmsg = "No such URL";
*freecb_args = NULL;
return (ISC_R_SUCCESS);
}
static void
{
isc_region_t r;
ENTER("recv");
NOTICE("recv destroying client");
goto out;
}
if (result == ISC_R_NOTFOUND) {
goto out;
}
goto out;
} else if (result != ISC_R_SUCCESS) {
goto out;
}
/*
* XXXMLG Call function here. Provide an add-header function
* which will append the common headers to a response we generate.
*/
isc_time_now(&now);
break;
}
NULL,
&httpd->bodybuffer,
&httpd->freecb_arg);
else
if (result != ISC_R_SUCCESS) {
goto out;
}
/*
* Link the data buffer into our send queue, should we have any data
* rendered into it. If no data is present, we won't do anything
* with the buffer.
*/
out:
isc_event_free(&ev);
EXIT("recv");
}
void
{
ENTER("isc_httpdmgr_shutdown");
}
EXIT("isc_httpdmgr_shutdown");
}
static isc_result_t
{
char *newspace;
unsigned int newlen;
isc_region_t r;
if (newlen > HTTP_SEND_MAXLEN)
return (ISC_R_NOSPACE);
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
{
unsigned int needlen;
if (result != ISC_R_SUCCESS)
return (result);
}
return (ISC_R_SUCCESS);
}
const char *val)
{
unsigned int needlen;
if (result != ISC_R_SUCCESS)
return (result);
}
else
"%s\r\n", name);
return (ISC_R_SUCCESS);
}
{
if (result != ISC_R_SUCCESS)
return (result);
}
return (ISC_R_SUCCESS);
}
unsigned int needlen;
char buf[sizeof "18446744073709551616"];
if (result != ISC_R_SUCCESS)
return (result);
}
return (ISC_R_SUCCESS);
}
static void
{
isc_region_t r;
ENTER("senddone");
/*
* First, unlink our header buffer from the socket's bufflist. This
* is sort of an evil hack, since we know our buffer will be there,
* and we know it's address, so we can just remove it directly.
*/
NOTICE("senddone unlinked header");
/*
* We will always want to clean up our receive buffer, even if we
* got an error on send or we are shutting down.
*
* We will pass in the buffer only if there is data in it. If
* there is no data, we will pass in a NULL.
*/
isc_buffer_t *b = NULL;
b = &httpd->bodybuffer;
NOTICE("senddone free callback performed");
}
NOTICE("senddone body buffer unlinked");
}
goto out;
}
goto out;
}
NOTICE("senddone restarting recv on socket");
httpd);
out:
isc_event_free(&ev);
EXIT("senddone");
}
static void
{
/*
* Catch errors here. We MUST be in RECV mode, and we MUST NOT have
* any outstanding buffers. If we have buffers, we have a leak.
*/
}
{
return (ISC_R_SUCCESS);
}
return (ISC_R_NOMEMORY);
return (ISC_R_NOMEMORY);
}
return (ISC_R_SUCCESS);
}