Skip to content

HTTP: Implement persistent connections (keep-alive) etc.#15053

Closed
DDvO wants to merge 14 commits intoopenssl:masterfrom
siemens:http_API_keep-alive
Closed

HTTP: Implement persistent connections (keep-alive) etc.#15053
DDvO wants to merge 14 commits intoopenssl:masterfrom
siemens:http_API_keep-alive

Conversation

@DDvO
Copy link
Contributor

@DDvO DDvO commented Apr 27, 2021

This PR implements feature request #14004.

The proposed semantics of the new feature implemented here is as follows.
Both at API and at CLI level (for the CMP app only, so far) there is a new parameter/option: keep_alive.

  • 0 means that 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 feature addition 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 the local struct ossl_http_req_ctx_st accordingly.

Also extend related diagnostic output for debugging.
Use the new feature for the CMP client,
which requires extending related transaction management of CMP client and test server.

Related further more general improvements (in separate commits) included because they are related:

  • OSSL_HTTP_get(): Do not close connection if redirect to same server
  • OSSL_HTTP_REQ_CTX_add1_headers(): Fix use with host == NULL (relative URLs)
  • OSSL_HTTP_transfer(): Fix error reporting in case rctx->server == NULL.
    Also improve doc of OSSL_parse_url() and OSSL_HTTP_parse_url()
  • Generalize API to arbitrary request and response contents
    Introducing OSSL_HTTP_i2d() and OSSL_HTTP_d2i() makes the API more clear and modular.
  • Allow streaming of request data (for POST method).
    Also clean up OSSL_HTTP_REQ_CTX_nbio() states and make it more efficient.
  • Allow streaming of response data (with possibly indefinite length).
    Also clean up max_resp_len and add OSSL_HTTP_REQ_CTX_get_resp_len()
  • Rename maxline parameter to buf_size for clarity.
  • Rename internal fields and functions in http_client.c for consistency.
Checklist
  • documentation is added or updated
  • tests are added or updated

@DDvO DDvO added this to the 3.0.0 beta1 milestone Apr 27, 2021
@DDvO DDvO force-pushed the http_API_keep-alive branch 3 times, most recently from e1ec2af to 6ddf09f Compare April 27, 2021 22:51
@DDvO DDvO added the triaged: feature The issue/pr requests/adds a feature label Apr 28, 2021
@DDvO
Copy link
Contributor Author

DDvO commented Apr 30, 2021

When submitting this PR some 2 days ago, I forgot to mention details about the proposed semantics of the new feature. Here they are:

  • If keep_alive is 0 then HTTP connections are not kept open after receiving a response, which is the default behavior for HTTP 1.0.
  • If the value is 1 or 2 then persistent connections are requested.
  • If the value is 2 then 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_REQ_CTX_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.

@DDvO DDvO removed this from the 3.0.0 beta1 milestone Apr 30, 2021
@DDvO
Copy link
Contributor Author

DDvO commented Apr 30, 2021

@openssl/otc, is there already someone willing to review this?
This must get in before feature freeze, also due to the changes to the new high-level HTTP client API introduced with 3.0.

@DDvO
Copy link
Contributor Author

DDvO commented Apr 30, 2021

BTW, I also have some high-level HTTP client API streamlining and generalization in the pipeline to be submitted as follow-up;
would be good if this will make it into 3.0 as well...

@DDvO
Copy link
Contributor Author

DDvO commented May 3, 2021

I meanwhile found that specific tests for the new keep-alive feature can be added in a rather straightforward way, so done this as well.

@DDvO
Copy link
Contributor Author

DDvO commented May 3, 2021

Also added a couple of related small fixes of the HTTP client implementation,
mostly regarding the special case of relative URLs, which came up due to the new tests.

@DDvO
Copy link
Contributor Author

DDvO commented May 3, 2021

Added a fixup on the documentation of OSSL_HTTP_REQ_CTX_set_expected(),
more clearly explaining the default behavior if this new low-level HTTP client API function is not used.

@DDvO
Copy link
Contributor Author

DDvO commented May 3, 2021

Since so far I have not seen any reaction on this PR that would indicate that someone has started reviewing this,
I've used the time to further generalize and actually simplify the high-level HTTP client API by making the OSSL_HTTP_{get,transfer}_asn1() functions independent of the request and response content type.

