scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 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->setHeader($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 return $this;
74 }
75  
76 public function getUrl()
77 {
78 return (string) $this->url;
79 }
80  
81 public function setQuery($query)
82 {
83 $this->url->setQuery($query);
84  
85 return $this;
86 }
87  
88 public function getQuery()
89 {
90 return $this->url->getQuery();
91 }
92  
93 public function setMethod($method)
94 {
95 $this->method = strtoupper($method);
96  
97 return $this;
98 }
99  
100 public function getMethod()
101 {
102 return $this->method;
103 }
104  
105 public function getScheme()
106 {
107 return $this->url->getScheme();
108 }
109  
110 public function setScheme($scheme)
111 {
112 $this->url->setScheme($scheme);
113  
114 return $this;
115 }
116  
117 public function getPort()
118 {
119 return $this->url->getPort();
120 }
121  
122 public function setPort($port)
123 {
124 $this->url->setPort($port);
125 $this->updateHostHeaderFromUrl();
126  
127 return $this;
128 }
129  
130 public function getHost()
131 {
132 return $this->url->getHost();
133 }
134  
135 public function setHost($host)
136 {
137 $this->url->setHost($host);
138 $this->updateHostHeaderFromUrl();
139  
140 return $this;
141 }
142  
143 public function getPath()
144 {
145 return '/' . ltrim($this->url->getPath(), '/');
146 }
147  
148 public function setPath($path)
149 {
150 $this->url->setPath($path);
151  
152 return $this;
153 }
154  
155 public function getResource()
156 {
157 $resource = $this->getPath();
158 if ($query = (string) $this->url->getQuery()) {
159 $resource .= '?' . $query;
160 }
161  
162 return $resource;
163 }
164  
165 public function getConfig()
166 {
167 return $this->transferOptions;
168 }
169  
170 protected function handleOptions(array &$options)
171 {
172 parent::handleOptions($options);
173 // Use a custom emitter if one is specified, and remove it from
174 // options that are exposed through getConfig()
175 if (isset($options['emitter'])) {
176 $this->emitter = $options['emitter'];
177 unset($options['emitter']);
178 }
179 }
180  
181 /**
182 * Adds a subscriber that ensures a request's body is prepared before
183 * sending.
184 */
185 private function addPrepareEvent()
186 {
187 static $subscriber;
188 if (!$subscriber) {
189 $subscriber = new Prepare();
190 }
191  
192 $this->getEmitter()->attach($subscriber);
193 }
194  
195 private function updateHostHeaderFromUrl()
196 {
197 $port = $this->url->getPort();
198 $scheme = $this->url->getScheme();
199 if ($host = $this->url->getHost()) {
200 if (($port == 80 && $scheme == 'http') ||
201 ($port == 443 && $scheme == 'https')
202 ) {
203 $this->setHeader('Host', $host);
204 } else {
205 $this->setHeader('Host', "{$host}:{$port}");
206 }
207 }
208 }
209 }