GuzzleHttpPsr7Request

PSR-7 request implementation.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Psr7/Request.php  
  1. class Request implements RequestInterface 
  2. use MessageTrait; 
  3.  
  4. /** @var string */ 
  5. private $method; 
  6.  
  7. /** @var null|string */ 
  8. private $requestTarget; 
  9.  
  10. /** @var null|UriInterface */ 
  11. private $uri; 
  12.  
  13. /** 
  14. * @param string $method HTTP method 
  15. * @param string|UriInterface $uri URI 
  16. * @param array $headers Request headers 
  17. * @param string|null|resource|StreamInterface $body Request body 
  18. * @param string $version Protocol version 
  19. */ 
  20. public function __construct( 
  21. $method,  
  22. $uri,  
  23. array $headers = [],  
  24. $body = null,  
  25. $version = '1.1' 
  26. ) { 
  27. if (!($uri instanceof UriInterface)) { 
  28. $uri = new Uri($uri); 
  29.  
  30. $this->method = strtoupper($method); 
  31. $this->uri = $uri; 
  32. $this->setHeaders($headers); 
  33. $this->protocol = $version; 
  34.  
  35. if (!$this->hasHeader('Host')) { 
  36. $this->updateHostFromUri(); 
  37.  
  38. if ($body !== '' && $body !== null) { 
  39. $this->stream = stream_for($body); 
  40.  
  41. public function getRequestTarget() 
  42. if ($this->requestTarget !== null) { 
  43. return $this->requestTarget; 
  44.  
  45. $target = $this->uri->getPath(); 
  46. if ($target == '') { 
  47. $target = '/'; 
  48. if ($this->uri->getQuery() != '') { 
  49. $target .= '?' . $this->uri->getQuery(); 
  50.  
  51. return $target; 
  52.  
  53. public function withRequestTarget($requestTarget) 
  54. if (preg_match('#\s#', $requestTarget)) { 
  55. throw new InvalidArgumentException( 
  56. 'Invalid request target provided; cannot contain whitespace' 
  57. ); 
  58.  
  59. $new = clone $this; 
  60. $new->requestTarget = $requestTarget; 
  61. return $new; 
  62.  
  63. public function getMethod() 
  64. return $this->method; 
  65.  
  66. public function withMethod($method) 
  67. $new = clone $this; 
  68. $new->method = strtoupper($method); 
  69. return $new; 
  70.  
  71. public function getUri() 
  72. return $this->uri; 
  73.  
  74. public function withUri(UriInterface $uri, $preserveHost = false) 
  75. if ($uri === $this->uri) { 
  76. return $this; 
  77.  
  78. $new = clone $this; 
  79. $new->uri = $uri; 
  80.  
  81. if (!$preserveHost) { 
  82. $new->updateHostFromUri(); 
  83.  
  84. return $new; 
  85.  
  86. private function updateHostFromUri() 
  87. $host = $this->uri->getHost(); 
  88.  
  89. if ($host == '') { 
  90. return; 
  91.  
  92. if (($port = $this->uri->getPort()) !== null) { 
  93. $host .= ':' . $port; 
  94.  
  95. if (isset($this->headerNames['host'])) { 
  96. $header = $this->headerNames['host']; 
  97. } else { 
  98. $header = 'Host'; 
  99. $this->headerNames['host'] = 'Host'; 
  100. // Ensure Host is the first header. 
  101. // See: http://tools.ietf.org/html/rfc7230#section-5.4 
  102. $this->headers = [$header => [$host]] + $this->headers;