@DDvO
Copy link
Contributor Author

DDvO commented May 3, 2021

On this occasion I've updated the NEWS.md and
extended the CHANGES.md entry on the HTTP client as follows:

   It supports arbitrary request and response content types, GET redirection,
   TLS, connections via HTTP(S) proxies, connections and exchange via
   user-defined BIOs (allowing implicit connections), persistent connections,
   and timeout checks.  See L<OSSL_HTTP_transfer(3)> etc. for details.
   The legacy OCSP-focused (and only partly documented) API
   is retained for backward compatibility, while most of it is deprecated.

@DDvO DDvO force-pushed the http_API_keep-alive branch from 14d968c to cce3d07 Compare May 3, 2021 15:05
@DDvO DDvO added this to the 3.0.0 beta1 milestone May 3, 2021
@DDvO
Copy link
Contributor Author

DDvO commented May 4, 2021

@openssl/otc, I am getting more and more concerned about how to get this PR approved and merged before the public API / feature freeze, which is going to be in 16 days from now.

This PR, submitted 6 days ago, implements a feature discussed in #14004. It is critical for one of our customers in order to be able to make their product compliant with ERTMS/ECTS requirements by UNISIG.
Unfortunately, so far I did not receive any reaction on it, while OTOH the deadline for getting public API changes included into 3.0 is approaching rapidly.

How to solve this?
Would it make sense for instance if I carve out the pure API changes such that they are reviewed and approved hopefully quicker and are merged safely before the deadline,
and then add the internal feature implementation as well as the related tests and documentation later (hopefully already into beta1, otherwise during the beta phase)?

@DDvO
Copy link
Contributor Author

DDvO commented May 4, 2021

Rebased again to fix simple merge conflicts

@DDvO DDvO force-pushed the http_API_keep-alive branch from 8fc334e to c958d0c Compare May 4, 2021 16:49
Copy link
Contributor

@FdaSilvaYY FdaSilvaYY left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work

openssl-machine pushed a commit that referenced this pull request May 14, 2021
openssl-machine pushed a commit that referenced this pull request May 14, 2021
openssl-machine pushed a commit that referenced this pull request May 14, 2021
openssl-machine pushed a commit that referenced this pull request May 14, 2021
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 <[email protected]>
(Merged from #15053)
openssl-machine pushed a commit that referenced this pull request May 14, 2021
openssl-machine pushed a commit that referenced this pull request May 14, 2021
Also improve doc of OSSL_parse_url() and OSSL_HTTP_parse_url().

Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #15053)
openssl-machine pushed a commit that referenced this pull request May 14, 2021
openssl-machine pushed a commit that referenced this pull request May 14, 2021
Also clean up OSSL_HTTP_REQ_CTX_nbio() states and make it more efficient.

Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #15053)
openssl-machine pushed a commit that referenced this pull request May 14, 2021
…te length)

Also clean up max_resp_len and add OSSL_HTTP_REQ_CTX_get_resp_len().

Reviewed-by: Tomas Mraz <[email protected]>
(Merged from #15053)
openssl-machine pushed a commit that referenced this pull request May 14, 2021
openssl-machine pushed a commit that referenced this pull request May 14, 2021
@DDvO
Copy link
Contributor Author

DDvO commented May 14, 2021

Merged - thanks a lot to all to contributed to being able to merge this PR in time!

@DDvO DDvO closed this May 14, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
This is a minimal version of pull request openssl#15053 including all the
proposed improvements to the HTTP client API and its documentation
but only those code adaptations strictly needed for it.

The proposed new features include
* support for persistent connections (keep-alive),
* generalization to arbitrary request and response types, and
* support for streaming BIOs for request and response data.

The related API changes include:
* 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().

Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#15147)
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
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 <[email protected]>
(Merged from openssl#15053)
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
Also improve doc of OSSL_parse_url() and OSSL_HTTP_parse_url().

Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#15053)
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
Also clean up OSSL_HTTP_REQ_CTX_nbio() states and make it more efficient.

Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#15053)
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
…te length)

Also clean up max_resp_len and add OSSL_HTTP_REQ_CTX_get_resp_len().

Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#15053)
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch triaged: feature The issue/pr requests/adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants