2006-12-21 06:03:37 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2006-12-21 06:03:37 +00:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2006-12-21 06:03:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
#include <ctype.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/httpd.h>
|
2023-04-24 12:45:54 +02:00
|
|
|
#include <isc/list.h>
|
2006-12-21 06:03:37 +00:00
|
|
|
#include <isc/mem.h>
|
2020-07-09 19:36:10 -07:00
|
|
|
#include <isc/netmgr.h>
|
2020-01-17 18:24:24 +11:00
|
|
|
#include <isc/refcount.h>
|
2021-10-03 00:27:52 -07:00
|
|
|
#include <isc/sockaddr.h>
|
2006-12-21 06:03:37 +00:00
|
|
|
#include <isc/string.h>
|
2014-01-09 15:14:57 -08:00
|
|
|
#include <isc/time.h>
|
2022-10-06 12:56:25 +02:00
|
|
|
#include <isc/url.h>
|
2006-12-21 06:03:37 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
#include "netmgr/netmgr-int.h"
|
|
|
|
#include "picohttpparser.h"
|
|
|
|
|
2015-10-02 10:45:10 +02:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
#include <zlib.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef HAVE_ZLIB */
|
2015-10-02 10:45:10 +02:00
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
#define CHECK(m) \
|
|
|
|
do { \
|
|
|
|
result = (m); \
|
|
|
|
if (result != ISC_R_SUCCESS) { \
|
|
|
|
goto cleanup; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
/*
|
|
|
|
* Size the recv buffer to hold at maximum two full buffers from isc_nm_read(),
|
|
|
|
* so we don't have to handle the truncation.
|
|
|
|
*/
|
2022-10-18 21:46:16 +02:00
|
|
|
#define HTTP_RECVLEN ISC_NETMGR_TCP_RECVBUF_SIZE * 2
|
|
|
|
#define HTTP_SENDLEN ISC_NETMGR_TCP_RECVBUF_SIZE
|
2022-11-10 16:34:26 +01:00
|
|
|
#define HTTP_HEADERS_NUM 100
|
2022-10-18 21:46:16 +02:00
|
|
|
#define HTTP_MAX_REQUEST_LEN 4096
|
2020-01-17 18:24:24 +11:00
|
|
|
|
2023-06-07 15:47:47 +01:00
|
|
|
typedef enum httpd_flags {
|
|
|
|
CONNECTION_CLOSE = 1 << 0, /* connection must close */
|
|
|
|
CONNECTION_KEEP_ALIVE = 1 << 1, /* response needs a keep-alive header */
|
|
|
|
ACCEPT_DEFLATE = 1 << 2, /* response can be compressed */
|
|
|
|
} httpd_flags_t;
|
2020-01-17 18:24:24 +11:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define HTTPD_MAGIC ISC_MAGIC('H', 't', 'p', 'd')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_HTTPD(m) ISC_MAGIC_VALID(m, HTTPD_MAGIC)
|
2020-01-17 18:24:24 +11:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define HTTPDMGR_MAGIC ISC_MAGIC('H', 'p', 'd', 'm')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_HTTPDMGR(m) ISC_MAGIC_VALID(m, HTTPDMGR_MAGIC)
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
/*%
|
2022-10-06 12:56:25 +02:00
|
|
|
* HTTP methods.
|
2020-07-09 19:36:10 -07:00
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
typedef enum { METHOD_UNKNOWN = 0, METHOD_GET = 1, METHOD_POST = 2 } method_t;
|
2020-07-09 19:36:10 -07:00
|
|
|
|
|
|
|
/*%
|
2022-10-06 12:56:25 +02:00
|
|
|
* HTTP urls. These are the URLs we manage, and the function to call to
|
|
|
|
* provide the data for it. We pass in the base url (so the same function
|
|
|
|
* can handle multiple requests), and a structure to fill in to return a
|
|
|
|
* result to the client. We also pass in a pointer to be filled in for
|
|
|
|
* the data cleanup function.
|
2020-07-09 19:36:10 -07:00
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
struct isc_httpdurl {
|
|
|
|
char *url;
|
|
|
|
isc_httpdaction_t *action;
|
|
|
|
void *action_arg;
|
|
|
|
bool isstatic;
|
|
|
|
isc_time_t loadtime;
|
|
|
|
ISC_LINK(isc_httpdurl_t) link;
|
|
|
|
};
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
/*% http client */
|
|
|
|
struct isc_httpd {
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int magic; /* HTTPD_MAGIC */
|
2024-01-19 10:27:41 +01:00
|
|
|
isc_refcount_t references;
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_httpdmgr_t *mgr; /*%< our parent */
|
|
|
|
ISC_LINK(isc_httpd_t) link;
|
2020-07-09 19:36:10 -07:00
|
|
|
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_nmhandle_t *handle; /* Permanent pointer to handle */
|
2020-09-03 13:31:27 -07:00
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
/*%
|
|
|
|
* Received data state.
|
|
|
|
*/
|
2020-02-13 14:44:37 -08:00
|
|
|
char recvbuf[HTTP_RECVLEN]; /*%< receive buffer */
|
2022-10-06 12:56:25 +02:00
|
|
|
size_t recvlen; /*%< length recv'd */
|
|
|
|
size_t consume; /*%< length of last command */
|
2020-02-12 13:59:18 +01:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
method_t method;
|
|
|
|
int minor_version;
|
2023-06-07 15:47:47 +01:00
|
|
|
httpd_flags_t flags;
|
2022-10-06 12:56:25 +02:00
|
|
|
const char *path;
|
|
|
|
isc_url_parser_t up;
|
|
|
|
isc_time_t if_modified_since;
|
2006-12-21 06:03:37 +00:00
|
|
|
};
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
#if ISC_HTTPD_TRACE
|
|
|
|
#define isc_httpd_ref(ptr) isc_httpd__ref(ptr, __func__, __FILE__, __LINE__)
|
|
|
|
#define isc_httpd_unref(ptr) isc_httpd__unref(ptr, __func__, __FILE__, __LINE__)
|
|
|
|
#define isc_httpd_attach(ptr, ptrp) \
|
|
|
|
isc_httpd__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
|
|
|
|
#define isc_httpd_detach(ptrp) \
|
|
|
|
isc_httpd__detach(ptrp, __func__, __FILE__, __LINE__)
|
|
|
|
ISC_REFCOUNT_TRACE_DECL(isc_httpd);
|
|
|
|
#else
|
|
|
|
ISC_REFCOUNT_DECL(isc_httpd);
|
|
|
|
#endif
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
struct isc_httpdmgr {
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int magic; /* HTTPDMGR_MAGIC */
|
|
|
|
isc_refcount_t references;
|
|
|
|
isc_mem_t *mctx;
|
2020-07-09 19:36:10 -07:00
|
|
|
isc_nmsocket_t *sock;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_httpdclientok_t *client_ok; /*%< client validator */
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_httpdondestroy_t *ondestroy; /*%< cleanup callback */
|
2020-02-13 14:44:37 -08:00
|
|
|
void *cb_arg; /*%< argument for the above */
|
2008-01-17 00:15:14 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int flags;
|
|
|
|
ISC_LIST(isc_httpd_t) running; /*%< running clients */
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_mutex_t lock;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_LIST(isc_httpdurl_t) urls; /*%< urls we manage */
|
|
|
|
isc_httpdaction_t *render_404;
|
|
|
|
isc_httpdaction_t *render_500;
|
2006-12-21 06:03:37 +00:00
|
|
|
};
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
#if ISC_HTTPD_TRACE
|
|
|
|
#define isc_httpdmgr_ref(ptr) \
|
|
|
|
isc_httpdmgr__ref(ptr, __func__, __FILE__, __LINE__)
|
|
|
|
#define isc_httpdmgr_unref(ptr) \
|
|
|
|
isc_httpdmgr__unref(ptr, __func__, __FILE__, __LINE__)
|
|
|
|
#define isc_httpdmgr_attach(ptr, ptrp) \
|
|
|
|
isc_httpdmgr__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
|
|
|
|
#define isc_httpdmgr_detach(ptrp) \
|
|
|
|
isc_httpdmgr__detach(ptrp, __func__, __FILE__, __LINE__)
|
|
|
|
ISC_REFCOUNT_TRACE_DECL(isc_httpdmgr);
|
|
|
|
#else
|
|
|
|
ISC_REFCOUNT_DECL(isc_httpdmgr);
|
|
|
|
#endif
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
typedef struct isc_httpd_sendreq {
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_httpd_t *httpd;
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Transmit data state.
|
|
|
|
*
|
|
|
|
* This is the data buffer we will transmit.
|
|
|
|
*
|
|
|
|
* This free function pointer is filled in by the rendering function
|
|
|
|
* we call. The free function is called after the data is transmitted
|
|
|
|
* to the client.
|
|
|
|
*
|
|
|
|
* We currently use three buffers total:
|
|
|
|
*
|
|
|
|
* sendbuffer - gets filled as we gather the data
|
|
|
|
*
|
|
|
|
* bodybuffer - for the client to fill in (which it manages, it provides
|
|
|
|
* the space for it, etc) -- we will pass that buffer structure back to
|
|
|
|
* the caller, who is responsible for managing the space it may have
|
|
|
|
* allocated as backing store for it. we only allocate the buffer
|
|
|
|
* itself, not the backing store.
|
|
|
|
*
|
|
|
|
* compbuffer - managed by us, that contains the compressed HTTP data,
|
|
|
|
* if compression is used.
|
|
|
|
*/
|
|
|
|
isc_buffer_t *sendbuffer;
|
|
|
|
isc_buffer_t *compbuffer;
|
|
|
|
|
|
|
|
isc_buffer_t bodybuffer;
|
|
|
|
|
|
|
|
const char *mimetype;
|
|
|
|
unsigned int retcode;
|
|
|
|
const char *retmsg;
|
|
|
|
isc_httpdfree_t *freecb;
|
|
|
|
void *freecb_arg;
|
|
|
|
|
|
|
|
} isc_httpd_sendreq_t;
|
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
static isc_result_t
|
|
|
|
httpd_newconn(isc_nmhandle_t *, isc_result_t, void *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2020-07-09 19:36:10 -07:00
|
|
|
httpd_request(isc_nmhandle_t *, isc_result_t, isc_region_t *, void *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2020-07-09 19:36:10 -07:00
|
|
|
httpd_senddone(isc_nmhandle_t *, isc_result_t, void *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
httpd_free(isc_httpd_t *httpd);
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
static void
|
|
|
|
httpd_addheader(isc_httpd_sendreq_t *, const char *, const char *);
|
|
|
|
static void
|
|
|
|
httpd_addheaderuint(isc_httpd_sendreq_t *, const char *, int);
|
|
|
|
static void
|
|
|
|
httpd_endheaders(isc_httpd_sendreq_t *);
|
|
|
|
static void
|
|
|
|
httpd_response(isc_httpd_t *, isc_httpd_sendreq_t *);
|
2020-07-09 19:36:10 -07:00
|
|
|
|
|
|
|
static isc_result_t
|
2022-10-06 12:56:25 +02:00
|
|
|
process_request(isc_httpd_t *, size_t);
|
2014-01-09 15:14:57 -08:00
|
|
|
|
|
|
|
static isc_httpdaction_t render_404;
|
|
|
|
static isc_httpdaction_t render_500;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-01-20 12:37:57 +01:00
|
|
|
#if ENABLE_AFL
|
2016-05-05 11:46:11 +02:00
|
|
|
static void (*finishhook)(void) = NULL;
|
2020-01-20 12:37:57 +01:00
|
|
|
#endif /* ENABLE_AFL */
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
isc_result_t
|
2025-07-14 17:12:35 +02:00
|
|
|
isc_httpdmgr_create(isc_mem_t *mctx, isc_sockaddr_t *addr,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_httpdclientok_t *client_ok,
|
2008-01-17 00:15:14 +00:00
|
|
|
isc_httpdondestroy_t *ondestroy, void *cb_arg,
|
2020-07-09 19:36:10 -07:00
|
|
|
isc_httpdmgr_t **httpdmgrp) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2020-07-09 19:36:10 -07:00
|
|
|
isc_httpdmgr_t *httpdmgr = NULL;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
2015-07-10 18:42:20 +10:00
|
|
|
REQUIRE(httpdmgrp != NULL && *httpdmgrp == NULL);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2015-07-10 18:42:20 +10:00
|
|
|
httpdmgr = isc_mem_get(mctx, sizeof(isc_httpdmgr_t));
|
2020-07-09 19:36:10 -07:00
|
|
|
*httpdmgr = (isc_httpdmgr_t){ .client_ok = client_ok,
|
2020-02-12 13:59:18 +01:00
|
|
|
.ondestroy = ondestroy,
|
|
|
|
.cb_arg = cb_arg,
|
|
|
|
.render_404 = render_404,
|
|
|
|
.render_500 = render_500 };
|
2020-01-20 12:37:57 +01:00
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&httpdmgr->lock);
|
2015-07-10 18:42:20 +10:00
|
|
|
isc_mem_attach(mctx, &httpdmgr->mctx);
|
|
|
|
|
|
|
|
ISC_LIST_INIT(httpdmgr->running);
|
|
|
|
ISC_LIST_INIT(httpdmgr->urls);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-01-17 18:24:24 +11:00
|
|
|
isc_refcount_init(&httpdmgr->references, 1);
|
|
|
|
|
2025-07-14 17:12:35 +02:00
|
|
|
CHECK(isc_nm_listentcp(ISC_NM_LISTEN_ONE, addr, httpd_newconn, httpdmgr,
|
|
|
|
5, NULL, &httpdmgr->sock));
|
2008-01-17 00:15:14 +00:00
|
|
|
|
2020-01-17 18:24:24 +11:00
|
|
|
httpdmgr->magic = HTTPDMGR_MAGIC;
|
2015-07-10 18:42:20 +10:00
|
|
|
*httpdmgrp = httpdmgr;
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2008-01-17 00:15:14 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2020-01-17 18:24:24 +11:00
|
|
|
httpdmgr->magic = 0;
|
2019-12-05 13:29:45 +11:00
|
|
|
isc_refcount_decrementz(&httpdmgr->references);
|
2020-01-17 18:24:24 +11:00
|
|
|
isc_refcount_destroy(&httpdmgr->references);
|
2015-07-10 18:42:20 +10:00
|
|
|
isc_mem_detach(&httpdmgr->mctx);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&httpdmgr->lock);
|
2015-07-10 18:42:20 +10:00
|
|
|
isc_mem_put(mctx, httpdmgr, sizeof(isc_httpdmgr_t));
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return result;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 12:37:57 +01:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
destroy_httpdmgr(isc_httpdmgr_t *httpdmgr) {
|
2020-01-20 12:37:57 +01:00
|
|
|
isc_refcount_destroy(&httpdmgr->references);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-01-17 18:24:24 +11:00
|
|
|
LOCK(&httpdmgr->lock);
|
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
REQUIRE((httpdmgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) != 0);
|
|
|
|
REQUIRE(ISC_LIST_EMPTY(httpdmgr->running));
|
2020-01-17 18:24:24 +11:00
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
httpdmgr->magic = 0;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
if (httpdmgr->sock != NULL) {
|
|
|
|
isc_nmsocket_close(&httpdmgr->sock);
|
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear out the list of all actions we know about. Just free the
|
|
|
|
* memory.
|
|
|
|
*/
|
2025-08-19 07:14:45 +02:00
|
|
|
ISC_LIST_FOREACH(httpdmgr->urls, url, link) {
|
2006-12-21 06:03:37 +00:00
|
|
|
isc_mem_free(httpdmgr->mctx, url->url);
|
|
|
|
ISC_LIST_UNLINK(httpdmgr->urls, url, link);
|
|
|
|
isc_mem_put(httpdmgr->mctx, url, sizeof(isc_httpdurl_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&httpdmgr->lock);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&httpdmgr->lock);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-01-17 18:24:24 +11:00
|
|
|
if (httpdmgr->ondestroy != NULL) {
|
2008-01-17 00:15:14 +00:00
|
|
|
(httpdmgr->ondestroy)(httpdmgr->cb_arg);
|
2020-01-17 18:24:24 +11:00
|
|
|
}
|
|
|
|
isc_mem_putanddetach(&httpdmgr->mctx, httpdmgr, sizeof(isc_httpdmgr_t));
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
#if ISC_HTTPD_TRACE
|
|
|
|
ISC_REFCOUNT_TRACE_IMPL(isc_httpdmgr, destroy_httpdmgr)
|
|
|
|
#else
|
|
|
|
ISC_REFCOUNT_IMPL(isc_httpdmgr, destroy_httpdmgr);
|
|
|
|
#endif
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
static bool
|
|
|
|
name_match(const struct phr_header *header, const char *match) {
|
|
|
|
size_t match_len = strlen(match);
|
|
|
|
if (match_len != header->name_len) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return false;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
2024-11-19 10:38:03 +01:00
|
|
|
return strncasecmp(header->name, match, match_len) == 0;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2022-10-06 12:56:25 +02:00
|
|
|
value_match(const struct phr_header *header, const char *match) {
|
|
|
|
size_t match_len = strlen(match);
|
|
|
|
size_t limit;
|
|
|
|
|
|
|
|
if (match_len > header->value_len) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return false;
|
2016-03-02 11:04:59 +11:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
limit = header->value_len - match_len + 1;
|
2016-03-05 19:50:42 -08:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
for (size_t i = 0; i < limit; i++) {
|
2023-09-20 17:23:28 +02:00
|
|
|
if (isspace((unsigned char)header->value[i])) {
|
|
|
|
while (i < limit &&
|
|
|
|
isspace((unsigned char)header->value[i]))
|
|
|
|
{
|
2022-10-06 12:56:25 +02:00
|
|
|
i++;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-03-02 11:04:59 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
if (strncasecmp(&header->value[i], match, match_len) == 0) {
|
|
|
|
i += match_len;
|
2016-03-02 11:04:59 +11:00
|
|
|
/*
|
2022-10-06 12:56:25 +02:00
|
|
|
* Sanity check; f.e. for 'deflate' match only
|
|
|
|
* 'deflate[,;]', but not 'deflateyou'
|
2016-03-02 11:04:59 +11:00
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
if (i == header->value_len || header->value[i] == ',' ||
|
|
|
|
header->value[i] == ';')
|
|
|
|
{
|
2024-11-19 10:38:03 +01:00
|
|
|
return true;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-03-02 11:04:59 +11:00
|
|
|
}
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
while (i < limit && header->value[i] != ',') {
|
|
|
|
i++;
|
|
|
|
}
|
2016-03-02 11:04:59 +11:00
|
|
|
}
|
2024-11-19 10:38:03 +01:00
|
|
|
return false;
|
2016-03-02 11:04:59 +11:00
|
|
|
}
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
static isc_result_t
|
2022-10-06 12:56:25 +02:00
|
|
|
process_request(isc_httpd_t *httpd, size_t last_len) {
|
|
|
|
int pret;
|
|
|
|
const char *method = NULL;
|
|
|
|
size_t method_len = 0;
|
|
|
|
const char *path;
|
|
|
|
size_t path_len = 0;
|
|
|
|
struct phr_header headers[HTTP_HEADERS_NUM];
|
|
|
|
size_t num_headers;
|
|
|
|
isc_result_t result;
|
2021-10-22 15:58:46 -07:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
num_headers = ARRAY_SIZE(headers);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
pret = phr_parse_request(httpd->recvbuf, httpd->recvlen, &method,
|
|
|
|
&method_len, &path, &path_len,
|
|
|
|
&httpd->minor_version, headers, &num_headers,
|
|
|
|
last_len);
|
|
|
|
|
|
|
|
if (pret == -1) {
|
|
|
|
/* Parse Error */
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_UNEXPECTED;
|
2022-10-06 12:56:25 +02:00
|
|
|
} else if (pret == -2) {
|
|
|
|
/* Need more data */
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_NOMORE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
INSIST(pret > 0);
|
|
|
|
|
2022-10-18 21:46:16 +02:00
|
|
|
if (pret > HTTP_MAX_REQUEST_LEN) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_RANGE;
|
2022-10-18 21:46:16 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd->consume = pret;
|
2016-03-02 11:04:59 +11:00
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
/*
|
|
|
|
* Determine if this is a POST or GET method. Any other values will
|
|
|
|
* cause an error to be returned.
|
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
if (strncmp(method, "GET ", method_len) == 0) {
|
2020-07-09 19:36:10 -07:00
|
|
|
httpd->method = METHOD_GET;
|
2022-10-06 12:56:25 +02:00
|
|
|
} else if (strncmp(method, "POST ", method_len) == 0) {
|
2020-07-09 19:36:10 -07:00
|
|
|
httpd->method = METHOD_POST;
|
2006-12-21 06:03:37 +00:00
|
|
|
} else {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_RANGE;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-10-06 12:56:25 +02:00
|
|
|
* Parse the URL
|
2006-12-21 06:03:37 +00:00
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
result = isc_url_parse(path, path_len, 0, &httpd->up);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd->path = path;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2023-06-07 15:47:47 +01:00
|
|
|
/*
|
|
|
|
* Examine headers that can affect this request's response
|
|
|
|
*/
|
|
|
|
httpd->flags = 0;
|
|
|
|
|
2023-06-09 09:33:57 +01:00
|
|
|
size_t content_len = 0;
|
2022-10-06 12:56:25 +02:00
|
|
|
bool keep_alive = false;
|
2023-06-07 15:47:47 +01:00
|
|
|
bool host_header = false;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
isc_time_set(&httpd->if_modified_since, 0, 0);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
for (size_t i = 0; i < num_headers; i++) {
|
|
|
|
struct phr_header *header = &headers[i];
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
if (name_match(header, "Content-Length")) {
|
|
|
|
char *endptr;
|
2023-06-09 09:33:57 +01:00
|
|
|
long val = strtol(header->value, &endptr, 10);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
/* ensure we consumed all digits */
|
2022-10-06 12:56:25 +02:00
|
|
|
if ((header->value + header->value_len) != endptr) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_BADNUMBER;
|
2023-06-09 09:33:57 +01:00
|
|
|
}
|
|
|
|
/* ensure there was no minus sign */
|
|
|
|
if (val < 0) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_BADNUMBER;
|
2023-06-09 09:33:57 +01:00
|
|
|
}
|
|
|
|
/* ensure it did not overflow */
|
|
|
|
if (errno != 0) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_RANGE;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
2023-06-09 09:33:57 +01:00
|
|
|
content_len = val;
|
2022-10-06 12:56:25 +02:00
|
|
|
} else if (name_match(header, "Connection")) {
|
2023-06-07 15:47:47 +01:00
|
|
|
/* multiple fields in a connection header are allowed */
|
2022-10-06 12:56:25 +02:00
|
|
|
if (value_match(header, "close")) {
|
2023-06-07 15:47:47 +01:00
|
|
|
httpd->flags |= CONNECTION_CLOSE;
|
|
|
|
}
|
|
|
|
if (value_match(header, "keep-alive")) {
|
2022-10-06 12:56:25 +02:00
|
|
|
keep_alive = true;
|
|
|
|
}
|
|
|
|
} else if (name_match(header, "Host")) {
|
2023-06-07 15:47:47 +01:00
|
|
|
host_header = true;
|
2022-10-06 12:56:25 +02:00
|
|
|
} else if (name_match(header, "Accept-Encoding")) {
|
|
|
|
if (value_match(header, "deflate")) {
|
2023-06-07 15:47:47 +01:00
|
|
|
httpd->flags |= ACCEPT_DEFLATE;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
2023-06-06 18:06:43 +01:00
|
|
|
} else if (name_match(header, "If-Modified-Since") &&
|
|
|
|
header->value_len < ISC_FORMATHTTPTIMESTAMP_SIZE)
|
|
|
|
{
|
2022-10-06 12:56:25 +02:00
|
|
|
char timestamp[ISC_FORMATHTTPTIMESTAMP_SIZE + 1];
|
|
|
|
memmove(timestamp, header->value, header->value_len);
|
|
|
|
timestamp[header->value_len] = 0;
|
|
|
|
|
|
|
|
/* Ignore the value if it can't be parsed */
|
|
|
|
(void)isc_time_parsehttptimestamp(
|
|
|
|
timestamp, &httpd->if_modified_since);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
|
2016-03-02 11:04:59 +11:00
|
|
|
/*
|
2022-10-06 12:56:25 +02:00
|
|
|
* The Content-Length is optional in an HTTP request.
|
|
|
|
* For a GET the length must be zero.
|
2016-03-02 11:04:59 +11:00
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
if (httpd->method == METHOD_GET && content_len != 0) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_BADNUMBER;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-07-20 10:18:56 +00:00
|
|
|
|
2023-06-09 09:33:57 +01:00
|
|
|
if (content_len >= HTTP_MAX_REQUEST_LEN) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_RANGE;
|
2022-07-20 10:18:56 +00:00
|
|
|
}
|
2023-06-09 09:33:57 +01:00
|
|
|
|
|
|
|
size_t consume = httpd->consume + content_len;
|
|
|
|
if (consume > httpd->recvlen) {
|
2022-10-06 12:56:25 +02:00
|
|
|
/* The request data isn't complete yet. */
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_NOMORE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
/* Consume the request's data, which we do not use. */
|
2023-06-09 09:33:57 +01:00
|
|
|
httpd->consume = consume;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
switch (httpd->minor_version) {
|
|
|
|
case 0:
|
2023-06-07 15:47:47 +01:00
|
|
|
/*
|
|
|
|
* RFC 9112 section 9.3 says close takes priority if
|
|
|
|
* keep-alive is also present
|
|
|
|
*/
|
|
|
|
if ((httpd->flags & CONNECTION_CLOSE) == 0 && keep_alive) {
|
|
|
|
httpd->flags |= CONNECTION_KEEP_ALIVE;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2023-06-07 15:47:47 +01:00
|
|
|
httpd->flags |= CONNECTION_CLOSE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-06-07 15:47:47 +01:00
|
|
|
if (!host_header) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_RANGE;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_UNEXPECTED;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2021-10-22 15:58:46 -07:00
|
|
|
/*
|
|
|
|
* Looks like a a valid request, so now we know we won't have
|
|
|
|
* to process this buffer again. We can NULL-terminate the
|
|
|
|
* URL for the caller's benefit, and set recvlen to 0 so
|
|
|
|
* the next read will overwrite this one instead of appending
|
|
|
|
* to the buffer.
|
|
|
|
*/
|
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 12:37:57 +01:00
|
|
|
static void
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
httpd_free(isc_httpd_t *httpd) {
|
2020-07-09 19:36:10 -07:00
|
|
|
isc_httpdmgr_t *httpdmgr = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_HTTPD(httpd));
|
|
|
|
|
|
|
|
httpdmgr = httpd->mgr;
|
|
|
|
|
|
|
|
REQUIRE(VALID_HTTPDMGR(httpdmgr));
|
|
|
|
|
|
|
|
LOCK(&httpdmgr->lock);
|
|
|
|
ISC_LIST_UNLINK(httpdmgr->running, httpd, link);
|
|
|
|
UNLOCK(&httpdmgr->lock);
|
|
|
|
|
|
|
|
httpd->recvbuf[0] = 0;
|
|
|
|
httpd->recvlen = 0;
|
2021-10-26 11:54:31 +11:00
|
|
|
httpd->consume = 0;
|
2020-07-09 19:36:10 -07:00
|
|
|
httpd->method = METHOD_UNKNOWN;
|
|
|
|
httpd->flags = 0;
|
2020-01-20 12:37:57 +01:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd->minor_version = -1;
|
|
|
|
httpd->path = NULL;
|
|
|
|
httpd->up = (isc_url_parser_t){ 0 };
|
|
|
|
isc_time_set(&httpd->if_modified_since, 0, 0);
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
|
|
|
|
httpd->magic = 0;
|
|
|
|
httpd->mgr = NULL;
|
|
|
|
|
|
|
|
isc_mem_put(httpdmgr->mctx, httpd, sizeof(*httpd));
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
isc_httpdmgr_detach(&httpdmgr);
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
|
|
|
|
#if ENABLE_AFL
|
|
|
|
if (finishhook != NULL) {
|
|
|
|
finishhook();
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_AFL */
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
#if ISC_HTTPD_TRACE
|
|
|
|
ISC_REFCOUNT_TRACE_IMPL(isc_httpd, httpd_free)
|
|
|
|
#else
|
|
|
|
ISC_REFCOUNT_IMPL(isc_httpd, httpd_free);
|
|
|
|
#endif
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
static void
|
|
|
|
isc__httpd_sendreq_free(isc_httpd_sendreq_t *req) {
|
|
|
|
/* Clean up buffers */
|
|
|
|
|
|
|
|
isc_buffer_free(&req->sendbuffer);
|
|
|
|
|
|
|
|
isc_mem_putanddetach(&req->mctx, req, sizeof(*req));
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_httpd_sendreq_t *
|
|
|
|
isc__httpd_sendreq_new(isc_httpd_t *httpd) {
|
|
|
|
isc_httpdmgr_t *httpdmgr = httpd->mgr;
|
|
|
|
isc_httpd_sendreq_t *req;
|
|
|
|
|
|
|
|
REQUIRE(VALID_HTTPDMGR(httpdmgr));
|
|
|
|
|
|
|
|
req = isc_mem_get(httpdmgr->mctx, sizeof(*req));
|
|
|
|
*req = (isc_httpd_sendreq_t){ 0 };
|
|
|
|
|
|
|
|
isc_mem_attach(httpdmgr->mctx, &req->mctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the buffer for our headers.
|
|
|
|
*/
|
|
|
|
isc_buffer_allocate(req->mctx, &req->sendbuffer, HTTP_SENDLEN);
|
|
|
|
isc_buffer_clear(req->sendbuffer);
|
|
|
|
|
|
|
|
isc_buffer_initnull(&req->bodybuffer);
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
isc_httpd_attach(httpd, &req->httpd);
|
2023-03-03 16:59:21 +01:00
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return req;
|
2020-07-09 19:36:10 -07:00
|
|
|
}
|
|
|
|
|
2020-10-21 12:52:09 +02:00
|
|
|
static void
|
2020-07-09 19:36:10 -07:00
|
|
|
new_httpd(isc_httpdmgr_t *httpdmgr, isc_nmhandle_t *handle) {
|
|
|
|
isc_httpd_t *httpd = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_HTTPDMGR(httpdmgr));
|
|
|
|
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
httpd = isc_mem_get(httpdmgr->mctx, sizeof(*httpd));
|
|
|
|
*httpd = (isc_httpd_t){
|
|
|
|
.magic = HTTPD_MAGIC,
|
|
|
|
.link = ISC_LINK_INITIALIZER,
|
2024-01-19 10:27:41 +01:00
|
|
|
.references = ISC_REFCOUNT_INITIALIZER(1),
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
};
|
2020-07-09 19:36:10 -07:00
|
|
|
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_nmhandle_attach(handle, &httpd->handle);
|
2020-01-20 12:37:57 +01:00
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
isc_httpdmgr_attach(httpdmgr, &httpd->mgr);
|
2020-07-09 19:36:10 -07:00
|
|
|
|
|
|
|
LOCK(&httpdmgr->lock);
|
|
|
|
ISC_LIST_APPEND(httpdmgr->running, httpd, link);
|
|
|
|
UNLOCK(&httpdmgr->lock);
|
|
|
|
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_nm_read(handle, httpd_request, httpd);
|
2020-01-20 12:37:57 +01:00
|
|
|
}
|
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
static isc_result_t
|
|
|
|
httpd_newconn(isc_nmhandle_t *handle, isc_result_t result, void *arg) {
|
|
|
|
isc_httpdmgr_t *httpdmgr = (isc_httpdmgr_t *)arg;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_sockaddr_t peeraddr;
|
2020-01-20 12:37:57 +01:00
|
|
|
|
|
|
|
REQUIRE(VALID_HTTPDMGR(httpdmgr));
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
if ((httpdmgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) != 0) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_CANCELED;
|
2020-07-09 19:36:10 -07:00
|
|
|
} else if (result == ISC_R_CANCELED) {
|
|
|
|
isc_httpdmgr_shutdown(&httpdmgr);
|
2024-11-19 10:38:03 +01:00
|
|
|
return result;
|
2020-07-09 19:36:10 -07:00
|
|
|
} else if (result != ISC_R_SUCCESS) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return result;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
peeraddr = isc_nmhandle_peeraddr(handle);
|
2008-01-17 00:15:14 +00:00
|
|
|
if (httpdmgr->client_ok != NULL &&
|
2020-02-13 14:44:37 -08:00
|
|
|
!(httpdmgr->client_ok)(&peeraddr, httpdmgr->cb_arg))
|
|
|
|
{
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_FAILURE;
|
2020-01-20 12:37:57 +01:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-10-21 12:52:09 +02:00
|
|
|
new_httpd(httpdmgr, handle);
|
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2022-10-06 12:56:25 +02:00
|
|
|
render_404(const isc_httpd_t *httpd, const isc_httpdurl_t *urlinfo, void *arg,
|
|
|
|
unsigned int *retcode, const char **retmsg, const char **mimetype,
|
|
|
|
isc_buffer_t *b, isc_httpdfree_t **freecb, void **freecb_args) {
|
2015-08-20 09:55:28 +10:00
|
|
|
static char msg[] = "No such URL.\r\n";
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
UNUSED(httpd);
|
2014-01-09 15:14:57 -08:00
|
|
|
UNUSED(urlinfo);
|
2006-12-21 06:03:37 +00:00
|
|
|
UNUSED(arg);
|
|
|
|
|
|
|
|
*retcode = 404;
|
|
|
|
*retmsg = "No such URL";
|
|
|
|
*mimetype = "text/plain";
|
|
|
|
isc_buffer_reinit(b, msg, strlen(msg));
|
|
|
|
isc_buffer_add(b, strlen(msg));
|
|
|
|
*freecb = NULL;
|
|
|
|
*freecb_args = NULL;
|
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 00:57:25 +00:00
|
|
|
static isc_result_t
|
2022-10-06 12:56:25 +02:00
|
|
|
render_500(const isc_httpd_t *httpd, const isc_httpdurl_t *urlinfo, void *arg,
|
|
|
|
unsigned int *retcode, const char **retmsg, const char **mimetype,
|
|
|
|
isc_buffer_t *b, isc_httpdfree_t **freecb, void **freecb_args) {
|
2015-08-20 09:55:28 +10:00
|
|
|
static char msg[] = "Internal server failure.\r\n";
|
2010-02-04 00:57:25 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
UNUSED(httpd);
|
2014-01-09 15:14:57 -08:00
|
|
|
UNUSED(urlinfo);
|
2010-02-04 00:57:25 +00:00
|
|
|
UNUSED(arg);
|
|
|
|
|
|
|
|
*retcode = 500;
|
|
|
|
*retmsg = "Internal server failure";
|
|
|
|
*mimetype = "text/plain";
|
|
|
|
isc_buffer_reinit(b, msg, strlen(msg));
|
|
|
|
isc_buffer_add(b, strlen(msg));
|
|
|
|
*freecb = NULL;
|
|
|
|
*freecb_args = NULL;
|
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2010-02-04 00:57:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 10:45:10 +02:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
/*%<
|
|
|
|
* Tries to compress httpd->bodybuffer to httpd->compbuffer, extending it
|
|
|
|
* if necessary.
|
|
|
|
*
|
|
|
|
* Requires:
|
|
|
|
*\li httpd a valid isc_httpd_t object
|
|
|
|
*
|
|
|
|
* Returns:
|
2015-10-02 18:47:33 -07:00
|
|
|
*\li #ISC_R_SUCCESS -- all is well.
|
|
|
|
*\li #ISC_R_FAILURE -- error during compression or compressed
|
|
|
|
* data would be larger than input data
|
2015-10-02 10:45:10 +02:00
|
|
|
*/
|
|
|
|
static isc_result_t
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_compress(isc_httpd_sendreq_t *req) {
|
2020-02-13 14:44:37 -08:00
|
|
|
z_stream zstr;
|
2022-08-16 16:26:02 -07:00
|
|
|
int ret, inputlen;
|
2015-10-02 10:45:10 +02:00
|
|
|
|
2015-10-02 18:47:33 -07:00
|
|
|
/*
|
|
|
|
* We're setting output buffer size to input size so it fails if the
|
2015-10-02 10:45:10 +02:00
|
|
|
* compressed data size would be bigger than the input size.
|
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
inputlen = isc_buffer_usedlength(&req->bodybuffer);
|
|
|
|
if (inputlen == 0) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_FAILURE;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_buffer_allocate(req->mctx, &req->compbuffer, inputlen);
|
|
|
|
isc_buffer_clear(req->compbuffer);
|
2015-10-02 10:45:10 +02:00
|
|
|
|
2022-08-16 16:26:02 -07:00
|
|
|
zstr = (z_stream){
|
|
|
|
.total_in = inputlen,
|
|
|
|
.avail_out = inputlen,
|
|
|
|
.avail_in = inputlen,
|
2022-10-06 12:56:25 +02:00
|
|
|
.next_in = isc_buffer_base(&req->bodybuffer),
|
|
|
|
.next_out = isc_buffer_base(req->compbuffer),
|
2022-08-16 16:26:02 -07:00
|
|
|
};
|
2015-10-02 10:45:10 +02:00
|
|
|
|
|
|
|
ret = deflateInit(&zstr, Z_DEFAULT_COMPRESSION);
|
|
|
|
if (ret == Z_OK) {
|
|
|
|
ret = deflate(&zstr, Z_FINISH);
|
|
|
|
}
|
|
|
|
deflateEnd(&zstr);
|
|
|
|
if (ret == Z_STREAM_END) {
|
2022-10-06 12:56:25 +02:00
|
|
|
isc_buffer_add(req->compbuffer, zstr.total_out);
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2015-10-02 10:45:10 +02:00
|
|
|
} else {
|
2022-10-06 12:56:25 +02:00
|
|
|
isc_buffer_free(&req->compbuffer);
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_FAILURE;
|
2015-10-02 10:45:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef HAVE_ZLIB */
|
2015-10-02 10:45:10 +02:00
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
static void
|
2023-03-03 16:59:21 +01:00
|
|
|
prepare_response(void *arg) {
|
|
|
|
isc_httpd_sendreq_t *req = arg;
|
|
|
|
isc_httpd_t *httpd = req->httpd;
|
|
|
|
isc_httpdmgr_t *mgr = httpd->mgr;
|
|
|
|
isc_time_t now = isc_time_now();
|
2020-02-13 14:44:37 -08:00
|
|
|
char datebuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
|
2022-10-18 21:46:16 +02:00
|
|
|
const char *path = "/";
|
|
|
|
size_t path_len = 1;
|
|
|
|
bool is_compressed = false;
|
|
|
|
isc_httpdurl_t *url = NULL;
|
|
|
|
isc_result_t result;
|
2020-01-20 12:37:57 +01:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
REQUIRE(VALID_HTTPD(httpd));
|
2023-03-03 16:59:21 +01:00
|
|
|
REQUIRE(req != NULL);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
|
|
|
isc_time_formathttptimestamp(&now, datebuf, sizeof(datebuf));
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
if (httpd->up.field_set & (1 << ISC_UF_PATH)) {
|
|
|
|
path = &httpd->path[httpd->up.field_data[ISC_UF_PATH].off];
|
|
|
|
path_len = httpd->up.field_data[ISC_UF_PATH].len;
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
LOCK(&mgr->lock);
|
2025-08-19 07:14:45 +02:00
|
|
|
ISC_LIST_FOREACH(mgr->urls, u, link) {
|
2025-03-20 15:46:35 -07:00
|
|
|
if (strncmp(path, u->url, path_len) == 0) {
|
|
|
|
url = u;
|
2006-12-21 06:03:37 +00:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
2020-07-09 19:36:10 -07:00
|
|
|
UNLOCK(&mgr->lock);
|
2020-01-17 18:24:24 +11:00
|
|
|
|
|
|
|
if (url == NULL) {
|
2022-10-06 12:56:25 +02:00
|
|
|
result = mgr->render_404(httpd, NULL, NULL, &req->retcode,
|
|
|
|
&req->retmsg, &req->mimetype,
|
|
|
|
&req->bodybuffer, &req->freecb,
|
|
|
|
&req->freecb_arg);
|
2020-01-17 18:24:24 +11:00
|
|
|
} else {
|
2022-10-06 12:56:25 +02:00
|
|
|
result = url->action(httpd, url, url->action_arg, &req->retcode,
|
|
|
|
&req->retmsg, &req->mimetype,
|
|
|
|
&req->bodybuffer, &req->freecb,
|
|
|
|
&req->freecb_arg);
|
2020-01-17 18:24:24 +11:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2022-10-06 12:56:25 +02:00
|
|
|
result = mgr->render_500(httpd, url, NULL, &req->retcode,
|
|
|
|
&req->retmsg, &req->mimetype,
|
|
|
|
&req->bodybuffer, &req->freecb,
|
|
|
|
&req->freecb_arg);
|
2011-03-11 06:11:27 +00:00
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2015-10-02 10:45:10 +02:00
|
|
|
#ifdef HAVE_ZLIB
|
2023-06-07 15:47:47 +01:00
|
|
|
if ((httpd->flags & ACCEPT_DEFLATE) != 0) {
|
2022-10-06 12:56:25 +02:00
|
|
|
result = httpd_compress(req);
|
2020-02-12 13:59:18 +01:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
is_compressed = true;
|
|
|
|
}
|
2015-10-02 10:45:10 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef HAVE_ZLIB */
|
2015-10-02 10:45:10 +02:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_response(httpd, req);
|
2023-06-07 15:47:47 +01:00
|
|
|
/* RFC 9112 § 9.6: SHOULD send Connection: close in last response */
|
|
|
|
if ((httpd->flags & CONNECTION_CLOSE) != 0) {
|
|
|
|
httpd_addheader(req, "Connection", "close");
|
|
|
|
} else if ((httpd->flags & CONNECTION_KEEP_ALIVE) != 0) {
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_addheader(req, "Connection", "Keep-Alive");
|
2020-01-17 18:24:24 +11:00
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_addheader(req, "Content-Type", req->mimetype);
|
|
|
|
httpd_addheader(req, "Date", datebuf);
|
|
|
|
httpd_addheader(req, "Expires", datebuf);
|
2014-01-09 15:14:57 -08:00
|
|
|
|
|
|
|
if (url != NULL && url->isstatic) {
|
2015-02-05 17:18:15 -08:00
|
|
|
char loadbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_time_formathttptimestamp(&url->loadtime, loadbuf,
|
|
|
|
sizeof(loadbuf));
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_addheader(req, "Last-Modified", loadbuf);
|
|
|
|
httpd_addheader(req, "Cache-Control: public", NULL);
|
2014-01-09 15:14:57 -08:00
|
|
|
} else {
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_addheader(req, "Last-Modified", datebuf);
|
|
|
|
httpd_addheader(req, "Pragma: no-cache", NULL);
|
|
|
|
httpd_addheader(req, "Cache-Control: no-cache", NULL);
|
2014-01-09 15:14:57 -08:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_addheader(req, "Server: libisc", NULL);
|
2015-10-02 10:45:10 +02:00
|
|
|
|
2020-03-30 13:47:58 -07:00
|
|
|
if (is_compressed) {
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_addheader(req, "Content-Encoding", "deflate");
|
|
|
|
httpd_addheaderuint(req, "Content-Length",
|
|
|
|
isc_buffer_usedlength(req->compbuffer));
|
2015-10-02 10:45:10 +02:00
|
|
|
} else {
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_addheaderuint(req, "Content-Length",
|
|
|
|
isc_buffer_usedlength(&req->bodybuffer));
|
2015-10-02 10:45:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd_endheaders(req); /* done */
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2018-10-31 13:46:52 +01:00
|
|
|
/*
|
|
|
|
* Append either the compressed or the non-compressed response body to
|
|
|
|
* the response headers and store the result in httpd->sendbuffer.
|
|
|
|
*/
|
2022-10-06 12:56:25 +02:00
|
|
|
if (is_compressed) {
|
|
|
|
isc_buffer_putmem(req->sendbuffer,
|
|
|
|
isc_buffer_base(req->compbuffer),
|
|
|
|
isc_buffer_usedlength(req->compbuffer));
|
|
|
|
isc_buffer_free(&req->compbuffer);
|
|
|
|
} else {
|
|
|
|
isc_buffer_putmem(req->sendbuffer,
|
|
|
|
isc_buffer_base(&req->bodybuffer),
|
|
|
|
isc_buffer_usedlength(&req->bodybuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the bodybuffer */
|
|
|
|
if (req->freecb != NULL && isc_buffer_length(&req->bodybuffer) > 0) {
|
|
|
|
req->freecb(&req->bodybuffer, req->freecb_arg);
|
|
|
|
}
|
2020-07-09 19:36:10 -07:00
|
|
|
|
2021-10-26 11:54:31 +11:00
|
|
|
/* Consume the request from the recv buffer. */
|
2022-10-06 12:56:25 +02:00
|
|
|
INSIST(httpd->consume != 0);
|
|
|
|
INSIST(httpd->consume <= httpd->recvlen);
|
|
|
|
if (httpd->consume < httpd->recvlen) {
|
|
|
|
memmove(httpd->recvbuf, httpd->recvbuf + httpd->consume,
|
|
|
|
httpd->recvlen - httpd->consume);
|
2021-10-26 11:54:31 +11:00
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
httpd->recvlen -= httpd->consume;
|
|
|
|
httpd->consume = 0;
|
2023-03-03 16:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prepare_response_done(void *arg) {
|
|
|
|
isc_region_t r;
|
|
|
|
isc_httpd_sendreq_t *req = arg;
|
|
|
|
isc_httpd_t *httpd = req->httpd;
|
2022-10-06 12:56:25 +02:00
|
|
|
|
|
|
|
/*
|
2023-03-03 16:59:21 +01:00
|
|
|
* Determine total response size.
|
2022-10-06 12:56:25 +02:00
|
|
|
*/
|
2023-03-03 16:59:21 +01:00
|
|
|
isc_buffer_usedregion(req->sendbuffer, &r);
|
2021-10-26 11:54:31 +11:00
|
|
|
|
2023-03-03 16:59:21 +01:00
|
|
|
isc_nm_send(httpd->handle, &r, httpd_senddone, req);
|
2022-10-18 21:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
httpd_request(isc_nmhandle_t *handle, isc_result_t eresult,
|
|
|
|
isc_region_t *region, void *arg) {
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_httpd_t *httpd = arg;
|
|
|
|
isc_httpdmgr_t *mgr = httpd->mgr;
|
2022-10-18 21:46:16 +02:00
|
|
|
size_t last_len = 0;
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_result_t result;
|
2022-10-18 21:46:16 +02:00
|
|
|
|
|
|
|
REQUIRE(VALID_HTTPD(httpd));
|
|
|
|
|
|
|
|
REQUIRE(httpd->handle == handle);
|
|
|
|
|
|
|
|
if (eresult != ISC_R_SUCCESS) {
|
|
|
|
goto close_readhandle;
|
|
|
|
}
|
|
|
|
|
2022-11-24 15:03:23 +01:00
|
|
|
REQUIRE((mgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) == 0);
|
|
|
|
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_nm_read_stop(handle);
|
2022-11-24 15:03:23 +01:00
|
|
|
|
2018-10-31 13:46:52 +01:00
|
|
|
/*
|
2022-10-18 21:46:16 +02:00
|
|
|
* If we are being called from httpd_senddone(), the last HTTP request
|
|
|
|
* was processed successfully, reset the last_len to 0, even if there's
|
|
|
|
* data in the httpd->recvbuf.
|
2018-10-31 13:46:52 +01:00
|
|
|
*/
|
2022-10-18 21:46:16 +02:00
|
|
|
last_len = (region == NULL) ? 0 : httpd->recvlen;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-18 21:46:16 +02:00
|
|
|
/* Store the received data into the recvbuf */
|
|
|
|
if (region != NULL) {
|
|
|
|
if (httpd->recvlen + region->length > sizeof(httpd->recvbuf)) {
|
|
|
|
goto close_readhandle;
|
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
|
2022-10-18 21:46:16 +02:00
|
|
|
memmove(httpd->recvbuf + httpd->recvlen, region->base,
|
|
|
|
region->length);
|
|
|
|
httpd->recvlen += region->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = process_request(httpd, last_len);
|
|
|
|
|
|
|
|
if (result == ISC_R_NOMORE) {
|
|
|
|
if (httpd->recvlen > HTTP_MAX_REQUEST_LEN) {
|
|
|
|
goto close_readhandle;
|
|
|
|
}
|
|
|
|
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
/* Wait for more data, the handle is still attached */
|
2022-10-18 21:46:16 +02:00
|
|
|
isc_nm_read(handle, httpd_request, arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-07 15:47:47 +01:00
|
|
|
/* XXXFANF it would be more polite to reply 400 bad request */
|
2022-10-18 21:46:16 +02:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto close_readhandle;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
|
|
|
|
2023-03-03 16:59:21 +01:00
|
|
|
isc_httpd_sendreq_t *req = isc__httpd_sendreq_new(httpd);
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_nmhandle_ref(handle);
|
2023-03-03 16:59:21 +01:00
|
|
|
isc_work_enqueue(isc_loop(), prepare_response, prepare_response_done,
|
|
|
|
req);
|
2022-03-10 14:39:26 +01:00
|
|
|
return;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-18 21:46:16 +02:00
|
|
|
close_readhandle:
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
isc_nmhandle_close(httpd->handle);
|
|
|
|
isc_nmhandle_detach(&httpd->handle);
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
isc_httpd_detach(&httpd);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_httpdmgr_shutdown(isc_httpdmgr_t **httpdmgrp) {
|
2022-07-26 13:03:45 +02:00
|
|
|
isc_httpdmgr_t *httpdmgr = NULL;
|
2020-01-17 18:24:24 +11:00
|
|
|
|
|
|
|
REQUIRE(httpdmgrp != NULL);
|
2020-07-09 19:36:10 -07:00
|
|
|
REQUIRE(VALID_HTTPDMGR(*httpdmgrp));
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
httpdmgr = *httpdmgrp;
|
|
|
|
*httpdmgrp = NULL;
|
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
isc_nm_stoplistening(httpdmgr->sock);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2020-07-09 19:36:10 -07:00
|
|
|
LOCK(&httpdmgr->lock);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2025-08-19 07:14:45 +02:00
|
|
|
ISC_LIST_FOREACH(httpdmgr->running, httpd, link) {
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
if (httpd->handle != NULL) {
|
|
|
|
httpd_request(httpd->handle, ISC_R_SUCCESS, NULL,
|
|
|
|
httpd);
|
2022-11-24 15:03:23 +01:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
2023-05-23 14:33:28 -07:00
|
|
|
|
|
|
|
httpdmgr->flags |= ISC_HTTPDMGR_SHUTTINGDOWN;
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
UNLOCK(&httpdmgr->lock);
|
|
|
|
|
2022-07-26 13:03:45 +02:00
|
|
|
isc_nmsocket_close(&httpdmgr->sock);
|
|
|
|
|
2024-01-19 10:27:41 +01:00
|
|
|
isc_httpdmgr_detach(&httpdmgr);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
static void
|
|
|
|
httpd_response(isc_httpd_t *httpd, isc_httpd_sendreq_t *req) {
|
2006-12-21 06:03:37 +00:00
|
|
|
isc_result_t result;
|
2020-01-17 18:24:24 +11:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
result = isc_buffer_printf(req->sendbuffer, "HTTP/1.%u %03u %s\r\n",
|
|
|
|
httpd->minor_version, req->retcode,
|
|
|
|
req->retmsg);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
static void
|
|
|
|
httpd_addheader(isc_httpd_sendreq_t *req, const char *name, const char *val) {
|
2006-12-21 06:03:37 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
2017-10-09 11:43:07 +02:00
|
|
|
if (val != NULL) {
|
2022-10-06 12:56:25 +02:00
|
|
|
result = isc_buffer_printf(req->sendbuffer, "%s: %s\r\n", name,
|
|
|
|
val);
|
2017-10-09 11:43:07 +02:00
|
|
|
} else {
|
2022-10-06 12:56:25 +02:00
|
|
|
result = isc_buffer_printf(req->sendbuffer, "%s\r\n", name);
|
2017-10-09 11:43:07 +02:00
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
static void
|
|
|
|
httpd_endheaders(isc_httpd_sendreq_t *req) {
|
2006-12-21 06:03:37 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
result = isc_buffer_printf(req->sendbuffer, "\r\n");
|
2020-01-17 18:24:24 +11:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
static void
|
|
|
|
httpd_addheaderuint(isc_httpd_sendreq_t *req, const char *name, int val) {
|
2006-12-21 06:03:37 +00:00
|
|
|
isc_result_t result;
|
2020-01-17 18:24:24 +11:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
result = isc_buffer_printf(req->sendbuffer, "%s: %d\r\n", name, val);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2022-10-06 12:56:25 +02:00
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-10-18 21:46:16 +02:00
|
|
|
httpd_senddone(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) {
|
2022-10-06 12:56:25 +02:00
|
|
|
isc_httpd_sendreq_t *req = (isc_httpd_sendreq_t *)arg;
|
|
|
|
isc_httpd_t *httpd = req->httpd;
|
2020-01-17 18:24:24 +11:00
|
|
|
|
|
|
|
REQUIRE(VALID_HTTPD(httpd));
|
2022-03-10 14:39:26 +01:00
|
|
|
|
2022-11-24 15:03:23 +01:00
|
|
|
if ((httpd->mgr->flags & ISC_HTTPDMGR_SHUTTINGDOWN) != 0) {
|
|
|
|
goto detach;
|
|
|
|
}
|
|
|
|
|
2023-06-07 15:47:47 +01:00
|
|
|
if (eresult == ISC_R_SUCCESS && (httpd->flags & CONNECTION_CLOSE) != 0)
|
|
|
|
{
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
eresult = ISC_R_EOF;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
/*
|
2023-06-07 15:47:47 +01:00
|
|
|
* Calling httpd_request() with region NULL restarts reading.
|
Fix the streaming read callback shutdown logic
When shutting down TCP sockets, the read callback calling logic was
flawed, it would call either one less callback or one extra. Fix the
logic in the way:
1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on
the handle, the read callback will be called with ISC_R_CANCELED to
cancel active reading from the socket/handle.
2. When isc_nm_read() has been called and isc_nm_read_stop() has been
called on the on the handle, the read callback will be called with
ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket
is being shut down.
3. The .reading and .recv_read flags are little bit tricky. The
.reading flag indicates if the outer layer is reading the data (that
would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream),
the .recv_read flag indicates whether somebody is interested in the
data read from the socket.
Usually, you would expect that the .reading should be false when
.recv_read is false, but it gets even more tricky with TLSStream as
the TLS protocol might need to read from the socket even when sending
data.
Fix the usage of the .recv_read and .reading flags in the TLSStream
to their true meaning - which mostly consist of using .recv_read
everywhere and then wrapping isc_nm_read() and isc_nm_read_stop()
with the .reading flag.
4. The TLS failed read helper has been modified to resemble the TCP code
as much as possible, clearing and re-setting the .recv_read flag in
the TCP timeout code has been fixed and .recv_read is now cleared
when isc_nm_read_stop() has been called on the streaming socket.
5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and
isc_httpd units have been greatly simplified due to the improved design.
6. More unit tests for TCP and TLS testing the shutdown conditions have
been added.
Co-authored-by: Ondřej Surý <ondrej@isc.org>
Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
|
|
|
*/
|
|
|
|
httpd_request(handle, eresult, NULL, httpd);
|
|
|
|
|
2022-11-24 15:03:23 +01:00
|
|
|
detach:
|
2022-10-06 12:56:25 +02:00
|
|
|
isc_nmhandle_detach(&handle);
|
|
|
|
isc__httpd_sendreq_free(req);
|
2024-01-19 10:27:41 +01:00
|
|
|
isc_httpd_detach(&httpd);
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-07-09 19:36:10 -07:00
|
|
|
isc_httpdmgr_addurl(isc_httpdmgr_t *httpdmgr, const char *url, bool isstatic,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_httpdaction_t *func, void *arg) {
|
2006-12-21 06:03:37 +00:00
|
|
|
isc_httpdurl_t *item;
|
|
|
|
|
2020-01-17 18:24:24 +11:00
|
|
|
REQUIRE(VALID_HTTPDMGR(httpdmgr));
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
if (url == NULL) {
|
|
|
|
httpdmgr->render_404 = func;
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpdurl_t));
|
|
|
|
|
|
|
|
item->url = isc_mem_strdup(httpdmgr->mctx, url);
|
|
|
|
|
|
|
|
item->action = func;
|
|
|
|
item->action_arg = arg;
|
2014-01-09 15:14:57 -08:00
|
|
|
item->isstatic = isstatic;
|
2023-03-31 00:12:33 +02:00
|
|
|
item->loadtime = isc_time_now();
|
2014-01-09 15:14:57 -08:00
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
ISC_LINK_INIT(item, link);
|
2020-01-17 18:24:24 +11:00
|
|
|
|
|
|
|
LOCK(&httpdmgr->lock);
|
2006-12-21 06:03:37 +00:00
|
|
|
ISC_LIST_APPEND(httpdmgr->urls, item, link);
|
2020-01-17 18:24:24 +11:00
|
|
|
UNLOCK(&httpdmgr->lock);
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2024-11-19 10:38:03 +01:00
|
|
|
return ISC_R_SUCCESS;
|
2006-12-21 06:03:37 +00:00
|
|
|
}
|
2016-05-05 11:46:11 +02:00
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_httpd_setfinishhook(void (*fn)(void)) {
|
2020-01-20 12:37:57 +01:00
|
|
|
#if ENABLE_AFL
|
2016-05-05 11:46:11 +02:00
|
|
|
finishhook = fn;
|
2020-02-12 13:59:18 +01:00
|
|
|
#else /* ENABLE_AFL */
|
2020-01-20 12:37:57 +01:00
|
|
|
UNUSED(fn);
|
|
|
|
#endif /* ENABLE_AFL */
|
2016-05-05 11:46:11 +02:00
|
|
|
}
|
2022-10-06 12:56:25 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
isc_httpdurl_isstatic(const isc_httpdurl_t *url) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return url->isstatic;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const isc_time_t *
|
|
|
|
isc_httpdurl_loadtime(const isc_httpdurl_t *url) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return &url->loadtime;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const isc_time_t *
|
|
|
|
isc_httpd_if_modified_since(const isc_httpd_t *httpd) {
|
2024-11-19 10:38:03 +01:00
|
|
|
return (const isc_time_t *)&httpd->if_modified_since;
|
2022-10-06 12:56:25 +02:00
|
|
|
}
|