spec.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. =============
  2. Specification
  3. =============
  4. RingPHP applications consist of handlers, requests, responses, and
  5. middleware.
  6. Handlers
  7. --------
  8. Handlers are implemented as a PHP ``callable`` that accept a request array
  9. and return a response array (``GuzzleHttp\Ring\Future\FutureArrayInterface``).
  10. For example:
  11. .. code-block:: php
  12. use GuzzleHttp\Ring\Future\CompletedFutureArray;
  13. $mockHandler = function (array $request) {
  14. return new CompletedFutureArray([
  15. 'status' => 200,
  16. 'headers' => ['X-Foo' => ['Bar']],
  17. 'body' => 'Hello!'
  18. ]);
  19. };
  20. This handler returns the same response each time it is invoked. All RingPHP
  21. handlers must return a ``GuzzleHttp\Ring\Future\FutureArrayInterface``. Use
  22. ``GuzzleHttp\Ring\Future\CompletedFutureArray`` when returning a response that
  23. has already completed.
  24. Requests
  25. --------
  26. A request array is a PHP associative array that contains the configuration
  27. settings need to send a request.
  28. .. code-block:: php
  29. $request = [
  30. 'http_method' => 'GET',
  31. 'scheme' => 'http',
  32. 'uri' => '/',
  33. 'body' => 'hello!',
  34. 'client' => ['timeout' => 1.0],
  35. 'headers' => [
  36. 'host' => ['httpbin.org'],
  37. 'X-Foo' => ['baz', 'bar']
  38. ]
  39. ];
  40. The request array contains the following key value pairs:
  41. request_method
  42. (string, required) The HTTP request method, must be all caps corresponding
  43. to a HTTP request method, such as ``GET`` or ``POST``.
  44. scheme
  45. (string) The transport protocol, must be one of ``http`` or ``https``.
  46. Defaults to ``http``.
  47. uri
  48. (string, required) The request URI excluding the query string. Must
  49. start with "/".
  50. query_string
  51. (string) The query string, if present (e.g., ``foo=bar``).
  52. version
  53. (string) HTTP protocol version. Defaults to ``1.1``.
  54. headers
  55. (required, array) Associative array of headers. Each key represents the
  56. header name. Each value contains an array of strings where each entry of
  57. the array SHOULD be sent over the wire on a separate header line.
  58. body
  59. (string, fopen resource, ``Iterator``, ``GuzzleHttp\Stream\StreamInterface``)
  60. The body of the request, if present. Can be a string, resource returned
  61. from fopen, an ``Iterator`` that yields chunks of data, an object that
  62. implemented ``__toString``, or a ``GuzzleHttp\Stream\StreamInterface``.
  63. future
  64. (bool, string) Controls the asynchronous behavior of a response.
  65. Set to ``true`` or omit the ``future`` option to *request* that a request
  66. will be completed asynchronously. Keep in mind that your request might not
  67. necessarily be completed asynchronously based on the handler you are using.
  68. Set the ``future`` option to ``false`` to request that a synchronous
  69. response be provided.
  70. You can provide a string value to specify fine-tuned future behaviors that
  71. may be specific to the underlying handlers you are using. There are,
  72. however, some common future options that handlers should implement if
  73. possible.
  74. lazy
  75. Requests that the handler does not open and send the request
  76. immediately, but rather only opens and sends the request once the
  77. future is dereferenced. This option is often useful for sending a large
  78. number of requests concurrently to allow handlers to take better
  79. advantage of non-blocking transfers by first building up a pool of
  80. requests.
  81. If an handler does not implement or understand a provided string value,
  82. then the request MUST be treated as if the user provided ``true`` rather
  83. than the string value.
  84. Future responses created by asynchronous handlers MUST attempt to complete
  85. any outstanding future responses when they are destructed. Asynchronous
  86. handlers MAY choose to automatically complete responses when the number
  87. of outstanding requests reaches an handler-specific threshold.
  88. Client Specific Options
  89. ~~~~~~~~~~~~~~~~~~~~~~~
  90. The following options are only used in ring client handlers.
  91. .. _client-options:
  92. client
  93. (array) Associative array of client specific transfer options. The
  94. ``client`` request key value pair can contain the following keys:
  95. cert
  96. (string, array) Set to a string to specify the path to a file
  97. containing a PEM formatted SSL client side certificate. If a password
  98. is required, then set ``cert`` to an array containing the path to the
  99. PEM file in the first array element followed by the certificate
  100. password in the second array element.
  101. connect_timeout
  102. (float) Float describing the number of seconds to wait while trying to
  103. connect to a server. Use ``0`` to wait indefinitely (the default
  104. behavior).
  105. debug
  106. (bool, fopen() resource) Set to true or set to a PHP stream returned by
  107. fopen() to enable debug output with the handler used to send a request.
  108. If set to ``true``, the output is written to PHP's STDOUT. If a PHP
  109. ``fopen`` resource handle is provided, the output is written to the
  110. stream.
  111. "Debug output" is handler specific: different handlers will yield
  112. different output and various various level of detail. For example, when
  113. using cURL to transfer requests, cURL's `CURLOPT_VERBOSE <http://curl.haxx.se/libcurl/c/CURLOPT_VERBOSE.html>`_
  114. will be used. When using the PHP stream wrapper, `stream notifications <http://php.net/manual/en/function.stream-notification-callback.php>`_
  115. will be emitted.
  116. decode_content
  117. (bool) Specify whether or not ``Content-Encoding`` responses
  118. (gzip, deflate, etc.) are automatically decoded. Set to ``true`` to
  119. automatically decode encoded responses. Set to ``false`` to not decode
  120. responses. By default, content is *not* decoded automatically.
  121. delay
  122. (int) The number of milliseconds to delay before sending the request.
  123. This is often used for delaying before retrying a request. Handlers
  124. SHOULD implement this if possible, but it is not a strict requirement.
  125. progress
  126. (function) Defines a function to invoke when transfer progress is made.
  127. The function accepts the following arguments:
  128. 1. The total number of bytes expected to be downloaded
  129. 2. The number of bytes downloaded so far
  130. 3. The number of bytes expected to be uploaded
  131. 4. The number of bytes uploaded so far
  132. proxy
  133. (string, array) Pass a string to specify an HTTP proxy, or an
  134. associative array to specify different proxies for different protocols
  135. where the scheme is the key and the value is the proxy address.
  136. .. code-block:: php
  137. $request = [
  138. 'http_method' => 'GET',
  139. 'headers' => ['host' => ['httpbin.org']],
  140. 'client' => [
  141. // Use different proxies for different URI schemes.
  142. 'proxy' => [
  143. 'http' => 'http://proxy.example.com:5100',
  144. 'https' => 'https://proxy.example.com:6100'
  145. ]
  146. ]
  147. ];
  148. ssl_key
  149. (string, array) Specify the path to a file containing a private SSL key
  150. in PEM format. If a password is required, then set to an array
  151. containing the path to the SSL key in the first array element followed
  152. by the password required for the certificate in the second element.
  153. save_to
  154. (string, fopen resource, ``GuzzleHttp\Stream\StreamInterface``)
  155. Specifies where the body of the response is downloaded. Pass a string to
  156. open a local file on disk and save the output to the file. Pass an fopen
  157. resource to save the output to a PHP stream resource. Pass a
  158. ``GuzzleHttp\Stream\StreamInterface`` to save the output to a Guzzle
  159. StreamInterface. Omitting this option will typically save the body of a
  160. response to a PHP temp stream.
  161. stream
  162. (bool) Set to true to stream a response rather than download it all
  163. up-front. This option will only be utilized when the corresponding
  164. handler supports it.
  165. timeout
  166. (float) Float describing the timeout of the request in seconds. Use 0 to
  167. wait indefinitely (the default behavior).
  168. verify
  169. (bool, string) Describes the SSL certificate verification behavior of a
  170. request. Set to true to enable SSL certificate verification using the
  171. system CA bundle when available (the default). Set to false to disable
  172. certificate verification (this is insecure!). Set to a string to provide
  173. the path to a CA bundle on disk to enable verification using a custom
  174. certificate.
  175. version
  176. (string) HTTP protocol version to use with the request.
  177. Server Specific Options
  178. ~~~~~~~~~~~~~~~~~~~~~~~
  179. The following options are only used in ring server handlers.
  180. server_port
  181. (integer) The port on which the request is being handled. This is only
  182. used with ring servers, and is required.
  183. server_name
  184. (string) The resolved server name, or the server IP address. Required when
  185. using a Ring server.
  186. remote_addr
  187. (string) The IP address of the client or the last proxy that sent the
  188. request. Required when using a Ring server.
  189. Responses
  190. ---------
  191. A response is an array-like object that implements
  192. ``GuzzleHttp\Ring\Future\FutureArrayInterface``. Responses contain the
  193. following key value pairs:
  194. body
  195. (string, fopen resource, ``Iterator``, ``GuzzleHttp\Stream\StreamInterface``)
  196. The body of the response, if present. Can be a string, resource returned
  197. from fopen, an ``Iterator`` that yields chunks of data, an object that
  198. implemented ``__toString``, or a ``GuzzleHttp\Stream\StreamInterface``.
  199. effective_url
  200. (string) The URL that returned the resulting response.
  201. error
  202. (``\Exception``) Contains an exception describing any errors that were
  203. encountered during the transfer.
  204. headers
  205. (Required, array) Associative array of headers. Each key represents the
  206. header name. Each value contains an array of strings where each entry of
  207. the array is a header line. The headers array MAY be an empty array in the
  208. event an error occurred before a response was received.
  209. reason
  210. (string) Optional reason phrase. This option should be provided when the
  211. reason phrase does not match the typical reason phrase associated with the
  212. ``status`` code. See `RFC 7231 <http://tools.ietf.org/html/rfc7231#section-6.1>`_
  213. for a list of HTTP reason phrases mapped to status codes.
  214. status
  215. (Required, integer) The HTTP status code. The status code MAY be set to
  216. ``null`` in the event an error occurred before a response was received
  217. (e.g., a networking error).
  218. transfer_stats
  219. (array) Provides an associative array of arbitrary transfer statistics if
  220. provided by the underlying handler.
  221. version
  222. (string) HTTP protocol version. Defaults to ``1.1``.
  223. Middleware
  224. ----------
  225. Ring middleware augments the functionality of handlers by invoking them in the
  226. process of generating responses. Middleware is typically implemented as a
  227. higher-order function that takes one or more handlers as arguments followed by
  228. an optional associative array of options as the last argument, returning a new
  229. handler with the desired compound behavior.
  230. Here's an example of a middleware that adds a Content-Type header to each
  231. request.
  232. .. code-block:: php
  233. use GuzzleHttp\Ring\Client\CurlHandler;
  234. use GuzzleHttp\Ring\Core;
  235. $contentTypeHandler = function(callable $handler, $contentType) {
  236. return function (array $request) use ($handler, $contentType) {
  237. return $handler(Core::setHeader('Content-Type', $contentType));
  238. };
  239. };
  240. $baseHandler = new CurlHandler();
  241. $wrappedHandler = $contentTypeHandler($baseHandler, 'text/html');
  242. $response = $wrappedHandler([/** request hash **/]);