PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / MessageTrait.php

MessageTrait.php in Media Cloud Sync 1.2.10, at includes/sdk/s3/GuzzleHttp/Psr7/MessageTrait.php

220 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
4
5 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
6 /**
7 * Trait implementing functionality common to requests and responses.
8 */
9 trait MessageTrait
10 {
11 /** @var array Map of all registered headers, as original name => array of values */
12 private $headers = [];
13 /** @var array Map of lowercase header name => original name at registration */
14 private $headerNames = [];
15 /** @var string */
16 private $protocol = '1.1';
17 /** @var StreamInterface|null */
18 private $stream;
19 public function getProtocolVersion()
20 {
21 return $this->protocol;
22 }
23 public function withProtocolVersion($version)
24 {
25 if ($this->protocol === $version) {
26 return $this;
27 }
28 $new = clone $this;
29 $new->protocol = $version;
30 return $new;
31 }
32 public function getHeaders()
33 {
34 return $this->headers;
35 }
36 public function hasHeader($header)
37 {
38 return isset($this->headerNames[\strtolower($header)]);
39 }
40 public function getHeader($header)
41 {
42 $header = \strtolower($header);
43 if (!isset($this->headerNames[$header])) {
44 return [];
45 }
46 $header = $this->headerNames[$header];
47 return $this->headers[$header];
48 }
49 public function getHeaderLine($header)
50 {
51 return \implode(', ', $this->getHeader($header));
52 }
53 public function withHeader($header, $value)
54 {
55 $this->assertHeader($header);
56 $value = $this->normalizeHeaderValue($value);
57 $normalized = \strtolower($header);
58 $new = clone $this;
59 if (isset($new->headerNames[$normalized])) {
60 unset($new->headers[$new->headerNames[$normalized]]);
61 }
62 $new->headerNames[$normalized] = $header;
63 $new->headers[$header] = $value;
64 return $new;
65 }
66 public function withAddedHeader($header, $value)
67 {
68 $this->assertHeader($header);
69 $value = $this->normalizeHeaderValue($value);
70 $normalized = \strtolower($header);
71 $new = clone $this;
72 if (isset($new->headerNames[$normalized])) {
73 $header = $this->headerNames[$normalized];
74 $new->headers[$header] = \array_merge($this->headers[$header], $value);
75 } else {
76 $new->headerNames[$normalized] = $header;
77 $new->headers[$header] = $value;
78 }
79 return $new;
80 }
81 public function withoutHeader($header)
82 {
83 $normalized = \strtolower($header);
84 if (!isset($this->headerNames[$normalized])) {
85 return $this;
86 }
87 $header = $this->headerNames[$normalized];
88 $new = clone $this;
89 unset($new->headers[$header], $new->headerNames[$normalized]);
90 return $new;
91 }
92 public function getBody()
93 {
94 if (!$this->stream) {
95 $this->stream = Utils::streamFor('');
96 }
97 return $this->stream;
98 }
99 public function withBody(StreamInterface $body)
100 {
101 if ($body === $this->stream) {
102 return $this;
103 }
104 $new = clone $this;
105 $new->stream = $body;
106 return $new;
107 }
108 private function setHeaders(array $headers)
109 {
110 $this->headerNames = $this->headers = [];
111 foreach ($headers as $header => $value) {
112 if (\is_int($header)) {
113 // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec
114 // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass.
115 $header = (string) $header;
116 }
117 $this->assertHeader($header);
118 $value = $this->normalizeHeaderValue($value);
119 $normalized = \strtolower($header);
120 if (isset($this->headerNames[$normalized])) {
121 $header = $this->headerNames[$normalized];
122 $this->headers[$header] = \array_merge($this->headers[$header], $value);
123 } else {
124 $this->headerNames[$normalized] = $header;
125 $this->headers[$header] = $value;
126 }
127 }
128 }
129 /**
130 * @param mixed $value
131 *
132 * @return string[]
133 */
134 private function normalizeHeaderValue($value)
135 {
136 if (!\is_array($value)) {
137 return $this->trimAndValidateHeaderValues([$value]);
138 }
139 if (\count($value) === 0) {
140 throw new \InvalidArgumentException('Header value can not be an empty array.');
141 }
142 return $this->trimAndValidateHeaderValues($value);
143 }
144 /**
145 * Trims whitespace from the header values.
146 *
147 * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field.
148 *
149 * header-field = field-name ":" OWS field-value OWS
150 * OWS = *( SP / HTAB )
151 *
152 * @param mixed[] $values Header values
153 *
154 * @return string[] Trimmed header values
155 *
156 * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
157 */
158 private function trimAndValidateHeaderValues(array $values)
159 {
160 return \array_map(function ($value) {
161 if (!\is_scalar($value) && null !== $value) {
162 throw new \InvalidArgumentException(\sprintf('Header value must be scalar or null but %s provided.', \is_object($value) ? \get_class($value) : \gettype($value)));
163 }
164 $trimmed = \trim((string) $value, " \t");
165 $this->assertValue($trimmed);
166 return $trimmed;
167 }, \array_values($values));
168 }
169 /**
170 * @see https://tools.ietf.org/html/rfc7230#section-3.2
171 *
172 * @param mixed $header
173 *
174 * @return void
175 */
176 private function assertHeader($header)
177 {
178 if (!\is_string($header)) {
179 throw new \InvalidArgumentException(\sprintf('Header name must be a string but %s provided.', \is_object($header) ? \get_class($header) : \gettype($header)));
180 }
181 if ($header === '') {
182 throw new \InvalidArgumentException('Header name can not be empty.');
183 }
184 if (!\preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) {
185 throw new \InvalidArgumentException(\sprintf('"%s" is not valid header name', $header));
186 }
187 }
188 /**
189 * @param string $value
190 *
191 * @return void
192 *
193 * @see https://tools.ietf.org/html/rfc7230#section-3.2
194 *
195 * field-value = *( field-content / obs-fold )
196 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
197 * field-vchar = VCHAR / obs-text
198 * VCHAR = %x21-7E
199 * obs-text = %x80-FF
200 * obs-fold = CRLF 1*( SP / HTAB )
201 */
202 private function assertValue($value)
203 {
204 // The regular expression intentionally does not support the obs-fold production, because as
205 // per RFC 7230#3.2.4:
206 //
207 // A sender MUST NOT generate a message that includes
208 // line folding (i.e., that has any field-value that contains a match to
209 // the obs-fold rule) unless the message is intended for packaging
210 // within the message/http media type.
211 //
212 // Clients must not send a request with line folding and a server sending folded headers is
213 // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting
214 // folding is not likely to break any legitimate use case.
215 if (!\preg_match('/^[\\x20\\x09\\x21-\\x7E\\x80-\\xFF]*$/', $value)) {
216 throw new \InvalidArgumentException(\sprintf('"%s" is not valid header value', $value));
217 }
218 }
219 }
220