PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
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 / Request.php

Request.php in Media Cloud Sync 1.2.13, at includes/sdk/s3/GuzzleHttp/Psr7/Request.php

125 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
5
6 use InvalidArgumentException;
7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
8 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
9 use Dudlewebs\WPMCS\s3\Psr\Http\Message\UriInterface;
10 /**
11 * PSR-7 request implementation.
12 */
13 class Request implements RequestInterface
14 {
15 use MessageTrait;
16 /** @var string */
17 private $method;
18 /** @var string|null */
19 private $requestTarget;
20 /** @var UriInterface */
21 private $uri;
22 /**
23 * @param string $method HTTP method
24 * @param string|UriInterface $uri URI
25 * @param (string|string[])[] $headers Request headers
26 * @param string|resource|StreamInterface|null $body Request body
27 * @param string $version Protocol version
28 */
29 public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1')
30 {
31 $this->assertMethod($method);
32 if (!$uri instanceof UriInterface) {
33 $uri = new Uri($uri);
34 }
35 $this->method = \strtoupper($method);
36 $this->uri = $uri;
37 $this->setHeaders($headers);
38 $this->protocol = $version;
39 if (!isset($this->headerNames['host'])) {
40 $this->updateHostFromUri();
41 }
42 if ($body !== '' && $body !== null) {
43 $this->stream = Utils::streamFor($body);
44 }
45 }
46 public function getRequestTarget() : string
47 {
48 if ($this->requestTarget !== null) {
49 return $this->requestTarget;
50 }
51 $target = $this->uri->getPath();
52 if ($target === '') {
53 $target = '/';
54 }
55 if ($this->uri->getQuery() != '') {
56 $target .= '?' . $this->uri->getQuery();
57 }
58 return $target;
59 }
60 public function withRequestTarget($requestTarget) : RequestInterface
61 {
62 if (\preg_match('#\\s#', $requestTarget)) {
63 throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
64 }
65 $new = clone $this;
66 $new->requestTarget = $requestTarget;
67 return $new;
68 }
69 public function getMethod() : string
70 {
71 return $this->method;
72 }
73 public function withMethod($method) : RequestInterface
74 {
75 $this->assertMethod($method);
76 $new = clone $this;
77 $new->method = \strtoupper($method);
78 return $new;
79 }
80 public function getUri() : UriInterface
81 {
82 return $this->uri;
83 }
84 public function withUri(UriInterface $uri, $preserveHost = \false) : RequestInterface
85 {
86 if ($uri === $this->uri) {
87 return $this;
88 }
89 $new = clone $this;
90 $new->uri = $uri;
91 if (!$preserveHost || !isset($this->headerNames['host'])) {
92 $new->updateHostFromUri();
93 }
94 return $new;
95 }
96 private function updateHostFromUri() : void
97 {
98 $host = $this->uri->getHost();
99 if ($host == '') {
100 return;
101 }
102 if (($port = $this->uri->getPort()) !== null) {
103 $host .= ':' . $port;
104 }
105 if (isset($this->headerNames['host'])) {
106 $header = $this->headerNames['host'];
107 } else {
108 $header = 'Host';
109 $this->headerNames['host'] = 'Host';
110 }
111 // Ensure Host is the first header.
112 // See: https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
113 $this->headers = [$header => [$host]] + $this->headers;
114 }
115 /**
116 * @param mixed $method
117 */
118 private function assertMethod($method) : void
119 {
120 if (!\is_string($method) || $method === '') {
121 throw new InvalidArgumentException('Method must be a non-empty string.');
122 }
123 }
124 }
125