RequestInterface.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. namespace Psr\Http\Message;
  4. /**
  5. * Representation of an outgoing, client-side request.
  6. *
  7. * Per the HTTP specification, this interface includes properties for
  8. * each of the following:
  9. *
  10. * - Protocol version
  11. * - HTTP method
  12. * - URI
  13. * - Headers
  14. * - Message body
  15. *
  16. * During construction, implementations MUST attempt to set the Host header from
  17. * a provided URI if no Host header is provided.
  18. *
  19. * Requests are considered immutable; all methods that might change state MUST
  20. * be implemented such that they retain the internal state of the current
  21. * message and return an instance that contains the changed state.
  22. */
  23. interface RequestInterface extends MessageInterface
  24. {
  25. /**
  26. * Retrieves the message's request target.
  27. *
  28. * Retrieves the message's request-target either as it will appear (for
  29. * clients), as it appeared at request (for servers), or as it was
  30. * specified for the instance (see withRequestTarget()).
  31. *
  32. * In most cases, this will be the origin-form of the composed URI,
  33. * unless a value was provided to the concrete implementation (see
  34. * withRequestTarget() below).
  35. *
  36. * If no URI is available, and no request-target has been specifically
  37. * provided, this method MUST return the string "/".
  38. *
  39. * @return string
  40. */
  41. public function getRequestTarget();
  42. /**
  43. * Return an instance with the specific request-target.
  44. *
  45. * If the request needs a non-origin-form request-target — e.g., for
  46. * specifying an absolute-form, authority-form, or asterisk-form —
  47. * this method may be used to create an instance with the specified
  48. * request-target, verbatim.
  49. *
  50. * This method MUST be implemented in such a way as to retain the
  51. * immutability of the message, and MUST return an instance that has the
  52. * changed request target.
  53. *
  54. * @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various
  55. * request-target forms allowed in request messages)
  56. * @param string $requestTarget
  57. * @return static
  58. */
  59. public function withRequestTarget(string $requestTarget);
  60. /**
  61. * Retrieves the HTTP method of the request.
  62. *
  63. * @return string Returns the request method.
  64. */
  65. public function getMethod();
  66. /**
  67. * Return an instance with the provided HTTP method.
  68. *
  69. * While HTTP method names are typically all uppercase characters, HTTP
  70. * method names are case-sensitive and thus implementations SHOULD NOT
  71. * modify the given string.
  72. *
  73. * This method MUST be implemented in such a way as to retain the
  74. * immutability of the message, and MUST return an instance that has the
  75. * changed request method.
  76. *
  77. * @param string $method Case-sensitive method.
  78. * @return static
  79. * @throws \InvalidArgumentException for invalid HTTP methods.
  80. */
  81. public function withMethod(string $method);
  82. /**
  83. * Retrieves the URI instance.
  84. *
  85. * This method MUST return a UriInterface instance.
  86. *
  87. * @link http://tools.ietf.org/html/rfc3986#section-4.3
  88. * @return UriInterface Returns a UriInterface instance
  89. * representing the URI of the request.
  90. */
  91. public function getUri();
  92. /**
  93. * Returns an instance with the provided URI.
  94. *
  95. * This method MUST update the Host header of the returned request by
  96. * default if the URI contains a host component. If the URI does not
  97. * contain a host component, any pre-existing Host header MUST be carried
  98. * over to the returned request.
  99. *
  100. * You can opt-in to preserving the original state of the Host header by
  101. * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  102. * `true`, this method interacts with the Host header in the following ways:
  103. *
  104. * - If the Host header is missing or empty, and the new URI contains
  105. * a host component, this method MUST update the Host header in the returned
  106. * request.
  107. * - If the Host header is missing or empty, and the new URI does not contain a
  108. * host component, this method MUST NOT update the Host header in the returned
  109. * request.
  110. * - If a Host header is present and non-empty, this method MUST NOT update
  111. * the Host header in the returned request.
  112. *
  113. * This method MUST be implemented in such a way as to retain the
  114. * immutability of the message, and MUST return an instance that has the
  115. * new UriInterface instance.
  116. *
  117. * @link http://tools.ietf.org/html/rfc3986#section-4.3
  118. * @param UriInterface $uri New request URI to use.
  119. * @param bool $preserveHost Preserve the original state of the Host header.
  120. * @return static
  121. */
  122. public function withUri(UriInterface $uri, bool $preserveHost = false);
  123. }