diff --git a/httprl.module b/httprl.module index 7649628..2568cd6 100644 --- a/httprl.module +++ b/httprl.module @@ -1984,6 +1984,44 @@ function httprl_decode_data(&$result) { $result->data = gzinflate($result->data); } } + + // Reassemble multipart/byteranges response. + if (isset($result->headers['content-type']) && strpos($result->headers['content-type'], 'multipart/byteranges; boundary=') !== FALSE) { + // Get boundary string. + $boundary = "\r\n--" . substr($result->headers['content-type'], 31); + $datas = explode($boundary, $result->data); + $result->data = ''; + foreach ($datas as $data) { + $split = preg_split("/\r\n\r\n|\n\n|\r\r/", $data, 2); + if (count($split) < 2) { + continue; + } + + // Separate the data from the headers. + list($response, $data) = $split; + $response = array_filter(preg_split("/\r\n|\n|\r/", $response)); + + // Parse the response headers. + while ($line = trim(array_shift($response))) { + list($name, $value) = explode(':', $line, 2); + $name = strtolower($name); + + // Add key value pairs to the header. + if ($name != 'content-range') { + $result->headers[$name] = trim($value); + } + } + $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); + } } /**