2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* Very basic HTTP server */
12 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
14 * On VMS, you need to define this to get the declaration of fileno(). The
15 * value 2 is to make sure no function defined in POSIX-2 is left undefined.
17 # define _POSIX_C_SOURCE 2
21 #include "http_server.h"
22 #include "internal/sockets.h"
23 #include <openssl/err.h>
24 #include <openssl/trace.h>
25 #include <openssl/rand.h>
30 # if defined(OPENSSL_TANDEM_FLOSS)
31 # include <floss.h(floss_fork)>
35 #define HTTP_PREFIX "HTTP/"
36 #define HTTP_VERSION_PATT "1." /* allow 1.x */
37 #define HTTP_PREFIX_VERSION HTTP_PREFIX""HTTP_VERSION_PATT
38 #define HTTP_1_0 HTTP_PREFIX_VERSION"0" /* "HTTP/1.0" */
39 #define HTTP_VERSION_STR " "HTTP_PREFIX_VERSION
41 #define log_HTTP(prog, level, text) \
42 trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, "%s", text)
43 #define log_HTTP1(prog, level, fmt, arg) \
44 trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, fmt, arg)
45 #define log_HTTP2(prog, level, fmt, arg1, arg2) \
46 trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, fmt, arg1, arg2)
47 #define log_HTTP3(prog, level, fmt, a1, a2, a3) \
48 trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, fmt, a1, a2, a3)
51 int n_responders = 0; /* run multiple responder processes, set by ocsp.c */
52 int acfd = (int)INVALID_SOCKET;
54 void socket_timeout(int signum)
56 if (acfd != (int)INVALID_SOCKET)
57 (void)shutdown(acfd, SHUT_RD);
60 static void killall(int ret, pid_t *kidpids)
64 for (i = 0; i < n_responders; ++i)
66 (void)kill(kidpids[i], SIGTERM);
67 OPENSSL_free(kidpids);
72 static int termsig = 0;
74 static void noteterm(int sig)
80 * Loop spawning up to `multi` child processes, only child processes return
81 * from this function. The parent process loops until receiving a termination
82 * signal, kills extant children and exits without returning.
84 void spawn_loop(const char *prog)
86 pid_t *kidpids = NULL;
91 openlog(prog, LOG_PID, LOG_DAEMON);
94 log_HTTP1(prog, LOG_CRIT,
95 "error detaching from parent process group: %s",
99 kidpids = app_malloc(n_responders * sizeof(*kidpids), "child PID array");
100 for (i = 0; i < n_responders; ++i)
103 signal(SIGINT, noteterm);
104 signal(SIGTERM, noteterm);
106 while (termsig == 0) {
110 * Wait for a child to replace when we're at the limit.
111 * Slow down if a child exited abnormally or waitpid() < 0
113 while (termsig == 0 && procs >= n_responders) {
114 if ((fpid = waitpid(-1, &status, 0)) > 0) {
115 for (i = 0; i < procs; ++i) {
116 if (kidpids[i] == fpid) {
122 if (i >= n_responders) {
123 log_HTTP1(prog, LOG_CRIT,
124 "internal error: no matching child slot for pid: %ld",
129 if (WIFEXITED(status)) {
130 log_HTTP2(prog, LOG_WARNING,
131 "child process: %ld, exit status: %d",
132 (long)fpid, WEXITSTATUS(status));
133 } else if (WIFSIGNALED(status)) {
137 if (WCOREDUMP(status))
138 dumped = " (core dumped)";
140 log_HTTP3(prog, LOG_WARNING,
141 "child process: %ld, term signal %d%s",
142 (long)fpid, WTERMSIG(status), dumped);
147 } else if (errno != EINTR) {
148 log_HTTP1(prog, LOG_CRIT,
149 "waitpid() failed: %s", strerror(errno));
156 switch (fpid = fork()) {
158 /* System critically low on memory, pause and try again later */
162 OPENSSL_free(kidpids);
163 signal(SIGINT, SIG_DFL);
164 signal(SIGTERM, SIG_DFL);
167 if (RAND_poll() <= 0) {
168 log_HTTP(prog, LOG_CRIT, "RAND_poll() failed");
172 default: /* parent */
173 for (i = 0; i < n_responders; ++i) {
174 if (kidpids[i] == 0) {
180 if (i >= n_responders) {
181 log_HTTP(prog, LOG_CRIT,
182 "internal error: no free child slots");
189 /* The loop above can only break on termsig */
190 log_HTTP1(prog, LOG_INFO, "terminating on signal: %d", termsig);
195 #ifndef OPENSSL_NO_SOCK
196 BIO *http_server_init(const char *prog, const char *port, int verb)
198 BIO *acbio = NULL, *bufbio;
203 snprintf(name, sizeof(name), "*:%s", port); /* port may be "0" */
204 if (verb >= 0 && !log_set_verbosity(prog, verb))
206 bufbio = BIO_new(BIO_f_buffer());
209 acbio = BIO_new(BIO_s_accept());
211 || BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
212 || BIO_set_accept_name(acbio, name) < 0) {
213 log_HTTP(prog, LOG_ERR, "error setting up accept BIO");
217 BIO_set_accept_bios(acbio, bufbio);
219 if (BIO_do_accept(acbio) <= 0) {
220 log_HTTP1(prog, LOG_ERR, "error setting accept on port %s", port);
224 /* Report back what address and port are used */
225 BIO_get_fd(acbio, &asock);
226 port_num = report_server_accept(bio_out, asock, 1, 1);
228 log_HTTP(prog, LOG_ERR, "error printing ACCEPT string");
235 ERR_print_errors(bio_err);
242 * Decode %xx URL-decoding in-place. Ignores malformed sequences.
244 static int urldecode(char *p)
246 unsigned char *out = (unsigned char *)p;
247 unsigned char *save = out;
252 } else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
253 /* Don't check, can't fail because of ixdigit() call. */
254 *out++ = (OPENSSL_hexchar2int(p[1]) << 4)
255 | OPENSSL_hexchar2int(p[2]);
262 return (int)(out - save);
265 /* if *pcbio != NULL, continue given connected session, else accept new */
266 /* if found_keep_alive != NULL, return this way connection persistence state */
267 int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq,
268 char **ppath, BIO **pcbio, BIO *acbio,
269 int *found_keep_alive,
270 const char *prog, int accept_get, int timeout)
272 BIO *cbio = *pcbio, *getbio = NULL, *b64 = NULL;
274 char reqbuf[2048], inbuf[2048];
275 char *meth, *url, *end;
286 get_sock_info_address(BIO_get_fd(acbio, NULL), NULL, &port);
288 log_HTTP(prog, LOG_ERR, "cannot get port listening on");
291 log_HTTP1(prog, LOG_DEBUG,
292 "awaiting new connection on port %s ...", port);
295 if (BIO_do_accept(acbio) <= 0)
296 /* Connection loss before accept() is routine, ignore silently */
299 *pcbio = cbio = BIO_pop(acbio);
301 log_HTTP(prog, LOG_DEBUG, "awaiting next request ...");
304 /* Cannot call http_server_send_status(..., cbio, ...) */
311 (void)BIO_get_fd(cbio, &acfd);
316 /* Read the request line. */
317 len = BIO_gets(cbio, reqbuf, sizeof(reqbuf));
322 log_HTTP(prog, LOG_WARNING, "request line read error");
323 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
327 if (((end = strchr(reqbuf, '\r')) != NULL && end[1] == '\n')
328 || (end = strchr(reqbuf, '\n')) != NULL)
330 if (log_get_verbosity() < LOG_TRACE)
331 trace_log_message(-1, prog, LOG_INFO,
332 "received request, 1st line: %s", reqbuf);
333 log_HTTP(prog, LOG_TRACE, "received request header:");
334 log_HTTP1(prog, LOG_TRACE, "%s", reqbuf);
336 log_HTTP(prog, LOG_WARNING,
337 "cannot parse HTTP header: missing end of line");
338 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
343 if ((accept_get && CHECK_AND_SKIP_PREFIX(url, "GET "))
344 || CHECK_AND_SKIP_PREFIX(url, "POST ")) {
346 /* Expecting (GET|POST) {sp} /URL {sp} HTTP/1.x */
351 log_HTTP2(prog, LOG_WARNING,
352 "invalid %s -- URL does not begin with '/': %s",
354 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
359 /* Splice off the HTTP version identifier. */
360 for (end = url; *end != '\0'; end++)
363 if (!HAS_PREFIX(end, HTTP_VERSION_STR)) {
364 log_HTTP2(prog, LOG_WARNING,
365 "invalid %s -- bad HTTP/version string: %s",
367 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
371 /* above HTTP 1.0, connection persistence is the default */
372 if (found_keep_alive != NULL)
373 *found_keep_alive = end[sizeof(HTTP_VERSION_STR) - 1] > '0';
376 * Skip "GET / HTTP..." requests often used by load-balancers.
377 * 'url' was incremented above to point to the first byte *after*
378 * the leading slash, so in case 'GET / ' it is now an empty string.
380 if (strlen(meth) == 3 && url[0] == '\0') {
381 (void)http_server_send_status(prog, cbio, 200, "OK");
385 len = urldecode(url);
387 log_HTTP2(prog, LOG_WARNING,
388 "invalid %s request -- bad URL encoding: %s", meth, url);
389 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
392 if (strlen(meth) == 3) { /* GET */
393 if ((getbio = BIO_new_mem_buf(url, len)) == NULL
394 || (b64 = BIO_new(BIO_f_base64())) == NULL) {
395 log_HTTP1(prog, LOG_ERR,
396 "could not allocate base64 bio with size = %d", len);
399 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
400 getbio = BIO_push(b64, getbio);
403 log_HTTP2(prog, LOG_WARNING,
404 "HTTP request does not begin with %sPOST: %s",
405 accept_get ? "GET or " : "", reqbuf);
406 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
410 /* chop any further/duplicate leading or trailing '/' */
413 while (end >= url + 2 && end[-2] == '/' && end[-1] == '/')
417 /* Read and skip past the headers. */
421 len = BIO_gets(cbio, inbuf, sizeof(inbuf));
423 log_HTTP(prog, LOG_WARNING, "error reading HTTP header");
424 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
428 if (((end = strchr(inbuf, '\r')) != NULL && end[1] == '\n')
429 || (end = strchr(inbuf, '\n')) != NULL)
431 log_HTTP1(prog, LOG_TRACE, "%s", *inbuf == '\0' ?
432 " " /* workaround for "" getting ignored */ : inbuf);
434 log_HTTP(prog, LOG_WARNING,
435 "error parsing HTTP header: missing end of line");
436 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
440 if (inbuf[0] == '\0')
444 value = strchr(key, ':');
446 log_HTTP(prog, LOG_WARNING,
447 "error parsing HTTP header: missing ':'");
448 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
452 while (*value == ' ')
454 /* https://tools.ietf.org/html/rfc7230#section-6.3 Persistence */
455 if (found_keep_alive != NULL
456 && OPENSSL_strcasecmp(key, "Connection") == 0) {
457 if (OPENSSL_strcasecmp(value, "keep-alive") == 0)
458 *found_keep_alive = 1;
459 else if (OPENSSL_strcasecmp(value, "close") == 0)
460 *found_keep_alive = 0;
465 /* Clear alarm before we close the client socket */
470 /* Try to read and parse request */
471 req = ASN1_item_d2i_bio(it, getbio != NULL ? getbio : cbio, NULL);
473 log_HTTP(prog, LOG_WARNING,
474 "error parsing DER-encoded request content");
475 (void)http_server_send_status(prog, cbio, 400, "Bad Request");
476 } else if (ppath != NULL && (*ppath = OPENSSL_strdup(url)) == NULL) {
477 log_HTTP1(prog, LOG_ERR,
478 "out of memory allocating %zu bytes", strlen(url) + 1);
479 ASN1_item_free(req, it);
486 BIO_free_all(getbio);
490 acfd = (int)INVALID_SOCKET;
495 (void)http_server_send_status(prog, cbio, 500, "Internal Server Error");
497 OPENSSL_free(*ppath);
506 /* assumes that cbio does not do an encoding that changes the output length */
507 int http_server_send_asn1_resp(const char *prog, BIO *cbio, int keep_alive,
508 const char *content_type,
509 const ASN1_ITEM *it, const ASN1_VALUE *resp)
512 int ret = BIO_snprintf(buf, sizeof(buf), HTTP_1_0" 200 OK\r\n%s"
513 "Content-type: %s\r\n"
514 "Content-Length: %d\r\n",
515 keep_alive ? "Connection: keep-alive\r\n" : "",
517 ASN1_item_i2d(resp, NULL, it));
519 if (ret < 0 || (size_t)ret >= sizeof(buf))
521 if (log_get_verbosity() < LOG_TRACE && (p = strchr(buf, '\r')) != NULL)
522 trace_log_message(-1, prog, LOG_INFO,
523 "sending response, 1st line: %.*s", (int)(p - buf),
525 log_HTTP1(prog, LOG_TRACE, "sending response header:\n%s", buf);
527 ret = BIO_printf(cbio, "%s\r\n", buf) > 0
528 && ASN1_item_i2d_bio(it, cbio, resp) > 0;
530 (void)BIO_flush(cbio);
534 int http_server_send_status(const char *prog, BIO *cbio,
535 int status, const char *reason)
538 int ret = BIO_snprintf(buf, sizeof(buf), HTTP_1_0" %d %s\r\n\r\n",
539 /* This implicitly cancels keep-alive */
542 if (ret < 0 || (size_t)ret >= sizeof(buf))
544 log_HTTP1(prog, LOG_TRACE, "sending response header:\n%s", buf);
546 ret = BIO_printf(cbio, "%s\r\n", buf) > 0;
547 (void)BIO_flush(cbio);