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