Commit 78c29eb4 authored by mikeytown2's avatar mikeytown2

Issue #1426854 by mikeytown2: Handle open Range headers.

parent 9a14ebcd
......@@ -1269,34 +1269,27 @@ function httprl_send_request($results = NULL) {
$responses[$id]->chunk_size = $responses[$id]->options['chunk_size_read'];
// If a range header is set, 200 was returned, method is GET,
// and Transfer-Encoding & Content-Encoding are not used, calculate
// how many bytes need to be downloaded.
// calculate how many bytes need to be downloaded.
if ( !empty($responses[$id]->options['headers']['Range'])
&& $responses[$id]->code == 200
&& $responses[$id]->options['method'] == 'GET'
&& (!isset($result->headers['transfer-encoding']) || $result->headers['transfer-encoding'] != 'chunked')
&& (!isset($result->headers['content-encoding']) || ($result->headers['content-encoding'] != 'gzip' && $result->headers['content-encoding'] != 'deflate'))) {
) {
$responses[$id]->ranges = httprl_get_ranges($responses[$id]->options['headers']['Range']);
$responses[$id]->options['max_data_size'] = httprl_get_last_byte_from_range($responses[$id]->ranges);
}
}
}
// Close the connection if a Range request was made and the currently
// downloaded data size is larger than the Range request.
// Close the connection if Transfer-Encoding & Content-Encoding are not
// used, a Range request was made and the currently downloaded data size
// is larger than the Range request.
if ( !empty($responses[$id]->options['max_data_size'])
&& !is_null($responses[$id]->options['max_data_size'])
&& is_numeric($responses[$id]->options['max_data_size'])
&& (!isset($result->headers['transfer-encoding']) || $result->headers['transfer-encoding'] != 'chunked')
&& (!isset($result->headers['content-encoding']) || ($result->headers['content-encoding'] != 'gzip' && $result->headers['content-encoding'] != 'deflate'))
&& $responses[$id]->options['max_data_size'] < httprl_strlen($responses[$id]->data)
) {
$responses[$id]->status = 'Done.';
$responses[$id]->code = 206;
// Make the data conform to the range request.
$new_data = array();
foreach ($responses[$id]->ranges as $range) {
$new_data[] = substr($responses[$id]->data, $range['start'], ($range['end']+1) - $range['start']);
}
$responses[$id]->data = implode('', $new_data);
// Do post processing on the stream.
httprl_post_processing($id, $responses, $output);
......@@ -1985,6 +1978,33 @@ function httprl_decode_data(&$result) {
}
}
// Cut up respose for one sided Range requests.
if (array_key_exists('max_data_size', $result->options)) {
$result->code = 206;
// Make the data conform to the range request.
$new_data = array();
foreach ($result->ranges as $range) {
// Get only the last X number of bytes.
if (!is_numeric($range['start'])) {
$new_data[] = substr($result->data, -$range['end']);
}
// Get all but the first X number of bytes.
elseif (!is_numeric($range['end'])) {
$new_data[] = substr($result->data, $range['start']);
}
else {
$new_data[] = substr($result->data, $range['start'], ($range['end']+1) - $range['start']);
}
}
$result->data = implode('', $new_data);
// Fix content-length for fake 206s.
if (isset($result->headers['content-length'])) {
$result->headers['content-length'] = httprl_strlen($result->data);
}
}
// Reassemble multipart/byteranges response.
if (isset($result->headers['content-type']) && strpos($result->headers['content-type'], 'multipart/byteranges; boundary=') !== FALSE) {
// Get boundary string.
......@@ -2013,14 +2033,10 @@ function httprl_decode_data(&$result) {
}
$result->data .= $data;
}
}
// Fix content-length for fake 206.
if ( !empty($result->options['max_data_size'])
&& !is_null($result->options['max_data_size'])
&& isset($result->headers['content-length'])
) {
$result->headers['content-length'] = httprl_strlen($result->data);
// Fix content-length for multipart/byteranges.
if (isset($result->headers['content-length'])) {
$result->headers['content-length'] = httprl_strlen($result->data);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment