2020-05-05 02:29:25 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2020-05-05 02:29:25 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Very basic HTTP server */
|
|
|
|
|
|
|
|
#if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
|
|
|
|
/*
|
|
|
|
* On VMS, you need to define this to get the declaration of fileno(). The
|
|
|
|
* value 2 is to make sure no function defined in POSIX-2 is left undefined.
|
|
|
|
*/
|
|
|
|
# define _POSIX_C_SOURCE 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "http_server.h"
|
|
|
|
#include "internal/sockets.h"
|
|
|
|
#include <openssl/err.h>
|
2022-05-29 00:44:02 +08:00
|
|
|
#include <openssl/trace.h>
|
2020-05-05 02:29:25 +08:00
|
|
|
#include <openssl/rand.h>
|
2021-05-14 18:25:11 +08:00
|
|
|
#include "s_apps.h"
|
2022-05-29 02:26:43 +08:00
|
|
|
#include "log.h"
|
2020-05-05 02:29:25 +08:00
|
|
|
|
2020-09-07 05:37:47 +08:00
|
|
|
#if defined(__TANDEM)
|
|
|
|
# if defined(OPENSSL_TANDEM_FLOSS)
|
|
|
|
# include <floss.h(floss_fork)>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
#define HTTP_PREFIX "HTTP/"
|
|
|
|
#define HTTP_VERSION_PATT "1." /* allow 1.x */
|
|
|
|
#define HTTP_PREFIX_VERSION HTTP_PREFIX""HTTP_VERSION_PATT
|
|
|
|
#define HTTP_1_0 HTTP_PREFIX_VERSION"0" /* "HTTP/1.0" */
|
2021-06-21 14:55:50 +08:00
|
|
|
#define HTTP_VERSION_STR " "HTTP_PREFIX_VERSION
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
|
2022-05-29 00:44:02 +08:00
|
|
|
#define log_HTTP(prog, level, text) \
|
|
|
|
trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, "%s", text)
|
|
|
|
#define log_HTTP1(prog, level, fmt, arg) \
|
|
|
|
trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, fmt, arg)
|
|
|
|
#define log_HTTP2(prog, level, fmt, arg1, arg2) \
|
|
|
|
trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, fmt, arg1, arg2)
|
2022-05-29 02:26:43 +08:00
|
|
|
#define log_HTTP3(prog, level, fmt, a1, a2, a3) \
|
|
|
|
trace_log_message(OSSL_TRACE_CATEGORY_HTTP, prog, level, fmt, a1, a2, a3)
|
2022-05-29 00:44:02 +08:00
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
#ifdef HTTP_DAEMON
|
2022-05-29 02:26:43 +08:00
|
|
|
int n_responders = 0; /* run multiple responder processes, set by ocsp.c */
|
|
|
|
int acfd = (int)INVALID_SOCKET;
|
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
void socket_timeout(int signum)
|
|
|
|
{
|
|
|
|
if (acfd != (int)INVALID_SOCKET)
|
|
|
|
(void)shutdown(acfd, SHUT_RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void killall(int ret, pid_t *kidpids)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-05-29 00:50:18 +08:00
|
|
|
for (i = 0; i < n_responders; ++i)
|
2020-05-05 02:29:25 +08:00
|
|
|
if (kidpids[i] != 0)
|
|
|
|
(void)kill(kidpids[i], SIGTERM);
|
|
|
|
OPENSSL_free(kidpids);
|
2022-10-03 13:22:52 +08:00
|
|
|
OSSL_sleep(1000);
|
2020-05-05 02:29:25 +08:00
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int termsig = 0;
|
|
|
|
|
|
|
|
static void noteterm(int sig)
|
|
|
|
{
|
|
|
|
termsig = sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop spawning up to `multi` child processes, only child processes return
|
|
|
|
* from this function. The parent process loops until receiving a termination
|
|
|
|
* signal, kills extant children and exits without returning.
|
|
|
|
*/
|
|
|
|
void spawn_loop(const char *prog)
|
|
|
|
{
|
|
|
|
pid_t *kidpids = NULL;
|
|
|
|
int status;
|
|
|
|
int procs = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
openlog(prog, LOG_PID, LOG_DAEMON);
|
|
|
|
|
|
|
|
if (setpgid(0, 0)) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP1(prog, LOG_CRIT,
|
|
|
|
"error detaching from parent process group: %s",
|
2022-05-29 02:26:43 +08:00
|
|
|
strerror(errno));
|
2020-05-05 02:29:25 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-05-29 00:50:18 +08:00
|
|
|
kidpids = app_malloc(n_responders * sizeof(*kidpids), "child PID array");
|
|
|
|
for (i = 0; i < n_responders; ++i)
|
2020-05-05 02:29:25 +08:00
|
|
|
kidpids[i] = 0;
|
|
|
|
|
|
|
|
signal(SIGINT, noteterm);
|
|
|
|
signal(SIGTERM, noteterm);
|
|
|
|
|
|
|
|
while (termsig == 0) {
|
|
|
|
pid_t fpid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for a child to replace when we're at the limit.
|
|
|
|
* Slow down if a child exited abnormally or waitpid() < 0
|
|
|
|
*/
|
2022-05-29 00:50:18 +08:00
|
|
|
while (termsig == 0 && procs >= n_responders) {
|
2020-05-05 02:29:25 +08:00
|
|
|
if ((fpid = waitpid(-1, &status, 0)) > 0) {
|
|
|
|
for (i = 0; i < procs; ++i) {
|
|
|
|
if (kidpids[i] == fpid) {
|
|
|
|
kidpids[i] = 0;
|
|
|
|
--procs;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 00:50:18 +08:00
|
|
|
if (i >= n_responders) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP1(prog, LOG_CRIT,
|
|
|
|
"internal error: no matching child slot for pid: %ld",
|
2022-05-29 02:26:43 +08:00
|
|
|
(long)fpid);
|
2020-05-05 02:29:25 +08:00
|
|
|
killall(1, kidpids);
|
|
|
|
}
|
|
|
|
if (status != 0) {
|
2022-05-29 02:26:43 +08:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
log_HTTP2(prog, LOG_WARNING,
|
|
|
|
"child process: %ld, exit status: %d",
|
|
|
|
(long)fpid, WEXITSTATUS(status));
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
char *dumped = "";
|
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
# ifdef WCOREDUMP
|
2022-05-29 02:26:43 +08:00
|
|
|
if (WCOREDUMP(status))
|
|
|
|
dumped = " (core dumped)";
|
2020-05-05 02:29:25 +08:00
|
|
|
# endif
|
2022-05-29 02:26:43 +08:00
|
|
|
log_HTTP3(prog, LOG_WARNING,
|
|
|
|
"child process: %ld, term signal %d%s",
|
|
|
|
(long)fpid, WTERMSIG(status), dumped);
|
|
|
|
}
|
2022-10-03 13:22:52 +08:00
|
|
|
OSSL_sleep(1000);
|
2020-05-05 02:29:25 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (errno != EINTR) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP1(prog, LOG_CRIT,
|
|
|
|
"waitpid() failed: %s", strerror(errno));
|
2020-05-05 02:29:25 +08:00
|
|
|
killall(1, kidpids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (termsig)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (fpid = fork()) {
|
|
|
|
case -1: /* error */
|
|
|
|
/* System critically low on memory, pause and try again later */
|
2022-10-03 13:22:52 +08:00
|
|
|
OSSL_sleep(30000);
|
2020-05-05 02:29:25 +08:00
|
|
|
break;
|
|
|
|
case 0: /* child */
|
|
|
|
OPENSSL_free(kidpids);
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
if (termsig)
|
|
|
|
_exit(0);
|
|
|
|
if (RAND_poll() <= 0) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_CRIT, "RAND_poll() failed");
|
2020-05-05 02:29:25 +08:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
default: /* parent */
|
2022-05-29 00:50:18 +08:00
|
|
|
for (i = 0; i < n_responders; ++i) {
|
2020-05-05 02:29:25 +08:00
|
|
|
if (kidpids[i] == 0) {
|
|
|
|
kidpids[i] = fpid;
|
|
|
|
procs++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 00:50:18 +08:00
|
|
|
if (i >= n_responders) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_CRIT,
|
|
|
|
"internal error: no free child slots");
|
2020-05-05 02:29:25 +08:00
|
|
|
killall(1, kidpids);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The loop above can only break on termsig */
|
2022-05-29 02:26:43 +08:00
|
|
|
log_HTTP1(prog, LOG_INFO, "terminating on signal: %d", termsig);
|
2020-05-05 02:29:25 +08:00
|
|
|
killall(0, kidpids);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_SOCK
|
2021-07-13 16:20:38 +08:00
|
|
|
BIO *http_server_init(const char *prog, const char *port, int verb)
|
2020-05-05 02:29:25 +08:00
|
|
|
{
|
|
|
|
BIO *acbio = NULL, *bufbio;
|
2021-05-14 18:25:11 +08:00
|
|
|
int asock;
|
2021-07-13 16:20:38 +08:00
|
|
|
int port_num;
|
2020-05-05 02:29:25 +08:00
|
|
|
|
2022-05-29 02:26:43 +08:00
|
|
|
if (verb >= 0 && !log_set_verbosity(prog, verb))
|
|
|
|
return NULL;
|
2020-05-05 02:29:25 +08:00
|
|
|
bufbio = BIO_new(BIO_f_buffer());
|
|
|
|
if (bufbio == NULL)
|
|
|
|
goto err;
|
|
|
|
acbio = BIO_new(BIO_s_accept());
|
|
|
|
if (acbio == NULL
|
|
|
|
|| BIO_set_bind_mode(acbio, BIO_BIND_REUSEADDR) < 0
|
2021-07-13 16:20:38 +08:00
|
|
|
|| BIO_set_accept_port(acbio, port /* may be "0" */) < 0) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_ERR, "error setting up accept BIO");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_set_accept_bios(acbio, bufbio);
|
|
|
|
bufbio = NULL;
|
|
|
|
if (BIO_do_accept(acbio) <= 0) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP1(prog, LOG_ERR, "error setting accept on port %s", port);
|
2020-05-05 02:29:25 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:25:11 +08:00
|
|
|
/* Report back what address and port are used */
|
|
|
|
BIO_get_fd(acbio, &asock);
|
2021-07-13 16:20:38 +08:00
|
|
|
port_num = report_server_accept(bio_out, asock, 1, 1);
|
|
|
|
if (port_num == 0) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_ERR, "error printing ACCEPT string");
|
2021-05-14 18:25:11 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
return acbio;
|
|
|
|
|
|
|
|
err:
|
2022-12-16 22:53:32 +08:00
|
|
|
ERR_print_errors(bio_err);
|
2020-05-05 02:29:25 +08:00
|
|
|
BIO_free_all(acbio);
|
|
|
|
BIO_free(bufbio);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decode %xx URL-decoding in-place. Ignores malformed sequences.
|
|
|
|
*/
|
|
|
|
static int urldecode(char *p)
|
|
|
|
{
|
|
|
|
unsigned char *out = (unsigned char *)p;
|
|
|
|
unsigned char *save = out;
|
|
|
|
|
|
|
|
for (; *p; p++) {
|
|
|
|
if (*p != '%') {
|
|
|
|
*out++ = *p;
|
|
|
|
} else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
|
|
|
|
/* Don't check, can't fail because of ixdigit() call. */
|
|
|
|
*out++ = (OPENSSL_hexchar2int(p[1]) << 4)
|
|
|
|
| OPENSSL_hexchar2int(p[2]);
|
|
|
|
p += 2;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
return (int)(out - save);
|
|
|
|
}
|
|
|
|
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
/* if *pcbio != NULL, continue given connected session, else accept new */
|
|
|
|
/* if found_keep_alive != NULL, return this way connection persistence state */
|
2020-05-05 02:29:25 +08:00
|
|
|
int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq,
|
2020-05-25 23:32:26 +08:00
|
|
|
char **ppath, BIO **pcbio, BIO *acbio,
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
int *found_keep_alive,
|
2021-07-13 16:20:38 +08:00
|
|
|
const char *prog, int accept_get, int timeout)
|
2020-05-05 02:29:25 +08:00
|
|
|
{
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
BIO *cbio = *pcbio, *getbio = NULL, *b64 = NULL;
|
2020-05-05 02:29:25 +08:00
|
|
|
int len;
|
|
|
|
char reqbuf[2048], inbuf[2048];
|
2020-05-25 23:32:26 +08:00
|
|
|
char *meth, *url, *end;
|
2020-05-05 02:29:25 +08:00
|
|
|
ASN1_VALUE *req;
|
2021-05-10 15:37:36 +08:00
|
|
|
int ret = 0;
|
2020-05-05 02:29:25 +08:00
|
|
|
|
|
|
|
*preq = NULL;
|
2020-05-25 23:32:26 +08:00
|
|
|
if (ppath != NULL)
|
|
|
|
*ppath = NULL;
|
2020-05-05 02:29:25 +08:00
|
|
|
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
if (cbio == NULL) {
|
2021-07-13 16:20:38 +08:00
|
|
|
char *port;
|
|
|
|
|
|
|
|
get_sock_info_address(BIO_get_fd(acbio, NULL), NULL, &port);
|
|
|
|
if (port == NULL) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_ERR, "cannot get port listening on");
|
2021-07-13 16:20:38 +08:00
|
|
|
goto fatal;
|
|
|
|
}
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP1(prog, LOG_DEBUG,
|
2022-12-16 22:53:32 +08:00
|
|
|
"awaiting new connection on port %s ...", port);
|
2021-07-13 16:20:38 +08:00
|
|
|
OPENSSL_free(port);
|
|
|
|
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
if (BIO_do_accept(acbio) <= 0)
|
|
|
|
/* Connection loss before accept() is routine, ignore silently */
|
|
|
|
return ret;
|
2020-05-05 02:29:25 +08:00
|
|
|
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
*pcbio = cbio = BIO_pop(acbio);
|
|
|
|
} else {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_DEBUG, "awaiting next request ...");
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
}
|
2020-05-05 02:29:25 +08:00
|
|
|
if (cbio == NULL) {
|
2022-05-29 00:44:02 +08:00
|
|
|
/* Cannot call http_server_send_status(..., cbio, ...) */
|
2020-05-05 02:29:25 +08:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef HTTP_DAEMON
|
|
|
|
if (timeout > 0) {
|
|
|
|
(void)BIO_get_fd(cbio, &acfd);
|
|
|
|
alarm(timeout);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/* Read the request line. */
|
|
|
|
len = BIO_gets(cbio, reqbuf, sizeof(reqbuf));
|
2021-05-10 15:37:36 +08:00
|
|
|
if (len == 0)
|
|
|
|
return ret;
|
|
|
|
ret = 1;
|
|
|
|
if (len < 0) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_WARNING, "request line read error");
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto out;
|
2020-05-25 23:32:26 +08:00
|
|
|
}
|
2022-05-29 00:44:02 +08:00
|
|
|
|
|
|
|
if (((end = strchr(reqbuf, '\r')) != NULL && end[1] == '\n')
|
2021-05-10 15:37:36 +08:00
|
|
|
|| (end = strchr(reqbuf, '\n')) != NULL)
|
|
|
|
*end = '\0';
|
2022-05-29 02:26:43 +08:00
|
|
|
if (log_get_verbosity() < LOG_TRACE)
|
2022-05-29 00:44:02 +08:00
|
|
|
trace_log_message(-1, prog, LOG_INFO,
|
2022-12-16 22:53:32 +08:00
|
|
|
"received request, 1st line: %s", reqbuf);
|
|
|
|
log_HTTP(prog, LOG_TRACE, "received request header:");
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP1(prog, LOG_TRACE, "%s", reqbuf);
|
|
|
|
if (end == NULL) {
|
|
|
|
log_HTTP(prog, LOG_WARNING,
|
2022-12-16 22:53:32 +08:00
|
|
|
"cannot parse HTTP header: missing end of line");
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
|
|
|
goto out;
|
|
|
|
}
|
2020-05-05 02:29:25 +08:00
|
|
|
|
2021-06-21 14:55:50 +08:00
|
|
|
url = meth = reqbuf;
|
|
|
|
if ((accept_get && CHECK_AND_SKIP_PREFIX(url, "GET "))
|
|
|
|
|| CHECK_AND_SKIP_PREFIX(url, "POST ")) {
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
|
2020-05-25 23:32:26 +08:00
|
|
|
/* Expecting (GET|POST) {sp} /URL {sp} HTTP/1.x */
|
2021-06-21 14:55:50 +08:00
|
|
|
url[-1] = '\0';
|
2020-05-25 23:32:26 +08:00
|
|
|
while (*url == ' ')
|
|
|
|
url++;
|
2020-05-05 02:29:25 +08:00
|
|
|
if (*url != '/') {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP2(prog, LOG_WARNING,
|
2022-12-16 22:53:32 +08:00
|
|
|
"invalid %s -- URL does not begin with '/': %s",
|
2022-05-29 00:44:02 +08:00
|
|
|
meth, url);
|
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
url++;
|
|
|
|
|
|
|
|
/* Splice off the HTTP version identifier. */
|
|
|
|
for (end = url; *end != '\0'; end++)
|
|
|
|
if (*end == ' ')
|
|
|
|
break;
|
2021-06-21 14:55:50 +08:00
|
|
|
if (!HAS_PREFIX(end, HTTP_VERSION_STR)) {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP2(prog, LOG_WARNING,
|
2022-12-16 22:53:32 +08:00
|
|
|
"invalid %s -- bad HTTP/version string: %s",
|
2022-05-29 00:44:02 +08:00
|
|
|
meth, end + 1);
|
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*end = '\0';
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
/* above HTTP 1.0, connection persistence is the default */
|
|
|
|
if (found_keep_alive != NULL)
|
2021-06-21 14:55:50 +08:00
|
|
|
*found_keep_alive = end[sizeof(HTTP_VERSION_STR) - 1] > '0';
|
2020-05-05 02:29:25 +08:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Skip "GET / HTTP..." requests often used by load-balancers.
|
|
|
|
* 'url' was incremented above to point to the first byte *after*
|
|
|
|
* the leading slash, so in case 'GET / ' it is now an empty string.
|
|
|
|
*/
|
2020-05-25 23:32:26 +08:00
|
|
|
if (strlen(meth) == 3 && url[0] == '\0') {
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 200, "OK");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto out;
|
2020-05-25 23:32:26 +08:00
|
|
|
}
|
2020-05-05 02:29:25 +08:00
|
|
|
|
|
|
|
len = urldecode(url);
|
2020-05-25 23:32:26 +08:00
|
|
|
if (len < 0) {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP2(prog, LOG_WARNING,
|
2022-12-16 22:53:32 +08:00
|
|
|
"invalid %s request -- bad URL encoding: %s", meth, url);
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2020-05-25 23:32:26 +08:00
|
|
|
if (strlen(meth) == 3) { /* GET */
|
|
|
|
if ((getbio = BIO_new_mem_buf(url, len)) == NULL
|
|
|
|
|| (b64 = BIO_new(BIO_f_base64())) == NULL) {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP1(prog, LOG_ERR,
|
2022-12-16 22:53:32 +08:00
|
|
|
"could not allocate base64 bio with size = %d", len);
|
2020-05-25 23:32:26 +08:00
|
|
|
goto fatal;
|
|
|
|
}
|
|
|
|
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
|
|
|
getbio = BIO_push(b64, getbio);
|
2020-05-05 02:29:25 +08:00
|
|
|
}
|
2020-05-25 23:32:26 +08:00
|
|
|
} else {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP2(prog, LOG_WARNING,
|
|
|
|
"HTTP request does not begin with %sPOST: %s",
|
|
|
|
accept_get ? "GET or " : "", reqbuf);
|
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:32:26 +08:00
|
|
|
/* chop any further/duplicate leading or trailing '/' */
|
|
|
|
while (*url == '/')
|
|
|
|
url++;
|
|
|
|
while (end >= url + 2 && end[-2] == '/' && end[-1] == '/')
|
|
|
|
end--;
|
|
|
|
*end = '\0';
|
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
/* Read and skip past the headers. */
|
|
|
|
for (;;) {
|
2022-05-29 00:44:02 +08:00
|
|
|
char *key, *value;
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
len = BIO_gets(cbio, inbuf, sizeof(inbuf));
|
|
|
|
if (len <= 0) {
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP(prog, LOG_WARNING, "error reading HTTP header");
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((end = strchr(inbuf, '\r')) != NULL && end[1] == '\n')
|
|
|
|
|| (end = strchr(inbuf, '\n')) != NULL)
|
|
|
|
*end = '\0';
|
|
|
|
log_HTTP1(prog, LOG_TRACE, "%s", *inbuf == '\0' ?
|
|
|
|
" " /* workaround for "" getting ignored */ : inbuf);
|
|
|
|
if (end == NULL) {
|
|
|
|
log_HTTP(prog, LOG_WARNING,
|
2022-12-16 22:53:32 +08:00
|
|
|
"error parsing HTTP header: missing end of line");
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
2020-05-05 02:29:25 +08:00
|
|
|
goto out;
|
|
|
|
}
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
|
2022-05-29 00:44:02 +08:00
|
|
|
if (inbuf[0] == '\0')
|
2020-05-05 02:29:25 +08:00
|
|
|
break;
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
|
|
|
|
key = inbuf;
|
|
|
|
value = strchr(key, ':');
|
2021-05-17 07:26:48 +08:00
|
|
|
if (value == NULL) {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP(prog, LOG_WARNING,
|
2022-12-16 22:53:32 +08:00
|
|
|
"error parsing HTTP header: missing ':'");
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2021-05-17 07:26:48 +08:00
|
|
|
*(value++) = '\0';
|
|
|
|
while (*value == ' ')
|
|
|
|
value++;
|
|
|
|
/* https://tools.ietf.org/html/rfc7230#section-6.3 Persistence */
|
2022-04-12 18:30:08 +08:00
|
|
|
if (found_keep_alive != NULL
|
|
|
|
&& OPENSSL_strcasecmp(key, "Connection") == 0) {
|
|
|
|
if (OPENSSL_strcasecmp(value, "keep-alive") == 0)
|
2021-05-17 07:26:48 +08:00
|
|
|
*found_keep_alive = 1;
|
2022-04-12 18:30:08 +08:00
|
|
|
else if (OPENSSL_strcasecmp(value, "close") == 0)
|
2021-05-17 07:26:48 +08:00
|
|
|
*found_keep_alive = 0;
|
|
|
|
}
|
2020-05-05 02:29:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef HTTP_DAEMON
|
|
|
|
/* Clear alarm before we close the client socket */
|
|
|
|
alarm(0);
|
|
|
|
timeout = 0;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/* Try to read and parse request */
|
|
|
|
req = ASN1_item_d2i_bio(it, getbio != NULL ? getbio : cbio, NULL);
|
2020-05-25 23:32:26 +08:00
|
|
|
if (req == NULL) {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP(prog, LOG_WARNING,
|
2022-12-16 22:53:32 +08:00
|
|
|
"error parsing DER-encoded request content");
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 400, "Bad Request");
|
2020-05-25 23:32:26 +08:00
|
|
|
} else if (ppath != NULL && (*ppath = OPENSSL_strdup(url)) == NULL) {
|
2022-05-29 00:44:02 +08:00
|
|
|
log_HTTP1(prog, LOG_ERR,
|
2022-12-16 22:53:32 +08:00
|
|
|
"out of memory allocating %zu bytes", strlen(url) + 1);
|
2020-05-25 23:32:26 +08:00
|
|
|
ASN1_item_free(req, it);
|
|
|
|
goto fatal;
|
|
|
|
}
|
2020-05-05 02:29:25 +08:00
|
|
|
|
|
|
|
*preq = req;
|
|
|
|
|
|
|
|
out:
|
|
|
|
BIO_free_all(getbio);
|
|
|
|
# ifdef HTTP_DAEMON
|
|
|
|
if (timeout > 0)
|
|
|
|
alarm(0);
|
|
|
|
acfd = (int)INVALID_SOCKET;
|
|
|
|
# endif
|
|
|
|
return ret;
|
2020-05-25 23:32:26 +08:00
|
|
|
|
|
|
|
fatal:
|
2022-05-29 00:44:02 +08:00
|
|
|
(void)http_server_send_status(prog, cbio, 500, "Internal Server Error");
|
2020-05-25 23:32:26 +08:00
|
|
|
if (ppath != NULL) {
|
|
|
|
OPENSSL_free(*ppath);
|
|
|
|
*ppath = NULL;
|
|
|
|
}
|
|
|
|
BIO_free_all(cbio);
|
|
|
|
*pcbio = NULL;
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
2020-05-05 02:29:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* assumes that cbio does not do an encoding that changes the output length */
|
2022-05-29 00:44:02 +08:00
|
|
|
int http_server_send_asn1_resp(const char *prog, BIO *cbio, int keep_alive,
|
HTTP: Implement persistent connections (keep-alive)
Both at API and at CLI level (for the CMP app only, so far)
there is a new parameter/option: keep_alive.
* 0 means HTTP connections are not kept open after
receiving a response, which is the default behavior for HTTP 1.0.
* 1 means that persistent connections are requested.
* 2 means that persistent connections are required, i.e.,
in case the server does not grant them an error occurs.
For the CMP app the default value is 1, which means preferring to keep
the connection open. For all other internal uses of the HTTP client
(fetching an OCSP response, a cert, or a CRL) it does not matter
because these operations just take one round trip.
If the client application requested or required a persistent connection
and this was granted by the server, it can keep the OSSL_HTTP_REQ_CTX *
as long as it wants to send further requests and OSSL_HTTP_is_alive()
returns nonzero,
else it should call OSSL_HTTP_REQ_CTX_free() or OSSL_HTTP_close().
In case the client application keeps the OSSL_HTTP_REQ_CTX *
but the connection then dies for any reason at the server side, it will
notice this obtaining an I/O error when trying to send the next request.
This requires extending the HTTP header parsing and
rearranging the high-level HTTP client API. In particular:
* Split the monolithic OSSL_HTTP_transfer() into OSSL_HTTP_open(),
OSSL_HTTP_set_request(), a lean OSSL_HTTP_transfer(), and OSSL_HTTP_close().
* Split the timeout functionality accordingly and improve default behavior.
* Extract part of OSSL_HTTP_REQ_CTX_new() to OSSL_HTTP_REQ_CTX_set_expected().
* Extend struct ossl_http_req_ctx_st accordingly.
Use the new feature for the CMP client, which requires extending
related transaction management of CMP client and test server.
Update the documentation and extend the tests accordingly.
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15053)
2021-04-28 06:26:14 +08:00
|
|
|
const char *content_type,
|
2020-05-05 02:29:25 +08:00
|
|
|
const ASN1_ITEM *it, const ASN1_VALUE *resp)
|
|
|
|
{
|
2022-05-29 00:44:02 +08:00
|
|
|
char buf[200], *p;
|
2023-01-17 06:49:01 +08:00
|
|
|
int ret = BIO_snprintf(buf, sizeof(buf), HTTP_1_0" 200 OK\r\n%s"
|
|
|
|
"Content-type: %s\r\n"
|
|
|
|
"Content-Length: %d\r\n",
|
|
|
|
keep_alive ? "Connection: keep-alive\r\n" : "",
|
|
|
|
content_type,
|
|
|
|
ASN1_item_i2d(resp, NULL, it));
|
2022-05-29 00:44:02 +08:00
|
|
|
|
|
|
|
if (ret < 0 || (size_t)ret >= sizeof(buf))
|
|
|
|
return 0;
|
2022-05-29 02:26:43 +08:00
|
|
|
if (log_get_verbosity() < LOG_TRACE && (p = strchr(buf, '\r')) != NULL)
|
2022-05-29 00:44:02 +08:00
|
|
|
trace_log_message(-1, prog, LOG_INFO,
|
2022-12-16 22:53:32 +08:00
|
|
|
"sending response, 1st line: %.*s", (int)(p - buf),
|
2022-05-29 00:44:02 +08:00
|
|
|
buf);
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP1(prog, LOG_TRACE, "sending response header:\n%s", buf);
|
2022-05-29 00:44:02 +08:00
|
|
|
|
|
|
|
ret = BIO_printf(cbio, "%s\r\n", buf) > 0
|
|
|
|
&& ASN1_item_i2d_bio(it, cbio, resp) > 0;
|
2020-05-05 02:29:25 +08:00
|
|
|
|
|
|
|
(void)BIO_flush(cbio);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-05-25 23:32:26 +08:00
|
|
|
|
2022-05-29 00:44:02 +08:00
|
|
|
int http_server_send_status(const char *prog, BIO *cbio,
|
|
|
|
int status, const char *reason)
|
2020-05-25 23:32:26 +08:00
|
|
|
{
|
2022-05-29 00:44:02 +08:00
|
|
|
char buf[200];
|
2023-01-17 06:49:01 +08:00
|
|
|
int ret = BIO_snprintf(buf, sizeof(buf), HTTP_1_0" %d %s\r\n\r\n",
|
|
|
|
/* This implicitly cancels keep-alive */
|
|
|
|
status, reason);
|
2022-05-29 00:44:02 +08:00
|
|
|
|
|
|
|
if (ret < 0 || (size_t)ret >= sizeof(buf))
|
|
|
|
return 0;
|
2022-12-16 22:53:32 +08:00
|
|
|
log_HTTP1(prog, LOG_TRACE, "sending response header:\n%s", buf);
|
2020-05-25 23:32:26 +08:00
|
|
|
|
2022-05-29 00:44:02 +08:00
|
|
|
ret = BIO_printf(cbio, "%s\r\n", buf) > 0;
|
2020-05-25 23:32:26 +08:00
|
|
|
(void)BIO_flush(cbio);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-05-05 02:29:25 +08:00
|
|
|
#endif
|