7 OSSL_HTTP_REQ_CTX_free,
8 OSSL_HTTP_REQ_CTX_set_request_line,
9 OSSL_HTTP_REQ_CTX_add1_header,
10 OSSL_HTTP_REQ_CTX_i2d,
11 OSSL_HTTP_REQ_CTX_nbio,
12 OSSL_HTTP_REQ_CTX_sendreq_d2i,
13 OSSL_HTTP_REQ_CTX_get0_mem_bio,
14 OSSL_HTTP_REQ_CTX_set_max_response_length
15 - HTTP client low-level functions
19 #include <openssl/http.h>
21 typedef struct ossl_http_req_ctx_st OSSL_HTTP_REQ_CTX;
23 OSSL_HTTP_REQ_CTX *OSSL_HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio,
24 int method_POST, int maxline,
25 unsigned long max_resp_len,
27 const char *expected_content_type,
29 void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx);
31 int OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX *rctx,
32 const char *server, const char *port,
34 int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx,
35 const char *name, const char *value);
37 int OSSL_HTTP_REQ_CTX_i2d(OSSL_HTTP_REQ_CTX *rctx, const char *content_type,
38 const ASN1_ITEM *it, ASN1_VALUE *req);
39 int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx);
40 ASN1_VALUE *OSSL_HTTP_REQ_CTX_sendreq_d2i(OSSL_HTTP_REQ_CTX *rctx,
43 BIO *OSSL_HTTP_REQ_CTX_get0_mem_bio(OSSL_HTTP_REQ_CTX *rctx);
44 void OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX *rctx,
49 B<OSSL_HTTP_REQ_CTX> is a context structure for an HTTP request, used to
50 collect all the necessary data to perform that request.
52 This file documents low-level HTTP functions rarely used directly. High-level
53 HTTP client functions like L<OSSL_HTTP_get(3)> and L<OSSL_HTTP_transfer(3)>
56 OSSL_HTTP_REQ_CTX_new() allocates a new HTTP request context structure,
57 which gets populated with the B<BIO> to send the request to (I<wbio>),
58 the B<BIO> to read the response from (I<rbio>, which may be equal to I<wbio>),
59 the request method (I<method_POST>, which may be 1 to indicate that the C<POST>
60 method is to be used, or 0 to indicate that the C<GET> method is to be used),
61 the maximum expected response header length (I<max_resp_len>,
62 where any zero or less indicates the default of 4KiB),
63 a response timeout measure in seconds (I<timeout>,
64 where 0 indicates no timeout, i.e., waiting indefinitely),
65 the expected MIME content type of the response (I<expected_content_type>,
66 which may be NULL for no expectation),
67 and a flag indicating that the response is expected to be
68 a DER encoded ASN.1 structure (I<expect_asn1>).
69 The allocated context structure is also populated with an internal allocated
70 memory B<BIO>, which collects the HTTP request and additional headers as text.
71 The returned context should only be used for a single HTTP request/response.
73 OSSL_HTTP_REQ_CTX_free() frees up the HTTP request context I<rctx>.
74 The I<wbio> and I<rbio> are not free'd and it is up to the application
77 OSSL_HTTP_REQ_CTX_set_request_line() adds the HTTP request line to the context.
78 The request method itself becomes C<GET> or C<POST> depending on the value
79 of I<method_POST> in the OSSL_HTTP_REQ_CTX_new() call. I<server> and I<port>
80 may be set to indicate a proxy server and port that the request should go
81 through, otherwise they should be left NULL. I<path> is the HTTP request path;
82 if left NULL, C</> is used.
84 OSSL_HTTP_REQ_CTX_add1_header() adds header I<name> with value I<value> to the
85 context I<rctx>. It can be called more than once to add multiple headers.
86 For example, to add a C<Host> header for C<example.com> you would call:
88 OSSL_HTTP_REQ_CTX_add1_header(ctx, "Host", "example.com");
90 OSSL_HTTP_REQ_CTX_i2d() finalizes the HTTP request context by adding the DER
91 encoding of I<req>, using the ASN.1 template I<it> to do the encoding. The
92 HTTP header C<Content-Length> is automatically filled out, and if
93 I<content_type> isn't NULL, the HTTP header C<Content-Type> is also added with
94 its content as value. All of this ends up in the internal memory B<BIO>.
95 This requires that I<method_POST> was 1 in the OSSL_HTTP_REQ_CTX_new() call.
97 OSSL_HTTP_REQ_CTX_nbio() attempts the exchange of request and response via HTTP,
98 using the I<rbio> and I<wbio> that were given in the OSSL_HTTP_REQ_CTX_new()
99 call. When successful, the contents of the internal memory B<BIO> is replaced
100 with the contents of the HTTP response, without the response headers.
101 It may need to be called again if its result is -1, which indicates
102 L<BIO_should_retry(3)>. In such a case it is advisable to sleep a little in
103 between to prevent a busy loop.
105 OSSL_HTTP_REQ_CTX_sendreq_d2i() calls OSSL_HTTP_REQ_CTX_nbio(), possibly
106 several times until a timeout is reached, and DER decodes the received
107 response using the ASN.1 template I<it>.
109 OSSL_HTTP_REQ_CTX_set_max_response_length() sets the maximum response length
110 for I<rctx> to I<len>. If the response exceeds this length an error occurs.
111 If not set a default value of 100k is used.
113 OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>. This can
114 be used to affect the HTTP request text. I<Use with caution!>
118 The server's response may be unexpected if the hostname that was used to
119 create the I<wbio>, any C<Host> header, and the host specified in the
120 request URL do not match.
122 Many of these functions must be called in a certain order.
124 First, the HTTP request context must be allocated:
125 OSSL_HTTP_REQ_CTX_new().
127 Then, the HTTP request must be prepared with request data:
133 Calling OSSL_HTTP_REQ_CTX_set_request_line(). This must be done exactly once.
137 Adding extra headers with OSSL_HTTP_REQ_CTX_add1_header().
138 This is optional and may be done multiple times with different names.
142 Add C<POST> data with OSSL_HTTP_REQ_CTX_i2d(). This may only be done if
143 I<method_POST> was 1 in the OSSL_HTTP_REQ_CTX_new() call, and must be done
144 exactly once in that case.
148 When the request context is fully prepared, the HTTP exchange may be performed
149 with OSSL_HTTP_REQ_CTX_nbio() or OSSL_HTTP_REQ_CTX_sendreq_d2i().
153 OSSL_HTTP_REQ_CTX_new() returns a pointer to a B<OSSL_HTTP_REQ_CTX>, or NULL
156 OSSL_HTTP_REQ_CTX_free() and OSSL_HTTP_REQ_CTX_set_max_response_length()
157 do not return values.
159 OSSL_HTTP_REQ_CTX_set_request_line(), OSSL_HTTP_REQ_CTX_add1_header(),
160 OSSL_HTTP_REQ_CTX_i2d() and OSSL_HTTP_REQ_CTX_nbio return 1 for success and 0
163 OSSL_HTTP_REQ_CTX_sendreq_d2i() returns a pointer to an B<ASN1_VALUE> for
164 success and NULL for failure.
166 OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.
170 L<OSSL_HTTP_transfer(3)>
174 Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
176 Licensed under the Apache License 2.0 (the "License"). You may not use
177 this file except in compliance with the License. You can obtain a copy
178 in the file LICENSE in the source distribution or at
179 L<https://www.openssl.org/source/license.html>.