PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.18
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.18
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / guzzlehttp / psr7 / src / Request.php

Request.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.18, at vendor_prefixed/guzzlehttp/psr7/src/Request.php

160 lines 5.1 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 WCPOS\Vendor\GuzzleHttp\Psr7;
5
6 use InvalidArgumentException;
7 use WCPOS\Vendor\Psr\Http\Message\RequestInterface;
8 use WCPOS\Vendor\Psr\Http\Message\StreamInterface;
9 use WCPOS\Vendor\Psr\Http\Message\UriInterface;
10 /**
11 * PSR-7 request implementation.
12 */
13 class Request implements RequestInterface
14 {
15 use MessageTrait;
16 private string $method;
17 private ?string $requestTarget = null;
18 private UriInterface $uri;
19 /**
20 * @param string $method HTTP method
21 * @param string|UriInterface $uri URI
22 * @param (string|string[])[] $headers Request headers
23 * @param string|resource|StreamInterface|null $body Request body
24 * @param string $version Protocol version
25 */
26 public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1')
27 {
28 $this->assertMethod($method);
29 $this->assertProtocolVersion($version);
30 if (!$uri instanceof UriInterface) {
31 $uri = new Uri($uri);
32 }
33 self::getRequestTargetFromUri($uri);
34 $this->method = $method;
35 $this->uri = $uri;
36 $this->setHeaders($headers);
37 $this->protocol = $version;
38 if (!isset($this->headerNames['host'])) {
39 $this->updateHostFromUri();
40 }
41 if ($body !== '' && $body !== null) {
42 $this->stream = Utils::streamFor($body);
43 }
44 }
45 public function getRequestTarget() : string
46 {
47 if ($this->requestTarget !== null) {
48 return $this->requestTarget;
49 }
50 return self::getRequestTargetFromUri($this->uri);
51 }
52 public function withRequestTarget(string $requestTarget) : RequestInterface
53 {
54 self::assertRequestTarget($requestTarget);
55 $new = clone $this;
56 $new->requestTarget = $requestTarget;
57 return $new;
58 }
59 public function getMethod() : string
60 {
61 return $this->method;
62 }
63 public function withMethod(string $method) : RequestInterface
64 {
65 $this->assertMethod($method);
66 $new = clone $this;
67 $new->method = $method;
68 return $new;
69 }
70 public function getUri() : UriInterface
71 {
72 return $this->uri;
73 }
74 public function withUri(UriInterface $uri, bool $preserveHost = \false) : RequestInterface
75 {
76 $sameUri = $uri === $this->uri;
77 if (!$sameUri && $this->requestTarget === null) {
78 self::getRequestTargetFromUri($uri);
79 }
80 $currentHost = $this->getHeaderLine('Host');
81 $host = null;
82 if (!$preserveHost || $currentHost === '') {
83 $host = $this->getHostFromUri($uri);
84 }
85 if ($sameUri && ($host === null || $currentHost === $host)) {
86 return $this;
87 }
88 $new = clone $this;
89 $new->uri = $uri;
90 if ($host !== null) {
91 $new->setHostHeader($host);
92 }
93 return $new;
94 }
95 private function updateHostFromUri() : void
96 {
97 $host = $this->getHostFromUri($this->uri);
98 if ($host === null) {
99 return;
100 }
101 $this->setHostHeader($host);
102 }
103 private function getHostFromUri(UriInterface $uri) : ?string
104 {
105 $host = $uri->getHost();
106 if ($host === '') {
107 return null;
108 }
109 Uri::assertValidHost($host);
110 if (($port = $uri->getPort()) !== null) {
111 $host .= ':' . $port;
112 }
113 $this->assertValue('Host', $host);
114 return $host;
115 }
116 private function setHostHeader(string $host) : void
117 {
118 if (isset($this->headerNames['host'])) {
119 $header = $this->headerNames['host'];
120 } else {
121 $header = 'Host';
122 $this->headerNames['host'] = 'Host';
123 }
124 // Ensure Host is the first header.
125 // See: https://datatracker.ietf.org/doc/html/rfc9110#section-7.2
126 $this->headers = [$header => [$host]] + $this->headers;
127 }
128 private function assertMethod(string $method) : void
129 {
130 if (!Rfc9110::isToken($method)) {
131 throw new InvalidArgumentException('Method must be a valid HTTP token.');
132 }
133 }
134 private static function getRequestTargetFromUri(UriInterface $uri) : string
135 {
136 $target = self::normalizePathForOriginForm($uri->getPath());
137 if ($target === '') {
138 $target = '/';
139 }
140 if ($uri->getQuery() != '') {
141 $target .= '?' . $uri->getQuery();
142 }
143 self::assertRequestTarget($target);
144 return $target;
145 }
146 private static function assertRequestTarget(string $requestTarget) : void
147 {
148 if (!Rfc9112::isValidRequestTarget($requestTarget)) {
149 throw new InvalidArgumentException('Invalid request target provided; cannot be empty or contain whitespace or control characters');
150 }
151 }
152 private static function normalizePathForOriginForm(string $path) : string
153 {
154 if (\str_starts_with($path, '//')) {
155 return '/' . \ltrim($path, '/');
156 }
157 return $path;
158 }
159 }
160