1e1051a39Sopenharmony_ci=pod 2e1051a39Sopenharmony_ci 3e1051a39Sopenharmony_ci=head1 NAME 4e1051a39Sopenharmony_ci 5e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX, 6e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_new, 7e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_free, 8e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_request_line, 9e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_add1_header, 10e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_expected, 11e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set1_req, 12e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_nbio, 13e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_nbio_d2i, 14e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_exchange, 15e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_get0_mem_bio, 16e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_get_resp_len, 17e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_max_response_length, 18e1051a39Sopenharmony_ciOSSL_HTTP_is_alive 19e1051a39Sopenharmony_ci- HTTP client low-level functions 20e1051a39Sopenharmony_ci 21e1051a39Sopenharmony_ci=head1 SYNOPSIS 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ci #include <openssl/http.h> 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_ci typedef struct ossl_http_req_ctx_st OSSL_HTTP_REQ_CTX; 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_ci OSSL_HTTP_REQ_CTX *OSSL_HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio, int buf_size); 28e1051a39Sopenharmony_ci void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx); 29e1051a39Sopenharmony_ci 30e1051a39Sopenharmony_ci int OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX *rctx, int method_POST, 31e1051a39Sopenharmony_ci const char *server, const char *port, 32e1051a39Sopenharmony_ci const char *path); 33e1051a39Sopenharmony_ci int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx, 34e1051a39Sopenharmony_ci const char *name, const char *value); 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ci int OSSL_HTTP_REQ_CTX_set_expected(OSSL_HTTP_REQ_CTX *rctx, 37e1051a39Sopenharmony_ci const char *content_type, int asn1, 38e1051a39Sopenharmony_ci int timeout, int keep_alive); 39e1051a39Sopenharmony_ci int OSSL_HTTP_REQ_CTX_set1_req(OSSL_HTTP_REQ_CTX *rctx, const char *content_type, 40e1051a39Sopenharmony_ci const ASN1_ITEM *it, const ASN1_VALUE *req); 41e1051a39Sopenharmony_ci int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx); 42e1051a39Sopenharmony_ci int OSSL_HTTP_REQ_CTX_nbio_d2i(OSSL_HTTP_REQ_CTX *rctx, 43e1051a39Sopenharmony_ci ASN1_VALUE **pval, const ASN1_ITEM *it); 44e1051a39Sopenharmony_ci BIO *OSSL_HTTP_REQ_CTX_exchange(OSSL_HTTP_REQ_CTX *rctx); 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci BIO *OSSL_HTTP_REQ_CTX_get0_mem_bio(const OSSL_HTTP_REQ_CTX *rctx); 47e1051a39Sopenharmony_ci size_t OSSL_HTTP_REQ_CTX_get_resp_len(const OSSL_HTTP_REQ_CTX *rctx); 48e1051a39Sopenharmony_ci void OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX *rctx, 49e1051a39Sopenharmony_ci unsigned long len); 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ci int OSSL_HTTP_is_alive(const OSSL_HTTP_REQ_CTX *rctx); 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_ci=head1 DESCRIPTION 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_ciB<OSSL_HTTP_REQ_CTX> is a context structure for an HTTP request and response, 56e1051a39Sopenharmony_ciused to collect all the necessary data to perform that request. 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ciThis file documents low-level HTTP functions rarely used directly. High-level 59e1051a39Sopenharmony_ciHTTP client functions like L<OSSL_HTTP_get(3)> and L<OSSL_HTTP_transfer(3)> 60e1051a39Sopenharmony_cishould be preferred. 61e1051a39Sopenharmony_ci 62e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_new() allocates a new HTTP request context structure, 63e1051a39Sopenharmony_ciwhich gets populated with the B<BIO> to write/send the request to (I<wbio>), 64e1051a39Sopenharmony_cithe B<BIO> to read/receive the response from (I<rbio>, which may be equal to 65e1051a39Sopenharmony_ciI<wbio>), and the maximum expected response header line length I<buf_size>. 66e1051a39Sopenharmony_ciA value <= 0 indicates that 67e1051a39Sopenharmony_cithe B<OSSL_HTTP_DEFAULT_MAX_LINE_LEN> of 4KiB should be used. 68e1051a39Sopenharmony_ciI<buf_size> is also used as the number of content bytes that are read at a time. 69e1051a39Sopenharmony_ciThe allocated context structure includes an internal memory B<BIO>, 70e1051a39Sopenharmony_ciwhich collects the HTTP request header lines. 71e1051a39Sopenharmony_ci 72e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_free() frees up the HTTP request context I<rctx>. 73e1051a39Sopenharmony_ciThe I<rbio> is not free'd, I<wbio> will be free'd if I<free_wbio> is set. 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_request_line() adds the HTTP request line to the context. 76e1051a39Sopenharmony_ciThe HTTP method is determined by I<method_POST>, 77e1051a39Sopenharmony_ciwhich should be 1 to indicate C<POST> or 0 to indicate C<GET>. 78e1051a39Sopenharmony_ciI<server> and I<port> may be set to indicate a proxy server and port 79e1051a39Sopenharmony_cithat the request should go through, otherwise they should be left NULL. 80e1051a39Sopenharmony_ciI<path> is the HTTP request path; if left NULL, C</> is used. 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_add1_header() adds header I<name> with value I<value> to the 83e1051a39Sopenharmony_cicontext I<rctx>. It can be called more than once to add multiple header lines. 84e1051a39Sopenharmony_ciFor example, to add a C<Host> header for C<example.com> you would call: 85e1051a39Sopenharmony_ci 86e1051a39Sopenharmony_ci OSSL_HTTP_REQ_CTX_add1_header(ctx, "Host", "example.com"); 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_expected() optionally sets in I<rctx> some expectations 89e1051a39Sopenharmony_ciof the HTTP client on the response. 90e1051a39Sopenharmony_ciDue to the structure of an HTTP request, if the I<keep_alive> argument is 91e1051a39Sopenharmony_cinonzero the function must be used before calling OSSL_HTTP_REQ_CTX_set1_req(). 92e1051a39Sopenharmony_ciIf the I<content_type> parameter 93e1051a39Sopenharmony_ciis not NULL then the client will check that the given content type string 94e1051a39Sopenharmony_ciis included in the HTTP header of the response and return an error if not. 95e1051a39Sopenharmony_ciIf the I<asn1> parameter is nonzero a structure in ASN.1 encoding will be 96e1051a39Sopenharmony_ciexpected as the response content and input streaming is disabled. This means 97e1051a39Sopenharmony_cithat an ASN.1 sequence header is required, its length field is checked, and 98e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_get0_mem_bio() should be used to get the buffered response. 99e1051a39Sopenharmony_ciOtherwise (by default) any input format is allowed without length checks. 100e1051a39Sopenharmony_ciIn this case the BIO given as I<rbio> argument to OSSL_HTTP_REQ_CTX_new() should 101e1051a39Sopenharmony_cibe used directly to read the response contents, which may support streaming. 102e1051a39Sopenharmony_ciIf the I<timeout> parameter is > 0 this indicates the maximum number of seconds 103e1051a39Sopenharmony_cithe subsequent HTTP transfer (sending the request and receiving a response) 104e1051a39Sopenharmony_ciis allowed to take. 105e1051a39Sopenharmony_ciI<timeout> == 0 enables waiting indefinitely, i.e., no timeout can occur. 106e1051a39Sopenharmony_ciThis is the default. 107e1051a39Sopenharmony_ciI<timeout> < 0 takes over any value set via the I<overall_timeout> argument of 108e1051a39Sopenharmony_ciL<OSSL_HTTP_open(3)> with the default being 0, which means no timeout. 109e1051a39Sopenharmony_ciIf the I<keep_alive> parameter is 0, which is the default, the connection is not 110e1051a39Sopenharmony_cikept open after receiving a response. This is the default behavior for HTTP 1.0. 111e1051a39Sopenharmony_ciIf the value is 1 or 2 then a persistent connection is requested. 112e1051a39Sopenharmony_ciIf the value is 2 then a persistent connection is required, 113e1051a39Sopenharmony_cii.e., an error occurs in case the server does not grant it. 114e1051a39Sopenharmony_ci 115e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set1_req() finalizes the HTTP request context. 116e1051a39Sopenharmony_ciIt is needed if the I<method_POST> parameter in the 117e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_request_line() call was 1 118e1051a39Sopenharmony_ciand an ASN.1-encoded request should be sent. 119e1051a39Sopenharmony_ciIt must also be used when requesting "keep-alive", 120e1051a39Sopenharmony_cieven if a GET request is going to be sent, in which case I<req> must be NULL. 121e1051a39Sopenharmony_ciUnless I<req> is NULL, the function adds the DER encoding of I<req> using 122e1051a39Sopenharmony_cithe ASN.1 template I<it> to do the encoding (which does not support streaming). 123e1051a39Sopenharmony_ciThe HTTP header C<Content-Length> is filled out with the length of the request. 124e1051a39Sopenharmony_ciI<content_type> must be NULL if I<req> is NULL. 125e1051a39Sopenharmony_ciIf I<content_type> isn't NULL, 126e1051a39Sopenharmony_cithe HTTP header C<Content-Type> is also added with the given string value. 127e1051a39Sopenharmony_ciThe header lines are added to the internal memory B<BIO> for the request header. 128e1051a39Sopenharmony_ci 129e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_nbio() attempts to send the request prepared in I<rctx> 130e1051a39Sopenharmony_ciand to gather the response via HTTP, using the I<wbio> and I<rbio> 131e1051a39Sopenharmony_cithat were given when calling OSSL_HTTP_REQ_CTX_new(). 132e1051a39Sopenharmony_ciThe function may need to be called again if its result is -1, which indicates 133e1051a39Sopenharmony_ciL<BIO_should_retry(3)>. In such a case it is advisable to sleep a little in 134e1051a39Sopenharmony_cibetween, using L<BIO_wait(3)> on the read BIO to prevent a busy loop. 135e1051a39Sopenharmony_ci 136e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_nbio_d2i() is like OSSL_HTTP_REQ_CTX_nbio() but on successs 137e1051a39Sopenharmony_ciin addition parses the response, which must be a DER-encoded ASN.1 structure, 138e1051a39Sopenharmony_ciusing the ASN.1 template I<it> and places the result in I<*pval>. 139e1051a39Sopenharmony_ci 140e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_exchange() calls OSSL_HTTP_REQ_CTX_nbio() as often as needed 141e1051a39Sopenharmony_ciin order to exchange a request and response or until a timeout is reached. 142e1051a39Sopenharmony_ciOn success it returns a pointer to the BIO that can be used to read the result. 143e1051a39Sopenharmony_ciIf an ASN.1-encoded response was expected, this is the BIO 144e1051a39Sopenharmony_cireturned by OSSL_HTTP_REQ_CTX_get0_mem_bio() when called after the exchange. 145e1051a39Sopenharmony_ciThis memory BIO does not support streaming. 146e1051a39Sopenharmony_ciOtherwise the returned BIO is the I<rbio> given to OSSL_HTTP_REQ_CTX_new(), 147e1051a39Sopenharmony_ciwhich may support streaming. 148e1051a39Sopenharmony_ciWhen this BIO is returned, it has been read past the end of the response header, 149e1051a39Sopenharmony_cisuch that the actual response body can be read from it. 150e1051a39Sopenharmony_ciThe returned BIO pointer MUST NOT be freed by the caller. 151e1051a39Sopenharmony_ci 152e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>. 153e1051a39Sopenharmony_ciBefore the HTTP request is sent, this could be used to adapt its header lines. 154e1051a39Sopenharmony_ciI<Use with caution!> 155e1051a39Sopenharmony_ciAfter receiving a response via HTTP, the BIO represents the current state of 156e1051a39Sopenharmony_cireading the response header. If the response was expected to be ASN.1 encoded, 157e1051a39Sopenharmony_ciits contents can be read via this BIO, which does not support streaming. 158e1051a39Sopenharmony_ciThe returned BIO pointer must not be freed by the caller. 159e1051a39Sopenharmony_ci 160e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_get_resp_len() returns the size of the response contents 161e1051a39Sopenharmony_ciin I<rctx> if provided by the server as <Content-Length> header field, else 0. 162e1051a39Sopenharmony_ci 163e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_max_response_length() sets the maximum allowed 164e1051a39Sopenharmony_ciresponse content length for I<rctx> to I<len>. If not set or I<len> is 0 165e1051a39Sopenharmony_cithen the B<OSSL_HTTP_DEFAULT_MAX_RESP_LEN> is used, which currently is 100 KiB. 166e1051a39Sopenharmony_ciIf the C<Content-Length> header is present and exceeds this value or 167e1051a39Sopenharmony_cithe content is an ASN.1 encoded structure with a length exceeding this value 168e1051a39Sopenharmony_cior both length indications are present but disagree then an error occurs. 169e1051a39Sopenharmony_ci 170e1051a39Sopenharmony_ciOSSL_HTTP_is_alive() can be used to query if the HTTP connection 171e1051a39Sopenharmony_cigiven by I<rctx> is still alive, i.e., has not been closed. 172e1051a39Sopenharmony_ciIt returns 0 if I<rctx> is NULL. 173e1051a39Sopenharmony_ci 174e1051a39Sopenharmony_ciIf the client application requested or required a persistent connection 175e1051a39Sopenharmony_ciand this was granted by the server, it can keep I<rctx> as long as it wants 176e1051a39Sopenharmony_cito send further requests and OSSL_HTTP_is_alive() returns nonzero, 177e1051a39Sopenharmony_cielse it should call I<OSSL_HTTP_REQ_CTX_free(rctx)> or L<OSSL_HTTP_close(3)>. 178e1051a39Sopenharmony_ciIn case the client application keeps I<rctx> but the connection then dies 179e1051a39Sopenharmony_cifor any reason at the server side, it will notice this obtaining an 180e1051a39Sopenharmony_ciI/O error when trying to send the next request via I<rctx>. 181e1051a39Sopenharmony_ci 182e1051a39Sopenharmony_ci=head1 WARNINGS 183e1051a39Sopenharmony_ci 184e1051a39Sopenharmony_ciThe server's response may be unexpected if the hostname that was used to 185e1051a39Sopenharmony_cicreate the I<wbio>, any C<Host> header, and the host specified in the 186e1051a39Sopenharmony_cirequest URL do not match. 187e1051a39Sopenharmony_ci 188e1051a39Sopenharmony_ciMany of these functions must be called in a certain order. 189e1051a39Sopenharmony_ci 190e1051a39Sopenharmony_ciFirst, the HTTP request context must be allocated: 191e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_new(). 192e1051a39Sopenharmony_ci 193e1051a39Sopenharmony_ciThen, the HTTP request must be prepared with request data: 194e1051a39Sopenharmony_ci 195e1051a39Sopenharmony_ci=over 4 196e1051a39Sopenharmony_ci 197e1051a39Sopenharmony_ci=item 1. 198e1051a39Sopenharmony_ci 199e1051a39Sopenharmony_ciCalling OSSL_HTTP_REQ_CTX_set_request_line(). 200e1051a39Sopenharmony_ci 201e1051a39Sopenharmony_ci=item 2. 202e1051a39Sopenharmony_ci 203e1051a39Sopenharmony_ciAdding extra header lines with OSSL_HTTP_REQ_CTX_add1_header(). 204e1051a39Sopenharmony_ciThis is optional and may be done multiple times with different names. 205e1051a39Sopenharmony_ci 206e1051a39Sopenharmony_ci=item 3. 207e1051a39Sopenharmony_ci 208e1051a39Sopenharmony_ciFinalize the request using OSSL_HTTP_REQ_CTX_set1_req(). 209e1051a39Sopenharmony_ciThis may be omitted if the GET method is used and "keep-alive" is not requested. 210e1051a39Sopenharmony_ci 211e1051a39Sopenharmony_ci=back 212e1051a39Sopenharmony_ci 213e1051a39Sopenharmony_ciWhen the request context is fully prepared, the HTTP exchange may be performed 214e1051a39Sopenharmony_ciwith OSSL_HTTP_REQ_CTX_nbio() or OSSL_HTTP_REQ_CTX_exchange(). 215e1051a39Sopenharmony_ci 216e1051a39Sopenharmony_ci=head1 RETURN VALUES 217e1051a39Sopenharmony_ci 218e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_new() returns a pointer to a B<OSSL_HTTP_REQ_CTX>, or NULL 219e1051a39Sopenharmony_cion error. 220e1051a39Sopenharmony_ci 221e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_free() and OSSL_HTTP_REQ_CTX_set_max_response_length() 222e1051a39Sopenharmony_cido not return values. 223e1051a39Sopenharmony_ci 224e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set_request_line(), OSSL_HTTP_REQ_CTX_add1_header(), 225e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_set1_req(), and OSSL_HTTP_REQ_CTX_set_expected() 226e1051a39Sopenharmony_cireturn 1 for success and 0 for failure. 227e1051a39Sopenharmony_ci 228e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_nbio() and OSSL_HTTP_REQ_CTX_nbio_d2i() 229e1051a39Sopenharmony_cireturn 1 for success, 0 on error or redirection, -1 if retry is needed. 230e1051a39Sopenharmony_ci 231e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_exchange() and OSSL_HTTP_REQ_CTX_get0_mem_bio() 232e1051a39Sopenharmony_cireturn a pointer to a B<BIO> on success as described above or NULL on failure. 233e1051a39Sopenharmony_ciThe returned BIO must not be freed by the caller. 234e1051a39Sopenharmony_ci 235e1051a39Sopenharmony_ciOSSL_HTTP_REQ_CTX_get_resp_len() returns the size of the response contents 236e1051a39Sopenharmony_cior 0 if not available or an error occurred. 237e1051a39Sopenharmony_ci 238e1051a39Sopenharmony_ciOSSL_HTTP_is_alive() returns 1 if its argument is non-NULL 239e1051a39Sopenharmony_ciand the client requested a persistent connection 240e1051a39Sopenharmony_ciand the server did not disagree on keeping the connection open, else 0. 241e1051a39Sopenharmony_ci 242e1051a39Sopenharmony_ci=head1 SEE ALSO 243e1051a39Sopenharmony_ci 244e1051a39Sopenharmony_ciL<BIO_should_retry(3)>, 245e1051a39Sopenharmony_ciL<BIO_wait(3)>, 246e1051a39Sopenharmony_ciL<ASN1_item_d2i_bio(3)>, 247e1051a39Sopenharmony_ciL<ASN1_item_i2d_mem_bio(3)>, 248e1051a39Sopenharmony_ciL<OSSL_HTTP_open(3)>, 249e1051a39Sopenharmony_ciL<OSSL_HTTP_get(3)>, 250e1051a39Sopenharmony_ciL<OSSL_HTTP_transfer(3)>, 251e1051a39Sopenharmony_ciL<OSSL_HTTP_close(3)> 252e1051a39Sopenharmony_ci 253e1051a39Sopenharmony_ci=head1 HISTORY 254e1051a39Sopenharmony_ci 255e1051a39Sopenharmony_ciThe functions described here were added in OpenSSL 3.0. 256e1051a39Sopenharmony_ci 257e1051a39Sopenharmony_ci=head1 COPYRIGHT 258e1051a39Sopenharmony_ci 259e1051a39Sopenharmony_ciCopyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved. 260e1051a39Sopenharmony_ci 261e1051a39Sopenharmony_ciLicensed under the Apache License 2.0 (the "License"). You may not use 262e1051a39Sopenharmony_cithis file except in compliance with the License. You can obtain a copy 263e1051a39Sopenharmony_ciin the file LICENSE in the source distribution or at 264e1051a39Sopenharmony_ciL<https://www.openssl.org/source/license.html>. 265e1051a39Sopenharmony_ci 266e1051a39Sopenharmony_ci=cut 267