4e30088de7b7d4dbda16f051158ff17b96349683
[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 request 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 the request type be C<POST>,
96 i.e., that I<method_POST> is 1 in the OSSL_HTTP_REQ_CTX_new() call.
97
98 OSSL_HTTP_REQ_CTX_nbio() attempts the exchange of request and response via HTTP,
99 using the I<rbio> and I<wbio> that were given in the OSSL_HTTP_REQ_CTX_new()
100 call.  When successful, the contents of the internal memory B<BIO> is replaced
101 with the contents of the HTTP response, without the response headers.
102 It may need to be called again if its result is -1, which indicates
103 L<BIO_should_retry(3)>.  In such a case it is advisable to sleep a little in
104 between to prevent a busy loop.
105
106 OSSL_HTTP_REQ_CTX_sendreq_d2i() calls OSSL_HTTP_REQ_CTX_nbio(), possibly
107 several times until a timeout is reached, and DER decodes the received
108 response using the ASN.1 template I<it>.
109
110 OSSL_HTTP_REQ_CTX_set_max_response_length() sets the maximum response length
111 for I<rctx> to I<len>. If the response exceeds this length an error occurs.
112 If not set a default value of 100k is used.
113
114 OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.  This can
115 be used to affect the HTTP request text.  I<Use with caution!>
116
117 =head1 WARNINGS
118
119 The server's response may be unexpected if the hostname that was used to
120 create the I<wbio>, any C<Host> header, and the host specified in the
121 request URL do not match.
122
123 Many of these functions must be called in a certain order.
124
125 First, the HTTP request context must be allocated:
126 OSSL_HTTP_REQ_CTX_new().
127
128 Then, the HTTP request must be prepared with request data:
129
130 =over 4
131
132 =item 1.
133
134 Calling OSSL_HTTP_REQ_CTX_set_request_line().  This must be done exactly once.
135
136 =item 2.
137
138 Adding extra headers with OSSL_HTTP_REQ_CTX_add1_header().
139 This is optional and may be done multiple times with different names.
140
141 =item 3.
142
143 Add C<POST> data with OSSL_HTTP_REQ_CTX_i2d().  This may only be done if
144 I<method_POST> was 1 in the OSSL_HTTP_REQ_CTX_new() call, and must be done
145 exactly once in that case.
146
147 =back
148
149 When the request context is fully prepared, the HTTP exchange may be performed
150 with OSSL_HTTP_REQ_CTX_nbio() or OSSL_HTTP_REQ_CTX_sendreq_d2i().
151
152 =head1 RETURN VALUES
153
154 OSSL_HTTP_REQ_CTX_new() returns a pointer to a B<OSSL_HTTP_REQ_CTX>, or NULL
155 on error.
156
157 OSSL_HTTP_REQ_CTX_free() and OSSL_HTTP_REQ_CTX_set_max_response_length()
158 do not return values.
159
160 OSSL_HTTP_REQ_CTX_set_request_line(), OSSL_HTTP_REQ_CTX_add1_header(),
161 OSSL_HTTP_REQ_CTX_i2d() and OSSL_HTTP_REQ_CTX_nbio return 1 for success and 0
162 for failure.
163
164 OSSL_HTTP_REQ_CTX_sendreq_d2i() returns a pointer to an B<ASN1_VALUE> for
165 success and NULL for failure.
166
167 OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.
168
169 =head1 SEE ALSO
170
171 L<OSSL_HTTP_transfer(3)>
172
173 =head1 COPYRIGHT
174
175 Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
176
177 Licensed under the Apache License 2.0 (the "License").  You may not use
178 this file except in compliance with the License.  You can obtain a copy
179 in the file LICENSE in the source distribution or at
180 L<https://www.openssl.org/source/license.html>.
181
182 =cut