_http_examples.qbk 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. [/
  2. Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Official repository: https://github.com/boostorg/beast
  6. ]
  7. [section:more_examples HTTP Examples]
  8. These examples in this section are working functions that may be found
  9. in the examples directory. They demonstrate the usage of the library for
  10. a variety of scenarios.
  11. [section:change_body_type Change Body Type __example__]
  12. Sophisticated servers may wish to defer the choice of the Body template type
  13. until after the header is available. Then, a body type may be chosen
  14. depending on the header contents. For example, depending on the verb,
  15. target path, or target query parameters. To accomplish this, a parser
  16. is declared to read in the header only, using a trivial body type such as
  17. [link beast.ref.boost__beast__http__empty_body `empty_body`]. Then, a new parser is constructed
  18. from this existing parser where the body type is conditionally determined
  19. by information from the header or elsewhere.
  20. This example illustrates how a server may make the commitment of a body
  21. type depending on the method verb:
  22. [example_http_defer_body]
  23. [endsect]
  24. [section:expect_100_continue_client Expect 100-continue (Client) __example__]
  25. The Expect field with the value "100-continue" in a request is special. It
  26. indicates that the after sending the message header, a client desires an
  27. immediate informational response before sending the message body, which
  28. presumably may be expensive to compute or large. This behavior is described in
  29. [@https://tools.ietf.org/html/rfc7231#section-5.1.1 rfc7231 section 5.1.1].
  30. Invoking the 100-continue behavior is implemented easily in a client by
  31. constructing a __serializer__ to send the header first, then receiving
  32. the server response, and finally conditionally send the body using the same
  33. serializer instance. A synchronous, simplified version (no timeout) of
  34. this client action looks like this:
  35. [example_http_send_expect_100_continue]
  36. [endsect]
  37. [section:expect_100_continue_server Expect 100-continue (Server) __example__]
  38. The Expect field with the value "100-continue" in a request is special. It
  39. indicates that the after sending the message header, a client desires an
  40. immediate informational response before sending the message body, which
  41. presumably may be expensive to compute or large. This behavior is described in
  42. [@https://tools.ietf.org/html/rfc7231#section-5.1.1 rfc7231 section 5.1.1].
  43. Handling the Expect field can be implemented easily in a server by constructing
  44. a __parser__ to read the header first, then send an informational HTTP
  45. response, and finally read the body using the same parser instance. A
  46. synchronous version of this server action looks like this:
  47. [example_http_receive_expect_100_continue]
  48. [endsect]
  49. [section:head_request_client HEAD request (Client) __example__]
  50. The
  51. [@https://tools.ietf.org/html/rfc7231#section-4.3.2 HEAD request]
  52. method indicates to the server that the client wishes to receive the
  53. entire header that would be delivered if the method was GET, except
  54. that the body is omitted. When a client wishes to receive the response
  55. to a HEAD request, it is necessary to inform the parser not to expect
  56. a body. This is done by calling
  57. [link beast.ref.boost__beast__http__basic_parser.skip `basic_parser::skip`]
  58. with the value `true`, as shown in this example:
  59. [example_http_do_head_request]
  60. [endsect]
  61. [section:head_response_server HEAD response (Server) __example__]
  62. When a server receives a
  63. [@https://tools.ietf.org/html/rfc7231#section-4.3.2 HEAD request],
  64. the response should contain the entire header that would be delivered
  65. if the method was GET, except that the body is omitted.
  66. [example_http_do_head_response]
  67. [endsect]
  68. [section:http_relay HTTP Relay __example__]
  69. An HTTP proxy acts as a relay between client and server. The proxy reads a
  70. request from the client and sends it to the server, possibly adjusting some
  71. of the headers and representation of the body along the way. Then, the
  72. proxy reads a response from the server and sends it back to the client,
  73. also with the possibility of changing the headers and body representation.
  74. The example that follows implements a synchronous HTTP relay. It uses a
  75. fixed size buffer, to avoid reading in the entire body so that the upstream
  76. connection sees a header without unnecessary latency. This example brings
  77. together all of the concepts discussed so far, it uses both a __serializer__
  78. and a __parser__ to achieve its goal:
  79. [example_http_relay]
  80. [endsect]
  81. [section:send_child_process_output Send Child Process Output __example__]
  82. Sometimes it is necessary to send a message whose body is not conveniently
  83. described by a single container. For example, when implementing an HTTP relay
  84. function a robust implementation needs to present body buffers individually
  85. as they become available from the downstream host. These buffers should be
  86. fixed in size, otherwise creating the unnecessary and inefficient burden of
  87. reading the complete message body before forwarding it to the upstream host.
  88. To enable these use-cases, the body type __buffer_body__ is provided. This
  89. body uses a caller-provided pointer and size instead of an owned container.
  90. To use this body, instantiate an instance of the serializer and fill in
  91. the pointer and size fields before calling a stream write function.
  92. This example reads from a child process and sends the output back in an
  93. HTTP response. The output of the process is sent as it becomes available:
  94. [example_http_send_cgi_response]
  95. [endsect]
  96. [endsect]