OSSL_HTTP_REQ_CTX.pod 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. =pod
  2. =head1 NAME
  3. OSSL_HTTP_REQ_CTX,
  4. OSSL_HTTP_REQ_CTX_new,
  5. OSSL_HTTP_REQ_CTX_free,
  6. OSSL_HTTP_REQ_CTX_set_request_line,
  7. OSSL_HTTP_REQ_CTX_add1_header,
  8. OSSL_HTTP_REQ_CTX_i2d,
  9. OSSL_HTTP_REQ_CTX_nbio,
  10. OSSL_HTTP_REQ_CTX_sendreq_d2i,
  11. OSSL_HTTP_REQ_CTX_get0_mem_bio,
  12. OSSL_HTTP_REQ_CTX_set_max_response_length
  13. - HTTP client low-level functions
  14. =head1 SYNOPSIS
  15. #include <openssl/http.h>
  16. typedef struct ossl_http_req_ctx_st OSSL_HTTP_REQ_CTX;
  17. OSSL_HTTP_REQ_CTX *OSSL_HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio,
  18. int method_POST, int maxline,
  19. unsigned long max_resp_len,
  20. int timeout,
  21. const char *expected_content_type,
  22. int expect_asn1);
  23. void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx);
  24. int OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX *rctx,
  25. const char *server, const char *port,
  26. const char *path);
  27. int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx,
  28. const char *name, const char *value);
  29. int OSSL_HTTP_REQ_CTX_i2d(OSSL_HTTP_REQ_CTX *rctx, const char *content_type,
  30. const ASN1_ITEM *it, ASN1_VALUE *req);
  31. int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx);
  32. ASN1_VALUE *OSSL_HTTP_REQ_CTX_sendreq_d2i(OSSL_HTTP_REQ_CTX *rctx,
  33. const ASN1_ITEM *it);
  34. BIO *OSSL_HTTP_REQ_CTX_get0_mem_bio(const OSSL_HTTP_REQ_CTX *rctx);
  35. void OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX *rctx,
  36. unsigned long len);
  37. =head1 DESCRIPTION
  38. B<OSSL_HTTP_REQ_CTX> is a context structure for an HTTP request, used to
  39. collect all the necessary data to perform that request.
  40. This file documents low-level HTTP functions rarely used directly. High-level
  41. HTTP client functions like L<OSSL_HTTP_get(3)> and L<OSSL_HTTP_transfer(3)>
  42. should be preferred.
  43. OSSL_HTTP_REQ_CTX_new() allocates a new HTTP request context structure,
  44. which gets populated with the B<BIO> to send the request to (I<wbio>),
  45. the B<BIO> to read the response from (I<rbio>, which may be equal to I<wbio>),
  46. the maximum expected response header line length (I<maxline>, where a value <= 0
  47. indicates that the B<HTTP_DEFAULT_MAX_LINE_LENGTH> of 4KiB should be used;
  48. this length is also used as the number of content bytes read at a time),
  49. the request method (I<method_POST>, which may be 1 to indicate that the C<POST>
  50. method is to be used, or 0 to indicate that the C<GET> method is to be used),
  51. the maximum allowed response content length (I<max_resp_len>, where 0 means
  52. that the B<HTTP_DEFAULT_MAX_RESP_LEN> is used, which currently is 100 KiB),
  53. a response timeout measure in seconds (I<timeout>,
  54. where 0 indicates no timeout, i.e., waiting indefinitely),
  55. the expected MIME content type of the response (I<expected_content_type>,
  56. which may be NULL for no expectation),
  57. and a flag indicating that the response is expected to be
  58. a DER encoded ASN.1 structure (I<expect_asn1>).
  59. The allocated context structure is also populated with an internal allocated
  60. memory B<BIO>, which collects the HTTP request and additional headers as text.
  61. The returned context should only be used for a single HTTP request/response.
  62. OSSL_HTTP_REQ_CTX_free() frees up the HTTP request context I<rctx>.
  63. The I<wbio> and I<rbio> are not free'd and it is up to the application
  64. to do so.
  65. OSSL_HTTP_REQ_CTX_set_request_line() adds the HTTP request line to the context.
  66. The request method itself becomes C<GET> or C<POST> depending on the value
  67. of I<method_POST> in the OSSL_HTTP_REQ_CTX_new() call. I<server> and I<port>
  68. may be set to indicate a proxy server and port that the request should go
  69. through, otherwise they should be left NULL. I<path> is the HTTP request path;
  70. if left NULL, C</> is used.
  71. OSSL_HTTP_REQ_CTX_add1_header() adds header I<name> with value I<value> to the
  72. context I<rctx>. It can be called more than once to add multiple headers.
  73. For example, to add a C<Host> header for C<example.com> you would call:
  74. OSSL_HTTP_REQ_CTX_add1_header(ctx, "Host", "example.com");
  75. OSSL_HTTP_REQ_CTX_i2d() finalizes the HTTP request context by adding
  76. the DER encoding of I<req>, using the ASN.1 template I<it> to do the encoding.
  77. The HTTP header C<Content-Length> is automatically filled out, and if
  78. I<content_type> isn't NULL, the HTTP header C<Content-Type> is also added with
  79. its content as value. All of this ends up in the internal memory B<BIO>.
  80. This requires that I<method_POST> was 1 in the OSSL_HTTP_REQ_CTX_new() call.
  81. OSSL_HTTP_REQ_CTX_nbio() attempts to send the request prepared I<rctx>
  82. and gathering the response via HTTP, using the I<rbio> and I<wbio>
  83. that were given when calling OSSL_HTTP_REQ_CTX_new().
  84. When successful, the contents of the internal memory B<BIO> contains
  85. the contents of the HTTP response, without the response headers.
  86. It may need to be called again if its result is -1, which indicates
  87. L<BIO_should_retry(3)>. In such a case it is advisable to sleep a little in
  88. between using L<BIO_wait(3)> on the read BIO to prevent a busy loop.
  89. OSSL_HTTP_REQ_CTX_sendreq_d2i() calls OSSL_HTTP_REQ_CTX_nbio(), possibly
  90. several times until a timeout is reached, and DER decodes the received
  91. response using the ASN.1 template I<it>.
  92. OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.
  93. Before sending the request, this could used to modify the HTTP request text.
  94. I<Use with caution!>
  95. After receiving a response via HTTP, the BIO represents
  96. the current state of reading the response headers and contents.
  97. OSSL_HTTP_REQ_CTX_set_max_response_length() sets the maximum allowed
  98. response content length for I<rctx> to I<len>. If not set or I<len> is 0
  99. then the B<HTTP_DEFAULT_MAX_RESP_LEN> is used, which currently is 100 KiB.
  100. If the C<Content-Length> header is present and exceeds this value or
  101. the content is an ASN.1 encoded structure with a length exceeding this value
  102. or both length indications are present but disagree then an error occurs.
  103. =head1 WARNINGS
  104. The server's response may be unexpected if the hostname that was used to
  105. create the I<wbio>, any C<Host> header, and the host specified in the
  106. request URL do not match.
  107. Many of these functions must be called in a certain order.
  108. First, the HTTP request context must be allocated:
  109. OSSL_HTTP_REQ_CTX_new().
  110. Then, the HTTP request must be prepared with request data:
  111. =over 4
  112. =item 1.
  113. Calling OSSL_HTTP_REQ_CTX_set_request_line(). This must be done exactly once.
  114. =item 2.
  115. Adding extra headers with OSSL_HTTP_REQ_CTX_add1_header().
  116. This is optional and may be done multiple times with different names.
  117. =item 3.
  118. Add C<POST> data with OSSL_HTTP_REQ_CTX_i2d(). This may only be done if
  119. I<method_POST> was 1 in the OSSL_HTTP_REQ_CTX_new() call, and must be done
  120. exactly once in that case.
  121. =back
  122. When the request context is fully prepared, the HTTP exchange may be performed
  123. with OSSL_HTTP_REQ_CTX_nbio() or OSSL_HTTP_REQ_CTX_sendreq_d2i().
  124. =head1 RETURN VALUES
  125. OSSL_HTTP_REQ_CTX_new() returns a pointer to a B<OSSL_HTTP_REQ_CTX>, or NULL
  126. on error.
  127. OSSL_HTTP_REQ_CTX_free() and OSSL_HTTP_REQ_CTX_set_max_response_length()
  128. do not return values.
  129. OSSL_HTTP_REQ_CTX_set_request_line(), OSSL_HTTP_REQ_CTX_add1_header(),
  130. OSSL_HTTP_REQ_CTX_i2d() and OSSL_HTTP_REQ_CTX_nbio return 1 for success and 0
  131. for failure.
  132. OSSL_HTTP_REQ_CTX_sendreq_d2i() returns a pointer to an B<ASN1_VALUE> for
  133. success and NULL for failure.
  134. OSSL_HTTP_REQ_CTX_get0_mem_bio() returns the internal memory B<BIO>.
  135. =head1 SEE ALSO
  136. L<BIO_should_retry(3)>,
  137. L<BIO_wait(3)>,
  138. L<OSSL_HTTP_get(3)>,
  139. L<OSSL_HTTP_transfer(3)>
  140. =head1 COPYRIGHT
  141. Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
  142. Licensed under the Apache License 2.0 (the "License"). You may not use
  143. this file except in compliance with the License. You can obtain a copy
  144. in the file LICENSE in the source distribution or at
  145. L<https://www.openssl.org/source/license.html>.
  146. =cut