OSSL_HTTP_transfer.3ossl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "OSSL_HTTP_TRANSFER 3ossl"
  58. .TH OSSL_HTTP_TRANSFER 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. OSSL_HTTP_open,
  65. OSSL_HTTP_bio_cb_t,
  66. OSSL_HTTP_proxy_connect,
  67. OSSL_HTTP_set1_request,
  68. OSSL_HTTP_exchange,
  69. OSSL_HTTP_get,
  70. OSSL_HTTP_transfer,
  71. OSSL_HTTP_close
  72. \&\- HTTP client high\-level functions
  73. .SH SYNOPSIS
  74. .IX Header "SYNOPSIS"
  75. .Vb 1
  76. \& #include <openssl/http.h>
  77. \&
  78. \& typedef BIO *(*OSSL_HTTP_bio_cb_t)(BIO *bio, void *arg,
  79. \& int connect, int detail);
  80. \& OSSL_HTTP_REQ_CTX *OSSL_HTTP_open(const char *server, const char *port,
  81. \& const char *proxy, const char *no_proxy,
  82. \& int use_ssl, BIO *bio, BIO *rbio,
  83. \& OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
  84. \& int buf_size, int overall_timeout);
  85. \& int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
  86. \& const char *proxyuser, const char *proxypass,
  87. \& int timeout, BIO *bio_err, const char *prog);
  88. \& int OSSL_HTTP_set1_request(OSSL_HTTP_REQ_CTX *rctx, const char *path,
  89. \& const STACK_OF(CONF_VALUE) *headers,
  90. \& const char *content_type, BIO *req,
  91. \& const char *expected_content_type, int expect_asn1,
  92. \& size_t max_resp_len, int timeout, int keep_alive);
  93. \& BIO *OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX *rctx, char **redirection_url);
  94. \& BIO *OSSL_HTTP_get(const char *url, const char *proxy, const char *no_proxy,
  95. \& BIO *bio, BIO *rbio,
  96. \& OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
  97. \& int buf_size, const STACK_OF(CONF_VALUE) *headers,
  98. \& const char *expected_content_type, int expect_asn1,
  99. \& size_t max_resp_len, int timeout);
  100. \& BIO *OSSL_HTTP_transfer(OSSL_HTTP_REQ_CTX **prctx,
  101. \& const char *server, const char *port,
  102. \& const char *path, int use_ssl,
  103. \& const char *proxy, const char *no_proxy,
  104. \& BIO *bio, BIO *rbio,
  105. \& OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
  106. \& int buf_size, const STACK_OF(CONF_VALUE) *headers,
  107. \& const char *content_type, BIO *req,
  108. \& const char *expected_content_type, int expect_asn1,
  109. \& size_t max_resp_len, int timeout, int keep_alive);
  110. \& int OSSL_HTTP_close(OSSL_HTTP_REQ_CTX *rctx, int ok);
  111. .Ve
  112. .SH DESCRIPTION
  113. .IX Header "DESCRIPTION"
  114. \&\fBOSSL_HTTP_open()\fR initiates an HTTP session using the \fIbio\fR argument if not
  115. NULL, else by connecting to a given \fIserver\fR optionally via a \fIproxy\fR.
  116. .PP
  117. Typically the OpenSSL build supports sockets and the \fIbio\fR parameter is NULL.
  118. In this case \fIrbio\fR must be NULL as well and the \fIserver\fR must be non-NULL.
  119. The function creates a network BIO internally using \fBBIO_new_connect\fR\|(3)
  120. for connecting to the given server and the optionally given \fIport\fR,
  121. defaulting to 80 for HTTP or 443 for HTTPS.
  122. Then this internal BIO is used for setting up a connection
  123. and for exchanging one or more request and response.
  124. If \fIbio\fR is given and \fIrbio\fR is NULL then this \fIbio\fR is used instead.
  125. If both \fIbio\fR and \fIrbio\fR are given (which may be memory BIOs for instance)
  126. then no explicit connection is set up, but
  127. \&\fIbio\fR is used for writing requests and \fIrbio\fR for reading responses.
  128. As soon as the client has flushed \fIbio\fR the server must be ready to provide
  129. a response or indicate a waiting condition via \fIrbio\fR.
  130. .PP
  131. If \fIbio\fR is given, it is an error to provide \fIproxy\fR or \fIno_proxy\fR arguments,
  132. while \fIserver\fR and \fIport\fR arguments may be given to support diagnostic output.
  133. If \fIbio\fR is NULL the optional \fIproxy\fR parameter can be used to set an
  134. HTTP(S) proxy to use (unless overridden by "no_proxy" settings).
  135. If TLS is not used this defaults to the environment variable \f(CW\*(C`http_proxy\*(C'\fR
  136. if set, else \f(CW\*(C`HTTP_PROXY\*(C'\fR.
  137. If \fIuse_ssl\fR != 0 it defaults to \f(CW\*(C`https_proxy\*(C'\fR if set, else \f(CW\*(C`HTTPS_PROXY\*(C'\fR.
  138. An empty proxy string \f(CW""\fR forbids using a proxy.
  139. Otherwise, the format is
  140. \&\f(CW\*(C`[http[s]://][userinfo@]host[:port][/path][?query][#fragment]\*(C'\fR,
  141. where any userinfo, path, query, and fragment given is ignored.
  142. If the host string is an IPv6 address, it must be enclosed in \f(CW\*(C`[\*(C'\fR and \f(CW\*(C`]\*(C'\fR.
  143. The default proxy port number is 80, or 443 in case "https:" is given.
  144. The HTTP client functions connect via the given proxy unless the \fIserver\fR
  145. is found in the optional list \fIno_proxy\fR of proxy hostnames or IP addresses
  146. separated by \f(CW\*(C`,\*(C'\fR and/or whitespace (if not NULL;
  147. default is the environment variable \f(CW\*(C`no_proxy\*(C'\fR if set, else \f(CW\*(C`NO_PROXY\*(C'\fR).
  148. Proxying plain HTTP is supported directly,
  149. while using a proxy for HTTPS connections requires a suitable callback function
  150. such as \fBOSSL_HTTP_proxy_connect()\fR, described below.
  151. .PP
  152. If \fIuse_ssl\fR is nonzero a TLS connection is requested
  153. and the \fIbio_update_fn\fR parameter must be provided.
  154. .PP
  155. The parameter \fIbio_update_fn\fR, which is optional if \fIuse_ssl\fR is 0,
  156. may be used to modify the connection BIO used by the HTTP client,
  157. but cannot be used when both \fIbio\fR and \fIrbio\fR are given.
  158. \&\fIbio_update_fn\fR is a BIO connect/disconnect callback function with prototype
  159. .PP
  160. .Vb 1
  161. \& BIO *(*OSSL_HTTP_bio_cb_t)(BIO *bio, void *arg, int connect, int detail)
  162. .Ve
  163. .PP
  164. The callback function may modify the BIO provided in the \fIbio\fR argument,
  165. whereby it may use an optional custom defined argument \fIarg\fR,
  166. which can for instance point to an \fBSSL_CTX\fR structure.
  167. During connection establishment, just after calling \fBBIO_do_connect_retry()\fR, the
  168. callback function is invoked with the \fIconnect\fR argument being 1 and
  169. \&\fIdetail\fR being 1 if \fIuse_ssl\fR is nonzero (i.e., HTTPS is requested), else 0.
  170. On disconnect \fIconnect\fR is 0 and \fIdetail\fR is 1 if no error occurred, else 0.
  171. For instance, on connect the callback may push an SSL BIO to implement HTTPS;
  172. after disconnect it may do some diagnostic output and pop and free the SSL BIO.
  173. .PP
  174. The callback function must return either the potentially modified BIO \fIbio\fR
  175. or NULL to indicate failure, in which case it should not modify the BIO.
  176. .PP
  177. Here is a simple example that supports TLS connections (but not via a proxy):
  178. .PP
  179. .Vb 5
  180. \& BIO *http_tls_cb(BIO *bio, void *arg, int connect, int detail)
  181. \& {
  182. \& if (connect && detail) { /* connecting with TLS */
  183. \& SSL_CTX *ctx = (SSL_CTX *)arg;
  184. \& BIO *sbio = BIO_new_ssl(ctx, 1);
  185. \&
  186. \& bio = sbio != NULL ? BIO_push(sbio, bio) : NULL;
  187. \& } else if (!connect) { /* disconnecting */
  188. \& BIO *hbio;
  189. \&
  190. \& if (!detail) { /* an error has occurred */
  191. \& /* optionally add diagnostics here */
  192. \& }
  193. \& BIO_ssl_shutdown(bio);
  194. \& hbio = BIO_pop(bio);
  195. \& BIO_free(bio); /* SSL BIO */
  196. \& bio = hbio;
  197. \& }
  198. \& return bio;
  199. \& }
  200. .Ve
  201. .PP
  202. After disconnect the modified BIO will be deallocated using \fBBIO_free_all()\fR.
  203. The optional callback function argument \fIarg\fR is not consumed,
  204. so must be freed by the caller when not needed any more.
  205. .PP
  206. The \fIbuf_size\fR parameter specifies the response header maximum line length.
  207. A value <= 0 means that the \fBOSSL_HTTP_DEFAULT_MAX_LINE_LEN\fR (4KiB) is used.
  208. \&\fIbuf_size\fR is also used as the number of content bytes that are read at a time.
  209. .PP
  210. If the \fIoverall_timeout\fR parameter is > 0 this indicates the maximum number of
  211. seconds the overall HTTP transfer (i.e., connection setup if needed,
  212. sending requests, and receiving responses) is allowed to take until completion.
  213. A value <= 0 enables waiting indefinitely, i.e., no timeout.
  214. .PP
  215. \&\fBOSSL_HTTP_proxy_connect()\fR may be used by an above BIO connect callback function
  216. to set up an SSL/TLS connection via an HTTPS proxy.
  217. It promotes the given BIO \fIbio\fR representing a connection
  218. pre-established with a TLS proxy using the HTTP CONNECT method,
  219. optionally using proxy client credentials \fIproxyuser\fR and \fIproxypass\fR,
  220. to connect with TLS protection ultimately to \fIserver\fR and \fIport\fR.
  221. If the \fIport\fR argument is NULL or the empty string it defaults to "443".
  222. If the \fItimeout\fR parameter is > 0 this indicates the maximum number of
  223. seconds the connection setup is allowed to take.
  224. A value <= 0 enables waiting indefinitely, i.e., no timeout.
  225. Since this function is typically called by applications such as
  226. \&\fBopenssl\-s_client\fR\|(1) it uses the \fIbio_err\fR and \fIprog\fR parameters (unless
  227. NULL) to print additional diagnostic information in a user-oriented way.
  228. .PP
  229. \&\fBOSSL_HTTP_set1_request()\fR sets up in \fIrctx\fR the request header and content data
  230. and expectations on the response using the following parameters.
  231. If <rctx> indicates using a proxy for HTTP (but not HTTPS), the server host
  232. (and optionally port) needs to be placed in the header; thus it must be present
  233. in \fIrctx\fR.
  234. For backward compatibility, the server (and optional port) may also be given in
  235. the \fIpath\fR argument beginning with \f(CW\*(C`http://\*(C'\fR (thus giving an absoluteURI).
  236. If \fIpath\fR is NULL it defaults to "/".
  237. If \fIreq\fR is NULL the HTTP GET method will be used to send the request
  238. else HTTP POST with the contents of \fIreq\fR and optional \fIcontent_type\fR, where
  239. the length of the data in \fIreq\fR does not need to be determined in advance: the
  240. BIO will be read on-the-fly while sending the request, which supports streaming.
  241. The optional list \fIheaders\fR may contain additional custom HTTP header lines.
  242. .PP
  243. If the \fIexpected_content_type\fR argument is not NULL,
  244. the client will check that the specified content-type string
  245. is included in the HTTP header of the response and return an error if not.
  246. In the content-type header line the specified string should be present either
  247. as a whole, or in case the specified string does not include a \f(CW\*(C`;\*(C'\fR character,
  248. it is sufficient that the specified string appears as a prefix
  249. in the header line, followed by a \f(CW\*(C`;\*(C'\fR character and any further text.
  250. For instance, if \fIexpected_content_type\fR specifies \f(CW\*(C`text/html\*(C'\fR,
  251. this is matched by \f(CW\*(C`text/html\*(C'\fR, \f(CW\*(C`text/html; charset=UTF\-8\*(C'\fR, etc.
  252. .PP
  253. If the \fIexpect_asn1\fR parameter is nonzero,
  254. a structure in ASN.1 encoding will be expected as response content.
  255. The \fImax_resp_len\fR parameter specifies the maximum allowed
  256. response content length, where the value 0 indicates no limit.
  257. If the \fItimeout\fR parameter is > 0 this indicates the maximum number of seconds
  258. the subsequent HTTP transfer (sending the request and receiving a response)
  259. is allowed to take.
  260. A value of 0 enables waiting indefinitely, i.e., no timeout.
  261. A value < 0 indicates that the \fIoverall_timeout\fR parameter value given
  262. when opening the HTTP transfer will be used instead.
  263. If \fIkeep_alive\fR is 0 the connection is not kept open
  264. after receiving a response, which is the default behavior for HTTP 1.0.
  265. If the value is 1 or 2 then a persistent connection is requested.
  266. If the value is 2 then a persistent connection is required,
  267. i.e., an error occurs in case the server does not grant it.
  268. .PP
  269. \&\fBOSSL_HTTP_exchange()\fR exchanges any form of HTTP request and response
  270. as specified by \fIrctx\fR, which must include both connection and request data,
  271. typically set up using \fBOSSL_HTTP_open()\fR and \fBOSSL_HTTP_set1_request()\fR.
  272. It implements the core of the functions described below.
  273. If the HTTP method is GET and \fIredirection_url\fR
  274. is not NULL the latter pointer is used to provide any new location that
  275. the server may return with HTTP code 301 (MOVED_PERMANENTLY) or 302 (FOUND).
  276. In this case the function returns NULL and the caller is
  277. responsible for deallocating the URL with \fBOPENSSL_free\fR\|(3).
  278. If the response header contains one or more "Content-Length" header lines and/or
  279. an ASN.1\-encoded response is expected, which should include a total length,
  280. the length indications received are checked for consistency
  281. and for not exceeding any given maximum response length.
  282. If an ASN.1\-encoded response is expected, the function returns on success
  283. the contents buffered in a memory BIO, which does not support streaming.
  284. Otherwise it returns directly the read BIO that holds the response contents,
  285. which allows a response of indefinite length and may support streaming.
  286. The caller is responsible for freeing the BIO pointer obtained.
  287. .PP
  288. \&\fBOSSL_HTTP_get()\fR uses HTTP GET to obtain data from \fIbio\fR if non-NULL,
  289. else from the server contained in the \fIurl\fR, and returns it as a BIO.
  290. It supports redirection via HTTP status code 301 or 302. It is meant for
  291. transfers with a single round trip, so does not support persistent connections.
  292. If \fIbio\fR is non-NULL, any host and port components in the \fIurl\fR are not used
  293. for connecting but the hostname is used, as usual, for the \f(CW\*(C`Host\*(C'\fR header.
  294. Any userinfo and fragment components in the \fIurl\fR are ignored.
  295. Any query component is handled as part of the path component.
  296. If the scheme component of the \fIurl\fR is \f(CW\*(C`https\*(C'\fR a TLS connection is requested
  297. and the \fIbio_update_fn\fR, as described for \fBOSSL_HTTP_open()\fR, must be provided.
  298. Also the remaining parameters are interpreted as described for \fBOSSL_HTTP_open()\fR
  299. and \fBOSSL_HTTP_set1_request()\fR, respectively.
  300. The caller is responsible for freeing the BIO pointer obtained.
  301. .PP
  302. \&\fBOSSL_HTTP_transfer()\fR exchanges an HTTP request and response
  303. over a connection managed via \fIprctx\fR without supporting redirection.
  304. It combines \fBOSSL_HTTP_open()\fR, \fBOSSL_HTTP_set1_request()\fR, \fBOSSL_HTTP_exchange()\fR,
  305. and \fBOSSL_HTTP_close()\fR.
  306. If \fIprctx\fR is not NULL it reuses any open connection represented by a non-NULL
  307. \&\fI*prctx\fR. It keeps the connection open if a persistent connection is requested
  308. or required and this was granted by the server, else it closes the connection
  309. and assigns NULL to \fI*prctx\fR.
  310. The remaining parameters are interpreted as described for \fBOSSL_HTTP_open()\fR
  311. and \fBOSSL_HTTP_set1_request()\fR, respectively.
  312. The caller is responsible for freeing the BIO pointer obtained.
  313. .PP
  314. \&\fBOSSL_HTTP_close()\fR closes the connection and releases \fIrctx\fR.
  315. The \fIok\fR parameter is passed to any BIO update function
  316. given during setup as described above for \fBOSSL_HTTP_open()\fR.
  317. It must be 1 if no error occurred during the HTTP transfer and 0 otherwise.
  318. .SH NOTES
  319. .IX Header "NOTES"
  320. The names of the environment variables used by this implementation:
  321. \&\f(CW\*(C`http_proxy\*(C'\fR, \f(CW\*(C`HTTP_PROXY\*(C'\fR, \f(CW\*(C`https_proxy\*(C'\fR, \f(CW\*(C`HTTPS_PROXY\*(C'\fR, \f(CW\*(C`no_proxy\*(C'\fR, and
  322. \&\f(CW\*(C`NO_PROXY\*(C'\fR, have been chosen for maximal compatibility with
  323. other HTTP client implementations such as wget, curl, and git.
  324. .PP
  325. When built with tracing enabled, \fBOSSL_HTTP_transfer()\fR and all functions using it
  326. may be traced using \fBOSSL_TRACE_CATEGORY_HTTP\fR.
  327. See also \fBOSSL_trace_enabled\fR\|(3) and \fBopenssl\-env\fR\|(7).
  328. .SH "RETURN VALUES"
  329. .IX Header "RETURN VALUES"
  330. \&\fBOSSL_HTTP_open()\fR returns on success a \fBOSSL_HTTP_REQ_CTX\fR, else NULL.
  331. .PP
  332. \&\fBOSSL_HTTP_proxy_connect()\fR and \fBOSSL_HTTP_set1_request()\fR
  333. return 1 on success, 0 on error.
  334. .PP
  335. On success, \fBOSSL_HTTP_exchange()\fR, \fBOSSL_HTTP_get()\fR, and \fBOSSL_HTTP_transfer()\fR
  336. return a memory BIO that buffers all the data received if an ASN.1\-encoded
  337. response is expected, otherwise a BIO that may support streaming.
  338. The BIO must be freed by the caller.
  339. On failure, they return NULL.
  340. Failure conditions include connection/transfer timeout, parse errors, etc.
  341. The caller is responsible for freeing the BIO pointer obtained.
  342. .PP
  343. \&\fBOSSL_HTTP_close()\fR returns 0 if anything went wrong while disconnecting, else 1.
  344. .SH "SEE ALSO"
  345. .IX Header "SEE ALSO"
  346. \&\fBOSSL_HTTP_parse_url\fR\|(3), \fBBIO_new_connect\fR\|(3),
  347. \&\fBASN1_item_i2d_mem_bio\fR\|(3), \fBASN1_item_d2i_bio\fR\|(3),
  348. \&\fBOSSL_HTTP_is_alive\fR\|(3),
  349. \&\fBOSSL_trace_enabled\fR\|(3), and \fBopenssl\-env\fR\|(7).
  350. .SH HISTORY
  351. .IX Header "HISTORY"
  352. All the functions described here were added in OpenSSL 3.0.
  353. .SH COPYRIGHT
  354. .IX Header "COPYRIGHT"
  355. Copyright 2019\-2024 The OpenSSL Project Authors. All Rights Reserved.
  356. .PP
  357. Licensed under the Apache License 2.0 (the "License"). You may not use
  358. this file except in compliance with the License. You can obtain a copy
  359. in the file LICENSE in the source distribution or at
  360. <https://www.openssl.org/source/license.html>.