2020-05-05 02:29:25 +08:00
|
|
|
/*
|
2021-05-20 21:22:33 +08:00
|
|
|
* Copyright 1995-2021 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSSL_HTTP_SERVER_H
|
|
|
|
# define OSSL_HTTP_SERVER_H
|
|
|
|
|
|
|
|
# include "apps.h"
|
|
|
|
|
|
|
|
# ifndef HAVE_FORK
|
|
|
|
# if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS)
|
|
|
|
# define HAVE_FORK 0
|
|
|
|
# else
|
|
|
|
# define HAVE_FORK 1
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if HAVE_FORK
|
|
|
|
# undef NO_FORK
|
|
|
|
# else
|
|
|
|
# define NO_FORK
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if !defined(NO_FORK) && !defined(OPENSSL_NO_SOCK) \
|
|
|
|
&& !defined(OPENSSL_NO_POSIX_IO)
|
|
|
|
# define HTTP_DAEMON
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/wait.h>
|
|
|
|
# include <syslog.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# define MAXERRLEN 1000 /* limit error text sent to syslog to 1000 bytes */
|
|
|
|
# else
|
2021-05-10 15:37:36 +08:00
|
|
|
# undef LOG_DEBUG
|
2020-05-05 02:29:25 +08:00
|
|
|
# undef LOG_INFO
|
|
|
|
# undef LOG_WARNING
|
|
|
|
# undef LOG_ERR
|
2021-05-10 15:37:36 +08:00
|
|
|
# define LOG_DEBUG 7
|
|
|
|
# define LOG_INFO 6
|
|
|
|
# define LOG_WARNING 4
|
|
|
|
# define LOG_ERR 3
|
2020-05-05 02:29:25 +08:00
|
|
|
# endif
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Log a message to syslog if multi-threaded HTTP_DAEMON, else to bio_err
|
|
|
|
* prog: the name of the current app
|
|
|
|
* level: the severity of the message, e.g., LOG_ERR
|
|
|
|
* fmt: message with potential extra parameters like with printf()
|
|
|
|
* returns nothing
|
|
|
|
*/
|
|
|
|
void log_message(const char *prog, int level, const char *fmt, ...);
|
|
|
|
|
|
|
|
# ifndef OPENSSL_NO_SOCK
|
|
|
|
/*-
|
|
|
|
* Initialize an HTTP server by setting up its listening BIO
|
|
|
|
* prog: the name of the current app
|
|
|
|
* port: the port to listen on
|
|
|
|
* returns a BIO for accepting requests, NULL on error
|
|
|
|
*/
|
|
|
|
BIO *http_server_init_bio(const char *prog, const char *port);
|
2020-05-25 23:32:26 +08:00
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
/*-
|
|
|
|
* Accept an ASN.1-formatted HTTP request
|
|
|
|
* it: the expected request ASN.1 type
|
|
|
|
* preq: pointer to variable where to place the parsed request
|
2020-05-25 23:32:26 +08:00
|
|
|
* ppath: pointer to variable where to place the request path, or NULL
|
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: pointer to variable where to place the BIO for sending the response to
|
2020-05-05 02:29:25 +08:00
|
|
|
* acbio: the listening bio (typically as returned by http_server_init_bio())
|
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
|
|
|
* found_keep_alive: for returning flag if client requests persistent connection
|
|
|
|
* prog: the name of the current app, for diagnostics only
|
|
|
|
* port: the local port listening to, for diagnostics only
|
2020-05-25 23:32:26 +08:00
|
|
|
* accept_get: whether to accept GET requests (in addition to POST requests)
|
2020-05-05 02:29:25 +08:00
|
|
|
* timeout: connection timeout (in seconds), or 0 for none/infinite
|
2020-05-25 23:32:26 +08:00
|
|
|
* returns 0 in case caller should retry, then *preq == *ppath == *pcbio == NULL
|
|
|
|
* returns -1 on fatal error; also then holds *preq == *ppath == *pcbio == NULL
|
|
|
|
* returns 1 otherwise. In this case it is guaranteed that *pcbio != NULL while
|
|
|
|
* *ppath == NULL and *preq == NULL if and only if the request is invalid,
|
|
|
|
* On return value 1 the caller is responsible for sending an HTTP response,
|
|
|
|
* using http_server_send_asn1_resp() or http_server_send_status().
|
|
|
|
* The caller must free any non-NULL *preq, *ppath, and *pcbio pointers.
|
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,
|
|
|
|
const char *prog, const char *port,
|
|
|
|
int accept_get, int timeout);
|
2020-05-25 23:32:26 +08:00
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
/*-
|
|
|
|
* Send an ASN.1-formatted HTTP response
|
|
|
|
* cbio: destination BIO (typically as returned by http_server_get_asn1_req())
|
|
|
|
* note: cbio should not do an encoding that changes the output length
|
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
|
|
|
* keep_alive: grant persistent connnection
|
2020-05-05 02:29:25 +08:00
|
|
|
* content_type: string identifying the type of the response
|
|
|
|
* it: the response ASN.1 type
|
|
|
|
* resp: the response to send
|
|
|
|
* returns 1 on success, 0 on failure
|
|
|
|
*/
|
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 http_server_send_asn1_resp(BIO *cbio, int keep_alive,
|
|
|
|
const char *content_type,
|
2020-05-05 02:29:25 +08:00
|
|
|
const ASN1_ITEM *it, const ASN1_VALUE *resp);
|
2020-05-25 23:32:26 +08:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Send a trivial HTTP response, typically to report an error or OK
|
|
|
|
* cbio: destination BIO (typically as returned by http_server_get_asn1_req())
|
|
|
|
* status: the status code to send
|
|
|
|
* reason: the corresponding human-readable string
|
|
|
|
* returns 1 on success, 0 on failure
|
|
|
|
*/
|
|
|
|
int http_server_send_status(BIO *cbio, int status, const char *reason);
|
|
|
|
|
2020-05-05 02:29:25 +08:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef HTTP_DAEMON
|
|
|
|
extern int multi;
|
|
|
|
extern int acfd;
|
|
|
|
|
|
|
|
void socket_timeout(int signum);
|
|
|
|
void spawn_loop(const char *prog);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif
|