01_primer.qbk 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 Protocol Primer]
  8. The HTTP protocol defines the
  9. [@https://tools.ietf.org/html/rfc7230#section-2.1 client and server roles]:
  10. clients send requests and servers send back responses. When a client and
  11. server have established a connection, the client sends a series of requests
  12. while the server sends back at least one response for each received request
  13. in the order those requests were received.
  14. A request or response is an
  15. [@https://tools.ietf.org/html/rfc7230#section-3 HTTP message]
  16. (referred to hereafter as "message") having two parts:
  17. a header with structured metadata and an optional variable-length body
  18. holding arbitrary data. A serialized header is one or more text lines
  19. where each line ends in a carriage return followed by linefeed (`"\r\n"`).
  20. An empty line marks the end of the header. The first line in the header
  21. is called the ['start-line]. The contents of the start line contents are
  22. different for requests and responses.
  23. Every message contains a set of zero or more field name/value pairs,
  24. collectively called "fields". The names and values are represented using
  25. text strings with various requirements. A serialized field contains the
  26. field name, then a colon followed by a space (`": "`), and finally the field
  27. value with a trailing CRLF.
  28. [heading Requests]
  29. Clients send requests, which contain a
  30. [@https://tools.ietf.org/html/rfc7230#section-3.1.1 method]
  31. and
  32. [@https://tools.ietf.org/html/rfc7230#section-5.3 request-target],
  33. and
  34. [@https://tools.ietf.org/html/rfc7230#section-2.6 HTTP-version].
  35. The method identifies the operation to be performed while the target
  36. identifies the object on the server to which the operation applies.
  37. The version is almost always 1.1, but older programs sometimes use 1.0.
  38. [table
  39. [[Serialized Request][Description]]
  40. [[
  41. ```
  42. GET / HTTP/1.1\r\n
  43. User-Agent: Beast\r\n
  44. \r\n
  45. ```
  46. ][
  47. This request has a method of "GET", a target of "/", and indicates
  48. HTTP version 1.1. It contains a single field called "User-Agent"
  49. whose value is "Beast". There is no message body.
  50. ]]
  51. ]
  52. [heading Responses]
  53. Servers send responses, which contain a
  54. [@https://tools.ietf.org/html/rfc7231#section-6 status-code],
  55. [@https://tools.ietf.org/html/rfc7230#section-3.1.2 reason-phrase], and
  56. [@https://tools.ietf.org/html/rfc7230#section-2.6 HTTP-version].
  57. The reason phrase is
  58. [@https://tools.ietf.org/html/rfc7230#section-3.1.2 obsolete]:
  59. clients SHOULD ignore the reason-phrase content. Here is a response which
  60. includes a body. The special
  61. [@https://tools.ietf.org/html/rfc7230#section-3.3.2 Content-Length]
  62. field informs the remote host of the size of the body which follows.
  63. [table
  64. [[Serialized Response][Description]]
  65. [[
  66. ```
  67. HTTP/1.1 200 OK\r\n
  68. Server: Beast\r\n
  69. Content-Length: 13\r\n
  70. \r\n
  71. Hello, world!
  72. ```
  73. ][
  74. This response has a
  75. [@https://tools.ietf.org/html/rfc7231#section-6 200 status code]
  76. meaning the operation requested completed successfully. The obsolete
  77. reason phrase is "OK". It specifies HTTP version 1.1, and contains
  78. a body 13 octets in size with the text "Hello, world!".
  79. ]]
  80. ]
  81. [heading Body]
  82. Messages may optionally carry a body. The size of the message body
  83. is determined by the semantics of the message and the special fields
  84. Content-Length and Transfer-Encoding.
  85. [@https://tools.ietf.org/html/rfc7230#section-3.3 rfc7230 section 3.3]
  86. provides a comprehensive description for how the body length is
  87. determined.
  88. [heading Special Fields]
  89. Certain fields appearing in messages are special. The library understands
  90. these fields when performing serialization and parsing, taking automatic
  91. action as needed when the fields are parsed in a message and also setting
  92. the fields if the caller requests it.
  93. [table Special Fields
  94. [[Field][Description]]
  95. [
  96. [
  97. [@https://tools.ietf.org/html/rfc7230#section-6.1 [*`Connection`]]
  98. [@https://tools.ietf.org/html/rfc7230#appendix-A.1.2 [*`Proxy-Connection`]]
  99. ][
  100. This field allows the sender to indicate desired control options
  101. for the current connection. Common values include "close",
  102. "keep-alive", and "upgrade".
  103. ]
  104. ][
  105. [
  106. [@https://tools.ietf.org/html/rfc7230#section-3.3.2 [*`Content-Length`]]
  107. ][
  108. When present, this field informs the recipient about the exact
  109. size in bytes of the body which follows the message header.
  110. ]
  111. ][
  112. [
  113. [@https://tools.ietf.org/html/rfc7230#section-3.3.1 [*`Transfer-Encoding`]]
  114. ][
  115. This optional field lists the names of the sequence of transfer codings
  116. that have been (or will be) applied to the content payload to form
  117. the message body.
  118. Beast understands the "chunked" coding scheme when it is the last
  119. (outermost) applied coding. The library will automatically apply
  120. chunked encoding when the content length is not known ahead of time
  121. during serialization, and the library will automatically remove chunked
  122. encoding from parsed messages when present.
  123. ]
  124. ][
  125. [
  126. [@https://tools.ietf.org/html/rfc7230#section-6.7 [*`Upgrade`]]
  127. ][
  128. The Upgrade header field provides a mechanism to transition from
  129. HTTP/1.1 to another protocol on the same connection. For example, it
  130. is the mechanism used by WebSocket's initial HTTP handshake to
  131. establish a WebSocket connection.
  132. ]
  133. ]
  134. ]
  135. [endsect]