1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PHP Generics package. |
4
|
|
|
* |
5
|
|
|
* @package Generics |
6
|
|
|
*/ |
7
|
|
|
namespace Generics\Client; |
8
|
|
|
|
9
|
|
|
use Generics\Util\Arrays; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This trait provides common http(s) header functionality |
13
|
|
|
* |
14
|
|
|
* @author Maik Greubel <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
trait HttpHeadersTrait |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Headers |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $headers; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The response status code |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $responseCode; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* @see \Generics\Streams\HttpStream::setHeader() |
37
|
|
|
* @return HttpClient |
38
|
|
|
*/ |
39
|
22 |
|
public function setHeader($headerName, $headerValue) |
40
|
|
|
{ |
41
|
22 |
|
$this->headers[$headerName] = $headerValue; |
42
|
22 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Reset the headers |
46
|
|
|
*/ |
47
|
25 |
|
public function resetHeaders() |
48
|
|
|
{ |
49
|
25 |
|
$this->headers = array(); |
50
|
25 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
* @see \Generics\Streams\HttpStream::getHeaders() |
56
|
|
|
*/ |
57
|
22 |
|
public function getHeaders(): array |
58
|
|
|
{ |
59
|
22 |
|
return $this->headers; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieve the response status code |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
16 |
|
public function getResponseCode(): int |
68
|
|
|
{ |
69
|
16 |
|
return $this->responseCode; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Adjust the headers by injecting default values for missing keys. |
74
|
|
|
*/ |
75
|
22 |
|
private function adjustHeaders($requestType) |
76
|
|
|
{ |
77
|
22 |
|
if (! array_key_exists('Accept', $this->headers) && $requestType != 'HEAD') { |
78
|
18 |
|
$this->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'); |
79
|
|
|
} |
80
|
|
|
|
81
|
22 |
|
if (! array_key_exists('Accept-Language', $this->headers) && $requestType != 'HEAD') { |
82
|
18 |
|
$this->setHeader('Accept-Language', 'en-US;q=0.7,en;q=0.3'); |
83
|
|
|
} |
84
|
|
|
|
85
|
22 |
|
if (! array_key_exists('User-Agent', $this->headers) && $requestType != 'HEAD') { |
86
|
18 |
|
$this->setHeader('User-Agent', 'phpGenerics 1.0'); |
87
|
|
|
} |
88
|
|
|
|
89
|
22 |
|
if (! array_key_exists('Connection', $this->headers) || strlen($this->headers['Connection']) == 0) { |
90
|
18 |
|
$this->adjustConnectionHeader($requestType); |
91
|
|
|
} |
92
|
|
|
|
93
|
22 |
|
if (! array_key_exists('Accept-Encoding', $this->headers)) { |
94
|
22 |
|
$encoding = ""; |
|
|
|
|
95
|
22 |
|
if (function_exists('gzinflate')) { |
96
|
22 |
|
$encoding = 'gzip, deflate'; |
97
|
|
|
} else { |
98
|
|
|
$encoding = 'identity'; |
99
|
|
|
} |
100
|
22 |
|
$this->setHeader('Accept-Encoding', $encoding); |
101
|
|
|
} |
102
|
22 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Depending on request type the connection header is either |
106
|
|
|
* set to keep-alive or close |
107
|
|
|
* |
108
|
|
|
* @param string $requestType |
109
|
|
|
*/ |
110
|
18 |
|
private function adjustConnectionHeader($requestType) |
111
|
|
|
{ |
112
|
18 |
|
if ($requestType == 'HEAD') { |
113
|
2 |
|
$this->setHeader('Connection', 'close'); |
114
|
|
|
} else { |
115
|
16 |
|
$this->setHeader('Connection', 'keep-alive'); |
116
|
|
|
} |
117
|
18 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Try to parse line as header and add the results to local header list |
121
|
|
|
* |
122
|
|
|
* @param string $line |
123
|
|
|
*/ |
124
|
18 |
|
private function addParsedHeader($line) |
125
|
|
|
{ |
126
|
18 |
|
if (strpos($line, ':') === false) { |
127
|
18 |
|
$this->responseCode = HttpStatus::parseStatus($line)->getCode(); |
128
|
|
|
} else { |
129
|
18 |
|
$line = trim($line); |
130
|
18 |
|
list ($headerName, $headerValue) = explode(':', $line, 2); |
131
|
18 |
|
$this->headers[$headerName] = trim($headerValue); |
132
|
|
|
} |
133
|
18 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Adjust number of bytes to read according content length header |
137
|
|
|
* |
138
|
|
|
* @param int $numBytes |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
18 |
|
private function adjustNumbytes($numBytes): int |
142
|
|
|
{ |
143
|
18 |
|
if (isset($this->headers['Content-Length'])) { |
144
|
|
|
// Try to read the whole payload at once |
145
|
18 |
|
$numBytes = intval($this->headers['Content-Length']); |
146
|
|
|
} |
147
|
|
|
|
148
|
18 |
|
return $numBytes; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Retrieve content type from headers |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
18 |
|
private function getContentEncoding(): string |
157
|
|
|
{ |
158
|
18 |
|
return $this->getHeader('Content-Encoding'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Retrieve an given header |
163
|
|
|
* |
164
|
|
|
* @param string $name |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
18 |
|
private function getHeader(string $name): string |
168
|
|
|
{ |
169
|
18 |
|
$result = ""; |
170
|
|
|
|
171
|
18 |
|
if (Arrays::hasElement($this->headers, $name)) { |
172
|
18 |
|
$result = $this->headers[$name]; |
173
|
|
|
} |
174
|
|
|
|
175
|
18 |
|
return $result; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.