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