Como decodificar facilmente uma string codificada em HTTP ao fazer uma solicitação HTTP bruta?

Eu quero fazer solicitação HTTP sem ter dependência de cURL eallow_url_fopen = 1 abrindo a conexão de soquete e envia uma solicitação HTTP bruta:

/**
 * Make HTTP GET request
 *
 * @param   string   the URL
 * @param   int      will be filled with HTTP response status code
 * @param   string   will be filled with HTTP response header
 * @return  string   HTTP response body
 */
function http_get_request($url, &$http_code = '', &$res_head = '') 
{
  $scheme = $host = $user = $pass = $query = $fragment = '';
  $path = '/';
  $port = substr($url, 0, 5) == 'https' ? 443 : 80;

  extract(parse_url($url)); 

  $path .= ($query ? "?$query" : '').($fragment ? "#$fragment" : '');

  $head = "GET $path HTTP/1.1\r\n"
        . "Host: $host\r\n"
        . "Authorization: Basic ".base64_encode("$user:$pass")."\r\n"
        . "Connection: close\r\n\r\n";

  $fp = fsockopen($scheme == 'https' ? "ssl://$host" : $host, $port) or 
    die('Cannot connect!');

  fputs($fp, $head);
  while(!feof($fp)) {
    $res .= fgets($fp, 4096);
  }
  fclose($fp);

  list($res_head, $res_body) = explode("\r\n\r\n", $res, 2);
  list(, $http_code, ) = explode(' ', $res_head, 3);

  return $res_body;
}

A função funciona ok, mas desde que eu estou usando HTTP / 1.1, o corpo da resposta geralmente retornouCodificado em pedaços corda. Por exemplo (da Wikipedia):

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

Eu não quero usarhttp_chunked_decode() já que tem dependência PECL e eu quero um código altamente portátil.

Como decodificar facilmente uma string codificada em HTTP para que minha função possa retornar o HTML original? Eu também tenho que ter certeza de que o comprimento da string decodificada coincida com oContent-Length: cabeçalho.

Qualquer ajuda seria apreciada. Obrigado.

questionAnswers(3)

yourAnswerToTheQuestion