miniflux-legacy/vendor/PicoFeed/Clients/Stream.php

111 lines
3.2 KiB
PHP
Raw Normal View History

2013-07-17 01:54:44 +02:00
<?php
namespace PicoFeed\Clients;
use \PicoFeed\Logging;
2014-03-17 02:35:57 +01:00
/**
* Stream context HTTP client
*
* @author Frederic Guillot
* @package client
*/
2013-07-17 01:54:44 +02:00
class Stream extends \PicoFeed\Client
{
2014-03-17 02:35:57 +01:00
/**
* Do the HTTP request
*
* @access public
* @return array HTTP response ['body' => ..., 'status' => ..., 'headers' => ...]
*/
2013-07-17 01:54:44 +02:00
public function doRequest()
{
// Prepare HTTP headers for the request
$headers = array(
'Connection: close',
'User-Agent: '.$this->user_agent,
2014-03-17 02:35:57 +01:00
'Accept-Encoding: gzip',
2013-07-17 01:54:44 +02:00
);
if ($this->etag) $headers[] = 'If-None-Match: '.$this->etag;
if ($this->last_modified) $headers[] = 'If-Modified-Since: '.$this->last_modified;
// Create context
$context_options = array(
'http' => array(
'method' => 'GET',
'protocol_version' => 1.1,
'timeout' => $this->timeout,
'max_redirects' => $this->max_redirects,
'header' => implode("\r\n", $headers)
)
);
2013-09-24 01:22:13 +02:00
if (parent::$proxy_hostname) {
$context_options['http']['proxy'] = 'tcp://'.parent::$proxy_hostname.':'.parent::$proxy_port;
$context_options['http']['request_fulluri'] = true;
if (parent::$proxy_username) {
$headers[] = 'Proxy-Authorization: Basic '.base64_encode(parent::$proxy_username.':'.parent::$proxy_password);
$context_options['http']['header'] = implode("\r\n", $headers);
}
}
2013-07-17 01:54:44 +02:00
$context = stream_context_create($context_options);
// Make HTTP request
$stream = @fopen($this->url, 'r', false, $context);
if (! is_resource($stream)) return false;
// Get the entire body until the max size
$body = stream_get_contents($stream, $this->max_body_size + 1);
// If the body size is too large abort everything
if (strlen($body) > $this->max_body_size) return false;
// Get HTTP headers response
$metadata = stream_get_meta_data($stream);
list($status, $headers) = $this->parseHeaders($metadata['wrapper_data']);
fclose($stream);
if (isset($headers['Transfer-Encoding']) && $headers['Transfer-Encoding'] === 'chunked') {
$body = $this->decodeChunked($body);
}
2014-03-17 02:35:57 +01:00
if (isset($headers['Content-Encoding']) && $headers['Content-Encoding'] === 'gzip') {
$body = gzdecode($body);
}
2013-07-17 01:54:44 +02:00
return array(
'status' => $status,
'body' => $body,
'headers' => $headers
);
}
2014-03-17 02:35:57 +01:00
/**
* Decode a chunked body
*
* @access public
* @param string $str Raw body
* @return string Decoded body
*/
public function decodeChunked($str)
{
for ($result = ''; ! empty($str); $str = trim($str)) {
// Get the chunk length
$pos = strpos($str, "\r\n");
$len = hexdec(substr($str, 0, $pos));
// Append the chunk to the result
$result .= substr($str, $pos + 2, $len);
$str = substr($str, $pos + 2 + $len);
}
return $result;
}
2014-03-17 02:35:57 +01:00
}