source: externals/php/vendor/guzzlehttp/psr7/src/Request.php@ 1ebaa4f

Last change on this file since 1ebaa4f was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[01a620f]1<?php
2namespace GuzzleHttp\Psr7;
3
4use InvalidArgumentException;
5use Psr\Http\Message\RequestInterface;
6use Psr\Http\Message\StreamInterface;
7use Psr\Http\Message\UriInterface;
8
9/**
10 * PSR-7 request implementation.
11 */
12class Request implements RequestInterface
13{
14 use MessageTrait {
15 withHeader as protected withParentHeader;
16 }
17
18 /** @var string */
19 private $method;
20
21 /** @var null|string */
22 private $requestTarget;
23
24 /** @var null|UriInterface */
25 private $uri;
26
27 /**
28 * @param null|string $method HTTP method for the request.
29 * @param null|string $uri URI for the request.
30 * @param array $headers Headers for the message.
31 * @param string|resource|StreamInterface $body Message body.
32 * @param string $protocolVersion HTTP protocol version.
33 *
34 * @throws InvalidArgumentException for an invalid URI
35 */
36 public function __construct(
37 $method,
38 $uri,
39 array $headers = [],
40 $body = null,
41 $protocolVersion = '1.1'
42 ) {
43 if (is_string($uri)) {
44 $uri = new Uri($uri);
45 } elseif (!($uri instanceof UriInterface)) {
46 throw new \InvalidArgumentException(
47 'URI must be a string or Psr\Http\Message\UriInterface'
48 );
49 }
50
51 $this->method = strtoupper($method);
52 $this->uri = $uri;
53 $this->setHeaders($headers);
54 $this->protocol = $protocolVersion;
55
56 $host = $uri->getHost();
57 if ($host && !$this->hasHeader('Host')) {
58 $this->updateHostFromUri($host);
59 }
60
61 if ($body) {
62 $this->stream = stream_for($body);
63 }
64 }
65
66 public function getRequestTarget()
67 {
68 if ($this->requestTarget !== null) {
69 return $this->requestTarget;
70 }
71
72 $target = $this->uri->getPath();
73 if ($target == null) {
74 $target = '/';
75 }
76 if ($this->uri->getQuery()) {
77 $target .= '?' . $this->uri->getQuery();
78 }
79
80 return $target;
81 }
82
83 public function withRequestTarget($requestTarget)
84 {
85 if (preg_match('#\s#', $requestTarget)) {
86 throw new InvalidArgumentException(
87 'Invalid request target provided; cannot contain whitespace'
88 );
89 }
90
91 $new = clone $this;
92 $new->requestTarget = $requestTarget;
93 return $new;
94 }
95
96 public function getMethod()
97 {
98 return $this->method;
99 }
100
101 public function withMethod($method)
102 {
103 $new = clone $this;
104 $new->method = strtoupper($method);
105 return $new;
106 }
107
108 public function getUri()
109 {
110 return $this->uri;
111 }
112
113 public function withUri(UriInterface $uri, $preserveHost = false)
114 {
115 if ($uri === $this->uri) {
116 return $this;
117 }
118
119 $new = clone $this;
120 $new->uri = $uri;
121
122 if (!$preserveHost) {
123 if ($host = $uri->getHost()) {
124 $new->updateHostFromUri($host);
125 }
126 }
127
128 return $new;
129 }
130
131 public function withHeader($header, $value)
132 {
133 /** @var Request $newInstance */
134 $newInstance = $this->withParentHeader($header, $value);
135 return $newInstance;
136 }
137
138 private function updateHostFromUri($host)
139 {
140 // Ensure Host is the first header.
141 // See: http://tools.ietf.org/html/rfc7230#section-5.4
142 if ($port = $this->uri->getPort()) {
143 $host .= ':' . $port;
144 }
145
146 $this->headerLines = ['Host' => [$host]] + $this->headerLines;
147 $this->headers = ['host' => [$host]] + $this->headers;
148 }
149}
Note: See TracBrowser for help on using the repository browser.