OCSP HTTP: Restore API of undocumented and recently deprecated functions
[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_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
16
17 =head1 SYNOPSIS
18
19  #include <openssl/http.h>
20
21  typedef struct ossl_http_req_ctx_st OSSL_HTTP_REQ_CTX;
22
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,
26                                           int timeout,
27                                           const char *expected_content_type,
28                                           int expect_asn1);
29  void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx);
30
31  int OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX *rctx,
32                                         const char *server, const char *port,
33                                         const char *path);
34  int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx,
35                                    const char *name, const char *value);
36
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,
41                                            const ASN1_ITEM *it);
42
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,
45                                                 unsigned long len);
46
47 =head1 DESCRIPTION
48
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.
51
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)>
54 should be preferred.
55
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.
72
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
75 to do so.
76
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.
83
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:
87
88  OSSL_HTTP_REQ_CTX_add1_header(ctx, "Host", "example.com");
89
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.
96
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.
104
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>.
108
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.
112
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!>
115
116 =head1 WARNINGS
117
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.
121
122 Many of these functions must be called in a certain order.
123
124 First, the HTTP request context must be allocated:
125 OSSL_HTTP_REQ_CTX_new().
126
127 Then, the HTTP request must be prepared with request data:
128
129 =over 4
130
131 =item 1.
132
133 Calling OSSL_HTTP_REQ_CTX_set_request_line().  This must be done exactly once.
134
135 =item 2.
136
137 Adding extra headers with OSSL_HTTP_REQ_CTX_add1_header().
138 This is optional and may be done multiple times with different names.
139
140 =item 3.
141
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.
145
146 =back
147
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().
150
151 =head1 RETURN VALUES
152
153 OSSL_HTTP_REQ_CTX_new() returns a pointer to a B<OSSL_HTTP_REQ_CTX>, or NULL
154 on error.
155
156 OSSL_HTTP_REQ_CTX_free() and OSSL_HTTP_REQ_CTX_set_max_response_length()
157 do not return values.
158
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
161 for failure.
162
163 OSSL_HTTP_REQ_CTX_sendreq_d2i() returns a pointer to an B<ASN1_VALUE> for
164 success and NULL for failure.
165
166 OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.
167
168 =head1 SEE ALSO
169
170 L<OSSL_HTTP_transfer(3)>
171
172 =head1 COPYRIGHT
173
174 Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
175
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>.
180
181 =cut