Debian Codegraph @

vendor/react/http/src/Client/RequestData.php

raw
1 <?php
2
3 namespace React\Http\Client;
4
5 /**
6 * @internal
7 */
8 class RequestData
9 {
10 private $method;
11 private $url;
12 private $headers;
13 private $protocolVersion;
14
15 public function __construct($method, $url, array $headers = array(), $protocolVersion = '1.0')
16 {
17 $this->method = $method;
18 $this->url = $url;
19 $this->headers = $headers;
20 $this->protocolVersion = $protocolVersion;
21 }
22
23 private function mergeDefaultheaders(array $headers)
24 {
25 $port = ($this->getDefaultPort() === $this->getPort()) ? '' : ":{$this->getPort()}";
26 $connectionHeaders = ('1.1' === $this->protocolVersion) ? array('Connection' => 'close') : array();
27 $authHeaders = $this->getAuthHeaders();
28
29 $defaults = array_merge(
30 array(
31 'Host' => $this->getHost().$port,
32 'User-Agent' => 'ReactPHP/1',
33 ),
34 $connectionHeaders,
35 $authHeaders
36 );
37
38 // remove all defaults that already exist in $headers
39 $lower = array_change_key_case($headers, CASE_LOWER);
40 foreach ($defaults as $key => $_) {
41 if (isset($lower[strtolower($key)])) {
42 unset($defaults[$key]);
43 }
44 }
45
46 return array_merge($defaults, $headers);
47 }
48
49 public function getScheme()
50 {
51 return parse_url($this->url, PHP_URL_SCHEME);
52 }
53
54 public function getHost()
55 {
56 return parse_url($this->url, PHP_URL_HOST);
57 }
58
59 public function getPort()
60 {
61 return (int) parse_url($this->url, PHP_URL_PORT) ?: $this->getDefaultPort();
62 }
63
64 public function getDefaultPort()
65 {
66 return ('https' === $this->getScheme()) ? 443 : 80;
67 }
68
69 public function getPath()
70 {
71 $path = parse_url($this->url, PHP_URL_PATH);
72 $queryString = parse_url($this->url, PHP_URL_QUERY);
73
74 // assume "/" path by default, but allow "OPTIONS *"
75 if ($path === null) {
76 $path = ($this->method === 'OPTIONS' && $queryString === null) ? '*': '/';
77 }
78 if ($queryString !== null) {
79 $path .= '?' . $queryString;
80 }
81
82 return $path;
83 }
84
85 public function setProtocolVersion($version)
86 {
87 $this->protocolVersion = $version;
88 }
89
90 public function __toString()
91 {
92 $headers = $this->mergeDefaultheaders($this->headers);
93
94 $data = '';
95 $data .= "{$this->method} {$this->getPath()} HTTP/{$this->protocolVersion}\r\n";
96 foreach ($headers as $name => $values) {
97 foreach ((array)$values as $value) {
98 $data .= "$name: $value\r\n";
99 }
100 }
101 $data .= "\r\n";
102
103 return $data;
104 }
105
106 private function getUrlUserPass()
107 {
108 $components = parse_url($this->url);
109
110 if (isset($components['user'])) {
111 return array(
112 'user' => $components['user'],
113 'pass' => isset($components['pass']) ? $components['pass'] : null,
114 );
115 }
116 }
117
118 private function getAuthHeaders()
119 {
120 if (null !== $auth = $this->getUrlUserPass()) {
121 return array(
122 'Authorization' => 'Basic ' . base64_encode($auth['user'].':'.$auth['pass']),
123 );
124 }
125
126 return array();
127 }
128 }