4 use InvalidArgumentException;
20 private $requestTarget;
39 $this->assertMethod($method);
44 $this->method = strtoupper($method);
46 $this->setHeaders($headers);
47 $this->protocol = $version;
49 if (!isset($this->headerNames[
'host'])) {
50 $this->updateHostFromUri();
53 if ($body !==
'' && $body !==
null) {
60 if ($this->requestTarget !==
null) {
61 return $this->requestTarget;
64 $target = $this->uri->getPath();
68 if ($this->uri->getQuery() !=
'') {
69 $target .=
'?' . $this->uri->getQuery();
77 if (preg_match(
'#\s#', $requestTarget)) {
78 throw new InvalidArgumentException(
79 'Invalid request target provided; cannot contain whitespace'
84 $new->requestTarget = $requestTarget;
95 $this->assertMethod($method);
97 $new->method = strtoupper($method);
108 if ($uri === $this->uri) {
115 if (!$preserveHost || !isset($this->headerNames[
'host'])) {
116 $new->updateHostFromUri();
122 private function updateHostFromUri()
124 $host = $this->uri->getHost();
130 if (($port = $this->uri->getPort()) !==
null) {
131 $host .=
':' . $port;
134 if (isset($this->headerNames[
'host'])) {
135 $header = $this->headerNames[
'host'];
138 $this->headerNames[
'host'] =
'Host';
142 $this->headers = [$header => [$host]] + $this->headers;
145 private function assertMethod($method)
147 if (!is_string($method) || $method ===
'') {
148 throw new \InvalidArgumentException(
'Method must be a non-empty string.');