Debian Codegraph @

vendor/react/http/src/Message/ServerRequest.php

raw
1 <?php
2
3 namespace React\Http\Message;
4
5 use Psr\Http\Message\ServerRequestInterface;
6 use Psr\Http\Message\StreamInterface;
7 use Psr\Http\Message\UriInterface;
8 use React\Http\Io\HttpBodyStream;
9 use React\Stream\ReadableStreamInterface;
10 use RingCentral\Psr7\Request;
11
12 /**
13 * Respresents an incoming server request message.
14 *
15 * This class implements the
16 * [PSR-7 `ServerRequestInterface`](https://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface)
17 * which extends the
18 * [PSR-7 `RequestInterface`](https://www.php-fig.org/psr/psr-7/#32-psrhttpmessagerequestinterface)
19 * which in turn extends the
20 * [PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface).
21 *
22 * This is mostly used internally to represent each incoming request message.
23 * Likewise, you can also use this class in test cases to test how your web
24 * application reacts to certain HTTP requests.
25 *
26 * > Internally, this implementation builds on top of an existing outgoing
27 * request message and only adds required server methods. This base class is
28 * considered an implementation detail that may change in the future.
29 *
30 * @see ServerRequestInterface
31 */
32 final class ServerRequest extends Request implements ServerRequestInterface
33 {
34 private $attributes = array();
35
36 private $serverParams;
37 private $fileParams = array();
38 private $cookies = array();
39 private $queryParams = array();
40 private $parsedBody;
41
42 /**
43 * @param string $method HTTP method for the request.
44 * @param string|UriInterface $url URL for the request.
45 * @param array<string,string|string[]> $headers Headers for the message.
46 * @param string|ReadableStreamInterface|StreamInterface $body Message body.
47 * @param string $version HTTP protocol version.
48 * @param array<string,string> $serverParams server-side parameters
49 * @throws \InvalidArgumentException for an invalid URL or body
50 */
51 public function __construct(
52 $method,
53 $url,
54 array $headers = array(),
55 $body = '',
56 $version = '1.1',
57 $serverParams = array()
58 ) {
59 $stream = null;
60 if ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) {
61 $stream = $body;
62 $body = null;
63 } elseif (!\is_string($body) && !$body instanceof StreamInterface) {
64 throw new \InvalidArgumentException('Invalid server request body given');
65 }
66
67 $this->serverParams = $serverParams;
68 parent::__construct($method, $url, $headers, $body, $version);
69
70 if ($stream !== null) {
71 $size = (int) $this->getHeaderLine('Content-Length');
72 if (\strtolower($this->getHeaderLine('Transfer-Encoding')) === 'chunked') {
73 $size = null;
74 }
75 $this->stream = new HttpBodyStream($stream, $size);
76 }
77
78 $query = $this->getUri()->getQuery();
79 if ($query !== '') {
80 \parse_str($query, $this->queryParams);
81 }
82
83 // Multiple cookie headers are not allowed according
84 // to https://tools.ietf.org/html/rfc6265#section-5.4
85 $cookieHeaders = $this->getHeader("Cookie");
86
87 if (count($cookieHeaders) === 1) {
88 $this->cookies = $this->parseCookie($cookieHeaders[0]);
89 }
90 }
91
92 public function getServerParams()
93 {
94 return $this->serverParams;
95 }
96
97 public function getCookieParams()
98 {
99 return $this->cookies;
100 }
101
102 public function withCookieParams(array $cookies)
103 {
104 $new = clone $this;
105 $new->cookies = $cookies;
106 return $new;
107 }
108
109 public function getQueryParams()
110 {
111 return $this->queryParams;
112 }
113
114 public function withQueryParams(array $query)
115 {
116 $new = clone $this;
117 $new->queryParams = $query;
118 return $new;
119 }
120
121 public function getUploadedFiles()
122 {
123 return $this->fileParams;
124 }
125
126 public function withUploadedFiles(array $uploadedFiles)
127 {
128 $new = clone $this;
129 $new->fileParams = $uploadedFiles;
130 return $new;
131 }
132
133 public function getParsedBody()
134 {
135 return $this->parsedBody;
136 }
137
138 public function withParsedBody($data)
139 {
140 $new = clone $this;
141 $new->parsedBody = $data;
142 return $new;
143 }
144
145 public function getAttributes()
146 {
147 return $this->attributes;
148 }
149
150 public function getAttribute($name, $default = null)
151 {
152 if (!\array_key_exists($name, $this->attributes)) {
153 return $default;
154 }
155 return $this->attributes[$name];
156 }
157
158 public function withAttribute($name, $value)
159 {
160 $new = clone $this;
161 $new->attributes[$name] = $value;
162 return $new;
163 }
164
165 public function withoutAttribute($name)
166 {
167 $new = clone $this;
168 unset($new->attributes[$name]);
169 return $new;
170 }
171
172 /**
173 * @param string $cookie
174 * @return array
175 */
176 private function parseCookie($cookie)
177 {
178 $cookieArray = \explode(';', $cookie);
179 $result = array();
180
181 foreach ($cookieArray as $pair) {
182 $pair = \trim($pair);
183 $nameValuePair = \explode('=', $pair, 2);
184
185 if (\count($nameValuePair) === 2) {
186 $key = \urldecode($nameValuePair[0]);
187 $value = \urldecode($nameValuePair[1]);
188 $result[$key] = $value;
189 }
190 }
191
192 return $result;
193 }
194 }