Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel response macro for streamed responses with seeking support
<?php
Response::macro('streamed', function($type, $size, $name, $callback) {
$start = 0;
$length = $size;
$status = 200;
$headers = [
'Content-Type' => $type,
'Content-Length' => $size,
'Accept-Ranges' => 'bytes'
];
if (false !== $range = Request::server('HTTP_RANGE', false)) {
list($param, $range) = explode('=', $range);
if (strtolower(trim($param)) !== 'bytes') {
header('HTTP/1.1 400 Invalid Request');
exit;
}
list($from, $to) = explode('-', $range);
if ($from === '') {
$end = $size - 1;
$start = $end - intval($from);
} elseif ($to === '') {
$start = intval($from);
$end = $size - 1;
} else {
$start = intval($from);
$end = intval($to);
}
$length = $end - $start + 1;
$status = 206;
$headers['Content-Range'] = sprintf('bytes %d-%d/%d', $start, $end, $size);
}
return Response::stream(function() use ($start, $length, $callback) {
call_user_func($callback, $start, $length);
}, $status, $headers);
});
@langemike

This comment has been minimized.

Copy link

@langemike langemike commented Dec 4, 2015

When implemented this i got a lot of ERR_CONTENT_LENGTH_MISMATCH errors in Chrome.

This was because:

  • When content is partially served the Content-Length header should update accordingly (it should be same as the range being served).
  • Also when a range is requested which overlaps the original Content-Length it should update accordingly (it must be the maximum length available).

I fixed these bugs here: https://gist.github.com/langemike/d196fe2d201c1e574205
Also it contains a usage example.

@AhmedElSaeedTalat

This comment has been minimized.

Copy link

@AhmedElSaeedTalat AhmedElSaeedTalat commented Jul 6, 2017

Hello! can you please let me know where to use this snippet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment