Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

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