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