25 $chunkEnd = strpos($buffer,
"\r\n") + 2;
26 $temp = substr($buffer, 0, $chunkEnd);
27 $chunkSize = hexdec(trim($temp));
28 $chunkStart = $chunkEnd;
29 while ($chunkSize > 0) {
30 $chunkEnd = strpos($buffer,
"\r\n", $chunkStart + $chunkSize);
33 if ($chunkEnd ==
false) {
34 $chunk = substr($buffer, $chunkStart);
37 $length += strlen($chunk);
42 $chunk = substr($buffer, $chunkStart, $chunkEnd - $chunkStart);
46 $length += strlen($chunk);
48 $chunkStart = $chunkEnd + 2;
50 $chunkEnd = strpos($buffer,
"\r\n", $chunkStart) + 2;
51 if ($chunkEnd ==
false) {
54 $temp = substr($buffer, $chunkStart, $chunkEnd - $chunkStart);
55 $chunkSize = hexdec(trim($temp));
56 $chunkStart = $chunkEnd;
73 $httpResponse = array(
'raw_data' => $data,
'headers'=> array(),
'cookies' => array());
76 if (preg_match(
'/^HTTP\/1\.[0-1] 200 Connection established/', $data)) {
79 $pos = strpos($data,
"\r\n\r\n");
80 if ($pos || is_int($pos)) {
83 $pos = strpos($data,
"\n\n");
84 if ($pos || is_int($pos)) {
94 $data = substr($data, $bd);
96 error_log(
'XML-RPC: ' . __METHOD__ .
': HTTPS via proxy error, tunnel connection possibly failed');
102 while (preg_match(
'/^HTTP\/1\.1 1[0-9]{2} /', $data)) {
103 $pos = strpos($data,
'HTTP', 12);
106 if (!$pos && !is_int($pos)) {
111 $data = substr($data, $pos);
113 if (!preg_match(
'/^HTTP\/[0-9.]+ 200 /', $data)) {
114 $errstr = substr($data, 0, strpos($data,
"\n") - 1);
115 error_log(
'XML-RPC: ' . __METHOD__ .
': HTTP error, got response: ' . $errstr);
121 $pos = strpos($data,
"\r\n\r\n");
122 if ($pos || is_int($pos)) {
125 $pos = strpos($data,
"\n\n");
126 if ($pos || is_int($pos)) {
135 $ar = preg_split(
"/\r?\n/", trim(substr($data, 0, $pos)));
136 while (list(, $line) = @each($ar)) {
138 $arr = explode(
':', $line, 2);
139 if (count($arr) > 1) {
140 $headerName = strtolower(trim($arr[0]));
145 if ($headerName ==
'set-cookie' || $headerName ==
'set-cookie2') {
146 if ($headerName ==
'set-cookie2') {
149 $cookies = explode(
',', $arr[1]);
151 $cookies = array($arr[1]);
153 foreach ($cookies as $cookie) {
156 if (isset($httpResponse[
'headers'][$headerName])) {
157 $httpResponse[
'headers'][$headerName] .=
', ' . trim($cookie);
159 $httpResponse[
'headers'][$headerName] = trim($cookie);
164 $cookie = explode(
';', $cookie);
165 foreach ($cookie as $pos => $val) {
166 $val = explode(
'=', $val, 2);
167 $tag = trim($val[0]);
168 $val = trim(@$val[1]);
172 $httpResponse[
'cookies'][$tag] = array();
173 $httpResponse[
'cookies'][$cookiename][
'value'] = urldecode($val);
175 if ($tag !=
'value') {
176 $httpResponse[
'cookies'][$cookiename][$tag] = $val;
182 $httpResponse[
'headers'][$headerName] = trim($arr[1]);
184 } elseif (isset($headerName)) {
186 $httpResponse[
'headers'][$headerName] .=
' ' . trim($line);
190 $data = substr($data, $bd);
192 if ($debug && count($httpResponse[
'headers'])) {
194 foreach ($httpResponse[
'headers'] as $header => $value) {
195 $msg .=
"HEADER: $header: $value\n";
197 foreach ($httpResponse[
'cookies'] as $header => $value) {
198 $msg .=
"COOKIE: $header={$value['value']}\n";
205 if (!$headersProcessed) {
207 if (isset($httpResponse[
'headers'][
'transfer-encoding']) && $httpResponse[
'headers'][
'transfer-encoding'] ==
'chunked') {
209 error_log(
'XML-RPC: ' . __METHOD__ .
': errors occurred when trying to rebuild the chunked data received from server');
216 if (isset($httpResponse[
'headers'][
'content-encoding'])) {
217 $httpResponse[
'headers'][
'content-encoding'] = str_replace(
'x-',
'', $httpResponse[
'headers'][
'content-encoding']);
218 if ($httpResponse[
'headers'][
'content-encoding'] ==
'deflate' || $httpResponse[
'headers'][
'content-encoding'] ==
'gzip') {
220 if (function_exists(
'gzinflate')) {
221 if ($httpResponse[
'headers'][
'content-encoding'] ==
'deflate' && $degzdata = @gzuncompress($data)) {
224 Logger::instance()->debugMessage(
"---INFLATED RESPONSE---[" . strlen($data) .
" chars]---\n$data\n---END---");
226 } elseif ($httpResponse[
'headers'][
'content-encoding'] ==
'gzip' && $degzdata = @gzinflate(substr($data, 10))) {
229 Logger::instance()->debugMessage(
"---INFLATED RESPONSE---[" . strlen($data) .
" chars]---\n$data\n---END---");
232 error_log(
'XML-RPC: ' . __METHOD__ .
': errors occurred when trying to decode the deflated data received from server');
236 error_log(
'XML-RPC: ' . __METHOD__ .
': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.');
243 return $httpResponse;