-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathRequestData.php
More file actions
81 lines (65 loc) · 1.93 KB
/
RequestData.php
File metadata and controls
81 lines (65 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace React\HttpClient;
class RequestData
{
private $method;
private $url;
private $headers;
private $protocolVersion = '1.1';
public function __construct($method, $url, array $headers = array())
{
$this->method = $method;
$this->url = $url;
$this->headers = $headers;
}
private function mergeDefaultheaders(array $headers)
{
$port = ($this->getDefaultPort() === $this->getPort()) ? '' : ":{$this->getPort()}";
$connectionHeaders = ('1.1' === $this->protocolVersion) ? array('Connection' => 'close') : array();
return array_merge(
array(
'Host' => $this->getHost().$port,
'User-Agent' => 'React/alpha',
),
$connectionHeaders,
$headers
);
}
public function getScheme()
{
return parse_url($this->url, PHP_URL_SCHEME);
}
public function getHost()
{
return parse_url($this->url, PHP_URL_HOST);
}
public function getPort()
{
return (int) parse_url($this->url, PHP_URL_PORT) ?: $this->getDefaultPort();
}
public function getDefaultPort()
{
return ('https' === $this->getScheme()) ? 443 : 80;
}
public function getPath()
{
$path = parse_url($this->url, PHP_URL_PATH) ?: '/';
$queryString = parse_url($this->url, PHP_URL_QUERY);
return $path.($queryString ? "?$queryString" : '');
}
public function setProtocolVersion($version)
{
$this->protocolVersion = $version;
}
public function __toString()
{
$headers = $this->mergeDefaultheaders($this->headers);
$data = '';
$data .= "{$this->method} {$this->getPath()} HTTP/{$this->protocolVersion}\r\n";
foreach ($headers as $name => $value) {
$data .= "$name: $value\r\n";
}
$data .= "\r\n";
return $data;
}
}