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