|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpXmlRpc\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use PhpXmlRpc\Exception\HttpException; |
|
6
|
|
|
use PhpXmlRpc\PhpXmlRpc; |
|
7
|
|
|
use PhpXmlRpc\Traits\LoggerAware; |
|
8
|
|
|
|
|
9
|
|
|
class Http |
|
10
|
|
|
{ |
|
11
|
|
|
use LoggerAware; |
|
12
|
|
|
|
|
13
|
|
|
protected $acceptedStatusCodes = array('200'); |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Decode a string that is encoded with "chunked" transfer encoding as defined in RFC 2068 par. 19.4.6. |
|
17
|
|
|
* Code shamelessly stolen from nusoap library by Dietrich Ayala. |
|
18
|
|
|
* @internal this function will become protected in the future |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $buffer the string to be decoded |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function decodeChunked($buffer) |
|
24
|
|
|
{ |
|
25
|
|
|
// length := 0 |
|
26
|
|
|
$length = 0; |
|
27
|
|
|
$new = ''; |
|
28
|
|
|
|
|
29
|
|
|
// read chunk-size, chunk-extension (if any) and crlf |
|
30
|
|
|
// get the position of the linebreak |
|
31
|
|
|
$chunkEnd = strpos($buffer, "\r\n") + 2; |
|
32
|
|
|
$temp = substr($buffer, 0, $chunkEnd); |
|
33
|
|
|
$chunkSize = hexdec(trim($temp)); |
|
34
|
|
|
$chunkStart = $chunkEnd; |
|
35
|
|
|
while ($chunkSize > 0) { |
|
36
|
|
|
$chunkEnd = strpos($buffer, "\r\n", $chunkStart + $chunkSize); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
// just in case we got a broken connection |
|
39
|
|
|
if ($chunkEnd == false) { |
|
|
|
|
|
|
40
|
|
|
$chunk = substr($buffer, $chunkStart); |
|
41
|
|
|
// append chunk-data to entity-body |
|
42
|
|
|
$new .= $chunk; |
|
43
|
|
|
$length += strlen($chunk); |
|
44
|
|
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// read chunk-data and crlf |
|
48
|
|
|
$chunk = substr($buffer, $chunkStart, $chunkEnd - $chunkStart); |
|
49
|
|
|
// append chunk-data to entity-body |
|
50
|
|
|
$new .= $chunk; |
|
51
|
|
|
// length := length + chunk-size |
|
52
|
|
|
$length += strlen($chunk); |
|
53
|
|
|
// read chunk-size and crlf |
|
54
|
|
|
$chunkStart = $chunkEnd + 2; |
|
55
|
|
|
|
|
56
|
|
|
$chunkEnd = strpos($buffer, "\r\n", $chunkStart) + 2; |
|
57
|
|
|
if ($chunkEnd == false) { |
|
|
|
|
|
|
58
|
|
|
break; // just in case we got a broken connection |
|
59
|
|
|
} |
|
60
|
|
|
$temp = substr($buffer, $chunkStart, $chunkEnd - $chunkStart); |
|
61
|
|
|
$chunkSize = hexdec(trim($temp)); |
|
62
|
|
|
$chunkStart = $chunkEnd; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $new; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Parses HTTP an http response's headers and separates them from the body. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $data the http response, headers and body. It will be stripped of headers |
|
72
|
|
|
* @param bool $headersProcessed when true, we assume that response inflating and dechunking has been already carried out |
|
73
|
699 |
|
* @param int $debug when > 0, logs to screen messages detailing info about the parsed data |
|
74
|
|
|
* @return array with keys 'headers', 'cookies', 'raw_data' and 'status_code' |
|
75
|
699 |
|
* @throws HttpException |
|
76
|
|
|
* |
|
77
|
|
|
* @todo if $debug is < 0, we could avoid populating 'raw_data' in the returned value - but that would |
|
78
|
699 |
|
* be a weird API... (note that we still need to always have headers parsed for content charset) |
|
79
|
|
|
*/ |
|
80
|
|
|
public function parseResponseHeaders(&$data, $headersProcessed = false, $debug = 0) |
|
81
|
32 |
|
{ |
|
82
|
32 |
|
$httpResponse = array('raw_data' => $data, 'headers'=> array(), 'cookies' => array(), 'status_code' => null); |
|
83
|
32 |
|
|
|
84
|
|
|
// Support "web-proxy-tunnelling" connections for https through proxies |
|
85
|
|
|
if (preg_match('/^HTTP\/1\.[0-1] 200 Connection established/', $data)) { |
|
86
|
|
|
// Look for CR/LF or simple LF as line separator (even though it is not valid http) |
|
87
|
|
|
$pos = strpos($data, "\r\n\r\n"); |
|
88
|
|
|
if ($pos || is_int($pos)) { |
|
|
|
|
|
|
89
|
|
|
$bd = $pos + 4; |
|
90
|
|
|
} else { |
|
91
|
|
|
$pos = strpos($data, "\n\n"); |
|
92
|
|
|
if ($pos || is_int($pos)) { |
|
93
|
32 |
|
$bd = $pos + 2; |
|
94
|
|
|
} else { |
|
95
|
|
|
// No separation between response headers and body: fault? |
|
96
|
32 |
|
$bd = 0; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
if ($bd) { |
|
100
|
|
|
// this filters out all http headers from proxy. maybe we could take them into account, too? |
|
101
|
|
|
$data = substr($data, $bd); |
|
102
|
|
|
} else { |
|
103
|
|
|
$this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': HTTPS via proxy error, tunnel connection possibly failed'); |
|
104
|
699 |
|
throw new HttpException(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (HTTPS via proxy error, tunnel connection possibly failed)', PhpXmlRpc::$xmlrpcerr['http_error']); |
|
105
|
1 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
// Strip HTTP 1.1 100 Continue header if present |
|
109
|
|
|
while (preg_match('/^HTTP\/1\.1 1[0-9]{2} /', $data)) { |
|
110
|
|
|
$pos = strpos($data, 'HTTP', 12); |
|
111
|
|
|
// server sent a Continue header without any (valid) content following... |
|
112
|
|
|
// give the client a chance to know it |
|
113
|
1 |
|
if (!$pos && !is_int($pos)) { |
|
114
|
|
|
/// @todo this construct works fine in php 3, 4 and 5 - 8; would it not be enough to have !== false now ? |
|
115
|
|
|
|
|
116
|
|
|
break; |
|
117
|
|
|
} |
|
118
|
699 |
|
$data = substr($data, $pos); |
|
119
|
32 |
|
} |
|
120
|
32 |
|
|
|
121
|
|
|
// When using Curl to query servers using Digest Auth, we get back a double set of http headers. |
|
122
|
|
|
// Same when following redirects |
|
123
|
|
|
// We strip out the 1st... |
|
124
|
699 |
|
/// @todo we should let the caller know that there was a redirect involved |
|
125
|
699 |
|
if ($headersProcessed && preg_match('/^HTTP\/[0-9](?:\.[0-9])? (?:401|30[1278]) /', $data)) { |
|
126
|
699 |
|
if (preg_match('/(\r?\n){2}HTTP\/[0-9](?:\.[0-9])? 200 /', $data)) { |
|
127
|
|
|
$data = preg_replace('/^HTTP\/[0-9](?:\.[0-9])? (?:401|30[1278]) .+?(?:\r?\n){2}(HTTP\/[0-9.]+ 200 )/s', '$1', $data, 1); |
|
128
|
|
|
} |
|
129
|
699 |
|
} |
|
130
|
3 |
|
|
|
131
|
3 |
|
if (preg_match('/^HTTP\/([0-9](?:\.[0-9])?) ([0-9]{3}) /', $data, $matches)) { |
|
132
|
3 |
|
$httpResponse['protocol_version'] = $matches[1]; |
|
133
|
|
|
$httpResponse['status_code'] = $matches[2]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (!in_array($httpResponse['status_code'], $this->acceptedStatusCodes)) { |
|
137
|
696 |
|
$errstr = substr($data, 0, strpos($data, "\n") - 1); |
|
138
|
696 |
|
$this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': HTTP error, got response: ' . $errstr); |
|
139
|
695 |
|
throw new HttpException(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (' . $errstr . ')', PhpXmlRpc::$xmlrpcerr['http_error'], null, $httpResponse['status_code']); |
|
140
|
|
|
} |
|
141
|
1 |
|
|
|
142
|
1 |
|
// be tolerant to usage of \n instead of \r\n to separate headers and data (even though it is not valid http) |
|
143
|
1 |
|
$pos = strpos($data, "\r\n\r\n"); |
|
144
|
|
|
if ($pos || is_int($pos)) { |
|
|
|
|
|
|
145
|
|
|
$bd = $pos + 4; |
|
146
|
|
|
} else { |
|
147
|
|
|
$pos = strpos($data, "\n\n"); |
|
148
|
|
|
if ($pos || is_int($pos)) { |
|
149
|
|
|
$bd = $pos + 2; |
|
150
|
|
|
} else { |
|
151
|
|
|
// No separation between response headers and body: fault? |
|
152
|
696 |
|
// we could take some action here instead of going on... |
|
153
|
|
|
$bd = 0; |
|
154
|
696 |
|
} |
|
155
|
|
|
} |
|
156
|
696 |
|
|
|
157
|
696 |
|
// be tolerant to line endings, and extra empty lines |
|
158
|
696 |
|
$ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos))); |
|
159
|
|
|
|
|
160
|
|
|
foreach ($ar as $line) { |
|
161
|
|
|
// take care of (multi-line) headers and cookies |
|
162
|
|
|
$arr = explode(':', $line, 2); |
|
163
|
|
|
if (count($arr) > 1) { |
|
164
|
696 |
|
/// @todo according to https://www.rfc-editor.org/rfc/rfc7230#section-3.2.4, we should reject with error |
|
165
|
22 |
|
/// 400 any messages where a space is present between the header name and colon |
|
166
|
|
|
$headerName = strtolower(trim($arr[0])); |
|
167
|
|
|
if ($headerName == 'set-cookie') { |
|
168
|
|
|
$cookie = $arr[1]; |
|
169
|
|
|
// glue together all received cookies, using a comma to separate them (same as php does with getallheaders()) |
|
170
|
22 |
|
if (isset($httpResponse['headers'][$headerName])) { |
|
171
|
|
|
$httpResponse['headers'][$headerName] .= ', ' . trim($cookie); |
|
172
|
22 |
|
} else { |
|
173
|
|
|
$httpResponse['headers'][$headerName] = trim($cookie); |
|
174
|
|
|
} |
|
175
|
22 |
|
// parse cookie attributes, in case user wants to correctly honour them |
|
176
|
22 |
|
// @todo support for server sending multiple time cookie with same name, but using different PATHs |
|
177
|
|
|
$cookie = explode(';', $cookie); |
|
178
|
22 |
|
foreach ($cookie as $pos => $val) { |
|
179
|
|
|
$val = explode('=', $val, 2); |
|
180
|
|
|
$tag = trim($val[0]); |
|
181
|
|
|
$val = isset($val[1]) ? trim($val[1]) : ''; |
|
182
|
|
|
if ($pos === 0) { |
|
183
|
22 |
|
$cookieName = $tag; |
|
184
|
22 |
|
// if present, we have strip leading and trailing " chars from $val |
|
185
|
22 |
|
if (preg_match('/^"(.*)"$/', $val, $matches)) { |
|
186
|
22 |
|
$val = $matches[1]; |
|
187
|
22 |
|
} |
|
188
|
|
|
$httpResponse['cookies'][$cookieName] = array('value' => urldecode($val)); |
|
189
|
22 |
|
} else { |
|
190
|
22 |
|
$httpResponse['cookies'][$cookieName][$tag] = $val; |
|
|
|
|
|
|
191
|
22 |
|
} |
|
192
|
22 |
|
} |
|
193
|
|
|
} else { |
|
194
|
22 |
|
/// @todo some other headers (the ones that allow a CSV list of values) do allow many values to be |
|
195
|
22 |
|
/// passed using multiple header lines. |
|
196
|
|
|
/// We should add content to $xmlrpc->_xh['headers'][$headerName] instead of replacing it for those... |
|
197
|
|
|
$httpResponse['headers'][$headerName] = trim($arr[1]); |
|
198
|
|
|
} |
|
199
|
|
|
} elseif (isset($headerName)) { |
|
200
|
|
|
/// @todo improve this: 1. check that the line starts with a space or tab; 2. according to |
|
201
|
696 |
|
/// https://www.rfc-editor.org/rfc/rfc7230#section-3.2.4, we should flat out refuse these messages |
|
202
|
|
|
$httpResponse['headers'][$headerName] .= ' ' . trim($line); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
1 |
|
|
|
206
|
|
|
$data = substr($data, $bd); |
|
207
|
|
|
|
|
208
|
|
|
if ($debug && count($httpResponse['headers'])) { |
|
209
|
696 |
|
$msg = ''; |
|
210
|
|
|
foreach ($httpResponse['headers'] as $header => $value) { |
|
211
|
696 |
|
$msg .= "HEADER: $header: $value\n"; |
|
212
|
3 |
|
} |
|
213
|
3 |
|
foreach ($httpResponse['cookies'] as $header => $value) { |
|
214
|
3 |
|
$msg .= "COOKIE: $header={$value['value']}\n"; |
|
215
|
|
|
} |
|
216
|
3 |
|
$this->getLogger()->debug($msg); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
3 |
|
// if CURL was used for the call, http headers have been processed, and dechunking + reinflating have been carried out |
|
220
|
|
|
if (!$headersProcessed) { |
|
221
|
|
|
|
|
222
|
|
|
// Decode chunked encoding sent by http 1.1 servers |
|
223
|
|
|
if (isset($httpResponse['headers']['transfer-encoding']) && $httpResponse['headers']['transfer-encoding'] == 'chunked') { |
|
224
|
696 |
|
if (!$data = static::decodeChunked($data)) { |
|
225
|
|
|
$this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to rebuild the chunked data received from server'); |
|
226
|
|
|
throw new HttpException(PhpXmlRpc::$xmlrpcstr['dechunk_fail'], PhpXmlRpc::$xmlrpcerr['dechunk_fail'], null, $httpResponse['status_code']); |
|
227
|
374 |
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// Decode gzip-compressed stuff |
|
231
|
|
|
// code shamelessly inspired from nusoap library by Dietrich Ayala |
|
232
|
|
|
if (isset($httpResponse['headers']['content-encoding'])) { |
|
233
|
|
|
$httpResponse['headers']['content-encoding'] = str_replace('x-', '', $httpResponse['headers']['content-encoding']); |
|
234
|
|
|
if ($httpResponse['headers']['content-encoding'] == 'deflate' || $httpResponse['headers']['content-encoding'] == 'gzip') { |
|
235
|
|
|
// if decoding works, use it. else assume data wasn't gzencoded |
|
236
|
374 |
|
if (function_exists('gzinflate')) { |
|
237
|
71 |
|
if ($httpResponse['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) { |
|
238
|
71 |
|
$data = $degzdata; |
|
239
|
|
|
if ($debug) { |
|
240
|
71 |
|
$this->getLogger()->debug("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); |
|
241
|
71 |
|
} |
|
242
|
32 |
|
} elseif ($httpResponse['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) { |
|
243
|
32 |
|
$data = $degzdata; |
|
244
|
32 |
|
if ($debug) { |
|
245
|
|
|
$this->getLogger()->debug("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); |
|
246
|
39 |
|
} |
|
247
|
39 |
|
} else { |
|
248
|
39 |
|
$this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server'); |
|
249
|
35 |
|
throw new HttpException(PhpXmlRpc::$xmlrpcstr['decompress_fail'], PhpXmlRpc::$xmlrpcerr['decompress_fail'], null, $httpResponse['status_code']); |
|
250
|
|
|
} |
|
251
|
|
|
} else { |
|
252
|
|
|
$this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); |
|
253
|
64 |
|
throw new HttpException(PhpXmlRpc::$xmlrpcstr['cannot_decompress'], PhpXmlRpc::$xmlrpcerr['cannot_decompress'], null, $httpResponse['status_code']); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
} // end of 'if needed, de-chunk, re-inflate response' |
|
258
|
|
|
|
|
259
|
|
|
return $httpResponse; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
696 |
|
* Parses one of the http headers which can have a list of values with quality param. |
|
264
|
|
|
* @see https://www.rfc-editor.org/rfc/rfc7231#section-5.3.1 |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $header |
|
267
|
|
|
* @return string[] |
|
268
|
|
|
*/ |
|
269
|
|
|
public function parseAcceptHeader($header) |
|
270
|
|
|
{ |
|
271
|
|
|
$accepted = array(); |
|
272
|
|
|
foreach(explode(',', $header) as $c) { |
|
273
|
|
|
if (preg_match('/^([^;]+); *q=([0-9.]+)/', $c, $matches)) { |
|
274
|
|
|
$c = $matches[1]; |
|
275
|
|
|
$w = $matches[2]; |
|
276
|
|
|
} else { |
|
277
|
|
|
$c = preg_replace('/;.*/', '', $c); |
|
278
|
|
|
$w = 1; |
|
279
|
|
|
} |
|
280
|
|
|
$accepted[(trim($c))] = $w; |
|
281
|
|
|
} |
|
282
|
|
|
arsort($accepted); |
|
283
|
|
|
return array_keys($accepted); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param string[] $statusCodes |
|
288
|
|
|
* @return void |
|
289
|
|
|
*/ |
|
290
|
|
|
public function setAcceptedStatusCodes($statusCodes) |
|
291
|
|
|
{ |
|
292
|
|
|
$this->acceptedStatusCodes = $statusCodes; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|