2013-07-17 01:54:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoFeed\Clients;
|
|
|
|
|
|
|
|
use \PicoFeed\Logging;
|
2014-05-20 20:20:27 +02:00
|
|
|
use \PicoFeed\Client;
|
2013-07-17 01:54:44 +02:00
|
|
|
|
2014-03-17 02:35:57 +01:00
|
|
|
/**
|
|
|
|
* Stream context HTTP client
|
|
|
|
*
|
|
|
|
* @author Frederic Guillot
|
|
|
|
* @package client
|
|
|
|
*/
|
2014-05-20 20:20:27 +02:00
|
|
|
class Stream extends Client
|
2013-07-17 01:54:44 +02:00
|
|
|
{
|
2014-03-17 02:35:57 +01:00
|
|
|
/**
|
2014-10-19 20:42:31 +02:00
|
|
|
* Prepare HTTP headers
|
2014-03-17 02:35:57 +01:00
|
|
|
*
|
2014-10-19 20:42:31 +02:00
|
|
|
* @access private
|
|
|
|
* @return array
|
2014-03-17 02:35:57 +01:00
|
|
|
*/
|
2014-10-19 20:42:31 +02:00
|
|
|
private function prepareHeaders()
|
2013-07-17 01:54:44 +02:00
|
|
|
{
|
|
|
|
$headers = array(
|
|
|
|
'Connection: close',
|
|
|
|
'User-Agent: '.$this->user_agent,
|
|
|
|
);
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
if (function_exists('gzdecode')) {
|
|
|
|
$headers[] = 'Accept-Encoding: gzip';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->etag) {
|
|
|
|
$headers[] = 'If-None-Match: '.$this->etag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->last_modified) {
|
|
|
|
$headers[] = 'If-Modified-Since: '.$this->last_modified;
|
|
|
|
}
|
2013-07-17 01:54:44 +02:00
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
if ($this->proxy_username) {
|
|
|
|
$headers[] = 'Proxy-Authorization: Basic '.base64_encode($this->proxy_username.':'.$this->proxy_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare stream context
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function prepareContext()
|
|
|
|
{
|
|
|
|
$context = array(
|
2013-07-17 01:54:44 +02:00
|
|
|
'http' => array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'protocol_version' => 1.1,
|
|
|
|
'timeout' => $this->timeout,
|
|
|
|
'max_redirects' => $this->max_redirects,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
if ($this->proxy_hostname) {
|
|
|
|
|
|
|
|
Logging::setMessage(get_called_class().' Proxy: '.$this->proxy_hostname.':'.$this->proxy_port);
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
$context['http']['proxy'] = 'tcp://'.$this->proxy_hostname.':'.$this->proxy_port;
|
|
|
|
$context['http']['request_fulluri'] = true;
|
2013-09-24 01:22:13 +02:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
if ($this->proxy_username) {
|
|
|
|
Logging::setMessage(get_called_class().' Proxy credentials: Yes');
|
2013-09-24 01:22:13 +02:00
|
|
|
}
|
2014-05-20 20:20:27 +02:00
|
|
|
else {
|
|
|
|
Logging::setMessage(get_called_class().' Proxy credentials: No');
|
|
|
|
}
|
2013-09-24 01:22:13 +02:00
|
|
|
}
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
$context['http']['header'] = implode("\r\n", $this->prepareHeaders());
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the HTTP request
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array HTTP response ['body' => ..., 'status' => ..., 'headers' => ...]
|
|
|
|
*/
|
|
|
|
public function doRequest()
|
|
|
|
{
|
|
|
|
// Create context
|
|
|
|
$context = stream_context_create($this->prepareContext());
|
2013-07-17 01:54:44 +02:00
|
|
|
|
|
|
|
// Make HTTP request
|
|
|
|
$stream = @fopen($this->url, 'r', false, $context);
|
2014-10-19 20:42:31 +02:00
|
|
|
if (! is_resource($stream)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 01:54:44 +02:00
|
|
|
|
|
|
|
// 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
|
2014-10-19 20:42:31 +02:00
|
|
|
if (strlen($body) > $this->max_body_size) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 01:54:44 +02:00
|
|
|
|
|
|
|
// Get HTTP headers response
|
|
|
|
$metadata = stream_get_meta_data($stream);
|
|
|
|
|
|
|
|
list($status, $headers) = $this->parseHeaders($metadata['wrapper_data']);
|
|
|
|
|
|
|
|
fclose($stream);
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
return array(
|
|
|
|
'status' => $status,
|
|
|
|
'body' => $this->decodeBody($body, $headers),
|
|
|
|
'headers' => $headers
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode body response according to the HTTP headers
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $body Raw body
|
|
|
|
* @param array $headers HTTP headers
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function decodeBody($body, array $headers)
|
|
|
|
{
|
2013-11-13 04:31:04 +01:00
|
|
|
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') {
|
2014-07-05 21:10:58 +02:00
|
|
|
$body = @gzdecode($body);
|
2014-03-17 02:35:57 +01:00
|
|
|
}
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
return $body;
|
2013-07-17 01:54:44 +02:00
|
|
|
}
|
2013-11-13 04:31:04 +01:00
|
|
|
|
2014-03-17 02:35:57 +01:00
|
|
|
/**
|
|
|
|
* Decode a chunked body
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $str Raw body
|
|
|
|
* @return string Decoded body
|
|
|
|
*/
|
2013-11-13 04:31:04 +01:00
|
|
|
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
|
|
|
}
|