diff options
author | Dr. David von Oheimb <David.von.Oheimb@siemens.com> | 2021-04-28 00:26:14 +0200 |
---|---|---|
committer | Dr. David von Oheimb <dev@ddvo.net> | 2021-05-14 19:24:42 +0200 |
commit | 19f97fe6f10bf0d1daec26a9ae2ad919127c67d5 (patch) | |
tree | 4734918abbc423e7c2153b59bb4420fe4022205a /apps/lib | |
parent | 19a39b29e846e465ee97e7519acf14ddc9302198 (diff) |
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)
Diffstat (limited to 'apps/lib')
-rw-r--r-- | apps/lib/http_server.c | 100 |
1 files changed, 82 insertions, 18 deletions
diff --git a/apps/lib/http_server.c b/apps/lib/http_server.c index 0bdc4bc316..691e5c9056 100644 --- a/apps/lib/http_server.c +++ b/apps/lib/http_server.c @@ -31,7 +31,14 @@ #endif static int verbosity = LOG_INFO; + +#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" */ + #ifdef HTTP_DAEMON + int multi = 0; /* run multiple responder processes */ int acfd = (int) INVALID_SOCKET; @@ -262,11 +269,15 @@ static int urldecode(char *p) return (int)(out - save); } +/* if *pcbio != NULL, continue given connected session, else accept new */ +/* if found_keep_alive != NULL, return this way connection persistence state */ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, char **ppath, BIO **pcbio, BIO *acbio, - const char *prog, int accept_get, int timeout) + int *found_keep_alive, + const char *prog, const char *port, + int accept_get, int timeout) { - BIO *cbio = NULL, *getbio = NULL, *b64 = NULL; + BIO *cbio = *pcbio, *getbio = NULL, *b64 = NULL; int len; char reqbuf[2048], inbuf[2048]; char *meth, *url, *end; @@ -276,14 +287,18 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, *preq = NULL; if (ppath != NULL) *ppath = NULL; - *pcbio = NULL; - log_message(prog, LOG_DEBUG, "Awaiting next request..."); -/* Connection loss before accept() is routine, ignore silently */ - if (BIO_do_accept(acbio) <= 0) - return ret; + if (cbio == NULL) { + log_message(prog, LOG_DEBUG, + "Awaiting new connection on port %s...", port); + if (BIO_do_accept(acbio) <= 0) + /* Connection loss before accept() is routine, ignore silently */ + return ret; - *pcbio = cbio = BIO_pop(acbio); + *pcbio = cbio = BIO_pop(acbio); + } else { + log_message(prog, LOG_DEBUG, "Awaiting next request..."); + } if (cbio == NULL) { /* Cannot call http_server_send_status(cbio, ...) */ ret = -1; @@ -316,6 +331,9 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, url = meth + 3; if ((accept_get && strncmp(meth, "GET ", 4) == 0) || (url++, strncmp(meth, "POST ", 5) == 0)) { + static const char http_version_str[] = " "HTTP_PREFIX_VERSION; + static const size_t http_version_str_len = sizeof(http_version_str) - 1; + /* Expecting (GET|POST) {sp} /URL {sp} HTTP/1.x */ *(url++) = '\0'; while (*url == ' ') @@ -333,7 +351,7 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, for (end = url; *end != '\0'; end++) if (*end == ' ') break; - if (strncmp(end, " HTTP/1.", 7) != 0) { + if (strncmp(end, http_version_str, http_version_str_len) != 0) { log_message(prog, LOG_WARNING, "Invalid %s -- bad HTTP/version string: %s", meth, end + 1); @@ -341,6 +359,9 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, goto out; } *end = '\0'; + /* above HTTP 1.0, connection persistence is the default */ + if (found_keep_alive != NULL) + *found_keep_alive = end[http_version_str_len] > '0'; /*- * Skip "GET / HTTP..." requests often used by load-balancers. @@ -373,7 +394,8 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, } } else { log_message(prog, LOG_WARNING, - "HTTP request does not start with GET/POST: %s", reqbuf); + "HTTP request does not begin with %sPOST: %s", + accept_get ? "GET or " : "", reqbuf); /* TODO provide better diagnosis in case client tries TLS */ (void)http_server_send_status(cbio, 400, "Bad Request"); goto out; @@ -388,15 +410,50 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, /* Read and skip past the headers. */ for (;;) { + char *key, *value, *line_end = NULL; + len = BIO_gets(cbio, inbuf, sizeof(inbuf)); if (len <= 0) { - log_message(prog, LOG_WARNING, - "Error skipping remaining HTTP headers"); + log_message(prog, LOG_WARNING, "Error reading HTTP header"); (void)http_server_send_status(cbio, 400, "Bad Request"); goto out; } - if ((inbuf[0] == '\r') || (inbuf[0] == '\n')) + + if (inbuf[0] == '\r' || inbuf[0] == '\n') break; + + key = inbuf; + value = strchr(key, ':'); + if (value != NULL) { + *(value++) = '\0'; + while (*value == ' ') + value++; + line_end = strchr(value, '\r'); + if (line_end == NULL) + line_end = strchr(value, '\n'); + if (line_end != NULL) + *line_end = '\0'; + } else { + log_message(prog, LOG_WARNING, + "Error parsing HTTP header: missing ':'"); + (void)http_server_send_status(cbio, 400, "Bad Request"); + goto out; + } + if (value != NULL && line_end != NULL) { + /* https://tools.ietf.org/html/rfc7230#section-6.3 Persistence */ + if (found_keep_alive != NULL && strcasecmp(key, "Connection") == 0) { + if (strcasecmp(value, "keep-alive") == 0) + *found_keep_alive = 1; + if (strcasecmp(value, "close") == 0) + *found_keep_alive = 0; + } + } else { + log_message(prog, LOG_WARNING, + "Error parsing HTTP header: missing end of line"); + (void)http_server_send_status(cbio, 400, "Bad Request"); + goto out; + } + } # ifdef HTTP_DAEMON @@ -408,7 +465,8 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, /* Try to read and parse request */ req = ASN1_item_d2i_bio(it, getbio != NULL ? getbio : cbio, NULL); if (req == NULL) { - log_message(prog, LOG_WARNING, "Error parsing DER-encoded request content"); + log_message(prog, LOG_WARNING, + "Error parsing DER-encoded request content"); (void)http_server_send_status(cbio, 400, "Bad Request"); } else if (ppath != NULL && (*ppath = OPENSSL_strdup(url)) == NULL) { log_message(prog, LOG_ERR, @@ -441,11 +499,15 @@ int http_server_get_asn1_req(const ASN1_ITEM *it, ASN1_VALUE **preq, } /* assumes that cbio does not do an encoding that changes the output length */ -int http_server_send_asn1_resp(BIO *cbio, const char *content_type, +int http_server_send_asn1_resp(BIO *cbio, int keep_alive, + const char *content_type, const ASN1_ITEM *it, const ASN1_VALUE *resp) { - int ret = BIO_printf(cbio, "HTTP/1.0 200 OK\r\nContent-type: %s\r\n" - "Content-Length: %d\r\n\r\n", content_type, + int ret = BIO_printf(cbio, HTTP_1_0" 200 OK\r\n%s" + "Content-type: %s\r\n" + "Content-Length: %d\r\n\r\n", + keep_alive ? "Connection: keep-alive\r\n" : "", + content_type, ASN1_item_i2d(resp, NULL, it)) > 0 && ASN1_item_i2d_bio(it, cbio, resp) > 0; @@ -455,7 +517,9 @@ int http_server_send_asn1_resp(BIO *cbio, const char *content_type, int http_server_send_status(BIO *cbio, int status, const char *reason) { - int ret = BIO_printf(cbio, "HTTP/1.0 %d %s\r\n\r\n", status, reason) > 0; + int ret = BIO_printf(cbio, HTTP_1_0" %d %s\r\n\r\n", + /* This implicitly cancels keep-alive */ + status, reason) > 0; (void)BIO_flush(cbio); return ret; |