2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 03/08/2011 *
4 * =================== Last change: 03/08/2011 *
6 * -------------------------------------------------------------------- *
7 * File : http-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : HTTP-related functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : HTTP-relevante Funktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Sends out all headers required for HTTP/1.1 reply
44 function sendHttpHeaders () {
46 $now = gmdate('D, d M Y H:i:s') . ' GMT';
49 addHttpHeader('HTTP/1.1 ' . getHttpStatus());
51 // General headers for no caching
52 addHttpHeader('Expires: ' . $now); // RFC2616 - Section 14.21
53 addHttpHeader('Last-Modified: ' . $now);
54 addHttpHeader('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
55 addHttpHeader('Pragma: no-cache'); // HTTP/1.0
56 addHttpHeader('Connection: Close');
57 // There shall be no output mode in raw/AJAX mode
58 if ((!isRawOutputMode()) && (!isAjaxOutputMode())) {
59 // Send content-type only in CSS/HTML mode
60 addHttpHeader('Content-Type: ' . getContentType() . '; charset=UTF-8');
64 addHttpHeader('Content-Language: ' . getLanguage());
67 // Checks wether the URL is full-qualified (http[s]:// + hostname [+ request data])
68 function isFullQualifiedUrl ($url) {
70 if (!isset($GLOBALS[__FUNCTION__][$url])) {
72 $GLOBALS[__FUNCTION__][$url] = ((substr($url, 0, 7) == 'http://') || (substr($url, 0, 8) == 'https://'));
76 return $GLOBALS[__FUNCTION__][$url];
79 // Generates the full GET URL from given base URL and data array
80 function generateGetUrlFromBaseUrlData ($baseUrl, $requestData = array()) {
84 // Is it full-qualified?
85 if (!isFullQualifiedUrl($getUrl)) {
86 // Need to prepend a slash?
87 if (substr($getUrl, 0, 1) != '/') {
89 $getUrl = '/' . $getUrl;
92 // Prepend http://hostname from mxchange.org server
93 $getUrl = getServerUrl() . $getUrl;
97 $body = http_build_query($requestData, '', '&');
99 // There should be data, else we don't need to extend $baseUrl with $body
101 // Do we have a question-mark in the script?
102 if (!isInString('?', $baseUrl)) {
103 // No, so first char must be question mark
113 // Remove trailed & to make it more conform
114 if (substr($getUrl, -1, 1) == '&') {
115 $getUrl = substr($getUrl, 0, -1);
123 // Removes http[s]://<hostname> from given url
124 function removeHttpHostNameFromUrl ($url) {
126 $remove = explode(':', $url);
127 $remove = explode('/', substr($remove[1], 3));
129 // Remove the first element (should be the hostname)
132 // implode() back all other elements and prepend a slash
133 $url = '/' . implode('/', $remove);
135 // Return prepared URL
139 // Sends a HTTP request (GET, POST, HEAD are currently supported)
140 function sendHttpRequest ($requestType, $baseUrl, $requestData = array(), $removeHeader = false) {
144 // Start "detecting" the request type
145 switch ($requestType) {
146 case 'HEAD': // Send a HTTP/1.1 HEAD request
147 $response = sendHeadRequest($baseUrl, $requestData);
150 case 'GET': // Send a HTTP/1.1 GET request
151 $response = sendGetRequest($baseUrl, $requestData, $removeHeader);
154 case 'POST': // Send a HTTP/1.1 POST request
155 $response = sendPostRequest($baseUrl, $requestData, $removeHeader);
158 default: // Unsupported HTTP request, this is really bad and needs fixing
159 debug_report_bug(__FUNCTION__, __LINE__, 'Unsupported request detected. requestType=' . $requestType . ',baseUrl=' . $baseUrl . ',requestData()=' . count($requestData));
167 // Sends a HEAD request
168 function sendHeadRequest ($baseUrl, $requestData = array()) {
169 // Generate full GET URL
170 $getUrl = generateGetUrlFromBaseUrlData($baseUrl, $requestData);
172 // Do we have http[s]:// in front of the URL?
173 if (isFullQualifiedUrl($getUrl)) {
174 // Remove http[s]://<hostname> from URL
175 $getUrl = removeHttpHostNameFromUrl($getUrl);
176 } elseif (substr($getUrl, 0, 1) != '/') {
178 $getUrl = '/' . $getUrl;
181 // Extract hostname and port from script
182 $host = extractHostnameFromUrl($baseUrl);
184 // Generate HEAD request header
185 $request = 'HEAD ' . (isProxyUsed() === true ? $baseUrl : '') . trim($getUrl) . ' HTTP/1.1' . getConfig('HTTP_EOL');
186 $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
187 $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
188 if (isConfigEntrySet('FULL_VERSION')) {
189 $request .= 'User-Agent: ' . getTitle() . '/' . getFullVersion() . getConfig('HTTP_EOL');
191 $request .= 'User-Agent: ' . getTitle() . '/' . getConfig('VERSION') . getConfig('HTTP_EOL');
193 $request .= 'Accept: image/png,image/*;q=0.8,text/plain,text/html,*/*;q=0.5' . getConfig('HTTP_EOL');
194 $request .= 'Accept-Charset: UTF-8,*' . getConfig('HTTP_EOL');
195 $request .= 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' . getConfig('HTTP_EOL');
196 $request .= 'Connection: close' . getConfig('HTTP_EOL');
197 $request .= getConfig('HTTP_EOL');
199 // Send the raw request
200 $response = sendRawRequest($host, $request);
202 // Return the result to the caller function
206 // Send a GET request
207 function sendGetRequest ($baseUrl, $requestData = array(), $removeHeader = false) {
208 // Generate full GET URL
209 $getUrl = generateGetUrlFromBaseUrlData($baseUrl, $requestData);
211 // Do we have http[s]:// in front of the URL?
212 if (isFullQualifiedUrl($getUrl)) {
213 // Remove http[s]://<hostname> from url
214 $getUrl = removeHttpHostNameFromUrl($getUrl);
215 } elseif (substr($getUrl, 0, 1) != '/') {
217 $getUrl = '/' . $getUrl;
220 // Extract hostname and port from script
221 $host = extractHostnameFromUrl($baseUrl);
223 // Generate GET request header
224 $request = 'GET ' . (isProxyUsed() === true ? $baseUrl : '') . trim($getUrl) . ' HTTP/1.1' . getConfig('HTTP_EOL');
225 $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
226 $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
227 if (isConfigEntrySet('FULL_VERSION')) {
228 $request .= 'User-Agent: ' . getTitle() . '/' . getFullVersion() . getConfig('HTTP_EOL');
230 $request .= 'User-Agent: ' . getTitle() . '/' . getConfig('VERSION') . getConfig('HTTP_EOL');
232 $request .= 'Accept: image/png,image/*;q=0.8,text/plain,text/html,*/*;q=0.5' . getConfig('HTTP_EOL');
233 $request .= 'Accept-Charset: UTF-8,*' . getConfig('HTTP_EOL');
234 $request .= 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' . getConfig('HTTP_EOL');
235 $request .= 'Connection: close' . getConfig('HTTP_EOL');
236 $request .= getConfig('HTTP_EOL');
238 // Send the raw request
239 $response = sendRawRequest($host, $request);
241 // Should we remove header lines?
242 if ($removeHeader === true) {
244 $response = removeHttpHeaderFromResponse($response);
247 // Return the result to the caller function
251 // Send a POST request
252 function sendPostRequest ($baseUrl, $requestData, $removeHeader = false) {
253 // Copy baseUrl to getUrl
256 // Do we have http[s]:// in front of the URL?
257 if (isFullQualifiedUrl($getUrl)) {
258 // Remove http[s]://<hostname> from url
259 $getUrl = removeHttpHostNameFromUrl($getUrl);
260 } elseif (substr($getUrl, 0, 1) != '/') {
262 $getUrl = '/' . $getUrl;
265 // Extract host name from script
266 $host = extractHostnameFromUrl($baseUrl);
268 // Construct request body
269 $body = http_build_query($requestData, '', '&');
271 // Generate POST request header
272 $request = 'POST ' . (isProxyUsed() === true ? $baseUrl : '') . trim($baseUrl) . ' HTTP/1.0' . getConfig('HTTP_EOL');
273 $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
274 $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
275 if (isConfigEntrySet('FULL_VERSION')) {
276 $request .= 'User-Agent: ' . getTitle() . '/' . getFullVersion() . getConfig('HTTP_EOL');
278 $request .= 'User-Agent: ' . getTitle() . '/' . getConfig('VERSION') . getConfig('HTTP_EOL');
280 $request .= 'Accept: text/plain;q=0.8' . getConfig('HTTP_EOL');
281 $request .= 'Accept-Charset: UTF-8,*' . getConfig('HTTP_EOL');
282 $request .= 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' . getConfig('HTTP_EOL');
283 $request .= 'Content-Type: application/x-www-form-urlencoded' . getConfig('HTTP_EOL');
284 $request .= 'Content-Length: ' . strlen($body) . getConfig('HTTP_EOL');
285 $request .= 'Connection: close' . getConfig('HTTP_EOL');
286 $request .= getConfig('HTTP_EOL');
291 // Send the raw request
292 $response = sendRawRequest($host, $request);
294 // Should we remove header lines?
295 if ($removeHeader === true) {
297 $response = removeHttpHeaderFromResponse($response);
300 // Return the result to the caller function
304 // Sends a raw request (string) to given host (hostnames will be solved)
305 function sendRawRequest ($host, $request) {
306 //* DEBUG: */ die('host='.$host.',request=<pre>'.$request.'</pre>');
307 // Init errno and errdesc with 'all fine' values
311 // Default port is 80
315 $response = array('', '', '');
317 // Default is non-broken HTTP server implementation
318 $GLOBALS['is_http_server_broken'] = false;
321 loadIncludeOnce('inc/classes/resolver.class.php');
323 // Extract port part from host
324 $portArray = explode(':', $host);
325 if (count($portArray) == 2) {
326 // Extract host and port
327 $host = $portArray[0];
328 $port = $portArray[1];
329 } elseif (count($portArray) > 2) {
330 // This should not happen!
331 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid ' . $host . '. Please report this to the Mailer-Project team.');
334 // Get resolver instance
335 $resolver = new HostnameResolver();
338 $proxyHost = compileRawCode(getProxyHost());
341 if (isProxyUsed() === true) {
342 // Resolve hostname into IP address
343 $ip = $resolver->resolveHostname($proxyHost);
345 // Connect to host through proxy connection
346 $resource = fsockopen($ip, bigintval(getProxyPort()), $errno, $errdesc, 30);
348 // Resolve hostname into IP address
349 $ip = $resolver->resolveHostname($host);
351 // Connect to host directly
352 $resource = fsockopen($ip, $port, $errno, $errdesc, 30);
354 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ip=' . $ip . ',host=' . $host . ',isProxyUsed()=' . intval(isProxyUsed()));
357 if (!is_resource($resource)) {
359 logDebugMessage(__FUNCTION__, __LINE__, $errdesc . ' (' . $errno . ')');
361 } elseif ((!stream_set_blocking($resource, 0)) || (!stream_set_timeout($resource, 1))) {
362 // Cannot set non-blocking mode or timeout
363 logDebugMessage(__FUNCTION__, __LINE__, socket_strerror(socket_last_error()));
368 if (isProxyUsed() === true) {
369 // Setup proxy tunnel
370 $response = setupProxyTunnel($host, $proxyHost, $port, $resource);
372 // If the response is invalid, abort
373 if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) {
375 logDebugMessage(__FUNCTION__, __LINE__, 'Proxy tunnel not working?');
381 fwrite($resource, $request);
384 $start = microtime(true);
387 while (!feof($resource)) {
388 // Get info from stream
389 $info = stream_get_meta_data($resource);
391 // Is it timed out? 15 seconds is a really patient...
392 if (($info['timed_out'] == true) || (microtime(true) - $start) > 15) {
394 logDebugMessage(__FUNCTION__, __LINE__, 'Timed out to get data from host ' . $host);
400 // Get line from stream
401 $line = fgets($resource, 128);
403 // Ignore empty lines because of non-blocking mode
405 // uslepp a little to avoid 100% CPU load
412 // Check for broken HTTP implementations
413 if (substr(strtolower($line), 0, 7) == 'server:') {
414 // Anomic (see http://anomic.de, http://yacy.net) is currently broken
415 $GLOBALS['is_http_server_broken'] = (count(getArrayKeysFromSubStrArray(strtolower($line), array('anomichttpd'))) > 0);
418 // Add it to response
419 //* DEBUG: */ print 'line='.$line.'<br />';
426 // Time request if debug-mode is enabled
427 if (isDebugModeEnabled()) {
428 // Add debug message...
429 logDebugMessage(__FUNCTION__, __LINE__, 'Request took ' . (microtime(true) - $start) . ' seconds and returned ' . count($response) . ' line(s).');
432 // Skip first empty lines
434 foreach ($resp as $idx => $line) {
438 // Is this line empty?
441 array_shift($response);
443 // Abort on first non-empty line
448 //* DEBUG: */ debugOutput('<strong>Request:</strong><pre>'.print_r($request, true).'</pre>');
449 //* DEBUG: */ debugOutput('<strong>Response:</strong><pre>'.print_r($response, true).'</pre>');
451 // Proxy agent found or something went wrong?
452 if (!isset($response[0])) {
453 // No response, maybe timeout
454 $response = array('', '', '');
455 logDebugMessage(__FUNCTION__, __LINE__, 'Invalid empty response array, maybe timed out?');
456 } elseif ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && (isProxyUsed() === true)) {
457 // Proxy header detected, so remove two lines
458 array_shift($response);
459 array_shift($response);
462 // Was the request successfull?
463 if ((!isInStringIgnoreCase('200 OK', $response[0])) || (empty($response[0]))) {
464 // Not found / access forbidden
465 logDebugMessage(__FUNCTION__, __LINE__, 'Unexpected status code ' . $response[0] . ' detected. "200 OK" was expected.');
466 $response = array('', '', '');
468 // Check array for chuncked encoding
469 $response = unchunkHttpResponse($response);
476 // Sets up a proxy tunnel for given hostname and through resource
477 function setupProxyTunnel ($host, $proxyHost, $port, $resource) {
479 $response = array('', '', '');
481 // Generate CONNECT request header
482 $proxyTunnel = 'CONNECT ' . $host . ':' . $port . ' HTTP/1.0' . getConfig('HTTP_EOL');
483 $proxyTunnel .= 'Host: ' . $host . getConfig('HTTP_EOL');
484 $proxyTunnel .= 'Proxy-Connection: Keep-Alive' . getConfig('HTTP_EOL');
486 // Use login data to proxy? (username at least!)
487 if (getProxyUsername() != '') {
489 $encodedAuth = base64_encode(compileRawCode(getProxyUsername()) . ':' . compileRawCode(getProxyPassword()));
490 $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . getConfig('HTTP_EOL');
494 $proxyTunnel .= getConfig('HTTP_EOL');
495 //* DEBUG: */ debugOutput('<strong>proxyTunnel=</strong><pre>' . $proxyTunnel.'</pre>');
498 fwrite($resource, $proxyTunnel);
501 if (feof($resource)) {
502 // No response received
506 // Read the first line
507 $resp = trim(fgets($resource, 10240));
508 $respArray = explode(' ', $resp);
509 if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
518 // Check array for chuncked encoding
519 function unchunkHttpResponse ($response) {
520 // Default is not chunked
523 // Check if we have chunks
524 foreach ($response as $line) {
525 // Make lower-case and trim it
529 if ((isInStringIgnoreCase('transfer-encoding', $line)) && (isInStringIgnoreCase('chunked', $line))) {
537 if ($isChunked === true) {
538 // Good, we still have the HTTP headers in there, so we need to get rid
539 // of them temporarly
540 //* DEBUG: */ die('<pre>'.htmlentities(print_r(removeHttpHeaderFromResponse($response), true)).'</pre>');
541 $tempResponse = http_chunked_decode(implode('', removeHttpHeaderFromResponse($response)));
543 // We got a string back from http_chunked_decode(), so we need to convert it back to an array
544 //* DEBUG: */ die('tempResponse['.strlen($tempResponse).']=<pre>'.replaceReturnNewLine(htmlentities($tempResponse)).'</pre>');
546 // Re-add the headers
547 $response = merge_array($GLOBALS['http_headers'], stringToArray("\n", $tempResponse));
550 // Return the unchunked array
554 // Removes HTTP header lines from a response array (e.g. output from send<Get|Post>Request() )
555 function removeHttpHeaderFromResponse ($response) {
556 // Save headers for later usage
557 $GLOBALS['http_headers'] = array();
559 // The first array element has to contain HTTP
560 if ((isset($response[0])) && (substr(strtoupper($response[0]), 0, 5) == 'HTTP/')) {
561 // Okay, we have headers, now remove them with a second array
562 $response2 = $response;
563 foreach ($response as $line) {
565 array_shift($response2);
567 // Add full line to temporary global array
568 $GLOBALS['http_headers'][] = $line;
570 // Trim it for testing
571 $lineTest = trim($line);
573 // Is this line empty?
574 if (empty($lineTest)) {
580 // Write back the array
581 $response = $response2;
584 // Return the modified response array
588 // Returns the flag if a broken HTTP server implementation was detected
589 function isBrokenHttpServerImplentation () {
591 $isBroken = ((isset($GLOBALS['is_http_server_broken'])) && ($GLOBALS['is_http_server_broken'] === true));
597 // Extract host from script name
598 function extractHostnameFromUrl (&$script) {
599 // Use default SERVER_URL by default... ;) So?
600 $url = getServerUrl();
602 // Is this URL valid?
603 if (substr($script, 0, 7) == 'http://') {
604 // Use the hostname from script URL as new hostname
605 $url = substr($script, 7);
606 $extract = explode('/', $url);
608 // Done extracting the URL :)
612 $host = str_replace('http://', '', $url);
613 if (isInString('/', $host)) {
614 $host = substr($host, 0, strpos($host, '/'));
617 // Generate relative URL
618 //* DEBUG: */ debugOutput('SCRIPT=' . $script);
619 if (substr(strtolower($script), 0, 7) == 'http://') {
620 // But only if http:// is in front!
621 $script = substr($script, (strlen($url) + 7));
622 } elseif (substr(strtolower($script), 0, 8) == 'https://') {
624 $script = substr($script, (strlen($url) + 8));
627 //* DEBUG: */ debugOutput('SCRIPT=' . $script);
628 if (substr($script, 0, 1) == '/') {
629 $script = substr($script, 1);
636 // Adds a HTTP header to array
637 function addHttpHeader ($header) {
639 //* DEBUG: */ logDebugMessage(__FUNCTION__ . ': header=' . $header);
640 $GLOBALS['http_header'][] = trim($header);
643 // Flushes all HTTP headers
644 function flushHttpHeaders () {
645 // Is the header already sent?
646 if (headers_sent()) {
648 debug_report_bug(__FUNCTION__, __LINE__, 'Headers already sent!');
651 // Flush all headers if found
652 if ((isset($GLOBALS['http_header'])) && (is_array($GLOBALS['http_header']))) {
653 foreach ($GLOBALS['http_header'] as $header) {
658 // Mark them as flushed
659 $GLOBALS['http_header'] = array();
662 //-----------------------------------------------------------------------------
663 // Automatically re-created functions, all taken from user comments on www.php.net
664 //-----------------------------------------------------------------------------
666 if (!function_exists('http_build_query')) {
667 // Taken from documentation on www.php.net, credits to Marco K. (Germany) and some light mods by R.Haeder
668 function http_build_query($requestData, $prefix = '', $sep = '', $key = '') {
670 foreach ((array) $requestData as $k => $v) {
671 if (is_int($k) && $prefix != null) {
672 $k = urlencode($prefix . $k);
675 if ((!empty($key)) || ($key === 0)) {
676 $k = $key . '[' . urlencode($k) . ']';
679 if (is_array($v) || is_object($v)) {
680 array_push($ret, http_build_query($v, '', $sep, $k));
682 array_push($ret, $k . '=' . urlencode($v));
687 $sep = ini_get('arg_separator.output');
690 return implode($sep, $ret);
694 if (!function_exists('http_chunked_decode')) {
696 * dechunk an HTTP 'transfer-encoding: chunked' message.
698 * @param $chunk The encoded message
699 * @return $dechunk The decoded message. If $chunk wasn't encoded properly debug_report_bug() is being called
700 * @author Marques Johansson (initial author)
701 * @author Roland Haeder (heavy modifications and simplification)
702 * @link http://php.net/manual/en/function.http-chunked-decode.php#89786
704 function http_chunked_decode ($chunk) {
705 // Detect multi-byte encoding
706 $mbPrefix = detectMultiBytePrefix($chunk);
707 //* DEBUG: */ print 'mbPrefix=' . $mbPrefix . '<br />';
709 // Init some variables
711 $len = call_user_func_array($mbPrefix . 'strlen', array(($chunk)));
714 // Walk through all chunks
715 while ($offset < $len) {
716 // Where does the \r\n begin?
717 $lineEndAt = call_user_func_array($mbPrefix . 'strpos', array($chunk, getConfig('HTTP_EOL'), $offset));
720 print 'lineEndAt[<em>'.__LINE__.'</em>]='.$lineEndAt.'<br />
721 offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
723 next[offset,10]=<pre>'.replaceReturnNewLine(htmlentities(call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, 10)))).'</pre>';
726 // Get next hex-coded chunk length
727 $chunkLenHex = call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, ($lineEndAt - $offset)));
730 print 'chunkLenHex[<em>'.__LINE__.'</em>]='.replaceReturnNewLine(htmlentities($chunkLenHex)).'<br />
734 // Validation if it is hexadecimal
735 if (!isHexadecimal($chunkLenHex)) {
736 // Please help debugging this
737 //* DEBUG: */ die('ABORT:chunkLenHex=<pre>'.replaceReturnNewLine(htmlentities($chunkLenHex)).'</pre>');
738 debug_report_bug(__FUNCTION__, __LINE__, 'Value ' . $chunkLenHex . ' is no valid hexa-decimal string.');
740 // This won't be reached
744 // Position of next chunk is right after \r\n
745 $offset = $offset + strlen($chunkLenHex) + strlen(getConfig('HTTP_EOL'));
746 $chunkLen = hexdec(rtrim($chunkLenHex, getConfig('HTTP_EOL')));
749 print 'chunkLen='.$chunkLen.'<br />
750 offset[<em>'.__LINE__.'</em>]='.$offset.'<br />';
753 // Moved out for debugging
754 $next = call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, $chunkLen));
755 //* DEBUG: */ print 'next=<pre>'.replaceReturnNewLine(htmlentities($next)).'</pre>';
758 * Hack for e.g. YaCy HTTPDaemon (Anomic Server), this HTTP server
759 * is currently (revision 7567 and maybe earlier) broken and does
760 * not include the \r\n characters when it sents a "chunked" HTTP
764 if (isBrokenHttpServerImplentation()) {
765 // Count occurrences of \r\n
766 $count = call_user_func_array($mbPrefix . 'substr_count', array($next, getConfig('HTTP_EOL')));
770 * Correct chunk length because some broken HTTP server
771 * implementation subtract occurrences of \r\n in their chunk
774 $chunkLen = hexdec(rtrim($chunkLenHex, getConfig('HTTP_EOL'))) - ($count * strlen(getConfig('HTTP_EOL')));
776 // Add next chunk to $dechunk
777 $dechunk .= call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, $chunkLen));
780 print('offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
781 lineEndAt[<em>'.__LINE__.'</em>]='.$lineEndAt.'<br />
783 count='.$count.'<br />
784 chunkLen='.$chunkLen.'<br />
785 chunkLenHex='.$chunkLenHex.'<br />
786 dechunk=<pre>'.replaceReturnNewLine(htmlentities($dechunk)).'</pre>
787 chunk=<pre>'.replaceReturnNewLine(htmlentities($chunk)).'</pre>');
790 // Is $offset + $chunkLen larger than or equal $len?
791 if (($offset + $chunkLen) >= $len) {
792 // Then stop processing here
796 // Calculate offset of next chunk
797 $offset = call_user_func_array($mbPrefix . 'strpos', array($chunk, getConfig('HTTP_EOL'), $offset + $chunkLen)) + 2;
800 print('offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
801 next[100]=<pre>'.replaceReturnNewLine(htmlentities(call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, 100)))).'</pre>
802 ---:---:---:---:---:---:---:---:---<br />
807 // Return de-chunked string