~ballot/wordpress/openstack-objectstorage-bis

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/src/Message/Request.php

  • Committer: Thomas Cuthbert
  • Date: 2020-04-23 05:22:45 UTC
  • Revision ID: thomas.cuthbert@canonical.com-20200423052245-1jxao3mw31w435js
[,r=trivial] bionic composer vendor update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
namespace GuzzleHttp\Message;
 
3
 
 
4
use GuzzleHttp\Collection;
 
5
use GuzzleHttp\Event\HasEmitterTrait;
 
6
use GuzzleHttp\Subscriber\Prepare;
 
7
use GuzzleHttp\Url;
 
8
 
 
9
/**
 
10
 * HTTP request class to send requests
 
11
 */
 
12
class Request extends AbstractMessage implements RequestInterface
 
13
{
 
14
    use HasEmitterTrait;
 
15
 
 
16
    /** @var Url HTTP Url */
 
17
    private $url;
 
18
 
 
19
    /** @var string HTTP method */
 
20
    private $method;
 
21
 
 
22
    /** @var Collection Transfer options */
 
23
    private $transferOptions;
 
24
 
 
25
    /**
 
26
     * @param string           $method  HTTP method
 
27
     * @param string|Url       $url     HTTP URL to connect to. The URI scheme,
 
28
     *   host header, and URI are parsed from the full URL. If query string
 
29
     *   parameters are present they will be parsed as well.
 
30
     * @param array|Collection $headers HTTP headers
 
31
     * @param mixed            $body    Body to send with the request
 
32
     * @param array            $options Array of options to use with the request
 
33
     *     - emitter: Event emitter to use with the request
 
34
     */
 
35
    public function __construct(
 
36
        $method,
 
37
        $url,
 
38
        $headers = [],
 
39
        $body = null,
 
40
        array $options = []
 
41
    ) {
 
42
        $this->setUrl($url);
 
43
        $this->method = strtoupper($method);
 
44
        $this->handleOptions($options);
 
45
        $this->transferOptions = new Collection($options);
 
46
        $this->addPrepareEvent();
 
47
 
 
48
        if ($body !== null) {
 
49
            $this->setBody($body);
 
50
        }
 
51
 
 
52
        if ($headers) {
 
53
            foreach ($headers as $key => $value) {
 
54
                $this->addHeader($key, $value);
 
55
            }
 
56
        }
 
57
    }
 
58
 
 
59
    public function __clone()
 
60
    {
 
61
        if ($this->emitter) {
 
62
            $this->emitter = clone $this->emitter;
 
63
        }
 
64
        $this->transferOptions = clone $this->transferOptions;
 
65
        $this->url = clone $this->url;
 
66
    }
 
67
 
 
68
    public function setUrl($url)
 
69
    {
 
70
        $this->url = $url instanceof Url ? $url : Url::fromString($url);
 
71
        $this->updateHostHeaderFromUrl();
 
72
    }
 
73
 
 
74
    public function getUrl()
 
75
    {
 
76
        return (string) $this->url;
 
77
    }
 
78
 
 
79
    public function setQuery($query)
 
80
    {
 
81
        $this->url->setQuery($query);
 
82
    }
 
83
 
 
84
    public function getQuery()
 
85
    {
 
86
        return $this->url->getQuery();
 
87
    }
 
88
 
 
89
    public function setMethod($method)
 
90
    {
 
91
        $this->method = strtoupper($method);
 
92
    }
 
93
 
 
94
    public function getMethod()
 
95
    {
 
96
        return $this->method;
 
97
    }
 
98
 
 
99
    public function getScheme()
 
100
    {
 
101
        return $this->url->getScheme();
 
102
    }
 
103
 
 
104
    public function setScheme($scheme)
 
105
    {
 
106
        $this->url->setScheme($scheme);
 
107
    }
 
108
 
 
109
    public function getPort()
 
110
    {
 
111
        return $this->url->getPort();
 
112
    }
 
113
 
 
114
    public function setPort($port)
 
115
    {
 
116
        $this->url->setPort($port);
 
117
        $this->updateHostHeaderFromUrl();
 
118
    }
 
119
 
 
120
    public function getHost()
 
121
    {
 
122
        return $this->url->getHost();
 
123
    }
 
124
 
 
125
    public function setHost($host)
 
126
    {
 
127
        $this->url->setHost($host);
 
128
        $this->updateHostHeaderFromUrl();
 
129
    }
 
130
 
 
131
    public function getPath()
 
132
    {
 
133
        return '/' . ltrim($this->url->getPath(), '/');
 
134
    }
 
135
 
 
136
    public function setPath($path)
 
137
    {
 
138
        $this->url->setPath($path);
 
139
    }
 
140
 
 
141
    public function getResource()
 
142
    {
 
143
        $resource = $this->getPath();
 
144
        if ($query = (string) $this->url->getQuery()) {
 
145
            $resource .= '?' . $query;
 
146
        }
 
147
 
 
148
        return $resource;
 
149
    }
 
150
 
 
151
    public function getConfig()
 
152
    {
 
153
        return $this->transferOptions;
 
154
    }
 
155
 
 
156
    protected function handleOptions(array &$options)
 
157
    {
 
158
        parent::handleOptions($options);
 
159
        // Use a custom emitter if one is specified, and remove it from
 
160
        // options that are exposed through getConfig()
 
161
        if (isset($options['emitter'])) {
 
162
            $this->emitter = $options['emitter'];
 
163
            unset($options['emitter']);
 
164
        }
 
165
    }
 
166
 
 
167
    /**
 
168
     * Adds a subscriber that ensures a request's body is prepared before
 
169
     * sending.
 
170
     */
 
171
    private function addPrepareEvent()
 
172
    {
 
173
        static $subscriber;
 
174
        if (!$subscriber) {
 
175
            $subscriber = new Prepare();
 
176
        }
 
177
 
 
178
        $this->getEmitter()->attach($subscriber);
 
179
    }
 
180
 
 
181
    private function updateHostHeaderFromUrl()
 
182
    {
 
183
        $port = $this->url->getPort();
 
184
        $scheme = $this->url->getScheme();
 
185
        if ($host = $this->url->getHost()) {
 
186
            if (($port == 80 && $scheme == 'http') ||
 
187
                ($port == 443 && $scheme == 'https')
 
188
            ) {
 
189
                $this->setHeader('Host', $host);
 
190
            } else {
 
191
                $this->setHeader('Host', "{$host}:{$port}");
 
192
            }
 
193
        }
 
194
    }
 
195
}