Home | History | Annotate | Line # | Download | only in isc
httpd.c revision 1.3
      1  1.2  christos /*	$NetBSD: httpd.c,v 1.3 2019/01/09 16:55:14 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.1  christos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     11  1.1  christos  * information regarding copyright ownership.
     12  1.1  christos  */
     13  1.1  christos 
     14  1.1  christos 
     15  1.1  christos /*! \file */
     16  1.1  christos 
     17  1.1  christos #include <config.h>
     18  1.1  christos 
     19  1.3  christos #include <inttypes.h>
     20  1.3  christos #include <stdbool.h>
     21  1.3  christos #include <string.h>
     22  1.3  christos 
     23  1.1  christos #include <isc/buffer.h>
     24  1.1  christos #include <isc/httpd.h>
     25  1.1  christos #include <isc/mem.h>
     26  1.1  christos #include <isc/print.h>
     27  1.1  christos #include <isc/socket.h>
     28  1.1  christos #include <isc/string.h>
     29  1.1  christos #include <isc/task.h>
     30  1.1  christos #include <isc/time.h>
     31  1.1  christos #include <isc/util.h>
     32  1.1  christos 
     33  1.1  christos #ifdef HAVE_ZLIB
     34  1.1  christos #include <zlib.h>
     35  1.1  christos #endif
     36  1.1  christos 
     37  1.1  christos /*%
     38  1.1  christos  * TODO:
     39  1.1  christos  *
     40  1.1  christos  *  o  Put in better checks to make certain things are passed in correctly.
     41  1.1  christos  *     This includes a magic number for externally-visible structures,
     42  1.1  christos  *     checking for NULL-ness before dereferencing, etc.
     43  1.1  christos  *  o  Make the URL processing external functions which will fill-in a buffer
     44  1.1  christos  *     structure we provide, or return an error and we will render a generic
     45  1.1  christos  *     page and close the client.
     46  1.1  christos  */
     47  1.1  christos 
     48  1.1  christos #define MSHUTTINGDOWN(cm) ((cm->flags & ISC_HTTPDMGR_FLAGSHUTTINGDOWN) != 0)
     49  1.1  christos #define MSETSHUTTINGDOWN(cm) (cm->flags |= ISC_HTTPDMGR_FLAGSHUTTINGDOWN)
     50  1.1  christos 
     51  1.1  christos #ifdef DEBUG_HTTPD
     52  1.2  christos #define ENTER(x) do { fprintf(stderr, "ENTER %s\n", (x)); } while (/*CONSTCOND*/0)
     53  1.2  christos #define EXIT(x) do { fprintf(stderr, "EXIT %s\n", (x)); } while (/*CONSTCOND*/0)
     54  1.2  christos #define NOTICE(x) do { fprintf(stderr, "NOTICE %s\n", (x)); } while (/*CONSTCOND*/0)
     55  1.1  christos #else
     56  1.2  christos #define ENTER(x) do { } while(/*CONSTCOND*/0)
     57  1.2  christos #define EXIT(x) do { } while(/*CONSTCOND*/0)
     58  1.2  christos #define NOTICE(x) do { } while(/*CONSTCOND*/0)
     59  1.1  christos #endif
     60  1.1  christos 
     61  1.1  christos #define HTTP_RECVLEN			1024
     62  1.1  christos #define HTTP_SENDGROW			1024
     63  1.1  christos #define HTTP_SEND_MAXLEN		10240
     64  1.1  christos 
     65  1.1  christos #define HTTPD_CLOSE		0x0001 /* Got a Connection: close header */
     66  1.1  christos #define HTTPD_FOUNDHOST		0x0002 /* Got a Host: header */
     67  1.1  christos #define HTTPD_KEEPALIVE		0x0004 /* Got a Connection: Keep-Alive */
     68  1.1  christos #define HTTPD_ACCEPT_DEFLATE   0x0008
     69  1.1  christos 
     70  1.1  christos /*% http client */
     71  1.1  christos struct isc_httpd {
     72  1.1  christos 	isc_httpdmgr_t	       *mgr;		/*%< our parent */
     73  1.1  christos 	ISC_LINK(isc_httpd_t)	link;
     74  1.1  christos 	unsigned int		state;
     75  1.1  christos 	isc_socket_t		*sock;
     76  1.1  christos 
     77  1.1  christos 	/*%
     78  1.1  christos 	 * Received data state.
     79  1.1  christos 	 */
     80  1.1  christos 	char			recvbuf[HTTP_RECVLEN]; /*%< receive buffer */
     81  1.3  christos 	uint32_t		recvlen;	/*%< length recv'd */
     82  1.1  christos 	char		       *headers;	/*%< set in process_request() */
     83  1.1  christos 	unsigned int		method;
     84  1.1  christos 	char		       *url;
     85  1.1  christos 	char		       *querystring;
     86  1.1  christos 	char		       *protocol;
     87  1.1  christos 
     88  1.1  christos 	/*
     89  1.1  christos 	 * Flags on the httpd client.
     90  1.1  christos 	 */
     91  1.1  christos 	int			flags;
     92  1.1  christos 
     93  1.1  christos 	/*%
     94  1.1  christos 	 * Transmit data state.
     95  1.1  christos 	 *
     96  1.1  christos 	 * This is the data buffer we will transmit.
     97  1.1  christos 	 *
     98  1.1  christos 	 * This free function pointer is filled in by the rendering function
     99  1.1  christos 	 * we call.  The free function is called after the data is transmitted
    100  1.1  christos 	 * to the client.
    101  1.1  christos 	 *
    102  1.1  christos 	 * The bufflist is the list of buffers we are currently transmitting.
    103  1.1  christos 	 * The headerbuffer is where we render our headers to.  If we run out of
    104  1.1  christos 	 * space when rendering a header, we will change the size of our
    105  1.1  christos 	 * buffer.  We will not free it until we are finished, and will
    106  1.1  christos 	 * allocate an additional HTTP_SENDGROW bytes per header space grow.
    107  1.1  christos 	 *
    108  1.1  christos 	 * We currently use three buffers total, one for the headers (which
    109  1.1  christos 	 * we manage), another for the client to fill in (which it manages,
    110  1.1  christos 	 * it provides the space for it, etc) -- we will pass that buffer
    111  1.1  christos 	 * structure back to the caller, who is responsible for managing the
    112  1.1  christos 	 * space it may have allocated as backing store for it.  This second
    113  1.1  christos 	 * buffer is bodybuffer, and we only allocate the buffer itself, not
    114  1.1  christos 	 * the backing store.
    115  1.1  christos 	 * The third buffer is compbuffer, managed by us, that contains the
    116  1.1  christos 	 * compressed HTTP data, if compression is used.
    117  1.1  christos 	 *
    118  1.1  christos 	 */
    119  1.1  christos 	isc_buffer_t		headerbuffer;
    120  1.1  christos 	isc_buffer_t		compbuffer;
    121  1.3  christos 	isc_buffer_t		*sendbuffer;
    122  1.1  christos 
    123  1.1  christos 	const char	       *mimetype;
    124  1.1  christos 	unsigned int		retcode;
    125  1.1  christos 	const char	       *retmsg;
    126  1.1  christos 	isc_buffer_t		bodybuffer;
    127  1.1  christos 	isc_httpdfree_t	       *freecb;
    128  1.1  christos 	void		       *freecb_arg;
    129  1.1  christos };
    130  1.1  christos 
    131  1.1  christos /*% lightweight socket manager for httpd output */
    132  1.1  christos struct isc_httpdmgr {
    133  1.1  christos 	isc_mem_t	       *mctx;
    134  1.1  christos 	isc_socket_t	       *sock;		/*%< listening socket */
    135  1.1  christos 	isc_task_t	       *task;		/*%< owning task */
    136  1.1  christos 	isc_timermgr_t	       *timermgr;
    137  1.1  christos 
    138  1.1  christos 	isc_httpdclientok_t    *client_ok;	/*%< client validator */
    139  1.1  christos 	isc_httpdondestroy_t   *ondestroy;	/*%< cleanup callback */
    140  1.1  christos 	void		       *cb_arg;		/*%< argument for the above */
    141  1.1  christos 
    142  1.1  christos 	unsigned int		flags;
    143  1.1  christos 	ISC_LIST(isc_httpd_t)	running;	/*%< running clients */
    144  1.1  christos 
    145  1.1  christos 	isc_mutex_t		lock;
    146  1.1  christos 
    147  1.1  christos 	ISC_LIST(isc_httpdurl_t) urls;		/*%< urls we manage */
    148  1.1  christos 	isc_httpdaction_t      *render_404;
    149  1.1  christos 	isc_httpdaction_t      *render_500;
    150  1.1  christos };
    151  1.1  christos 
    152  1.1  christos /*%
    153  1.1  christos  * HTTP methods.
    154  1.1  christos  */
    155  1.1  christos #define ISC_HTTPD_METHODUNKNOWN	0
    156  1.1  christos #define ISC_HTTPD_METHODGET	1
    157  1.1  christos #define ISC_HTTPD_METHODPOST	2
    158  1.1  christos 
    159  1.1  christos /*%
    160  1.1  christos  * Client states.
    161  1.1  christos  *
    162  1.1  christos  * _IDLE	The client is not doing anything at all.  This state should
    163  1.1  christos  *		only occur just after creation, and just before being
    164  1.1  christos  *		destroyed.
    165  1.1  christos  *
    166  1.1  christos  * _RECV	The client is waiting for data after issuing a socket recv().
    167  1.1  christos  *
    168  1.1  christos  * _RECVDONE	Data has been received, and is being processed.
    169  1.1  christos  *
    170  1.1  christos  * _SEND	All data for a response has completed, and a reply was
    171  1.1  christos  *		sent via a socket send() call.
    172  1.1  christos  *
    173  1.1  christos  * _SENDDONE	Send is completed.
    174  1.1  christos  *
    175  1.1  christos  * Badly formatted state table:
    176  1.1  christos  *
    177  1.1  christos  *	IDLE -> RECV when client has a recv() queued.
    178  1.1  christos  *
    179  1.1  christos  *	RECV -> RECVDONE when recvdone event received.
    180  1.1  christos  *
    181  1.1  christos  *	RECVDONE -> SEND if the data for a reply is at hand.
    182  1.1  christos  *
    183  1.1  christos  *	SEND -> RECV when a senddone event was received.
    184  1.1  christos  *
    185  1.1  christos  *	At any time -> RECV on error.  If RECV fails, the client will
    186  1.1  christos  *	self-destroy, closing the socket and freeing memory.
    187  1.1  christos  */
    188  1.1  christos #define ISC_HTTPD_STATEIDLE	0
    189  1.1  christos #define ISC_HTTPD_STATERECV	1
    190  1.1  christos #define ISC_HTTPD_STATERECVDONE	2
    191  1.1  christos #define ISC_HTTPD_STATESEND	3
    192  1.1  christos #define ISC_HTTPD_STATESENDDONE	4
    193  1.1  christos 
    194  1.1  christos #define ISC_HTTPD_ISRECV(c)	((c)->state == ISC_HTTPD_STATERECV)
    195  1.1  christos #define ISC_HTTPD_ISRECVDONE(c)	((c)->state == ISC_HTTPD_STATERECVDONE)
    196  1.1  christos #define ISC_HTTPD_ISSEND(c)	((c)->state == ISC_HTTPD_STATESEND)
    197  1.1  christos #define ISC_HTTPD_ISSENDDONE(c)	((c)->state == ISC_HTTPD_STATESENDDONE)
    198  1.1  christos 
    199  1.1  christos /*%
    200  1.1  christos  * Overall magic test that means we're not idle.
    201  1.1  christos  */
    202  1.1  christos #define ISC_HTTPD_SETRECV(c)	((c)->state = ISC_HTTPD_STATERECV)
    203  1.1  christos #define ISC_HTTPD_SETRECVDONE(c)	((c)->state = ISC_HTTPD_STATERECVDONE)
    204  1.1  christos #define ISC_HTTPD_SETSEND(c)	((c)->state = ISC_HTTPD_STATESEND)
    205  1.1  christos #define ISC_HTTPD_SETSENDDONE(c)	((c)->state = ISC_HTTPD_STATESENDDONE)
    206  1.1  christos 
    207  1.1  christos static void isc_httpd_accept(isc_task_t *, isc_event_t *);
    208  1.1  christos static void isc_httpd_recvdone(isc_task_t *, isc_event_t *);
    209  1.1  christos static void isc_httpd_senddone(isc_task_t *, isc_event_t *);
    210  1.1  christos static void destroy_client(isc_httpd_t **);
    211  1.1  christos static isc_result_t process_request(isc_httpd_t *, int);
    212  1.1  christos static void httpdmgr_destroy(isc_httpdmgr_t *);
    213  1.1  christos static isc_result_t grow_headerspace(isc_httpd_t *);
    214  1.1  christos static void reset_client(isc_httpd_t *httpd);
    215  1.1  christos 
    216  1.1  christos static isc_httpdaction_t render_404;
    217  1.1  christos static isc_httpdaction_t render_500;
    218  1.1  christos 
    219  1.1  christos static void (*finishhook)(void) = NULL;
    220  1.1  christos 
    221  1.1  christos static void
    222  1.1  christos destroy_client(isc_httpd_t **httpdp) {
    223  1.1  christos 	isc_httpd_t *httpd = *httpdp;
    224  1.1  christos 	isc_httpdmgr_t *httpdmgr = httpd->mgr;
    225  1.1  christos 	isc_region_t r;
    226  1.1  christos 
    227  1.1  christos 	*httpdp = NULL;
    228  1.1  christos 
    229  1.1  christos 	LOCK(&httpdmgr->lock);
    230  1.1  christos 
    231  1.1  christos 	isc_socket_detach(&httpd->sock);
    232  1.1  christos 	ISC_LIST_UNLINK(httpdmgr->running, httpd, link);
    233  1.1  christos 
    234  1.1  christos 	isc_buffer_region(&httpd->headerbuffer, &r);
    235  1.1  christos 	if (r.length > 0) {
    236  1.1  christos 		isc_mem_put(httpdmgr->mctx, r.base, r.length);
    237  1.1  christos 	}
    238  1.1  christos 
    239  1.1  christos 	isc_buffer_region(&httpd->compbuffer, &r);
    240  1.1  christos 	if (r.length > 0) {
    241  1.1  christos 		isc_mem_put(httpdmgr->mctx, r.base, r.length);
    242  1.1  christos 	}
    243  1.1  christos 
    244  1.1  christos 	isc_mem_put(httpdmgr->mctx, httpd, sizeof(isc_httpd_t));
    245  1.1  christos 
    246  1.1  christos 	UNLOCK(&httpdmgr->lock);
    247  1.1  christos 
    248  1.1  christos 	if (finishhook != NULL)
    249  1.1  christos 		finishhook();
    250  1.1  christos 
    251  1.1  christos 	httpdmgr_destroy(httpdmgr);
    252  1.1  christos }
    253  1.1  christos 
    254  1.1  christos isc_result_t
    255  1.1  christos isc_httpdmgr_create(isc_mem_t *mctx, isc_socket_t *sock, isc_task_t *task,
    256  1.1  christos 		    isc_httpdclientok_t *client_ok,
    257  1.1  christos 		    isc_httpdondestroy_t *ondestroy, void *cb_arg,
    258  1.1  christos 		    isc_timermgr_t *tmgr, isc_httpdmgr_t **httpdmgrp)
    259  1.1  christos {
    260  1.1  christos 	isc_result_t result;
    261  1.1  christos 	isc_httpdmgr_t *httpdmgr;
    262  1.1  christos 
    263  1.1  christos 	REQUIRE(mctx != NULL);
    264  1.1  christos 	REQUIRE(sock != NULL);
    265  1.1  christos 	REQUIRE(task != NULL);
    266  1.1  christos 	REQUIRE(tmgr != NULL);
    267  1.1  christos 	REQUIRE(httpdmgrp != NULL && *httpdmgrp == NULL);
    268  1.1  christos 
    269  1.1  christos 	httpdmgr = isc_mem_get(mctx, sizeof(isc_httpdmgr_t));
    270  1.1  christos 	if (httpdmgr == NULL)
    271  1.1  christos 		return (ISC_R_NOMEMORY);
    272  1.1  christos 
    273  1.3  christos 	isc_mutex_init(&httpdmgr->lock);
    274  1.1  christos 	httpdmgr->mctx = NULL;
    275  1.1  christos 	isc_mem_attach(mctx, &httpdmgr->mctx);
    276  1.1  christos 	httpdmgr->sock = NULL;
    277  1.1  christos 	isc_socket_attach(sock, &httpdmgr->sock);
    278  1.1  christos 	httpdmgr->task = NULL;
    279  1.1  christos 	isc_task_attach(task, &httpdmgr->task);
    280  1.1  christos 	httpdmgr->timermgr = tmgr; /* XXXMLG no attach function? */
    281  1.1  christos 	httpdmgr->client_ok = client_ok;
    282  1.1  christos 	httpdmgr->ondestroy = ondestroy;
    283  1.1  christos 	httpdmgr->cb_arg = cb_arg;
    284  1.1  christos 	httpdmgr->flags = 0;
    285  1.1  christos 
    286  1.1  christos 	ISC_LIST_INIT(httpdmgr->running);
    287  1.1  christos 	ISC_LIST_INIT(httpdmgr->urls);
    288  1.1  christos 
    289  1.1  christos 	/* XXXMLG ignore errors on isc_socket_listen() */
    290  1.1  christos 	result = isc_socket_listen(sock, SOMAXCONN);
    291  1.1  christos 	if (result != ISC_R_SUCCESS) {
    292  1.1  christos 		UNEXPECTED_ERROR(__FILE__, __LINE__,
    293  1.1  christos 				 "isc_socket_listen() failed: %s",
    294  1.1  christos 				 isc_result_totext(result));
    295  1.1  christos 		goto cleanup;
    296  1.1  christos 	}
    297  1.1  christos 
    298  1.1  christos 	(void)isc_socket_filter(sock, "httpready");
    299  1.1  christos 
    300  1.1  christos 	result = isc_socket_accept(sock, task, isc_httpd_accept, httpdmgr);
    301  1.1  christos 	if (result != ISC_R_SUCCESS)
    302  1.1  christos 		goto cleanup;
    303  1.1  christos 
    304  1.1  christos 	httpdmgr->render_404 = render_404;
    305  1.1  christos 	httpdmgr->render_500 = render_500;
    306  1.1  christos 
    307  1.1  christos 	*httpdmgrp = httpdmgr;
    308  1.1  christos 	return (ISC_R_SUCCESS);
    309  1.1  christos 
    310  1.1  christos   cleanup:
    311  1.1  christos 	isc_task_detach(&httpdmgr->task);
    312  1.1  christos 	isc_socket_detach(&httpdmgr->sock);
    313  1.1  christos 	isc_mem_detach(&httpdmgr->mctx);
    314  1.3  christos 	isc_mutex_destroy(&httpdmgr->lock);
    315  1.1  christos 	isc_mem_put(mctx, httpdmgr, sizeof(isc_httpdmgr_t));
    316  1.1  christos 	return (result);
    317  1.1  christos }
    318  1.1  christos 
    319  1.1  christos static void
    320  1.1  christos httpdmgr_destroy(isc_httpdmgr_t *httpdmgr) {
    321  1.1  christos 	isc_mem_t *mctx;
    322  1.1  christos 	isc_httpdurl_t *url;
    323  1.1  christos 
    324  1.1  christos 	ENTER("httpdmgr_destroy");
    325  1.1  christos 
    326  1.1  christos 	LOCK(&httpdmgr->lock);
    327  1.1  christos 
    328  1.1  christos 	if (!MSHUTTINGDOWN(httpdmgr)) {
    329  1.1  christos 		NOTICE("httpdmgr_destroy not shutting down yet");
    330  1.1  christos 		UNLOCK(&httpdmgr->lock);
    331  1.1  christos 		return;
    332  1.1  christos 	}
    333  1.1  christos 
    334  1.1  christos 	/*
    335  1.1  christos 	 * If all clients are not shut down, don't do anything yet.
    336  1.1  christos 	 */
    337  1.1  christos 	if (!ISC_LIST_EMPTY(httpdmgr->running)) {
    338  1.1  christos 		NOTICE("httpdmgr_destroy clients still active");
    339  1.1  christos 		UNLOCK(&httpdmgr->lock);
    340  1.1  christos 		return;
    341  1.1  christos 	}
    342  1.1  christos 
    343  1.1  christos 	NOTICE("httpdmgr_destroy detaching socket, task, and timermgr");
    344  1.1  christos 
    345  1.1  christos 	isc_socket_detach(&httpdmgr->sock);
    346  1.1  christos 	isc_task_detach(&httpdmgr->task);
    347  1.1  christos 	httpdmgr->timermgr = NULL;
    348  1.1  christos 
    349  1.1  christos 	/*
    350  1.1  christos 	 * Clear out the list of all actions we know about.  Just free the
    351  1.1  christos 	 * memory.
    352  1.1  christos 	 */
    353  1.1  christos 	url = ISC_LIST_HEAD(httpdmgr->urls);
    354  1.1  christos 	while (url != NULL) {
    355  1.1  christos 		isc_mem_free(httpdmgr->mctx, url->url);
    356  1.1  christos 		ISC_LIST_UNLINK(httpdmgr->urls, url, link);
    357  1.1  christos 		isc_mem_put(httpdmgr->mctx, url, sizeof(isc_httpdurl_t));
    358  1.1  christos 		url = ISC_LIST_HEAD(httpdmgr->urls);
    359  1.1  christos 	}
    360  1.1  christos 
    361  1.1  christos 	UNLOCK(&httpdmgr->lock);
    362  1.3  christos 	isc_mutex_destroy(&httpdmgr->lock);
    363  1.1  christos 
    364  1.1  christos 	if (httpdmgr->ondestroy != NULL)
    365  1.1  christos 		(httpdmgr->ondestroy)(httpdmgr->cb_arg);
    366  1.1  christos 
    367  1.1  christos 	mctx = httpdmgr->mctx;
    368  1.1  christos 	isc_mem_putanddetach(&mctx, httpdmgr, sizeof(isc_httpdmgr_t));
    369  1.1  christos 
    370  1.1  christos 	EXIT("httpdmgr_destroy");
    371  1.1  christos }
    372  1.1  christos 
    373  1.1  christos #define LENGTHOK(s) (httpd->recvbuf - (s) < (int)httpd->recvlen)
    374  1.1  christos #define BUFLENOK(s) (httpd->recvbuf - (s) < HTTP_RECVLEN)
    375  1.1  christos 
    376  1.1  christos /*
    377  1.1  christos  * Look for the given header in headers.
    378  1.1  christos  * If value is specified look for it terminated with a character in eov.
    379  1.1  christos  */
    380  1.3  christos static bool
    381  1.1  christos have_header(isc_httpd_t *httpd, const char *header, const char *value,
    382  1.1  christos 	    const char *eov)
    383  1.1  christos {
    384  1.1  christos 	char *cr, *nl, *h;
    385  1.1  christos 	size_t hlen, vlen = 0;
    386  1.1  christos 
    387  1.1  christos 	h = httpd->headers;
    388  1.1  christos 	hlen = strlen(header);
    389  1.1  christos 	if (value != NULL) {
    390  1.1  christos 		INSIST(eov != NULL);
    391  1.1  christos 		vlen = strlen(value);
    392  1.1  christos 	}
    393  1.1  christos 
    394  1.1  christos 	for (;;) {
    395  1.1  christos 		if (strncasecmp(h, header, hlen) != 0) {
    396  1.1  christos 			/*
    397  1.1  christos 			 * Skip to next line;
    398  1.1  christos 			 */
    399  1.1  christos 			cr = strchr(h, '\r');
    400  1.1  christos 			if (cr != NULL && cr[1] == '\n')
    401  1.1  christos 				cr++;
    402  1.1  christos 			nl = strchr(h, '\n');
    403  1.1  christos 
    404  1.1  christos 			/* last header? */
    405  1.1  christos 			h = cr;
    406  1.1  christos 			if (h == NULL || (nl != NULL && nl < h))
    407  1.1  christos 				h = nl;
    408  1.1  christos 			if (h == NULL)
    409  1.3  christos 				return (false);
    410  1.1  christos 			h++;
    411  1.1  christos 			continue;
    412  1.1  christos 		}
    413  1.1  christos 
    414  1.1  christos 		if (value == NULL)
    415  1.3  christos 			return (true);
    416  1.1  christos 
    417  1.1  christos 		/*
    418  1.1  christos 		 * Skip optional leading white space.
    419  1.1  christos 		 */
    420  1.1  christos 		h += hlen;
    421  1.1  christos 		while (*h == ' ' || *h == '\t')
    422  1.1  christos 			h++;
    423  1.1  christos 		/*
    424  1.1  christos 		 * Terminate token search on NULL or EOL.
    425  1.1  christos 		 */
    426  1.1  christos 		while (*h != 0 && *h != '\r' && *h != '\n') {
    427  1.1  christos 			if (strncasecmp(h, value, vlen) == 0)
    428  1.1  christos 				if (strchr(eov, h[vlen]) != NULL)
    429  1.3  christos 					return (true);
    430  1.1  christos 			/*
    431  1.1  christos 			 * Skip to next token.
    432  1.1  christos 			 */
    433  1.1  christos 			h += strcspn(h, eov);
    434  1.1  christos 			if (h[0] == '\r' && h[1] == '\n')
    435  1.1  christos 				h++;
    436  1.1  christos 			if (h[0] != 0)
    437  1.1  christos 				h++;
    438  1.1  christos 		}
    439  1.3  christos 		return (false);
    440  1.1  christos 	}
    441  1.1  christos }
    442  1.1  christos 
    443  1.1  christos static isc_result_t
    444  1.1  christos process_request(isc_httpd_t *httpd, int length) {
    445  1.1  christos 	char *s;
    446  1.1  christos 	char *p;
    447  1.1  christos 	int delim;
    448  1.1  christos 
    449  1.1  christos 	ENTER("request");
    450  1.1  christos 
    451  1.1  christos 	httpd->recvlen += length;
    452  1.1  christos 
    453  1.1  christos 	httpd->recvbuf[httpd->recvlen] = 0;
    454  1.1  christos 	httpd->headers = NULL;
    455  1.1  christos 
    456  1.1  christos 	/*
    457  1.1  christos 	 * If we don't find a blank line in our buffer, return that we need
    458  1.1  christos 	 * more data.
    459  1.1  christos 	 */
    460  1.1  christos 	s = strstr(httpd->recvbuf, "\r\n\r\n");
    461  1.1  christos 	delim = 2;
    462  1.1  christos 	if (s == NULL) {
    463  1.1  christos 		s = strstr(httpd->recvbuf, "\n\n");
    464  1.1  christos 		delim = 1;
    465  1.1  christos 	}
    466  1.1  christos 	if (s == NULL)
    467  1.1  christos 		return (ISC_R_NOTFOUND);
    468  1.1  christos 
    469  1.1  christos 	/*
    470  1.1  christos 	 * NUL terminate request at the blank line.
    471  1.1  christos 	 */
    472  1.1  christos 	s[delim] = 0;
    473  1.1  christos 
    474  1.1  christos 	/*
    475  1.1  christos 	 * Determine if this is a POST or GET method.  Any other values will
    476  1.1  christos 	 * cause an error to be returned.
    477  1.1  christos 	 */
    478  1.1  christos 	if (strncmp(httpd->recvbuf, "GET ", 4) == 0) {
    479  1.1  christos 		httpd->method = ISC_HTTPD_METHODGET;
    480  1.1  christos 		p = httpd->recvbuf + 4;
    481  1.1  christos 	} else if (strncmp(httpd->recvbuf, "POST ", 5) == 0) {
    482  1.1  christos 		httpd->method = ISC_HTTPD_METHODPOST;
    483  1.1  christos 		p = httpd->recvbuf + 5;
    484  1.1  christos 	} else {
    485  1.1  christos 		return (ISC_R_RANGE);
    486  1.1  christos 	}
    487  1.1  christos 
    488  1.1  christos 	/*
    489  1.1  christos 	 * From now on, p is the start of our buffer.
    490  1.1  christos 	 */
    491  1.1  christos 
    492  1.1  christos 	/*
    493  1.1  christos 	 * Extract the URL.
    494  1.1  christos 	 */
    495  1.1  christos 	s = p;
    496  1.1  christos 	while (LENGTHOK(s) && BUFLENOK(s) &&
    497  1.1  christos 	       (*s != '\n' && *s != '\r' && *s != '\0' && *s != ' '))
    498  1.1  christos 		s++;
    499  1.1  christos 	if (!LENGTHOK(s))
    500  1.1  christos 		return (ISC_R_NOTFOUND);
    501  1.1  christos 	if (!BUFLENOK(s))
    502  1.1  christos 		return (ISC_R_NOMEMORY);
    503  1.1  christos 	*s = 0;
    504  1.1  christos 
    505  1.1  christos 	/*
    506  1.1  christos 	 * Make the URL relative.
    507  1.1  christos 	 */
    508  1.1  christos 	if ((strncmp(p, "http:/", 6) == 0)
    509  1.1  christos 	    || (strncmp(p, "https:/", 7) == 0)) {
    510  1.1  christos 		/* Skip first / */
    511  1.1  christos 		while (*p != '/' && *p != 0)
    512  1.1  christos 			p++;
    513  1.1  christos 		if (*p == 0)
    514  1.1  christos 			return (ISC_R_RANGE);
    515  1.1  christos 		p++;
    516  1.1  christos 		/* Skip second / */
    517  1.1  christos 		while (*p != '/' && *p != 0)
    518  1.1  christos 			p++;
    519  1.1  christos 		if (*p == 0)
    520  1.1  christos 			return (ISC_R_RANGE);
    521  1.1  christos 		p++;
    522  1.1  christos 		/* Find third / */
    523  1.1  christos 		while (*p != '/' && *p != 0)
    524  1.1  christos 			p++;
    525  1.1  christos 		if (*p == 0) {
    526  1.1  christos 			p--;
    527  1.1  christos 			*p = '/';
    528  1.1  christos 		}
    529  1.1  christos 	}
    530  1.1  christos 
    531  1.1  christos 	httpd->url = p;
    532  1.1  christos 	p = s + 1;
    533  1.1  christos 	s = p;
    534  1.1  christos 
    535  1.1  christos 	/*
    536  1.1  christos 	 * Now, see if there is a ? mark in the URL.  If so, this is
    537  1.1  christos 	 * part of the query string, and we will split it from the URL.
    538  1.1  christos 	 */
    539  1.1  christos 	httpd->querystring = strchr(httpd->url, '?');
    540  1.1  christos 	if (httpd->querystring != NULL) {
    541  1.1  christos 		*(httpd->querystring) = 0;
    542  1.1  christos 		httpd->querystring++;
    543  1.1  christos 	}
    544  1.1  christos 
    545  1.1  christos 	/*
    546  1.1  christos 	 * Extract the HTTP/1.X protocol.  We will bounce on anything but
    547  1.1  christos 	 * HTTP/1.0 or HTTP/1.1 for now.
    548  1.1  christos 	 */
    549  1.1  christos 	while (LENGTHOK(s) && BUFLENOK(s) &&
    550  1.1  christos 	       (*s != '\n' && *s != '\r' && *s != '\0'))
    551  1.1  christos 		s++;
    552  1.1  christos 	if (!LENGTHOK(s))
    553  1.1  christos 		return (ISC_R_NOTFOUND);
    554  1.1  christos 	if (!BUFLENOK(s))
    555  1.1  christos 		return (ISC_R_NOMEMORY);
    556  1.1  christos 	/*
    557  1.1  christos 	 * Check that we have the expected eol delimiter.
    558  1.1  christos 	 */
    559  1.1  christos 	if (strncmp(s, delim == 1 ? "\n" : "\r\n", delim) != 0)
    560  1.1  christos 		return (ISC_R_RANGE);
    561  1.1  christos 	*s = 0;
    562  1.1  christos 	if ((strncmp(p, "HTTP/1.0", 8) != 0)
    563  1.1  christos 	    && (strncmp(p, "HTTP/1.1", 8) != 0))
    564  1.1  christos 		return (ISC_R_RANGE);
    565  1.1  christos 	httpd->protocol = p;
    566  1.1  christos 	p = s + delim;	/* skip past eol */
    567  1.1  christos 	s = p;
    568  1.1  christos 
    569  1.1  christos 	httpd->headers = s;
    570  1.1  christos 
    571  1.1  christos 	if (have_header(httpd, "Connection:", "close", ", \t\r\n"))
    572  1.1  christos 		httpd->flags |= HTTPD_CLOSE;
    573  1.1  christos 
    574  1.1  christos 	if (have_header(httpd, "Host:", NULL, NULL))
    575  1.1  christos 		httpd->flags |= HTTPD_FOUNDHOST;
    576  1.1  christos 
    577  1.1  christos 	if (strncmp(httpd->protocol, "HTTP/1.0", 8) == 0) {
    578  1.1  christos 		if (have_header(httpd, "Connection:", "Keep-Alive",
    579  1.1  christos 				", \t\r\n"))
    580  1.1  christos 			httpd->flags |= HTTPD_KEEPALIVE;
    581  1.1  christos 		else
    582  1.1  christos 			httpd->flags |= HTTPD_CLOSE;
    583  1.1  christos 	}
    584  1.1  christos 
    585  1.1  christos 	/*
    586  1.1  christos 	 * Check for Accept-Encoding:
    587  1.1  christos 	 */
    588  1.1  christos #ifdef HAVE_ZLIB
    589  1.1  christos 	if (have_header(httpd, "Accept-Encoding:", "deflate", ";, \t\r\n"))
    590  1.1  christos 		httpd->flags |= HTTPD_ACCEPT_DEFLATE;
    591  1.1  christos #endif
    592  1.1  christos 
    593  1.1  christos 	/*
    594  1.1  christos 	 * Standards compliance hooks here.
    595  1.1  christos 	 */
    596  1.1  christos 	if (strcmp(httpd->protocol, "HTTP/1.1") == 0
    597  1.1  christos 	    && ((httpd->flags & HTTPD_FOUNDHOST) == 0))
    598  1.1  christos 		return (ISC_R_RANGE);
    599  1.1  christos 
    600  1.1  christos 	EXIT("request");
    601  1.1  christos 
    602  1.1  christos 	return (ISC_R_SUCCESS);
    603  1.1  christos }
    604  1.1  christos 
    605  1.1  christos static void
    606  1.1  christos isc_httpd_accept(isc_task_t *task, isc_event_t *ev) {
    607  1.1  christos 	isc_result_t result;
    608  1.1  christos 	isc_httpdmgr_t *httpdmgr = ev->ev_arg;
    609  1.1  christos 	isc_httpd_t *httpd;
    610  1.1  christos 	isc_region_t r;
    611  1.1  christos 	isc_socket_newconnev_t *nev = (isc_socket_newconnev_t *)ev;
    612  1.1  christos 	isc_sockaddr_t peeraddr;
    613  1.1  christos 	char *headerdata;
    614  1.1  christos 
    615  1.1  christos 	ENTER("accept");
    616  1.1  christos 
    617  1.1  christos 	LOCK(&httpdmgr->lock);
    618  1.1  christos 	if (MSHUTTINGDOWN(httpdmgr)) {
    619  1.1  christos 		NOTICE("accept shutting down, goto out");
    620  1.1  christos 		goto out;
    621  1.1  christos 	}
    622  1.1  christos 
    623  1.1  christos 	if (nev->result == ISC_R_CANCELED) {
    624  1.1  christos 		NOTICE("accept canceled, goto out");
    625  1.1  christos 		goto out;
    626  1.1  christos 	}
    627  1.1  christos 
    628  1.1  christos 	if (nev->result != ISC_R_SUCCESS) {
    629  1.1  christos 		/* XXXMLG log failure */
    630  1.1  christos 		NOTICE("accept returned failure, goto requeue");
    631  1.1  christos 		goto requeue;
    632  1.1  christos 	}
    633  1.1  christos 
    634  1.1  christos 	(void)isc_socket_getpeername(nev->newsocket, &peeraddr);
    635  1.1  christos 	if (httpdmgr->client_ok != NULL &&
    636  1.1  christos 	    !(httpdmgr->client_ok)(&peeraddr, httpdmgr->cb_arg)) {
    637  1.1  christos 		isc_socket_detach(&nev->newsocket);
    638  1.1  christos 		goto requeue;
    639  1.1  christos 	}
    640  1.1  christos 
    641  1.1  christos 	httpd = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpd_t));
    642  1.1  christos 	if (httpd == NULL) {
    643  1.1  christos 		/* XXXMLG log failure */
    644  1.1  christos 		NOTICE("accept failed to allocate memory, goto requeue");
    645  1.1  christos 		isc_socket_detach(&nev->newsocket);
    646  1.1  christos 		goto requeue;
    647  1.1  christos 	}
    648  1.1  christos 
    649  1.1  christos 	httpd->mgr = httpdmgr;
    650  1.1  christos 	ISC_LINK_INIT(httpd, link);
    651  1.1  christos 	ISC_LIST_APPEND(httpdmgr->running, httpd, link);
    652  1.1  christos 	ISC_HTTPD_SETRECV(httpd);
    653  1.1  christos 	httpd->sock = nev->newsocket;
    654  1.1  christos 	isc_socket_setname(httpd->sock, "httpd", NULL);
    655  1.1  christos 	httpd->flags = 0;
    656  1.1  christos 
    657  1.1  christos 	/*
    658  1.1  christos 	 * Initialize the buffer for our headers.
    659  1.1  christos 	 */
    660  1.1  christos 	headerdata = isc_mem_get(httpdmgr->mctx, HTTP_SENDGROW);
    661  1.1  christos 	if (headerdata == NULL) {
    662  1.1  christos 		isc_mem_put(httpdmgr->mctx, httpd, sizeof(isc_httpd_t));
    663  1.1  christos 		isc_socket_detach(&nev->newsocket);
    664  1.1  christos 		goto requeue;
    665  1.1  christos 	}
    666  1.1  christos 	isc_buffer_init(&httpd->headerbuffer, headerdata, HTTP_SENDGROW);
    667  1.1  christos 
    668  1.1  christos 	isc_buffer_initnull(&httpd->compbuffer);
    669  1.1  christos 	isc_buffer_initnull(&httpd->bodybuffer);
    670  1.3  christos 	httpd->sendbuffer = NULL;
    671  1.1  christos 	reset_client(httpd);
    672  1.1  christos 
    673  1.1  christos 	r.base = (unsigned char *)httpd->recvbuf;
    674  1.1  christos 	r.length = HTTP_RECVLEN - 1;
    675  1.1  christos 	result = isc_socket_recv(httpd->sock, &r, 1, task, isc_httpd_recvdone,
    676  1.1  christos 				 httpd);
    677  1.1  christos 	/* FIXME!!! */
    678  1.1  christos 	POST(result);
    679  1.1  christos 	NOTICE("accept queued recv on socket");
    680  1.1  christos 
    681  1.1  christos  requeue:
    682  1.1  christos 	result = isc_socket_accept(httpdmgr->sock, task, isc_httpd_accept,
    683  1.1  christos 				   httpdmgr);
    684  1.1  christos 	if (result != ISC_R_SUCCESS) {
    685  1.1  christos 		/* XXXMLG what to do?  Log failure... */
    686  1.1  christos 		NOTICE("accept could not reaccept due to failure");
    687  1.1  christos 	}
    688  1.1  christos 
    689  1.1  christos  out:
    690  1.1  christos 	UNLOCK(&httpdmgr->lock);
    691  1.1  christos 
    692  1.1  christos 	httpdmgr_destroy(httpdmgr);
    693  1.1  christos 
    694  1.1  christos 	isc_event_free(&ev);
    695  1.1  christos 
    696  1.1  christos 	EXIT("accept");
    697  1.1  christos }
    698  1.1  christos 
    699  1.1  christos static isc_result_t
    700  1.1  christos render_404(const char *url, isc_httpdurl_t *urlinfo,
    701  1.1  christos 	   const char *querystring, const char *headers, void *arg,
    702  1.1  christos 	   unsigned int *retcode, const char **retmsg,
    703  1.1  christos 	   const char **mimetype, isc_buffer_t *b,
    704  1.1  christos 	   isc_httpdfree_t **freecb, void **freecb_args)
    705  1.1  christos {
    706  1.1  christos 	static char msg[] = "No such URL.\r\n";
    707  1.1  christos 
    708  1.1  christos 	UNUSED(url);
    709  1.1  christos 	UNUSED(urlinfo);
    710  1.1  christos 	UNUSED(querystring);
    711  1.1  christos 	UNUSED(headers);
    712  1.1  christos 	UNUSED(arg);
    713  1.1  christos 
    714  1.1  christos 	*retcode = 404;
    715  1.1  christos 	*retmsg = "No such URL";
    716  1.1  christos 	*mimetype = "text/plain";
    717  1.1  christos 	isc_buffer_reinit(b, msg, strlen(msg));
    718  1.1  christos 	isc_buffer_add(b, strlen(msg));
    719  1.1  christos 	*freecb = NULL;
    720  1.1  christos 	*freecb_args = NULL;
    721  1.1  christos 
    722  1.1  christos 	return (ISC_R_SUCCESS);
    723  1.1  christos }
    724  1.1  christos 
    725  1.1  christos static isc_result_t
    726  1.1  christos render_500(const char *url, isc_httpdurl_t *urlinfo,
    727  1.1  christos 	   const char *querystring, const char *headers, void *arg,
    728  1.1  christos 	   unsigned int *retcode, const char **retmsg,
    729  1.1  christos 	   const char **mimetype, isc_buffer_t *b,
    730  1.1  christos 	   isc_httpdfree_t **freecb, void **freecb_args)
    731  1.1  christos {
    732  1.1  christos 	static char msg[] = "Internal server failure.\r\n";
    733  1.1  christos 
    734  1.1  christos 	UNUSED(url);
    735  1.1  christos 	UNUSED(urlinfo);
    736  1.1  christos 	UNUSED(querystring);
    737  1.1  christos 	UNUSED(headers);
    738  1.1  christos 	UNUSED(arg);
    739  1.1  christos 
    740  1.1  christos 	*retcode = 500;
    741  1.1  christos 	*retmsg = "Internal server failure";
    742  1.1  christos 	*mimetype = "text/plain";
    743  1.1  christos 	isc_buffer_reinit(b, msg, strlen(msg));
    744  1.1  christos 	isc_buffer_add(b, strlen(msg));
    745  1.1  christos 	*freecb = NULL;
    746  1.1  christos 	*freecb_args = NULL;
    747  1.1  christos 
    748  1.1  christos 	return (ISC_R_SUCCESS);
    749  1.1  christos }
    750  1.1  christos 
    751  1.1  christos #ifdef HAVE_ZLIB
    752  1.1  christos /*%<
    753  1.1  christos  * Reallocates compbuffer to size, does nothing if compbuffer is already
    754  1.1  christos  * larger than size.
    755  1.1  christos  *
    756  1.1  christos  * Requires:
    757  1.1  christos  *\li	httpd a valid isc_httpd_t object
    758  1.1  christos  *
    759  1.1  christos  * Returns:
    760  1.1  christos  *\li	#ISC_R_SUCCESS		-- all is well.
    761  1.1  christos  *\li	#ISC_R_NOMEMORY		-- not enough memory to extend buffer
    762  1.1  christos  */
    763  1.1  christos static isc_result_t
    764  1.1  christos alloc_compspace(isc_httpd_t *httpd, unsigned int size) {
    765  1.1  christos 	char *newspace;
    766  1.1  christos 	isc_region_t r;
    767  1.1  christos 
    768  1.1  christos 	isc_buffer_region(&httpd->compbuffer, &r);
    769  1.1  christos 	if (size < r.length) {
    770  1.1  christos 		return (ISC_R_SUCCESS);
    771  1.1  christos 	}
    772  1.1  christos 
    773  1.1  christos 	newspace = isc_mem_get(httpd->mgr->mctx, size);
    774  1.1  christos 	if (newspace == NULL)
    775  1.1  christos 		return (ISC_R_NOMEMORY);
    776  1.1  christos 	isc_buffer_reinit(&httpd->compbuffer, newspace, size);
    777  1.1  christos 
    778  1.1  christos 	if (r.base != NULL) {
    779  1.1  christos 		isc_mem_put(httpd->mgr->mctx, r.base, r.length);
    780  1.1  christos 	}
    781  1.1  christos 
    782  1.1  christos 	return (ISC_R_SUCCESS);
    783  1.1  christos }
    784  1.1  christos 
    785  1.1  christos /*%<
    786  1.1  christos  * Tries to compress httpd->bodybuffer to httpd->compbuffer, extending it
    787  1.1  christos  * if necessary.
    788  1.1  christos  *
    789  1.1  christos  * Requires:
    790  1.1  christos  *\li	httpd a valid isc_httpd_t object
    791  1.1  christos  *
    792  1.1  christos  * Returns:
    793  1.1  christos  *\li	#ISC_R_SUCCESS	  -- all is well.
    794  1.1  christos  *\li	#ISC_R_NOMEMORY	  -- not enough memory to compress data
    795  1.1  christos  *\li	#ISC_R_FAILURE	  -- error during compression or compressed
    796  1.1  christos  *			     data would be larger than input data
    797  1.1  christos  */
    798  1.1  christos static isc_result_t
    799  1.1  christos isc_httpd_compress(isc_httpd_t *httpd) {
    800  1.1  christos 	z_stream zstr;
    801  1.1  christos 	isc_region_t r;
    802  1.1  christos 	isc_result_t result;
    803  1.1  christos 	int ret;
    804  1.1  christos 	int inputlen;
    805  1.1  christos 
    806  1.1  christos 	inputlen = isc_buffer_usedlength(&httpd->bodybuffer);
    807  1.1  christos 	result = alloc_compspace(httpd, inputlen);
    808  1.1  christos 	if (result != ISC_R_SUCCESS) {
    809  1.1  christos 		return result;
    810  1.1  christos 	}
    811  1.1  christos 	isc_buffer_region(&httpd->compbuffer, &r);
    812  1.1  christos 
    813  1.1  christos 	/*
    814  1.1  christos 	 * We're setting output buffer size to input size so it fails if the
    815  1.1  christos 	 * compressed data size would be bigger than the input size.
    816  1.1  christos 	 */
    817  1.1  christos 	memset(&zstr, 0, sizeof(zstr));
    818  1.1  christos 	zstr.total_in = zstr.avail_in =
    819  1.1  christos 			zstr.total_out = zstr.avail_out = inputlen;
    820  1.1  christos 
    821  1.1  christos 	zstr.next_in = isc_buffer_base(&httpd->bodybuffer);
    822  1.1  christos 	zstr.next_out = r.base;
    823  1.1  christos 
    824  1.1  christos 	ret = deflateInit(&zstr, Z_DEFAULT_COMPRESSION);
    825  1.1  christos 	if (ret == Z_OK) {
    826  1.1  christos 		ret = deflate(&zstr, Z_FINISH);
    827  1.1  christos 	}
    828  1.1  christos 	deflateEnd(&zstr);
    829  1.1  christos 	if (ret == Z_STREAM_END) {
    830  1.1  christos 		isc_buffer_add(&httpd->compbuffer, inputlen - zstr.avail_out);
    831  1.1  christos 		return (ISC_R_SUCCESS);
    832  1.1  christos 	} else {
    833  1.1  christos 		return (ISC_R_FAILURE);
    834  1.1  christos 	}
    835  1.1  christos }
    836  1.1  christos #endif
    837  1.1  christos 
    838  1.1  christos static void
    839  1.1  christos isc_httpd_recvdone(isc_task_t *task, isc_event_t *ev) {
    840  1.1  christos 	isc_result_t result;
    841  1.1  christos 	isc_httpd_t *httpd = ev->ev_arg;
    842  1.1  christos 	isc_socketevent_t *sev = (isc_socketevent_t *)ev;
    843  1.3  christos 	isc_buffer_t *databuffer;
    844  1.1  christos 	isc_httpdurl_t *url;
    845  1.1  christos 	isc_time_t now;
    846  1.3  christos 	isc_region_t r;
    847  1.3  christos 	bool is_compressed = false;
    848  1.1  christos 	char datebuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
    849  1.1  christos 
    850  1.1  christos 	ENTER("recv");
    851  1.1  christos 
    852  1.1  christos 	INSIST(ISC_HTTPD_ISRECV(httpd));
    853  1.1  christos 
    854  1.1  christos 	if (sev->result != ISC_R_SUCCESS) {
    855  1.1  christos 		NOTICE("recv destroying client");
    856  1.1  christos 		destroy_client(&httpd);
    857  1.1  christos 		goto out;
    858  1.1  christos 	}
    859  1.1  christos 
    860  1.1  christos 	result = process_request(httpd, sev->n);
    861  1.1  christos 	if (result == ISC_R_NOTFOUND) {
    862  1.1  christos 		if (httpd->recvlen >= HTTP_RECVLEN - 1) {
    863  1.1  christos 			destroy_client(&httpd);
    864  1.1  christos 			goto out;
    865  1.1  christos 		}
    866  1.1  christos 		r.base = (unsigned char *)httpd->recvbuf + httpd->recvlen;
    867  1.1  christos 		r.length = HTTP_RECVLEN - httpd->recvlen - 1;
    868  1.1  christos 		/* check return code? */
    869  1.1  christos 		(void)isc_socket_recv(httpd->sock, &r, 1, task,
    870  1.1  christos 				      isc_httpd_recvdone, httpd);
    871  1.1  christos 		goto out;
    872  1.1  christos 	} else if (result != ISC_R_SUCCESS) {
    873  1.1  christos 		destroy_client(&httpd);
    874  1.1  christos 		goto out;
    875  1.1  christos 	}
    876  1.1  christos 
    877  1.1  christos 	ISC_HTTPD_SETSEND(httpd);
    878  1.1  christos 
    879  1.1  christos 	/*
    880  1.1  christos 	 * XXXMLG Call function here.  Provide an add-header function
    881  1.1  christos 	 * which will append the common headers to a response we generate.
    882  1.1  christos 	 */
    883  1.1  christos 	isc_buffer_initnull(&httpd->bodybuffer);
    884  1.1  christos 	isc_time_now(&now);
    885  1.1  christos 	isc_time_formathttptimestamp(&now, datebuf, sizeof(datebuf));
    886  1.1  christos 	url = ISC_LIST_HEAD(httpd->mgr->urls);
    887  1.1  christos 	while (url != NULL) {
    888  1.1  christos 		if (strcmp(httpd->url, url->url) == 0)
    889  1.1  christos 			break;
    890  1.1  christos 		url = ISC_LIST_NEXT(url, link);
    891  1.1  christos 	}
    892  1.1  christos 	if (url == NULL)
    893  1.1  christos 		result = httpd->mgr->render_404(httpd->url, NULL,
    894  1.1  christos 						httpd->querystring,
    895  1.1  christos 						NULL, NULL,
    896  1.1  christos 						&httpd->retcode,
    897  1.1  christos 						&httpd->retmsg,
    898  1.1  christos 						&httpd->mimetype,
    899  1.1  christos 						&httpd->bodybuffer,
    900  1.1  christos 						&httpd->freecb,
    901  1.1  christos 						&httpd->freecb_arg);
    902  1.1  christos 	else
    903  1.1  christos 		result = url->action(httpd->url, url,
    904  1.1  christos 				     httpd->querystring,
    905  1.1  christos 				     httpd->headers,
    906  1.1  christos 				     url->action_arg,
    907  1.1  christos 				     &httpd->retcode, &httpd->retmsg,
    908  1.1  christos 				     &httpd->mimetype, &httpd->bodybuffer,
    909  1.1  christos 				     &httpd->freecb, &httpd->freecb_arg);
    910  1.1  christos 	if (result != ISC_R_SUCCESS) {
    911  1.1  christos 		result = httpd->mgr->render_500(httpd->url, url,
    912  1.1  christos 						httpd->querystring,
    913  1.1  christos 						NULL, NULL,
    914  1.1  christos 						&httpd->retcode,
    915  1.1  christos 						&httpd->retmsg,
    916  1.1  christos 						&httpd->mimetype,
    917  1.1  christos 						&httpd->bodybuffer,
    918  1.1  christos 						&httpd->freecb,
    919  1.1  christos 						&httpd->freecb_arg);
    920  1.1  christos 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
    921  1.1  christos 	}
    922  1.1  christos 
    923  1.1  christos #ifdef HAVE_ZLIB
    924  1.3  christos 	if ((httpd->flags & HTTPD_ACCEPT_DEFLATE) != 0) {
    925  1.1  christos 			result = isc_httpd_compress(httpd);
    926  1.1  christos 			if (result == ISC_R_SUCCESS) {
    927  1.3  christos 				is_compressed = true;
    928  1.1  christos 			}
    929  1.1  christos 	}
    930  1.1  christos #endif
    931  1.1  christos 
    932  1.1  christos 	isc_httpd_response(httpd);
    933  1.1  christos 	if ((httpd->flags & HTTPD_KEEPALIVE) != 0)
    934  1.1  christos 		isc_httpd_addheader(httpd, "Connection", "Keep-Alive");
    935  1.1  christos 	isc_httpd_addheader(httpd, "Content-Type", httpd->mimetype);
    936  1.1  christos 	isc_httpd_addheader(httpd, "Date", datebuf);
    937  1.1  christos 	isc_httpd_addheader(httpd, "Expires", datebuf);
    938  1.1  christos 
    939  1.1  christos 	if (url != NULL && url->isstatic) {
    940  1.1  christos 		char loadbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
    941  1.1  christos 		isc_time_formathttptimestamp(&url->loadtime,
    942  1.1  christos 					     loadbuf, sizeof(loadbuf));
    943  1.1  christos 		isc_httpd_addheader(httpd, "Last-Modified", loadbuf);
    944  1.1  christos 		isc_httpd_addheader(httpd, "Cache-Control: public", NULL);
    945  1.1  christos 	} else {
    946  1.1  christos 		isc_httpd_addheader(httpd, "Last-Modified", datebuf);
    947  1.1  christos 		isc_httpd_addheader(httpd, "Pragma: no-cache", NULL);
    948  1.1  christos 		isc_httpd_addheader(httpd, "Cache-Control: no-cache", NULL);
    949  1.1  christos 	}
    950  1.1  christos 
    951  1.1  christos 	isc_httpd_addheader(httpd, "Server: libisc", NULL);
    952  1.1  christos 
    953  1.3  christos 	if (is_compressed == true) {
    954  1.1  christos 		isc_httpd_addheader(httpd, "Content-Encoding", "deflate");
    955  1.1  christos 		isc_httpd_addheaderuint(httpd, "Content-Length",
    956  1.1  christos 					isc_buffer_usedlength(&httpd->compbuffer));
    957  1.1  christos 	} else {
    958  1.1  christos 		isc_httpd_addheaderuint(httpd, "Content-Length",
    959  1.1  christos 		isc_buffer_usedlength(&httpd->bodybuffer));
    960  1.1  christos 	}
    961  1.1  christos 
    962  1.1  christos 	isc_httpd_endheaders(httpd);  /* done */
    963  1.1  christos 
    964  1.1  christos 	/*
    965  1.3  christos 	 * Append either the compressed or the non-compressed response body to
    966  1.3  christos 	 * the response headers and store the result in httpd->sendbuffer.
    967  1.1  christos 	 */
    968  1.3  christos 	isc_buffer_dup(httpd->mgr->mctx,
    969  1.3  christos 		       &httpd->sendbuffer, &httpd->headerbuffer);
    970  1.3  christos 	isc_buffer_setautorealloc(httpd->sendbuffer, true);
    971  1.3  christos 	databuffer = (is_compressed ? &httpd->compbuffer : &httpd->bodybuffer);
    972  1.3  christos 	isc_buffer_usedregion(databuffer, &r);
    973  1.3  christos 	result = isc_buffer_copyregion(httpd->sendbuffer, &r);
    974  1.3  christos 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
    975  1.3  christos 	/*
    976  1.3  christos 	 * Determine total response size.
    977  1.3  christos 	 */
    978  1.3  christos 	isc_buffer_usedregion(httpd->sendbuffer, &r);
    979  1.1  christos 
    980  1.1  christos 	/* check return code? */
    981  1.3  christos 	(void)isc_socket_send(httpd->sock, &r, task,
    982  1.1  christos 			       isc_httpd_senddone, httpd);
    983  1.1  christos 
    984  1.1  christos  out:
    985  1.1  christos 	isc_event_free(&ev);
    986  1.1  christos 	EXIT("recv");
    987  1.1  christos }
    988  1.1  christos 
    989  1.1  christos void
    990  1.1  christos isc_httpdmgr_shutdown(isc_httpdmgr_t **httpdmgrp) {
    991  1.1  christos 	isc_httpdmgr_t *httpdmgr;
    992  1.1  christos 	isc_httpd_t *httpd;
    993  1.1  christos 	httpdmgr = *httpdmgrp;
    994  1.1  christos 	*httpdmgrp = NULL;
    995  1.1  christos 
    996  1.1  christos 	ENTER("isc_httpdmgr_shutdown");
    997  1.1  christos 
    998  1.1  christos 	LOCK(&httpdmgr->lock);
    999  1.1  christos 
   1000  1.1  christos 	MSETSHUTTINGDOWN(httpdmgr);
   1001  1.1  christos 
   1002  1.1  christos 	isc_socket_cancel(httpdmgr->sock, httpdmgr->task, ISC_SOCKCANCEL_ALL);
   1003  1.1  christos 
   1004  1.1  christos 	httpd = ISC_LIST_HEAD(httpdmgr->running);
   1005  1.1  christos 	while (httpd != NULL) {
   1006  1.1  christos 		isc_socket_cancel(httpd->sock, httpdmgr->task,
   1007  1.1  christos 				  ISC_SOCKCANCEL_ALL);
   1008  1.1  christos 		httpd = ISC_LIST_NEXT(httpd, link);
   1009  1.1  christos 	}
   1010  1.1  christos 
   1011  1.1  christos 	UNLOCK(&httpdmgr->lock);
   1012  1.1  christos 
   1013  1.1  christos 	EXIT("isc_httpdmgr_shutdown");
   1014  1.1  christos }
   1015  1.1  christos 
   1016  1.1  christos static isc_result_t
   1017  1.1  christos grow_headerspace(isc_httpd_t *httpd) {
   1018  1.1  christos 	char *newspace;
   1019  1.1  christos 	unsigned int newlen;
   1020  1.1  christos 	isc_region_t r;
   1021  1.1  christos 
   1022  1.1  christos 	isc_buffer_region(&httpd->headerbuffer, &r);
   1023  1.1  christos 	newlen = r.length + HTTP_SENDGROW;
   1024  1.1  christos 	if (newlen > HTTP_SEND_MAXLEN)
   1025  1.1  christos 		return (ISC_R_NOSPACE);
   1026  1.1  christos 
   1027  1.1  christos 	newspace = isc_mem_get(httpd->mgr->mctx, newlen);
   1028  1.1  christos 	if (newspace == NULL)
   1029  1.1  christos 		return (ISC_R_NOMEMORY);
   1030  1.1  christos 
   1031  1.1  christos 	isc_buffer_reinit(&httpd->headerbuffer, newspace, newlen);
   1032  1.1  christos 
   1033  1.1  christos 	isc_mem_put(httpd->mgr->mctx, r.base, r.length);
   1034  1.1  christos 
   1035  1.1  christos 	return (ISC_R_SUCCESS);
   1036  1.1  christos }
   1037  1.1  christos 
   1038  1.1  christos isc_result_t
   1039  1.1  christos isc_httpd_response(isc_httpd_t *httpd) {
   1040  1.1  christos 	isc_result_t result;
   1041  1.1  christos 	unsigned int needlen;
   1042  1.1  christos 
   1043  1.1  christos 	needlen = strlen(httpd->protocol) + 1; /* protocol + space */
   1044  1.1  christos 	needlen += 3 + 1;  /* room for response code, always 3 bytes */
   1045  1.1  christos 	needlen += strlen(httpd->retmsg) + 2;  /* return msg + CRLF */
   1046  1.1  christos 
   1047  1.1  christos 	while (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
   1048  1.1  christos 		result = grow_headerspace(httpd);
   1049  1.1  christos 		if (result != ISC_R_SUCCESS)
   1050  1.1  christos 			return (result);
   1051  1.1  christos 	}
   1052  1.1  christos 
   1053  1.1  christos 	return (isc_buffer_printf(&httpd->headerbuffer, "%s %03u %s\r\n",
   1054  1.1  christos 				   httpd->protocol, httpd->retcode,
   1055  1.1  christos 				   httpd->retmsg));
   1056  1.1  christos }
   1057  1.1  christos 
   1058  1.1  christos isc_result_t
   1059  1.1  christos isc_httpd_addheader(isc_httpd_t *httpd, const char *name,
   1060  1.1  christos 		    const char *val)
   1061  1.1  christos {
   1062  1.1  christos 	isc_result_t result;
   1063  1.1  christos 	unsigned int needlen;
   1064  1.1  christos 
   1065  1.1  christos 	needlen = strlen(name); /* name itself */
   1066  1.1  christos 	if (val != NULL)
   1067  1.1  christos 		needlen += 2 + strlen(val); /* :<space> and val */
   1068  1.1  christos 	needlen += 2; /* CRLF */
   1069  1.1  christos 
   1070  1.1  christos 	while (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
   1071  1.1  christos 		result = grow_headerspace(httpd);
   1072  1.1  christos 		if (result != ISC_R_SUCCESS)
   1073  1.1  christos 			return (result);
   1074  1.1  christos 	}
   1075  1.1  christos 
   1076  1.1  christos 	if (val != NULL) {
   1077  1.1  christos 		return (isc_buffer_printf(&httpd->headerbuffer, "%s: %s\r\n",
   1078  1.1  christos 					   name, val));
   1079  1.1  christos 	} else {
   1080  1.1  christos 		return (isc_buffer_printf(&httpd->headerbuffer, "%s\r\n",
   1081  1.1  christos 					   name));
   1082  1.1  christos 	}
   1083  1.1  christos }
   1084  1.1  christos 
   1085  1.1  christos isc_result_t
   1086  1.1  christos isc_httpd_endheaders(isc_httpd_t *httpd) {
   1087  1.1  christos 	isc_result_t result;
   1088  1.1  christos 
   1089  1.1  christos 	while (isc_buffer_availablelength(&httpd->headerbuffer) < 2) {
   1090  1.1  christos 		result = grow_headerspace(httpd);
   1091  1.1  christos 		if (result != ISC_R_SUCCESS)
   1092  1.1  christos 			return (result);
   1093  1.1  christos 	}
   1094  1.1  christos 
   1095  1.1  christos 	return (isc_buffer_printf(&httpd->headerbuffer, "\r\n"));
   1096  1.1  christos }
   1097  1.1  christos 
   1098  1.1  christos isc_result_t
   1099  1.1  christos isc_httpd_addheaderuint(isc_httpd_t *httpd, const char *name, int val) {
   1100  1.1  christos 	isc_result_t result;
   1101  1.1  christos 	unsigned int needlen;
   1102  1.1  christos 	char buf[sizeof "18446744073709551616"];
   1103  1.1  christos 
   1104  1.1  christos 	snprintf(buf, sizeof(buf), "%d", val);
   1105  1.1  christos 
   1106  1.1  christos 	needlen = strlen(name); /* name itself */
   1107  1.1  christos 	needlen += 2 + strlen(buf); /* :<space> and val */
   1108  1.1  christos 	needlen += 2; /* CRLF */
   1109  1.1  christos 
   1110  1.1  christos 	while (isc_buffer_availablelength(&httpd->headerbuffer) < needlen) {
   1111  1.1  christos 		result = grow_headerspace(httpd);
   1112  1.1  christos 		if (result != ISC_R_SUCCESS)
   1113  1.1  christos 			return (result);
   1114  1.1  christos 	}
   1115  1.1  christos 
   1116  1.1  christos 	return (isc_buffer_printf(&httpd->headerbuffer, "%s: %s\r\n", name,
   1117  1.1  christos 				   buf));
   1118  1.1  christos }
   1119  1.1  christos 
   1120  1.1  christos static void
   1121  1.1  christos isc_httpd_senddone(isc_task_t *task, isc_event_t *ev) {
   1122  1.1  christos 	isc_httpd_t *httpd = ev->ev_arg;
   1123  1.1  christos 	isc_region_t r;
   1124  1.1  christos 	isc_socketevent_t *sev = (isc_socketevent_t *)ev;
   1125  1.1  christos 
   1126  1.1  christos 	ENTER("senddone");
   1127  1.1  christos 	INSIST(ISC_HTTPD_ISSEND(httpd));
   1128  1.1  christos 
   1129  1.3  christos 	isc_buffer_free(&httpd->sendbuffer);
   1130  1.1  christos 
   1131  1.1  christos 	/*
   1132  1.1  christos 	 * We will always want to clean up our receive buffer, even if we
   1133  1.1  christos 	 * got an error on send or we are shutting down.
   1134  1.1  christos 	 *
   1135  1.1  christos 	 * We will pass in the buffer only if there is data in it.  If
   1136  1.1  christos 	 * there is no data, we will pass in a NULL.
   1137  1.1  christos 	 */
   1138  1.1  christos 	if (httpd->freecb != NULL) {
   1139  1.1  christos 		isc_buffer_t *b = NULL;
   1140  1.1  christos 		if (isc_buffer_length(&httpd->bodybuffer) > 0) {
   1141  1.1  christos 			b = &httpd->bodybuffer;
   1142  1.1  christos 			httpd->freecb(b, httpd->freecb_arg);
   1143  1.1  christos 		}
   1144  1.1  christos 		NOTICE("senddone free callback performed");
   1145  1.1  christos 	}
   1146  1.1  christos 
   1147  1.1  christos 	if (sev->result != ISC_R_SUCCESS) {
   1148  1.1  christos 		destroy_client(&httpd);
   1149  1.1  christos 		goto out;
   1150  1.1  christos 	}
   1151  1.1  christos 
   1152  1.1  christos 	if ((httpd->flags & HTTPD_CLOSE) != 0) {
   1153  1.1  christos 		destroy_client(&httpd);
   1154  1.1  christos 		goto out;
   1155  1.1  christos 	}
   1156  1.1  christos 
   1157  1.1  christos 	ISC_HTTPD_SETRECV(httpd);
   1158  1.1  christos 
   1159  1.1  christos 	NOTICE("senddone restarting recv on socket");
   1160  1.1  christos 
   1161  1.1  christos 	reset_client(httpd);
   1162  1.1  christos 
   1163  1.1  christos 	r.base = (unsigned char *)httpd->recvbuf;
   1164  1.1  christos 	r.length = HTTP_RECVLEN - 1;
   1165  1.1  christos 	/* check return code? */
   1166  1.1  christos 	(void)isc_socket_recv(httpd->sock, &r, 1, task,
   1167  1.1  christos 			      isc_httpd_recvdone, httpd);
   1168  1.1  christos 
   1169  1.1  christos out:
   1170  1.1  christos 	isc_event_free(&ev);
   1171  1.1  christos 	EXIT("senddone");
   1172  1.1  christos }
   1173  1.1  christos 
   1174  1.1  christos static void
   1175  1.1  christos reset_client(isc_httpd_t *httpd) {
   1176  1.1  christos 	/*
   1177  1.1  christos 	 * Catch errors here.  We MUST be in RECV mode, and we MUST NOT have
   1178  1.1  christos 	 * any outstanding buffers.  If we have buffers, we have a leak.
   1179  1.1  christos 	 */
   1180  1.1  christos 	INSIST(ISC_HTTPD_ISRECV(httpd));
   1181  1.1  christos 	INSIST(!ISC_LINK_LINKED(&httpd->headerbuffer, link));
   1182  1.1  christos 	INSIST(!ISC_LINK_LINKED(&httpd->bodybuffer, link));
   1183  1.1  christos 
   1184  1.1  christos 	httpd->recvbuf[0] = 0;
   1185  1.1  christos 	httpd->recvlen = 0;
   1186  1.1  christos 	httpd->headers = NULL;
   1187  1.1  christos 	httpd->method = ISC_HTTPD_METHODUNKNOWN;
   1188  1.1  christos 	httpd->url = NULL;
   1189  1.1  christos 	httpd->querystring = NULL;
   1190  1.1  christos 	httpd->protocol = NULL;
   1191  1.1  christos 	httpd->flags = 0;
   1192  1.1  christos 
   1193  1.1  christos 	isc_buffer_clear(&httpd->headerbuffer);
   1194  1.1  christos 	isc_buffer_clear(&httpd->compbuffer);
   1195  1.1  christos 	isc_buffer_invalidate(&httpd->bodybuffer);
   1196  1.1  christos }
   1197  1.1  christos 
   1198  1.1  christos isc_result_t
   1199  1.1  christos isc_httpdmgr_addurl(isc_httpdmgr_t *httpdmgr, const char *url,
   1200  1.1  christos 		    isc_httpdaction_t *func, void *arg)
   1201  1.1  christos {
   1202  1.3  christos 	return (isc_httpdmgr_addurl2(httpdmgr, url, false, func, arg));
   1203  1.1  christos }
   1204  1.1  christos 
   1205  1.1  christos isc_result_t
   1206  1.1  christos isc_httpdmgr_addurl2(isc_httpdmgr_t *httpdmgr, const char *url,
   1207  1.3  christos 		     bool isstatic,
   1208  1.1  christos 		     isc_httpdaction_t *func, void *arg)
   1209  1.1  christos {
   1210  1.1  christos 	isc_httpdurl_t *item;
   1211  1.1  christos 
   1212  1.1  christos 	if (url == NULL) {
   1213  1.1  christos 		httpdmgr->render_404 = func;
   1214  1.1  christos 		return (ISC_R_SUCCESS);
   1215  1.1  christos 	}
   1216  1.1  christos 
   1217  1.1  christos 	item = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpdurl_t));
   1218  1.1  christos 	if (item == NULL)
   1219  1.1  christos 		return (ISC_R_NOMEMORY);
   1220  1.1  christos 
   1221  1.1  christos 	item->url = isc_mem_strdup(httpdmgr->mctx, url);
   1222  1.1  christos 	if (item->url == NULL) {
   1223  1.1  christos 		isc_mem_put(httpdmgr->mctx, item, sizeof(isc_httpdurl_t));
   1224  1.1  christos 		return (ISC_R_NOMEMORY);
   1225  1.1  christos 	}
   1226  1.1  christos 
   1227  1.1  christos 	item->action = func;
   1228  1.1  christos 	item->action_arg = arg;
   1229  1.1  christos 	item->isstatic = isstatic;
   1230  1.1  christos 	isc_time_now(&item->loadtime);
   1231  1.1  christos 
   1232  1.1  christos 	ISC_LINK_INIT(item, link);
   1233  1.1  christos 	ISC_LIST_APPEND(httpdmgr->urls, item, link);
   1234  1.1  christos 
   1235  1.1  christos 	return (ISC_R_SUCCESS);
   1236  1.1  christos }
   1237  1.1  christos 
   1238  1.1  christos void
   1239  1.1  christos isc_httpd_setfinishhook(void (*fn)(void))
   1240  1.1  christos {
   1241  1.1  christos 	finishhook = fn;
   1242  1.1  christos }
   1243