Skip to content

Commit

Permalink
Merge pull request #914 from philippe44/byte-skipping
Browse files Browse the repository at this point in the history
add byte skipping to workaround HTTP servers that can't do range
  • Loading branch information
mherger committed Oct 9, 2023
2 parents 1a7557b + d238d9f commit 5ed8db9
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions Slim/Player/Protocols/HTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use base qw(Slim::Formats::RemoteStream);

use IO::Socket qw(:crlf);
use Scalar::Util qw(blessed);
use List::Util qw(min);

use Slim::Formats::RemoteMetadata;
use Slim::Music::Info;
Expand Down Expand Up @@ -86,13 +87,21 @@ sub response {
my $self = shift;
my ($args, $request, @headers) = @_;

# re-parse the request string as it might have been overloaded by subclasses
my $request_object = HTTP::Request->parse($request);

# do we have the range we requested
if ($request_object->header('Range') =~ /^bytes=(\d+)-/ &&
$1 != ($self->contentRange =~ /(\d+)-/)) {
${*$self}{'_skip'} = $1;
$log->info("range request not served, skipping $1 bytes");
}

# HTTP headers have now been acquired in a blocking way
my $enhance = $self->canEnhanceHTTP($args->{'client'}, $args->{'url'});
return unless $enhance;

if ($enhance == PERSISTENT || !$self->contentLength) {
# re-parse the request string as it might have been overloaded by subclasses
my $request_object = HTTP::Request->parse($request);
my $uri = $request_object->uri;

# re-set full uri if it is not absolute (e.g. not proxied)
Expand Down Expand Up @@ -138,7 +147,7 @@ sub request {
my $track = $song->currentTrack;
my $processor = $track->processors($song->wantFormat);

# no other guidance, define AudioBlock to make sure that audio_offset is skipped in requestString
# no other guidance, define AudioBlock if needed so that audio_offset is skipped in requestString
if (!$processor || $song->stripHeader) {
$song->initialAudioBlock('') if $song->stripHeader;
return $self->SUPER::request($args);
Expand Down Expand Up @@ -579,7 +588,18 @@ sub saveStream {
# object's parent, not the package's parent
# see http://modernperlbooks.com/mt/2009/09/when-super-isnt.html
sub _sysread {
return CORE::sysread($_[0], $_[1], $_[2], $_[3]);
my $self = $_[0];
return CORE::sysread($_[0], $_[1], $_[2], $_[3]) unless ${*$self}{'_skip'};

# skip what we need until done or EOF
my $bytes = CORE::sysread($_[0], $_[1], min(${*$self}{'_skip'}, $_[2]), $_[3]);
return $bytes if defined $bytes && !$bytes;

# pretend we don't have received anything until we've skipped all
${*$self}{'_skip'} -= $bytes if $bytes;
$_[1]= '';
$! = EINTR;
return undef;
}

sub sysread {
Expand Down

0 comments on commit 5ed8db9

Please sign in to comment.