httpd.c revision 31a1cbf1012ad83b0c2387152d53ec2da8a6d5ed
/*
* Copyright (C) 2006-2008, 2010-2016 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$ */
/*! \file */
#include <config.h>
#include <string.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
/*%
* 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
#define HTTPD_ACCEPT_DEFLATE 0x0008
/*% http client */
struct isc_httpd {
unsigned int state;
/*%
* Received data state.
*/
char *headers; /*%< set in process_request() */
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 headerbuffer 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 three buffers total, one for the headers (which
* we manage), 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.
* The third buffer is compbuffer, managed by us, that contains the
* compressed HTTP data, if compression is used.
*
*/
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_httpdaction_t render_404;
static isc_httpdaction_t render_500;
static void
isc_region_t r;
if (r.length > 0) {
}
if (r.length > 0) {
}
}
{
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");
}
/*
* Look for the given header in headers.
* If value is specified look for it terminated with a character in eov.
*/
static isc_boolean_t
const char *eov)
{
}
for (;;) {
/*
* Skip to next line;
*/
cr++;
/* last header? */
h = cr;
h = nl;
if (h == NULL)
return (ISC_FALSE);
h++;
continue;
}
return (ISC_TRUE);
/*
* Skip optional leading white space.
*/
h += hlen;
while (*h == ' ' || *h == '\t')
h++;
/*
* Terminate token search on NULL or EOL.
*/
while (*h != 0 && *h != '\r' && *h != '\n') {
return (ISC_TRUE);
/*
* Skip to next token.
*/
if (h[0] == '\r' && h[1] == '\n')
h++;
if (h[0] != 0)
h++;
}
return (ISC_FALSE);
}
}
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 = 2;
if (s == NULL) {
delim = 1;
}
if (s == NULL)
return (ISC_R_NOTFOUND);
/*
* NUL terminate request at the blank line.
*/
s[delim] = 0;
/*
* 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 + 1;
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.0 or 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);
/*
* Check that we have the expected eol delimiter.
*/
return (ISC_R_RANGE);
*s = 0;
return (ISC_R_RANGE);
p = s + delim; /* skip past eol */
s = p;
", \t\r\n"))
else
}
/*
* Check for Accept-Encoding:
*/
#ifdef HAVE_ZLIB
#endif
/*
* Standards compliance hooks here.
*/
return (ISC_R_RANGE);
EXIT("request");
return (ISC_R_SUCCESS);
}
static void
isc_region_t r;
char *headerdata;
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.
*/
if (headerdata == NULL) {
goto requeue;
}
httpd);
/* FIXME!!! */
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
const char **mimetype, isc_buffer_t *b,
{
static char msg[] = "No such URL.\r\n";
*retcode = 404;
*retmsg = "No such URL";
*freecb_args = NULL;
return (ISC_R_SUCCESS);
}
static isc_result_t
const char **mimetype, isc_buffer_t *b,
{
static char msg[] = "Internal server failure.\r\n";
*retcode = 500;
*retmsg = "Internal server failure";
*freecb_args = NULL;
return (ISC_R_SUCCESS);
}
#ifdef HAVE_ZLIB
/*%<
* Reallocates compbuffer to size, does nothing if compbuffer is already
* larger than size.
*
* Requires:
*\li httpd a valid isc_httpd_t object
*
* Returns:
*\li #ISC_R_SUCCESS -- all is well.
*\li #ISC_R_NOMEMORY -- not enough memory to extend buffer
*/
static isc_result_t
char *newspace;
isc_region_t r;
return (ISC_R_SUCCESS);
}
return (ISC_R_NOMEMORY);
}
return (ISC_R_SUCCESS);
}
/*%<
* Tries to compress httpd->bodybuffer to httpd->compbuffer, extending it
* if necessary.
*
* Requires:
*\li httpd a valid isc_httpd_t object
*
* Returns:
*\li #ISC_R_SUCCESS -- all is well.
*\li #ISC_R_NOMEMORY -- not enough memory to compress data
*\li #ISC_R_FAILURE -- error during compression or compressed
* data would be larger than input data
*/
static isc_result_t
isc_region_t r;
int ret;
int inputlen;
if (result != ISC_R_SUCCESS) {
return result;
}
/*
* We're setting output buffer size to input size so it fails if the
* compressed data size would be bigger than the input size.
*/
}
deflateEnd(&zstr);
if (ret == Z_STREAM_END) {
return (ISC_R_SUCCESS);
} else {
return (ISC_R_FAILURE);
}
}
#endif
static void
isc_region_t r;
ENTER("recv");
NOTICE("recv destroying client");
goto out;
}
if (result == ISC_R_NOTFOUND) {
goto out;
}
/* check return code? */
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;
}
&httpd->bodybuffer,
&httpd->freecb_arg);
else
if (result != ISC_R_SUCCESS) {
&httpd->bodybuffer,
&httpd->freecb_arg);
}
#ifdef HAVE_ZLIB
if (result == ISC_R_SUCCESS) {
}
}
#endif
} else {
}
if (is_compressed == ISC_TRUE) {
} else {
}
/*
* 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.
*/
if (is_compressed == ISC_TRUE) {
} else {
}
}
/* check return code? */
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");
NOTICE("senddone compressed data unlinked and freed");
}
goto out;
}
goto out;
}
NOTICE("senddone restarting recv on socket");
/* check return code? */
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);
}