Home | History | Annotate | Line # | Download | only in isc
httpd.c revision 1.1.1.9
      1      1.1  christos /*	$NetBSD: httpd.c,v 1.1.1.9 2025/01/26 16:12:30 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.1.6  christos  * SPDX-License-Identifier: MPL-2.0
      7  1.1.1.6  christos  *
      8      1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      9      1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  1.1.1.5  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11      1.1  christos  *
     12      1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     13      1.1  christos  * information regarding copyright ownership.
     14      1.1  christos  */
     15      1.1  christos 
     16      1.1  christos /*! \file */
     17      1.1  christos 
     18  1.1.1.8  christos #include <ctype.h>
     19  1.1.1.2  christos #include <inttypes.h>
     20  1.1.1.2  christos #include <stdbool.h>
     21  1.1.1.2  christos #include <string.h>
     22  1.1.1.2  christos 
     23      1.1  christos #include <isc/buffer.h>
     24      1.1  christos #include <isc/httpd.h>
     25  1.1.1.9  christos #include <isc/list.h>
     26      1.1  christos #include <isc/mem.h>
     27  1.1.1.8  christos #include <isc/netmgr.h>
     28  1.1.1.3  christos #include <isc/refcount.h>
     29  1.1.1.8  christos #include <isc/sockaddr.h>
     30      1.1  christos #include <isc/string.h>
     31      1.1  christos #include <isc/time.h>
     32  1.1.1.8  christos #include <isc/url.h>
     33      1.1  christos #include <isc/util.h>
     34      1.1  christos 
     35  1.1.1.8  christos #include "netmgr/netmgr-int.h"
     36  1.1.1.8  christos #include "picohttpparser.h"
     37  1.1.1.8  christos 
     38      1.1  christos #ifdef HAVE_ZLIB
     39      1.1  christos #include <zlib.h>
     40  1.1.1.3  christos #endif /* ifdef HAVE_ZLIB */
     41      1.1  christos 
     42  1.1.1.8  christos #define CHECK(m)                               \
     43  1.1.1.8  christos 	do {                                   \
     44  1.1.1.8  christos 		result = (m);                  \
     45  1.1.1.8  christos 		if (result != ISC_R_SUCCESS) { \
     46  1.1.1.8  christos 			goto cleanup;          \
     47  1.1.1.8  christos 		}                              \
     48  1.1.1.8  christos 	} while (0)
     49      1.1  christos 
     50  1.1.1.8  christos /*
     51  1.1.1.8  christos  * Size the recv buffer to hold at maximum two full buffers from isc_nm_read(),
     52  1.1.1.8  christos  * so we don't have to handle the truncation.
     53  1.1.1.8  christos  */
     54  1.1.1.8  christos #define HTTP_RECVLEN	     ISC_NETMGR_TCP_RECVBUF_SIZE * 2
     55  1.1.1.8  christos #define HTTP_SENDLEN	     ISC_NETMGR_TCP_RECVBUF_SIZE
     56  1.1.1.8  christos #define HTTP_HEADERS_NUM     100
     57  1.1.1.8  christos #define HTTP_MAX_REQUEST_LEN 4096
     58  1.1.1.8  christos 
     59  1.1.1.8  christos typedef enum httpd_flags {
     60  1.1.1.8  christos 	CONNECTION_CLOSE = 1 << 0,	/* connection must close */
     61  1.1.1.8  christos 	CONNECTION_KEEP_ALIVE = 1 << 1, /* response needs a keep-alive header */
     62  1.1.1.8  christos 	ACCEPT_DEFLATE = 1 << 2,	/* response can be compressed */
     63  1.1.1.8  christos } httpd_flags_t;
     64  1.1.1.3  christos 
     65  1.1.1.3  christos #define HTTPD_MAGIC    ISC_MAGIC('H', 't', 'p', 'd')
     66  1.1.1.3  christos #define VALID_HTTPD(m) ISC_MAGIC_VALID(m, HTTPD_MAGIC)
     67  1.1.1.3  christos 
     68  1.1.1.3  christos #define HTTPDMGR_MAGIC	  ISC_MAGIC('H', 'p', 'd', 'm')
     69  1.1.1.3  christos #define VALID_HTTPDMGR(m) ISC_MAGIC_VALID(m, HTTPDMGR_MAGIC)
     70      1.1  christos 
     71  1.1.1.8  christos /*%
     72  1.1.1.8  christos  * HTTP methods.
     73  1.1.1.8  christos  */
     74  1.1.1.8  christos typedef enum { METHOD_UNKNOWN = 0, METHOD_GET = 1, METHOD_POST = 2 } method_t;
     75  1.1.1.8  christos 
     76  1.1.1.8  christos /*%
     77  1.1.1.8  christos  * HTTP urls.  These are the URLs we manage, and the function to call to
     78  1.1.1.8  christos  * provide the data for it.  We pass in the base url (so the same function
     79  1.1.1.8  christos  * can handle multiple requests), and a structure to fill in to return a
     80  1.1.1.8  christos  * result to the client.  We also pass in a pointer to be filled in for
     81  1.1.1.8  christos  * the data cleanup function.
     82  1.1.1.8  christos  */
     83  1.1.1.8  christos struct isc_httpdurl {
     84  1.1.1.8  christos 	char *url;
     85  1.1.1.8  christos 	isc_httpdaction_t *action;
     86  1.1.1.8  christos 	void *action_arg;
     87  1.1.1.8  christos 	bool isstatic;
     88  1.1.1.8  christos 	isc_time_t loadtime;
     89  1.1.1.8  christos 	ISC_LINK(isc_httpdurl_t) link;
     90  1.1.1.8  christos };
     91  1.1.1.8  christos 
     92      1.1  christos /*% http client */
     93      1.1  christos struct isc_httpd {
     94  1.1.1.3  christos 	unsigned int magic; /* HTTPD_MAGIC */
     95  1.1.1.9  christos 	isc_refcount_t references;
     96  1.1.1.8  christos 
     97  1.1.1.3  christos 	isc_httpdmgr_t *mgr; /*%< our parent */
     98  1.1.1.3  christos 	ISC_LINK(isc_httpd_t) link;
     99  1.1.1.8  christos 
    100  1.1.1.9  christos 	isc_nmhandle_t *handle; /* Permanent pointer to handle */
    101      1.1  christos 
    102      1.1  christos 	/*%
    103      1.1  christos 	 * Received data state.
    104      1.1  christos 	 */
    105  1.1.1.3  christos 	char recvbuf[HTTP_RECVLEN]; /*%< receive buffer */
    106  1.1.1.8  christos 	size_t recvlen;		    /*%< length recv'd */
    107  1.1.1.8  christos 	size_t consume;		    /*%< length of last command */
    108      1.1  christos 
    109  1.1.1.8  christos 	method_t method;
    110  1.1.1.8  christos 	int minor_version;
    111  1.1.1.8  christos 	httpd_flags_t flags;
    112  1.1.1.8  christos 	const char *path;
    113  1.1.1.8  christos 	isc_url_parser_t up;
    114  1.1.1.8  christos 	isc_time_t if_modified_since;
    115      1.1  christos };
    116      1.1  christos 
    117  1.1.1.9  christos #if ISC_HTTPD_TRACE
    118  1.1.1.9  christos #define isc_httpd_ref(ptr)   isc_httpd__ref(ptr, __func__, __FILE__, __LINE__)
    119  1.1.1.9  christos #define isc_httpd_unref(ptr) isc_httpd__unref(ptr, __func__, __FILE__, __LINE__)
    120  1.1.1.9  christos #define isc_httpd_attach(ptr, ptrp) \
    121  1.1.1.9  christos 	isc_httpd__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
    122  1.1.1.9  christos #define isc_httpd_detach(ptrp) \
    123  1.1.1.9  christos 	isc_httpd__detach(ptrp, __func__, __FILE__, __LINE__)
    124  1.1.1.9  christos ISC_REFCOUNT_TRACE_DECL(isc_httpd);
    125  1.1.1.9  christos #else
    126  1.1.1.9  christos ISC_REFCOUNT_DECL(isc_httpd);
    127  1.1.1.9  christos #endif
    128  1.1.1.9  christos 
    129      1.1  christos struct isc_httpdmgr {
    130  1.1.1.3  christos 	unsigned int magic; /* HTTPDMGR_MAGIC */
    131  1.1.1.3  christos 	isc_refcount_t references;
    132  1.1.1.3  christos 	isc_mem_t *mctx;
    133  1.1.1.8  christos 	isc_nmsocket_t *sock;
    134  1.1.1.3  christos 
    135  1.1.1.3  christos 	isc_httpdclientok_t *client_ok;	 /*%< client validator */
    136  1.1.1.3  christos 	isc_httpdondestroy_t *ondestroy; /*%< cleanup callback */
    137  1.1.1.3  christos 	void *cb_arg;			 /*%< argument for the above */
    138  1.1.1.3  christos 
    139  1.1.1.3  christos 	unsigned int flags;
    140  1.1.1.3  christos 	ISC_LIST(isc_httpd_t) running; /*%< running clients */
    141  1.1.1.3  christos 
    142  1.1.1.3  christos 	isc_mutex_t lock;
    143  1.1.1.3  christos 
    144  1.1.1.3  christos 	ISC_LIST(isc_httpdurl_t) urls; /*%< urls we manage */
    145  1.1.1.3  christos 	isc_httpdaction_t *render_404;
    146  1.1.1.3  christos 	isc_httpdaction_t *render_500;
    147      1.1  christos };
    148      1.1  christos 
    149  1.1.1.9  christos #if ISC_HTTPD_TRACE
    150  1.1.1.9  christos #define isc_httpdmgr_ref(ptr) \
    151  1.1.1.9  christos 	isc_httpdmgr__ref(ptr, __func__, __FILE__, __LINE__)
    152  1.1.1.9  christos #define isc_httpdmgr_unref(ptr) \
    153  1.1.1.9  christos 	isc_httpdmgr__unref(ptr, __func__, __FILE__, __LINE__)
    154  1.1.1.9  christos #define isc_httpdmgr_attach(ptr, ptrp) \
    155  1.1.1.9  christos 	isc_httpdmgr__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
    156  1.1.1.9  christos #define isc_httpdmgr_detach(ptrp) \
    157  1.1.1.9  christos 	isc_httpdmgr__detach(ptrp, __func__, __FILE__, __LINE__)
    158  1.1.1.9  christos ISC_REFCOUNT_TRACE_DECL(isc_httpdmgr);
    159  1.1.1.9  christos #else
    160  1.1.1.9  christos ISC_REFCOUNT_DECL(isc_httpdmgr);
    161  1.1.1.9  christos #endif
    162  1.1.1.9  christos 
    163  1.1.1.8  christos typedef struct isc_httpd_sendreq {
    164  1.1.1.8  christos 	isc_mem_t *mctx;
    165  1.1.1.8  christos 	isc_httpd_t *httpd;
    166      1.1  christos 
    167  1.1.1.8  christos 	/*%
    168  1.1.1.8  christos 	 * Transmit data state.
    169  1.1.1.8  christos 	 *
    170  1.1.1.8  christos 	 * This is the data buffer we will transmit.
    171  1.1.1.8  christos 	 *
    172  1.1.1.8  christos 	 * This free function pointer is filled in by the rendering function
    173  1.1.1.8  christos 	 * we call.  The free function is called after the data is transmitted
    174  1.1.1.8  christos 	 * to the client.
    175  1.1.1.8  christos 	 *
    176  1.1.1.8  christos 	 * We currently use three buffers total:
    177  1.1.1.8  christos 	 *
    178  1.1.1.8  christos 	 * sendbuffer - gets filled as we gather the data
    179  1.1.1.8  christos 	 *
    180  1.1.1.8  christos 	 * bodybuffer - for the client to fill in (which it manages, it provides
    181  1.1.1.8  christos 	 * the space for it, etc) -- we will pass that buffer structure back to
    182  1.1.1.8  christos 	 * the caller, who is responsible for managing the space it may have
    183  1.1.1.8  christos 	 * allocated as backing store for it.  we only allocate the buffer
    184  1.1.1.8  christos 	 * itself, not the backing store.
    185  1.1.1.8  christos 	 *
    186  1.1.1.8  christos 	 * compbuffer - managed by us, that contains the compressed HTTP data,
    187  1.1.1.8  christos 	 * if compression is used.
    188  1.1.1.8  christos 	 */
    189  1.1.1.8  christos 	isc_buffer_t *sendbuffer;
    190  1.1.1.8  christos 	isc_buffer_t *compbuffer;
    191  1.1.1.3  christos 
    192  1.1.1.8  christos 	isc_buffer_t bodybuffer;
    193      1.1  christos 
    194  1.1.1.8  christos 	const char *mimetype;
    195  1.1.1.8  christos 	unsigned int retcode;
    196  1.1.1.8  christos 	const char *retmsg;
    197  1.1.1.8  christos 	isc_httpdfree_t *freecb;
    198  1.1.1.8  christos 	void *freecb_arg;
    199      1.1  christos 
    200  1.1.1.8  christos } isc_httpd_sendreq_t;
    201      1.1  christos 
    202  1.1.1.8  christos static isc_result_t
    203  1.1.1.8  christos httpd_newconn(isc_nmhandle_t *, isc_result_t, void *);
    204      1.1  christos static void
    205  1.1.1.8  christos httpd_request(isc_nmhandle_t *, isc_result_t, isc_region_t *, void *);
    206  1.1.1.3  christos static void
    207  1.1.1.8  christos httpd_senddone(isc_nmhandle_t *, isc_result_t, void *);
    208  1.1.1.3  christos static void
    209  1.1.1.9  christos httpd_free(isc_httpd_t *httpd);
    210      1.1  christos 
    211  1.1.1.3  christos static void
    212  1.1.1.8  christos httpd_addheader(isc_httpd_sendreq_t *, const char *, const char *);
    213  1.1.1.3  christos static void
    214  1.1.1.8  christos httpd_addheaderuint(isc_httpd_sendreq_t *, const char *, int);
    215  1.1.1.6  christos static void
    216  1.1.1.8  christos httpd_endheaders(isc_httpd_sendreq_t *);
    217  1.1.1.3  christos static void
    218  1.1.1.8  christos httpd_response(isc_httpd_t *, isc_httpd_sendreq_t *);
    219      1.1  christos 
    220  1.1.1.8  christos static isc_result_t
    221  1.1.1.8  christos process_request(isc_httpd_t *, size_t);
    222  1.1.1.3  christos 
    223  1.1.1.8  christos static isc_httpdaction_t render_404;
    224  1.1.1.8  christos static isc_httpdaction_t render_500;
    225  1.1.1.3  christos 
    226  1.1.1.3  christos #if ENABLE_AFL
    227  1.1.1.8  christos static void (*finishhook)(void) = NULL;
    228  1.1.1.3  christos #endif /* ENABLE_AFL */
    229  1.1.1.3  christos 
    230      1.1  christos isc_result_t
    231  1.1.1.8  christos isc_httpdmgr_create(isc_nm_t *nm, isc_mem_t *mctx, isc_sockaddr_t *addr,
    232      1.1  christos 		    isc_httpdclientok_t *client_ok,
    233      1.1  christos 		    isc_httpdondestroy_t *ondestroy, void *cb_arg,
    234  1.1.1.8  christos 		    isc_httpdmgr_t **httpdmgrp) {
    235      1.1  christos 	isc_result_t result;
    236  1.1.1.8  christos 	isc_httpdmgr_t *httpdmgr = NULL;
    237      1.1  christos 
    238  1.1.1.8  christos 	REQUIRE(nm != NULL);
    239      1.1  christos 	REQUIRE(mctx != NULL);
    240      1.1  christos 	REQUIRE(httpdmgrp != NULL && *httpdmgrp == NULL);
    241      1.1  christos 
    242      1.1  christos 	httpdmgr = isc_mem_get(mctx, sizeof(isc_httpdmgr_t));
    243  1.1.1.8  christos 	*httpdmgr = (isc_httpdmgr_t){ .client_ok = client_ok,
    244  1.1.1.3  christos 				      .ondestroy = ondestroy,
    245  1.1.1.3  christos 				      .cb_arg = cb_arg,
    246  1.1.1.3  christos 				      .render_404 = render_404,
    247  1.1.1.3  christos 				      .render_500 = render_500 };
    248      1.1  christos 
    249  1.1.1.2  christos 	isc_mutex_init(&httpdmgr->lock);
    250      1.1  christos 	isc_mem_attach(mctx, &httpdmgr->mctx);
    251      1.1  christos 
    252      1.1  christos 	ISC_LIST_INIT(httpdmgr->running);
    253      1.1  christos 	ISC_LIST_INIT(httpdmgr->urls);
    254      1.1  christos 
    255  1.1.1.3  christos 	isc_refcount_init(&httpdmgr->references, 1);
    256  1.1.1.3  christos 
    257  1.1.1.9  christos 	CHECK(isc_nm_listentcp(nm, ISC_NM_LISTEN_ONE, addr, httpd_newconn,
    258  1.1.1.9  christos 			       httpdmgr, 5, NULL, &httpdmgr->sock));
    259      1.1  christos 
    260  1.1.1.3  christos 	httpdmgr->magic = HTTPDMGR_MAGIC;
    261      1.1  christos 	*httpdmgrp = httpdmgr;
    262  1.1.1.8  christos 
    263  1.1.1.9  christos 	return ISC_R_SUCCESS;
    264      1.1  christos 
    265  1.1.1.3  christos cleanup:
    266  1.1.1.3  christos 	httpdmgr->magic = 0;
    267  1.1.1.5  christos 	isc_refcount_decrementz(&httpdmgr->references);
    268  1.1.1.3  christos 	isc_refcount_destroy(&httpdmgr->references);
    269      1.1  christos 	isc_mem_detach(&httpdmgr->mctx);
    270  1.1.1.2  christos 	isc_mutex_destroy(&httpdmgr->lock);
    271      1.1  christos 	isc_mem_put(mctx, httpdmgr, sizeof(isc_httpdmgr_t));
    272  1.1.1.8  christos 
    273  1.1.1.9  christos 	return result;
    274  1.1.1.3  christos }
    275  1.1.1.3  christos 
    276  1.1.1.3  christos static void
    277  1.1.1.3  christos destroy_httpdmgr(isc_httpdmgr_t *httpdmgr) {
    278  1.1.1.3  christos 	isc_refcount_destroy(&httpdmgr->references);
    279  1.1.1.3  christos 
    280  1.1.1.3  christos 	LOCK(&httpdmgr->lock);
    281  1.1.1.3  christos 
    282  1.1.1.8  christos 	REQUIRE((httpdmgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) != 0);
    283  1.1.1.8  christos 	REQUIRE(ISC_LIST_EMPTY(httpdmgr->running));
    284  1.1.1.3  christos 
    285  1.1.1.8  christos 	httpdmgr->magic = 0;
    286      1.1  christos 
    287  1.1.1.8  christos 	if (httpdmgr->sock != NULL) {
    288  1.1.1.8  christos 		isc_nmsocket_close(&httpdmgr->sock);
    289  1.1.1.8  christos 	}
    290      1.1  christos 
    291      1.1  christos 	/*
    292      1.1  christos 	 * Clear out the list of all actions we know about.  Just free the
    293      1.1  christos 	 * memory.
    294      1.1  christos 	 */
    295  1.1.1.9  christos 	isc_httpdurl_t *url, *next;
    296  1.1.1.9  christos 	ISC_LIST_FOREACH_SAFE (httpdmgr->urls, url, link, next) {
    297      1.1  christos 		isc_mem_free(httpdmgr->mctx, url->url);
    298      1.1  christos 		ISC_LIST_UNLINK(httpdmgr->urls, url, link);
    299      1.1  christos 		isc_mem_put(httpdmgr->mctx, url, sizeof(isc_httpdurl_t));
    300      1.1  christos 	}
    301      1.1  christos 
    302      1.1  christos 	UNLOCK(&httpdmgr->lock);
    303  1.1.1.2  christos 	isc_mutex_destroy(&httpdmgr->lock);
    304      1.1  christos 
    305  1.1.1.3  christos 	if (httpdmgr->ondestroy != NULL) {
    306      1.1  christos 		(httpdmgr->ondestroy)(httpdmgr->cb_arg);
    307  1.1.1.3  christos 	}
    308  1.1.1.3  christos 	isc_mem_putanddetach(&httpdmgr->mctx, httpdmgr, sizeof(isc_httpdmgr_t));
    309      1.1  christos }
    310      1.1  christos 
    311  1.1.1.9  christos #if ISC_HTTPD_TRACE
    312  1.1.1.9  christos ISC_REFCOUNT_TRACE_IMPL(isc_httpdmgr, destroy_httpdmgr)
    313  1.1.1.9  christos #else
    314  1.1.1.9  christos ISC_REFCOUNT_IMPL(isc_httpdmgr, destroy_httpdmgr);
    315  1.1.1.9  christos #endif
    316  1.1.1.9  christos 
    317  1.1.1.8  christos static bool
    318  1.1.1.8  christos name_match(const struct phr_header *header, const char *match) {
    319  1.1.1.8  christos 	size_t match_len = strlen(match);
    320  1.1.1.8  christos 	if (match_len != header->name_len) {
    321  1.1.1.9  christos 		return false;
    322  1.1.1.8  christos 	}
    323  1.1.1.9  christos 	return strncasecmp(header->name, match, match_len) == 0;
    324  1.1.1.8  christos }
    325      1.1  christos 
    326  1.1.1.2  christos static bool
    327  1.1.1.8  christos value_match(const struct phr_header *header, const char *match) {
    328  1.1.1.8  christos 	size_t match_len = strlen(match);
    329  1.1.1.8  christos 	size_t limit;
    330  1.1.1.8  christos 
    331  1.1.1.8  christos 	if (match_len > header->value_len) {
    332  1.1.1.9  christos 		return false;
    333      1.1  christos 	}
    334      1.1  christos 
    335  1.1.1.8  christos 	limit = header->value_len - match_len + 1;
    336      1.1  christos 
    337  1.1.1.8  christos 	for (size_t i = 0; i < limit; i++) {
    338  1.1.1.8  christos 		if (isspace((unsigned char)header->value[i])) {
    339  1.1.1.8  christos 			while (i < limit &&
    340  1.1.1.8  christos 			       isspace((unsigned char)header->value[i]))
    341  1.1.1.8  christos 			{
    342  1.1.1.8  christos 				i++;
    343  1.1.1.3  christos 			}
    344      1.1  christos 			continue;
    345      1.1  christos 		}
    346      1.1  christos 
    347  1.1.1.8  christos 		if (strncasecmp(&header->value[i], match, match_len) == 0) {
    348  1.1.1.8  christos 			i += match_len;
    349      1.1  christos 			/*
    350  1.1.1.8  christos 			 * Sanity check; f.e. for 'deflate' match only
    351  1.1.1.8  christos 			 * 'deflate[,;]', but not 'deflateyou'
    352      1.1  christos 			 */
    353  1.1.1.8  christos 			if (i == header->value_len || header->value[i] == ',' ||
    354  1.1.1.8  christos 			    header->value[i] == ';')
    355  1.1.1.8  christos 			{
    356  1.1.1.9  christos 				return true;
    357  1.1.1.3  christos 			}
    358      1.1  christos 		}
    359  1.1.1.8  christos 
    360  1.1.1.8  christos 		while (i < limit && header->value[i] != ',') {
    361  1.1.1.8  christos 			i++;
    362  1.1.1.8  christos 		}
    363      1.1  christos 	}
    364  1.1.1.9  christos 	return false;
    365      1.1  christos }
    366      1.1  christos 
    367      1.1  christos static isc_result_t
    368  1.1.1.8  christos process_request(isc_httpd_t *httpd, size_t last_len) {
    369  1.1.1.8  christos 	int pret;
    370  1.1.1.8  christos 	const char *method = NULL;
    371  1.1.1.8  christos 	size_t method_len = 0;
    372  1.1.1.8  christos 	const char *path;
    373  1.1.1.8  christos 	size_t path_len = 0;
    374  1.1.1.8  christos 	struct phr_header headers[HTTP_HEADERS_NUM];
    375  1.1.1.8  christos 	size_t num_headers;
    376  1.1.1.8  christos 	isc_result_t result;
    377      1.1  christos 
    378  1.1.1.8  christos 	num_headers = ARRAY_SIZE(headers);
    379      1.1  christos 
    380  1.1.1.8  christos 	pret = phr_parse_request(httpd->recvbuf, httpd->recvlen, &method,
    381  1.1.1.8  christos 				 &method_len, &path, &path_len,
    382  1.1.1.8  christos 				 &httpd->minor_version, headers, &num_headers,
    383  1.1.1.8  christos 				 last_len);
    384      1.1  christos 
    385  1.1.1.8  christos 	if (pret == -1) {
    386  1.1.1.8  christos 		/* Parse Error */
    387  1.1.1.9  christos 		return ISC_R_UNEXPECTED;
    388  1.1.1.8  christos 	} else if (pret == -2) {
    389  1.1.1.8  christos 		/* Need more data */
    390  1.1.1.9  christos 		return ISC_R_NOMORE;
    391      1.1  christos 	}
    392  1.1.1.8  christos 
    393  1.1.1.8  christos 	INSIST(pret > 0);
    394  1.1.1.8  christos 
    395  1.1.1.8  christos 	if (pret > HTTP_MAX_REQUEST_LEN) {
    396  1.1.1.9  christos 		return ISC_R_RANGE;
    397  1.1.1.3  christos 	}
    398      1.1  christos 
    399  1.1.1.8  christos 	httpd->consume = pret;
    400      1.1  christos 
    401      1.1  christos 	/*
    402      1.1  christos 	 * Determine if this is a POST or GET method.  Any other values will
    403      1.1  christos 	 * cause an error to be returned.
    404      1.1  christos 	 */
    405  1.1.1.8  christos 	if (strncmp(method, "GET ", method_len) == 0) {
    406  1.1.1.8  christos 		httpd->method = METHOD_GET;
    407  1.1.1.8  christos 	} else if (strncmp(method, "POST ", method_len) == 0) {
    408  1.1.1.8  christos 		httpd->method = METHOD_POST;
    409      1.1  christos 	} else {
    410  1.1.1.9  christos 		return ISC_R_RANGE;
    411      1.1  christos 	}
    412      1.1  christos 
    413      1.1  christos 	/*
    414  1.1.1.8  christos 	 * Parse the URL
    415      1.1  christos 	 */
    416  1.1.1.8  christos 	result = isc_url_parse(path, path_len, 0, &httpd->up);
    417  1.1.1.8  christos 	if (result != ISC_R_SUCCESS) {
    418  1.1.1.9  christos 		return result;
    419  1.1.1.3  christos 	}
    420  1.1.1.8  christos 	httpd->path = path;
    421      1.1  christos 
    422      1.1  christos 	/*
    423  1.1.1.8  christos 	 * Examine headers that can affect this request's response
    424      1.1  christos 	 */
    425  1.1.1.8  christos 	httpd->flags = 0;
    426      1.1  christos 
    427  1.1.1.8  christos 	size_t content_len = 0;
    428  1.1.1.8  christos 	bool keep_alive = false;
    429  1.1.1.8  christos 	bool host_header = false;
    430      1.1  christos 
    431  1.1.1.8  christos 	isc_time_set(&httpd->if_modified_since, 0, 0);
    432      1.1  christos 
    433  1.1.1.8  christos 	for (size_t i = 0; i < num_headers; i++) {
    434  1.1.1.8  christos 		struct phr_header *header = &headers[i];
    435  1.1.1.8  christos 
    436  1.1.1.8  christos 		if (name_match(header, "Content-Length")) {
    437  1.1.1.8  christos 			char *endptr;
    438  1.1.1.8  christos 			long val = strtol(header->value, &endptr, 10);
    439  1.1.1.8  christos 
    440  1.1.1.8  christos 			errno = 0;
    441  1.1.1.8  christos 
    442  1.1.1.8  christos 			/* ensure we consumed all digits */
    443  1.1.1.8  christos 			if ((header->value + header->value_len) != endptr) {
    444  1.1.1.9  christos 				return ISC_R_BADNUMBER;
    445  1.1.1.8  christos 			}
    446  1.1.1.8  christos 			/* ensure there was no minus sign */
    447  1.1.1.8  christos 			if (val < 0) {
    448  1.1.1.9  christos 				return ISC_R_BADNUMBER;
    449  1.1.1.8  christos 			}
    450  1.1.1.8  christos 			/* ensure it did not overflow */
    451  1.1.1.8  christos 			if (errno != 0) {
    452  1.1.1.9  christos 				return ISC_R_RANGE;
    453  1.1.1.8  christos 			}
    454  1.1.1.8  christos 			content_len = val;
    455  1.1.1.8  christos 		} else if (name_match(header, "Connection")) {
    456  1.1.1.8  christos 			/* multiple fields in a connection header are allowed */
    457  1.1.1.8  christos 			if (value_match(header, "close")) {
    458  1.1.1.8  christos 				httpd->flags |= CONNECTION_CLOSE;
    459  1.1.1.8  christos 			}
    460  1.1.1.8  christos 			if (value_match(header, "keep-alive")) {
    461  1.1.1.8  christos 				keep_alive = true;
    462  1.1.1.8  christos 			}
    463  1.1.1.8  christos 		} else if (name_match(header, "Host")) {
    464  1.1.1.8  christos 			host_header = true;
    465  1.1.1.8  christos 		} else if (name_match(header, "Accept-Encoding")) {
    466  1.1.1.8  christos 			if (value_match(header, "deflate")) {
    467  1.1.1.8  christos 				httpd->flags |= ACCEPT_DEFLATE;
    468  1.1.1.8  christos 			}
    469  1.1.1.8  christos 		} else if (name_match(header, "If-Modified-Since") &&
    470  1.1.1.8  christos 			   header->value_len < ISC_FORMATHTTPTIMESTAMP_SIZE)
    471  1.1.1.8  christos 		{
    472  1.1.1.8  christos 			char timestamp[ISC_FORMATHTTPTIMESTAMP_SIZE + 1];
    473  1.1.1.8  christos 			memmove(timestamp, header->value, header->value_len);
    474  1.1.1.8  christos 			timestamp[header->value_len] = 0;
    475  1.1.1.8  christos 
    476  1.1.1.8  christos 			/* Ignore the value if it can't be parsed */
    477  1.1.1.8  christos 			(void)isc_time_parsehttptimestamp(
    478  1.1.1.8  christos 				timestamp, &httpd->if_modified_since);
    479  1.1.1.8  christos 		}
    480  1.1.1.3  christos 	}
    481  1.1.1.8  christos 
    482      1.1  christos 	/*
    483  1.1.1.8  christos 	 * The Content-Length is optional in an HTTP request.
    484  1.1.1.8  christos 	 * For a GET the length must be zero.
    485      1.1  christos 	 */
    486  1.1.1.8  christos 	if (httpd->method == METHOD_GET && content_len != 0) {
    487  1.1.1.9  christos 		return ISC_R_BADNUMBER;
    488  1.1.1.3  christos 	}
    489  1.1.1.8  christos 
    490  1.1.1.8  christos 	if (content_len >= HTTP_MAX_REQUEST_LEN) {
    491  1.1.1.9  christos 		return ISC_R_RANGE;
    492  1.1.1.3  christos 	}
    493      1.1  christos 
    494  1.1.1.8  christos 	size_t consume = httpd->consume + content_len;
    495  1.1.1.8  christos 	if (consume > httpd->recvlen) {
    496  1.1.1.8  christos 		/* The request data isn't complete yet. */
    497  1.1.1.9  christos 		return ISC_R_NOMORE;
    498  1.1.1.3  christos 	}
    499      1.1  christos 
    500  1.1.1.8  christos 	/* Consume the request's data, which we do not use. */
    501  1.1.1.8  christos 	httpd->consume = consume;
    502      1.1  christos 
    503  1.1.1.8  christos 	switch (httpd->minor_version) {
    504  1.1.1.8  christos 	case 0:
    505  1.1.1.8  christos 		/*
    506  1.1.1.8  christos 		 * RFC 9112 section 9.3 says close takes priority if
    507  1.1.1.8  christos 		 * keep-alive is also present
    508  1.1.1.8  christos 		 */
    509  1.1.1.8  christos 		if ((httpd->flags & CONNECTION_CLOSE) == 0 && keep_alive) {
    510  1.1.1.8  christos 			httpd->flags |= CONNECTION_KEEP_ALIVE;
    511  1.1.1.3  christos 		} else {
    512  1.1.1.8  christos 			httpd->flags |= CONNECTION_CLOSE;
    513  1.1.1.3  christos 		}
    514  1.1.1.8  christos 		break;
    515  1.1.1.8  christos 	case 1:
    516  1.1.1.8  christos 		if (!host_header) {
    517  1.1.1.9  christos 			return ISC_R_RANGE;
    518  1.1.1.8  christos 		}
    519  1.1.1.8  christos 		break;
    520  1.1.1.8  christos 	default:
    521  1.1.1.9  christos 		return ISC_R_UNEXPECTED;
    522      1.1  christos 	}
    523      1.1  christos 
    524      1.1  christos 	/*
    525  1.1.1.8  christos 	 * Looks like a a valid request, so now we know we won't have
    526  1.1.1.8  christos 	 * to process this buffer again. We can NULL-terminate the
    527  1.1.1.8  christos 	 * URL for the caller's benefit, and set recvlen to 0 so
    528  1.1.1.8  christos 	 * the next read will overwrite this one instead of appending
    529  1.1.1.8  christos 	 * to the buffer.
    530      1.1  christos 	 */
    531      1.1  christos 
    532  1.1.1.9  christos 	return ISC_R_SUCCESS;
    533      1.1  christos }
    534      1.1  christos 
    535      1.1  christos static void
    536  1.1.1.9  christos httpd_free(isc_httpd_t *httpd) {
    537  1.1.1.8  christos 	isc_httpdmgr_t *httpdmgr = NULL;
    538  1.1.1.8  christos 
    539  1.1.1.8  christos 	REQUIRE(VALID_HTTPD(httpd));
    540  1.1.1.8  christos 
    541  1.1.1.8  christos 	httpdmgr = httpd->mgr;
    542  1.1.1.3  christos 
    543  1.1.1.3  christos 	REQUIRE(VALID_HTTPDMGR(httpdmgr));
    544  1.1.1.3  christos 
    545  1.1.1.8  christos 	LOCK(&httpdmgr->lock);
    546  1.1.1.8  christos 	ISC_LIST_UNLINK(httpdmgr->running, httpd, link);
    547  1.1.1.8  christos 	UNLOCK(&httpdmgr->lock);
    548  1.1.1.8  christos 
    549  1.1.1.8  christos 	httpd->recvbuf[0] = 0;
    550  1.1.1.8  christos 	httpd->recvlen = 0;
    551  1.1.1.8  christos 	httpd->consume = 0;
    552  1.1.1.8  christos 	httpd->method = METHOD_UNKNOWN;
    553  1.1.1.8  christos 	httpd->flags = 0;
    554  1.1.1.8  christos 
    555  1.1.1.8  christos 	httpd->minor_version = -1;
    556  1.1.1.8  christos 	httpd->path = NULL;
    557  1.1.1.8  christos 	httpd->up = (isc_url_parser_t){ 0 };
    558  1.1.1.8  christos 	isc_time_set(&httpd->if_modified_since, 0, 0);
    559  1.1.1.9  christos 
    560  1.1.1.9  christos 	httpd->magic = 0;
    561  1.1.1.9  christos 	httpd->mgr = NULL;
    562  1.1.1.9  christos 
    563  1.1.1.9  christos 	isc_mem_put(httpdmgr->mctx, httpd, sizeof(*httpd));
    564  1.1.1.9  christos 
    565  1.1.1.9  christos 	isc_httpdmgr_detach(&httpdmgr);
    566  1.1.1.9  christos 
    567  1.1.1.9  christos #if ENABLE_AFL
    568  1.1.1.9  christos 	if (finishhook != NULL) {
    569  1.1.1.9  christos 		finishhook();
    570  1.1.1.9  christos 	}
    571  1.1.1.9  christos #endif /* ENABLE_AFL */
    572  1.1.1.8  christos }
    573  1.1.1.8  christos 
    574  1.1.1.9  christos #if ISC_HTTPD_TRACE
    575  1.1.1.9  christos ISC_REFCOUNT_TRACE_IMPL(isc_httpd, httpd_free)
    576  1.1.1.9  christos #else
    577  1.1.1.9  christos ISC_REFCOUNT_IMPL(isc_httpd, httpd_free);
    578  1.1.1.9  christos #endif
    579  1.1.1.9  christos 
    580  1.1.1.8  christos static void
    581  1.1.1.8  christos isc__httpd_sendreq_free(isc_httpd_sendreq_t *req) {
    582  1.1.1.8  christos 	/* Clean up buffers */
    583  1.1.1.8  christos 
    584  1.1.1.8  christos 	isc_buffer_free(&req->sendbuffer);
    585  1.1.1.8  christos 
    586  1.1.1.8  christos 	isc_mem_putanddetach(&req->mctx, req, sizeof(*req));
    587  1.1.1.8  christos }
    588  1.1.1.8  christos 
    589  1.1.1.8  christos static isc_httpd_sendreq_t *
    590  1.1.1.8  christos isc__httpd_sendreq_new(isc_httpd_t *httpd) {
    591  1.1.1.8  christos 	isc_httpdmgr_t *httpdmgr = httpd->mgr;
    592  1.1.1.8  christos 	isc_httpd_sendreq_t *req;
    593  1.1.1.3  christos 
    594  1.1.1.8  christos 	REQUIRE(VALID_HTTPDMGR(httpdmgr));
    595  1.1.1.3  christos 
    596  1.1.1.8  christos 	req = isc_mem_get(httpdmgr->mctx, sizeof(*req));
    597  1.1.1.8  christos 	*req = (isc_httpd_sendreq_t){ 0 };
    598  1.1.1.3  christos 
    599  1.1.1.8  christos 	isc_mem_attach(httpdmgr->mctx, &req->mctx);
    600  1.1.1.3  christos 
    601  1.1.1.3  christos 	/*
    602  1.1.1.3  christos 	 * Initialize the buffer for our headers.
    603  1.1.1.3  christos 	 */
    604  1.1.1.8  christos 	isc_buffer_allocate(req->mctx, &req->sendbuffer, HTTP_SENDLEN);
    605  1.1.1.8  christos 	isc_buffer_clear(req->sendbuffer);
    606  1.1.1.3  christos 
    607  1.1.1.8  christos 	isc_buffer_initnull(&req->bodybuffer);
    608  1.1.1.3  christos 
    609  1.1.1.9  christos 	isc_httpd_attach(httpd, &req->httpd);
    610  1.1.1.8  christos 
    611  1.1.1.9  christos 	return req;
    612  1.1.1.3  christos }
    613  1.1.1.3  christos 
    614  1.1.1.3  christos static void
    615  1.1.1.8  christos new_httpd(isc_httpdmgr_t *httpdmgr, isc_nmhandle_t *handle) {
    616  1.1.1.3  christos 	isc_httpd_t *httpd = NULL;
    617      1.1  christos 
    618  1.1.1.3  christos 	REQUIRE(VALID_HTTPDMGR(httpdmgr));
    619      1.1  christos 
    620  1.1.1.9  christos 	httpd = isc_mem_get(httpdmgr->mctx, sizeof(*httpd));
    621  1.1.1.9  christos 	*httpd = (isc_httpd_t){
    622  1.1.1.9  christos 		.magic = HTTPD_MAGIC,
    623  1.1.1.9  christos 		.link = ISC_LINK_INITIALIZER,
    624  1.1.1.9  christos 		.references = ISC_REFCOUNT_INITIALIZER(1),
    625  1.1.1.9  christos 	};
    626      1.1  christos 
    627  1.1.1.9  christos 	isc_nmhandle_attach(handle, &httpd->handle);
    628      1.1  christos 
    629  1.1.1.9  christos 	isc_httpdmgr_attach(httpdmgr, &httpd->mgr);
    630      1.1  christos 
    631  1.1.1.8  christos 	LOCK(&httpdmgr->lock);
    632  1.1.1.8  christos 	ISC_LIST_APPEND(httpdmgr->running, httpd, link);
    633  1.1.1.8  christos 	UNLOCK(&httpdmgr->lock);
    634      1.1  christos 
    635  1.1.1.9  christos 	isc_nm_read(handle, httpd_request, httpd);
    636  1.1.1.8  christos }
    637      1.1  christos 
    638  1.1.1.8  christos static isc_result_t
    639  1.1.1.8  christos httpd_newconn(isc_nmhandle_t *handle, isc_result_t result, void *arg) {
    640  1.1.1.8  christos 	isc_httpdmgr_t *httpdmgr = (isc_httpdmgr_t *)arg;
    641  1.1.1.8  christos 	isc_sockaddr_t peeraddr;
    642      1.1  christos 
    643  1.1.1.8  christos 	REQUIRE(VALID_HTTPDMGR(httpdmgr));
    644  1.1.1.3  christos 
    645  1.1.1.8  christos 	if ((httpdmgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) != 0) {
    646  1.1.1.9  christos 		return ISC_R_CANCELED;
    647  1.1.1.8  christos 	} else if (result == ISC_R_CANCELED) {
    648  1.1.1.8  christos 		isc_httpdmgr_shutdown(&httpdmgr);
    649  1.1.1.9  christos 		return result;
    650  1.1.1.8  christos 	} else if (result != ISC_R_SUCCESS) {
    651  1.1.1.9  christos 		return result;
    652  1.1.1.8  christos 	}
    653      1.1  christos 
    654  1.1.1.8  christos 	peeraddr = isc_nmhandle_peeraddr(handle);
    655  1.1.1.8  christos 	if (httpdmgr->client_ok != NULL &&
    656  1.1.1.8  christos 	    !(httpdmgr->client_ok)(&peeraddr, httpdmgr->cb_arg))
    657  1.1.1.8  christos 	{
    658  1.1.1.9  christos 		return ISC_R_FAILURE;
    659  1.1.1.3  christos 	}
    660      1.1  christos 
    661  1.1.1.8  christos 	new_httpd(httpdmgr, handle);
    662  1.1.1.8  christos 
    663  1.1.1.9  christos 	return ISC_R_SUCCESS;
    664      1.1  christos }
    665      1.1  christos 
    666      1.1  christos static isc_result_t
    667  1.1.1.8  christos render_404(const isc_httpd_t *httpd, const isc_httpdurl_t *urlinfo, void *arg,
    668  1.1.1.8  christos 	   unsigned int *retcode, const char **retmsg, const char **mimetype,
    669  1.1.1.8  christos 	   isc_buffer_t *b, isc_httpdfree_t **freecb, void **freecb_args) {
    670      1.1  christos 	static char msg[] = "No such URL.\r\n";
    671      1.1  christos 
    672  1.1.1.8  christos 	UNUSED(httpd);
    673      1.1  christos 	UNUSED(urlinfo);
    674      1.1  christos 	UNUSED(arg);
    675      1.1  christos 
    676      1.1  christos 	*retcode = 404;
    677      1.1  christos 	*retmsg = "No such URL";
    678      1.1  christos 	*mimetype = "text/plain";
    679      1.1  christos 	isc_buffer_reinit(b, msg, strlen(msg));
    680      1.1  christos 	isc_buffer_add(b, strlen(msg));
    681      1.1  christos 	*freecb = NULL;
    682      1.1  christos 	*freecb_args = NULL;
    683      1.1  christos 
    684  1.1.1.9  christos 	return ISC_R_SUCCESS;
    685      1.1  christos }
    686      1.1  christos 
    687      1.1  christos static isc_result_t
    688  1.1.1.8  christos render_500(const isc_httpd_t *httpd, const isc_httpdurl_t *urlinfo, void *arg,
    689  1.1.1.8  christos 	   unsigned int *retcode, const char **retmsg, const char **mimetype,
    690  1.1.1.8  christos 	   isc_buffer_t *b, isc_httpdfree_t **freecb, void **freecb_args) {
    691      1.1  christos 	static char msg[] = "Internal server failure.\r\n";
    692      1.1  christos 
    693  1.1.1.8  christos 	UNUSED(httpd);
    694      1.1  christos 	UNUSED(urlinfo);
    695      1.1  christos 	UNUSED(arg);
    696      1.1  christos 
    697      1.1  christos 	*retcode = 500;
    698      1.1  christos 	*retmsg = "Internal server failure";
    699      1.1  christos 	*mimetype = "text/plain";
    700      1.1  christos 	isc_buffer_reinit(b, msg, strlen(msg));
    701      1.1  christos 	isc_buffer_add(b, strlen(msg));
    702      1.1  christos 	*freecb = NULL;
    703      1.1  christos 	*freecb_args = NULL;
    704      1.1  christos 
    705  1.1.1.9  christos 	return ISC_R_SUCCESS;
    706      1.1  christos }
    707      1.1  christos 
    708      1.1  christos #ifdef HAVE_ZLIB
    709      1.1  christos /*%<
    710      1.1  christos  * Tries to compress httpd->bodybuffer to httpd->compbuffer, extending it
    711      1.1  christos  * if necessary.
    712      1.1  christos  *
    713      1.1  christos  * Requires:
    714      1.1  christos  *\li	httpd a valid isc_httpd_t object
    715      1.1  christos  *
    716      1.1  christos  * Returns:
    717      1.1  christos  *\li	#ISC_R_SUCCESS	  -- all is well.
    718      1.1  christos  *\li	#ISC_R_NOMEMORY	  -- not enough memory to compress data
    719      1.1  christos  *\li	#ISC_R_FAILURE	  -- error during compression or compressed
    720      1.1  christos  *			     data would be larger than input data
    721      1.1  christos  */
    722      1.1  christos static isc_result_t
    723  1.1.1.8  christos httpd_compress(isc_httpd_sendreq_t *req) {
    724      1.1  christos 	z_stream zstr;
    725  1.1.1.8  christos 	int ret, inputlen;
    726      1.1  christos 
    727      1.1  christos 	/*
    728      1.1  christos 	 * We're setting output buffer size to input size so it fails if the
    729      1.1  christos 	 * compressed data size would be bigger than the input size.
    730      1.1  christos 	 */
    731  1.1.1.8  christos 	inputlen = isc_buffer_usedlength(&req->bodybuffer);
    732  1.1.1.8  christos 	if (inputlen == 0) {
    733  1.1.1.9  christos 		return ISC_R_FAILURE;
    734  1.1.1.8  christos 	}
    735  1.1.1.8  christos 
    736  1.1.1.8  christos 	isc_buffer_allocate(req->mctx, &req->compbuffer, inputlen);
    737  1.1.1.8  christos 	isc_buffer_clear(req->compbuffer);
    738      1.1  christos 
    739  1.1.1.8  christos 	zstr = (z_stream){
    740  1.1.1.8  christos 		.total_in = inputlen,
    741  1.1.1.8  christos 		.avail_out = inputlen,
    742  1.1.1.8  christos 		.avail_in = inputlen,
    743  1.1.1.8  christos 		.next_in = isc_buffer_base(&req->bodybuffer),
    744  1.1.1.8  christos 		.next_out = isc_buffer_base(req->compbuffer),
    745  1.1.1.8  christos 	};
    746      1.1  christos 
    747      1.1  christos 	ret = deflateInit(&zstr, Z_DEFAULT_COMPRESSION);
    748      1.1  christos 	if (ret == Z_OK) {
    749      1.1  christos 		ret = deflate(&zstr, Z_FINISH);
    750      1.1  christos 	}
    751      1.1  christos 	deflateEnd(&zstr);
    752      1.1  christos 	if (ret == Z_STREAM_END) {
    753  1.1.1.8  christos 		isc_buffer_add(req->compbuffer, zstr.total_out);
    754  1.1.1.9  christos 		return ISC_R_SUCCESS;
    755      1.1  christos 	} else {
    756  1.1.1.8  christos 		isc_buffer_free(&req->compbuffer);
    757  1.1.1.9  christos 		return ISC_R_FAILURE;
    758      1.1  christos 	}
    759      1.1  christos }
    760  1.1.1.3  christos #endif /* ifdef HAVE_ZLIB */
    761      1.1  christos 
    762      1.1  christos static void
    763  1.1.1.9  christos prepare_response(void *arg) {
    764  1.1.1.9  christos 	isc_httpd_sendreq_t *req = arg;
    765  1.1.1.9  christos 	isc_httpd_t *httpd = req->httpd;
    766  1.1.1.9  christos 	isc_httpdmgr_t *mgr = httpd->mgr;
    767  1.1.1.9  christos 	isc_time_t now = isc_time_now();
    768      1.1  christos 	char datebuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
    769  1.1.1.8  christos 	const char *path = "/";
    770  1.1.1.8  christos 	size_t path_len = 1;
    771  1.1.1.8  christos 	bool is_compressed = false;
    772  1.1.1.8  christos 	isc_httpdurl_t *url = NULL;
    773  1.1.1.8  christos 	isc_result_t result;
    774      1.1  christos 
    775  1.1.1.3  christos 	REQUIRE(VALID_HTTPD(httpd));
    776  1.1.1.9  christos 	REQUIRE(req != NULL);
    777      1.1  christos 
    778  1.1.1.8  christos 	isc_time_formathttptimestamp(&now, datebuf, sizeof(datebuf));
    779  1.1.1.3  christos 
    780  1.1.1.8  christos 	if (httpd->up.field_set & (1 << ISC_UF_PATH)) {
    781  1.1.1.8  christos 		path = &httpd->path[httpd->up.field_data[ISC_UF_PATH].off];
    782  1.1.1.8  christos 		path_len = httpd->up.field_data[ISC_UF_PATH].len;
    783      1.1  christos 	}
    784      1.1  christos 
    785  1.1.1.8  christos 	LOCK(&mgr->lock);
    786  1.1.1.9  christos 	ISC_LIST_FOREACH (mgr->urls, url, link) {
    787  1.1.1.8  christos 		if (strncmp(path, url->url, path_len) == 0) {
    788      1.1  christos 			break;
    789  1.1.1.3  christos 		}
    790      1.1  christos 	}
    791  1.1.1.8  christos 	UNLOCK(&mgr->lock);
    792  1.1.1.8  christos 
    793  1.1.1.3  christos 	if (url == NULL) {
    794  1.1.1.8  christos 		result = mgr->render_404(httpd, NULL, NULL, &req->retcode,
    795  1.1.1.8  christos 					 &req->retmsg, &req->mimetype,
    796  1.1.1.8  christos 					 &req->bodybuffer, &req->freecb,
    797  1.1.1.8  christos 					 &req->freecb_arg);
    798  1.1.1.3  christos 	} else {
    799  1.1.1.8  christos 		result = url->action(httpd, url, url->action_arg, &req->retcode,
    800  1.1.1.8  christos 				     &req->retmsg, &req->mimetype,
    801  1.1.1.8  christos 				     &req->bodybuffer, &req->freecb,
    802  1.1.1.8  christos 				     &req->freecb_arg);
    803  1.1.1.3  christos 	}
    804      1.1  christos 	if (result != ISC_R_SUCCESS) {
    805  1.1.1.8  christos 		result = mgr->render_500(httpd, url, NULL, &req->retcode,
    806  1.1.1.8  christos 					 &req->retmsg, &req->mimetype,
    807  1.1.1.8  christos 					 &req->bodybuffer, &req->freecb,
    808  1.1.1.8  christos 					 &req->freecb_arg);
    809      1.1  christos 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
    810      1.1  christos 	}
    811      1.1  christos 
    812      1.1  christos #ifdef HAVE_ZLIB
    813  1.1.1.8  christos 	if ((httpd->flags & ACCEPT_DEFLATE) != 0) {
    814  1.1.1.8  christos 		result = httpd_compress(req);
    815  1.1.1.3  christos 		if (result == ISC_R_SUCCESS) {
    816  1.1.1.3  christos 			is_compressed = true;
    817  1.1.1.3  christos 		}
    818      1.1  christos 	}
    819  1.1.1.3  christos #endif /* ifdef HAVE_ZLIB */
    820      1.1  christos 
    821  1.1.1.8  christos 	httpd_response(httpd, req);
    822  1.1.1.8  christos 	/* RFC 9112  9.6: SHOULD send Connection: close in last response */
    823  1.1.1.8  christos 	if ((httpd->flags & CONNECTION_CLOSE) != 0) {
    824  1.1.1.8  christos 		httpd_addheader(req, "Connection", "close");
    825  1.1.1.8  christos 	} else if ((httpd->flags & CONNECTION_KEEP_ALIVE) != 0) {
    826  1.1.1.8  christos 		httpd_addheader(req, "Connection", "Keep-Alive");
    827  1.1.1.8  christos 	}
    828  1.1.1.8  christos 	httpd_addheader(req, "Content-Type", req->mimetype);
    829  1.1.1.8  christos 	httpd_addheader(req, "Date", datebuf);
    830  1.1.1.8  christos 	httpd_addheader(req, "Expires", datebuf);
    831      1.1  christos 
    832      1.1  christos 	if (url != NULL && url->isstatic) {
    833      1.1  christos 		char loadbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
    834  1.1.1.3  christos 		isc_time_formathttptimestamp(&url->loadtime, loadbuf,
    835  1.1.1.3  christos 					     sizeof(loadbuf));
    836  1.1.1.8  christos 		httpd_addheader(req, "Last-Modified", loadbuf);
    837  1.1.1.8  christos 		httpd_addheader(req, "Cache-Control: public", NULL);
    838      1.1  christos 	} else {
    839  1.1.1.8  christos 		httpd_addheader(req, "Last-Modified", datebuf);
    840  1.1.1.8  christos 		httpd_addheader(req, "Pragma: no-cache", NULL);
    841  1.1.1.8  christos 		httpd_addheader(req, "Cache-Control: no-cache", NULL);
    842      1.1  christos 	}
    843      1.1  christos 
    844  1.1.1.8  christos 	httpd_addheader(req, "Server: libisc", NULL);
    845      1.1  christos 
    846  1.1.1.4  christos 	if (is_compressed) {
    847  1.1.1.8  christos 		httpd_addheader(req, "Content-Encoding", "deflate");
    848  1.1.1.8  christos 		httpd_addheaderuint(req, "Content-Length",
    849  1.1.1.8  christos 				    isc_buffer_usedlength(req->compbuffer));
    850      1.1  christos 	} else {
    851  1.1.1.8  christos 		httpd_addheaderuint(req, "Content-Length",
    852  1.1.1.8  christos 				    isc_buffer_usedlength(&req->bodybuffer));
    853      1.1  christos 	}
    854      1.1  christos 
    855  1.1.1.8  christos 	httpd_endheaders(req); /* done */
    856      1.1  christos 
    857      1.1  christos 	/*
    858  1.1.1.2  christos 	 * Append either the compressed or the non-compressed response body to
    859  1.1.1.2  christos 	 * the response headers and store the result in httpd->sendbuffer.
    860      1.1  christos 	 */
    861  1.1.1.8  christos 	if (is_compressed) {
    862  1.1.1.8  christos 		isc_buffer_putmem(req->sendbuffer,
    863  1.1.1.8  christos 				  isc_buffer_base(req->compbuffer),
    864  1.1.1.8  christos 				  isc_buffer_usedlength(req->compbuffer));
    865  1.1.1.8  christos 		isc_buffer_free(&req->compbuffer);
    866  1.1.1.8  christos 	} else {
    867  1.1.1.8  christos 		isc_buffer_putmem(req->sendbuffer,
    868  1.1.1.8  christos 				  isc_buffer_base(&req->bodybuffer),
    869  1.1.1.8  christos 				  isc_buffer_usedlength(&req->bodybuffer));
    870  1.1.1.8  christos 	}
    871  1.1.1.8  christos 
    872  1.1.1.8  christos 	/* Free the bodybuffer */
    873  1.1.1.8  christos 	if (req->freecb != NULL && isc_buffer_length(&req->bodybuffer) > 0) {
    874  1.1.1.8  christos 		req->freecb(&req->bodybuffer, req->freecb_arg);
    875  1.1.1.8  christos 	}
    876  1.1.1.8  christos 
    877  1.1.1.8  christos 	/* Consume the request from the recv buffer. */
    878  1.1.1.8  christos 	INSIST(httpd->consume != 0);
    879  1.1.1.8  christos 	INSIST(httpd->consume <= httpd->recvlen);
    880  1.1.1.8  christos 	if (httpd->consume < httpd->recvlen) {
    881  1.1.1.8  christos 		memmove(httpd->recvbuf, httpd->recvbuf + httpd->consume,
    882  1.1.1.8  christos 			httpd->recvlen - httpd->consume);
    883  1.1.1.8  christos 	}
    884  1.1.1.8  christos 	httpd->recvlen -= httpd->consume;
    885  1.1.1.8  christos 	httpd->consume = 0;
    886  1.1.1.9  christos }
    887  1.1.1.9  christos 
    888  1.1.1.9  christos static void
    889  1.1.1.9  christos prepare_response_done(void *arg) {
    890  1.1.1.9  christos 	isc_region_t r;
    891  1.1.1.9  christos 	isc_httpd_sendreq_t *req = arg;
    892  1.1.1.9  christos 	isc_httpd_t *httpd = req->httpd;
    893  1.1.1.8  christos 
    894  1.1.1.2  christos 	/*
    895  1.1.1.9  christos 	 * Determine total response size.
    896  1.1.1.2  christos 	 */
    897  1.1.1.9  christos 	isc_buffer_usedregion(req->sendbuffer, &r);
    898      1.1  christos 
    899  1.1.1.9  christos 	isc_nm_send(httpd->handle, &r, httpd_senddone, req);
    900      1.1  christos }
    901      1.1  christos 
    902  1.1.1.8  christos static void
    903  1.1.1.8  christos httpd_request(isc_nmhandle_t *handle, isc_result_t eresult,
    904  1.1.1.8  christos 	      isc_region_t *region, void *arg) {
    905  1.1.1.9  christos 	isc_httpd_t *httpd = arg;
    906  1.1.1.9  christos 	isc_httpdmgr_t *mgr = httpd->mgr;
    907  1.1.1.8  christos 	size_t last_len = 0;
    908  1.1.1.9  christos 	isc_result_t result;
    909      1.1  christos 
    910  1.1.1.8  christos 	REQUIRE(VALID_HTTPD(httpd));
    911      1.1  christos 
    912  1.1.1.8  christos 	REQUIRE(httpd->handle == handle);
    913      1.1  christos 
    914  1.1.1.8  christos 	if (eresult != ISC_R_SUCCESS) {
    915  1.1.1.8  christos 		goto close_readhandle;
    916      1.1  christos 	}
    917      1.1  christos 
    918  1.1.1.9  christos 	REQUIRE((mgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) == 0);
    919      1.1  christos 
    920  1.1.1.9  christos 	isc_nm_read_stop(handle);
    921      1.1  christos 
    922  1.1.1.8  christos 	/*
    923  1.1.1.8  christos 	 * If we are being called from httpd_senddone(), the last HTTP request
    924  1.1.1.8  christos 	 * was processed successfully, reset the last_len to 0, even if there's
    925  1.1.1.8  christos 	 * data in the httpd->recvbuf.
    926  1.1.1.8  christos 	 */
    927  1.1.1.8  christos 	last_len = (region == NULL) ? 0 : httpd->recvlen;
    928  1.1.1.8  christos 
    929  1.1.1.8  christos 	/* Store the received data into the recvbuf */
    930  1.1.1.8  christos 	if (region != NULL) {
    931  1.1.1.8  christos 		if (httpd->recvlen + region->length > sizeof(httpd->recvbuf)) {
    932  1.1.1.8  christos 			goto close_readhandle;
    933  1.1.1.8  christos 		}
    934      1.1  christos 
    935  1.1.1.8  christos 		memmove(httpd->recvbuf + httpd->recvlen, region->base,
    936  1.1.1.8  christos 			region->length);
    937  1.1.1.8  christos 		httpd->recvlen += region->length;
    938  1.1.1.3  christos 	}
    939      1.1  christos 
    940  1.1.1.8  christos 	result = process_request(httpd, last_len);
    941      1.1  christos 
    942  1.1.1.8  christos 	if (result == ISC_R_NOMORE) {
    943  1.1.1.8  christos 		if (httpd->recvlen > HTTP_MAX_REQUEST_LEN) {
    944  1.1.1.8  christos 			goto close_readhandle;
    945  1.1.1.8  christos 		}
    946      1.1  christos 
    947  1.1.1.9  christos 		/* Wait for more data, the handle is still attached */
    948  1.1.1.9  christos 		isc_nm_read(handle, httpd_request, arg);
    949  1.1.1.8  christos 		return;
    950  1.1.1.8  christos 	}
    951      1.1  christos 
    952  1.1.1.8  christos 	/* XXXFANF it would be more polite to reply 400 bad request */
    953  1.1.1.8  christos 	if (result != ISC_R_SUCCESS) {
    954  1.1.1.8  christos 		goto close_readhandle;
    955  1.1.1.8  christos 	}
    956  1.1.1.8  christos 
    957  1.1.1.9  christos 	isc_httpd_sendreq_t *req = isc__httpd_sendreq_new(httpd);
    958  1.1.1.9  christos 	isc_nmhandle_ref(handle);
    959  1.1.1.9  christos 	isc_work_enqueue(isc_loop(), prepare_response, prepare_response_done,
    960  1.1.1.9  christos 			 req);
    961  1.1.1.8  christos 	return;
    962  1.1.1.8  christos 
    963  1.1.1.8  christos close_readhandle:
    964  1.1.1.9  christos 	isc_nmhandle_close(httpd->handle);
    965  1.1.1.9  christos 	isc_nmhandle_detach(&httpd->handle);
    966  1.1.1.9  christos 
    967  1.1.1.9  christos 	isc_httpd_detach(&httpd);
    968      1.1  christos }
    969      1.1  christos 
    970  1.1.1.8  christos void
    971  1.1.1.8  christos isc_httpdmgr_shutdown(isc_httpdmgr_t **httpdmgrp) {
    972  1.1.1.9  christos 	isc_httpdmgr_t *httpdmgr = NULL;
    973      1.1  christos 
    974  1.1.1.8  christos 	REQUIRE(httpdmgrp != NULL);
    975  1.1.1.8  christos 	REQUIRE(VALID_HTTPDMGR(*httpdmgrp));
    976  1.1.1.3  christos 
    977  1.1.1.8  christos 	httpdmgr = *httpdmgrp;
    978  1.1.1.8  christos 	*httpdmgrp = NULL;
    979  1.1.1.8  christos 
    980  1.1.1.8  christos 	isc_nm_stoplistening(httpdmgr->sock);
    981  1.1.1.8  christos 
    982  1.1.1.8  christos 	LOCK(&httpdmgr->lock);
    983  1.1.1.8  christos 
    984  1.1.1.9  christos 	isc_httpd_t *httpd = NULL, *next = NULL;
    985  1.1.1.9  christos 	ISC_LIST_FOREACH_SAFE (httpdmgr->running, httpd, link, next) {
    986  1.1.1.9  christos 		if (httpd->handle != NULL) {
    987  1.1.1.9  christos 			httpd_request(httpd->handle, ISC_R_SUCCESS, NULL,
    988  1.1.1.9  christos 				      httpd);
    989  1.1.1.9  christos 		}
    990      1.1  christos 	}
    991  1.1.1.9  christos 
    992  1.1.1.9  christos 	httpdmgr->flags |= ISC_HTTPDMGR_SHUTTINGDOWN;
    993  1.1.1.9  christos 
    994  1.1.1.8  christos 	UNLOCK(&httpdmgr->lock);
    995  1.1.1.8  christos 
    996  1.1.1.8  christos 	isc_nmsocket_close(&httpdmgr->sock);
    997      1.1  christos 
    998  1.1.1.9  christos 	isc_httpdmgr_detach(&httpdmgr);
    999      1.1  christos }
   1000      1.1  christos 
   1001  1.1.1.8  christos static void
   1002  1.1.1.8  christos httpd_response(isc_httpd_t *httpd, isc_httpd_sendreq_t *req) {
   1003      1.1  christos 	isc_result_t result;
   1004      1.1  christos 
   1005  1.1.1.8  christos 	result = isc_buffer_printf(req->sendbuffer, "HTTP/1.%u %03u %s\r\n",
   1006  1.1.1.8  christos 				   httpd->minor_version, req->retcode,
   1007  1.1.1.8  christos 				   req->retmsg);
   1008  1.1.1.3  christos 
   1009  1.1.1.8  christos 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
   1010  1.1.1.8  christos }
   1011      1.1  christos 
   1012  1.1.1.8  christos static void
   1013  1.1.1.8  christos httpd_addheader(isc_httpd_sendreq_t *req, const char *name, const char *val) {
   1014  1.1.1.8  christos 	isc_result_t result;
   1015      1.1  christos 
   1016      1.1  christos 	if (val != NULL) {
   1017  1.1.1.8  christos 		result = isc_buffer_printf(req->sendbuffer, "%s: %s\r\n", name,
   1018  1.1.1.8  christos 					   val);
   1019      1.1  christos 	} else {
   1020  1.1.1.8  christos 		result = isc_buffer_printf(req->sendbuffer, "%s\r\n", name);
   1021      1.1  christos 	}
   1022  1.1.1.8  christos 
   1023  1.1.1.8  christos 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
   1024      1.1  christos }
   1025      1.1  christos 
   1026  1.1.1.8  christos static void
   1027  1.1.1.8  christos httpd_endheaders(isc_httpd_sendreq_t *req) {
   1028      1.1  christos 	isc_result_t result;
   1029      1.1  christos 
   1030  1.1.1.8  christos 	result = isc_buffer_printf(req->sendbuffer, "\r\n");
   1031      1.1  christos 
   1032  1.1.1.8  christos 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
   1033      1.1  christos }
   1034      1.1  christos 
   1035  1.1.1.8  christos static void
   1036  1.1.1.8  christos httpd_addheaderuint(isc_httpd_sendreq_t *req, const char *name, int val) {
   1037      1.1  christos 	isc_result_t result;
   1038      1.1  christos 
   1039  1.1.1.8  christos 	result = isc_buffer_printf(req->sendbuffer, "%s: %d\r\n", name, val);
   1040      1.1  christos 
   1041  1.1.1.8  christos 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
   1042      1.1  christos }
   1043      1.1  christos 
   1044      1.1  christos static void
   1045  1.1.1.8  christos httpd_senddone(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) {
   1046  1.1.1.8  christos 	isc_httpd_sendreq_t *req = (isc_httpd_sendreq_t *)arg;
   1047  1.1.1.8  christos 	isc_httpd_t *httpd = req->httpd;
   1048      1.1  christos 
   1049  1.1.1.3  christos 	REQUIRE(VALID_HTTPD(httpd));
   1050  1.1.1.3  christos 
   1051  1.1.1.9  christos 	if ((httpd->mgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) != 0) {
   1052  1.1.1.8  christos 		goto detach;
   1053      1.1  christos 	}
   1054      1.1  christos 
   1055  1.1.1.9  christos 	if (eresult == ISC_R_SUCCESS && (httpd->flags & CONNECTION_CLOSE) != 0)
   1056  1.1.1.8  christos 	{
   1057  1.1.1.9  christos 		eresult = ISC_R_EOF;
   1058      1.1  christos 	}
   1059      1.1  christos 
   1060  1.1.1.9  christos 	/*
   1061  1.1.1.9  christos 	 * Calling httpd_request() with region NULL restarts reading.
   1062  1.1.1.9  christos 	 */
   1063  1.1.1.9  christos 	httpd_request(handle, eresult, NULL, httpd);
   1064  1.1.1.9  christos 
   1065  1.1.1.8  christos detach:
   1066  1.1.1.8  christos 	isc_nmhandle_detach(&handle);
   1067  1.1.1.8  christos 	isc__httpd_sendreq_free(req);
   1068  1.1.1.9  christos 	isc_httpd_detach(&httpd);
   1069      1.1  christos }
   1070      1.1  christos 
   1071      1.1  christos isc_result_t
   1072  1.1.1.8  christos isc_httpdmgr_addurl(isc_httpdmgr_t *httpdmgr, const char *url, bool isstatic,
   1073  1.1.1.3  christos 		    isc_httpdaction_t *func, void *arg) {
   1074      1.1  christos 	isc_httpdurl_t *item;
   1075      1.1  christos 
   1076  1.1.1.3  christos 	REQUIRE(VALID_HTTPDMGR(httpdmgr));
   1077  1.1.1.3  christos 
   1078      1.1  christos 	if (url == NULL) {
   1079      1.1  christos 		httpdmgr->render_404 = func;
   1080  1.1.1.9  christos 		return ISC_R_SUCCESS;
   1081      1.1  christos 	}
   1082      1.1  christos 
   1083      1.1  christos 	item = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpdurl_t));
   1084      1.1  christos 
   1085      1.1  christos 	item->url = isc_mem_strdup(httpdmgr->mctx, url);
   1086      1.1  christos 
   1087      1.1  christos 	item->action = func;
   1088      1.1  christos 	item->action_arg = arg;
   1089      1.1  christos 	item->isstatic = isstatic;
   1090  1.1.1.9  christos 	item->loadtime = isc_time_now();
   1091      1.1  christos 
   1092      1.1  christos 	ISC_LINK_INIT(item, link);
   1093  1.1.1.3  christos 
   1094  1.1.1.3  christos 	LOCK(&httpdmgr->lock);
   1095      1.1  christos 	ISC_LIST_APPEND(httpdmgr->urls, item, link);
   1096  1.1.1.3  christos 	UNLOCK(&httpdmgr->lock);
   1097      1.1  christos 
   1098  1.1.1.9  christos 	return ISC_R_SUCCESS;
   1099      1.1  christos }
   1100      1.1  christos 
   1101      1.1  christos void
   1102  1.1.1.3  christos isc_httpd_setfinishhook(void (*fn)(void)) {
   1103  1.1.1.3  christos #if ENABLE_AFL
   1104      1.1  christos 	finishhook = fn;
   1105  1.1.1.3  christos #else  /* ENABLE_AFL */
   1106  1.1.1.3  christos 	UNUSED(fn);
   1107  1.1.1.3  christos #endif /* ENABLE_AFL */
   1108      1.1  christos }
   1109  1.1.1.8  christos 
   1110  1.1.1.8  christos bool
   1111  1.1.1.8  christos isc_httpdurl_isstatic(const isc_httpdurl_t *url) {
   1112  1.1.1.9  christos 	return url->isstatic;
   1113  1.1.1.8  christos }
   1114  1.1.1.8  christos 
   1115  1.1.1.8  christos const isc_time_t *
   1116  1.1.1.8  christos isc_httpdurl_loadtime(const isc_httpdurl_t *url) {
   1117  1.1.1.9  christos 	return &url->loadtime;
   1118  1.1.1.8  christos }
   1119  1.1.1.8  christos 
   1120  1.1.1.8  christos const isc_time_t *
   1121  1.1.1.8  christos isc_httpd_if_modified_since(const isc_httpd_t *httpd) {
   1122  1.1.1.9  christos 	return (const isc_time_t *)&httpd->if_modified_since;
   1123  1.1.1.8  christos }
   1124