rename OSSL_HTTP_REQ_CTX_header to OSSL_HTTP_REQ_CTX_set_request_line
[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_GET, 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, which
57 gets populated with the B<BIO> to send the request to (I<wbio>), the B<BIO> to
58 read the response from (I<rbio>, which may be the same as I<wbio>), the
59 request method (I<method_GET>, which may be 1 to indicate that the C<GET>
60 method is to be used, or 0 to indicate that the C<POST> method is to be used),
61 the maximum expected response header length (I<max_resp_len>, where any zero
62 or less indicates the default of 4KiB), a response timeout measure in seconds
63 (I<timeout>, where 0 indicates no timeout, i.e., waiting indefinitely), the
64 expected MIME content type of the response (I<expected_content_type>, which
65 may be NULL for no expectation), and a flag indicating that the response is
66 expected to be a DER encoded ASN.1 structure (I<expect_asn1>).
67 The allocated context structure is also populated with an internal allocated
68 memory B<BIO>, which collects the HTTP request and additional headers as text.
69 The returned context should only be used for a single HTTP request/response.
70
71 OSSL_HTTP_REQ_CTX_free() frees up the HTTP request context I<rctx>.
72 The I<wbio> and I<rbio> are not free'd and it is up to the application
73 to do so.
74
75 OSSL_HTTP_REQ_CTX_set_request_line() adds the HTTP request line to the context.
76 The request command itself becomes C<GET> or C<POST> depending on the value
77 of I<method_GET> in the OSSL_HTTP_REQ_CTX_new() call.  I<server> and I<port>
78 may be set to indicate a proxy server and port that the request should go
79 through, otherwise they should be left NULL.  I<path> is the HTTP request path;
80 if left NULL, C</> is used.
81
82 OSSL_HTTP_REQ_CTX_add1_header() adds header I<name> with value I<value> to the
83 context I<rctx>. It can be called more than once to add multiple headers.
84 For example, to add a C<Host> header for C<example.com> you would call:
85
86  OSSL_HTTP_REQ_CTX_add1_header(ctx, "Host", "example.com");
87
88 OSSL_HTTP_REQ_CTX_i2d() finalizes the HTTP request context by adding the DER
89 encoding of I<req>, using the ASN.1 template I<it> to do the encoding.  The
90 HTTP header C<Content-Length> is automatically filled out, and if
91 I<content_type> isn't NULL, the HTTP header C<Content-Type> is also added with
92 its content as value.  All of this ends up in the internal memory B<BIO>.
93 This requires that the request type be C<POST>, i.e. that I<method_GET> is 0
94 in the OSSL_HTTP_REQ_CTX_new() call.
95
96 OSSL_HTTP_REQ_CTX_nbio() attempts the exchange of request and response via HTTP,
97 using the I<rbio> and I<wbio> that were given in the OSSL_HTTP_REQ_CTX_new()
98 call.  When successful, the contents of the internal memory B<BIO> is replaced
99 with the contents of the HTTP response, without the response headers.
100 It may need to be called again if its result is -1, which indicates
101 L<BIO_should_retry(3)>.  In such a case it is advisable to sleep a little in
102 between to prevent a busy loop.
103
104 OSSL_HTTP_REQ_CTX_sendreq_d2i() calls OSSL_HTTP_REQ_CTX_nbio(), possibly
105 several times until a timeout is reached, and DER decodes the received
106 response using the ASN.1 template I<it>.
107
108 OSSL_HTTP_REQ_CTX_set_max_response_length() sets the maximum response length
109 for I<rctx> to I<len>. If the response exceeds this length an error occurs.
110 If not set a default value of 100k is used.
111
112 OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.  This can
113 be used to affect the HTTP request text.  I<Use with caution!>
114
115 =head1 WARNINGS
116
117 The server's response may be unexpected if the hostname that was used to
118 create the I<wbio>, any C<Host> header, and the host specified in the
119 request URL do not match.
120
121 Many of these functions must be called in a certain order.
122
123 First, the HTTP request context must be allocated:
124 OSSL_HTTP_REQ_CTX_new().
125
126 Then, the HTTP request must be prepared with request data:
127
128 =over 4
129
130 =item 1.
131
132 Calling OSSL_HTTP_REQ_CTX_set_request_line().  This must be done exactly once.
133
134 =item 2.
135
136 Adding extra headers with OSSL_HTTP_REQ_CTX_add1_header().  This is optional.
137
138 =item 3.
139
140 Add C<POST> data with OSSL_HTTP_REQ_CTX_i2d().  This may only be done if
141 I<method_GET> was 0 in the OSSL_HTTP_REQ_CTX_new() call, and must be done
142 exactly once in that case.
143
144 =back
145
146 When the request context is fully prepared, the HTTP exchange may be performed
147 with OSSL_HTTP_REQ_CTX_nbio() or OSSL_HTTP_REQ_CTX_sendreq_d2i().
148
149 Furthermore, all calls of OSSL_HTTP_REQ_CTX_set_request_line() and
150 OSSL_HTTP_REQ_CTX_add1_header() must be done before any call to
151 int OSSL_HTTP_REQ_CTX_nbio() or OSSL_HTTP_REQ_CTX_sendreq_d2i().
152
153 =head1 RETURN VALUES
154
155 OSSL_HTTP_REQ_CTX_new() returns a pointer to a B<OSSL_HTTP_REQ_CTX>, or NULL
156 on error.
157
158 OSSL_HTTP_REQ_CTX_free() and OSSL_HTTP_REQ_CTX_set_max_response_length()
159 do not return values.
160
161 OSSL_HTTP_REQ_CTX_set_request_line(), OSSL_HTTP_REQ_CTX_add1_header(),
162 OSSL_HTTP_REQ_CTX_i2d() and OSSL_HTTP_REQ_CTX_nbio return 1 for success and 0
163 for failure.
164
165 OSSL_HTTP_REQ_CTX_sendreq_d2i() returns a pointer to an B<ASN1_VALUE> for
166 success and NULL for failure.
167
168 OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.
169
170 =head1 SEE ALSO
171
172 L<OSSL_HTTP_transfer(3)>
173
174 =head1 COPYRIGHT
175
176 Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
177
178 Licensed under the Apache License 2.0 (the "License").  You may not use
179 this file except in compliance with the License.  You can obtain a copy
180 in the file LICENSE in the source distribution or at
181 L<https://www.openssl.org/source/license.html>.
182
183 =cut