jupe has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I have been banging my head against a problem for a day, so I have finally decided to ask for a bit of help from the community. Basic premise: I want to stream files such as MP3's and video files from a Perl script. It mostly works (should be dead-brained easy), but I'm having an infuriating set of issues that I think are related, and are probably due to the HTTP headers. Here is the code, then I will explain the issue in more detail:
my $file = "/path/to/file.mp3"; my $filesize = -s "$file"; print $cgi->header( -type => "audio/mp3", -Content_length => "$filesize", -content_disposition => "attachment; filename=$ +params->{cgi}->{fileId}.$params->{cgi}->{fileType}"); #This has been +tried with -content_disposition and without open(FILE, "$file"); binmode(FILE); binmode(STDOUT); while (<FILE>) { print; } close(FILE);
Here are the issues: I've looked at the headers, and they look sane. They have all the options I've specified correctly it seems, I just don't know what I'm doing wrong. Anyone have any thoughts on how I can troubleshoot this?

Replies are listed 'Best First'.
Re: Perl, CGI, and streaming files
by mikeraz (Friar) on May 05, 2009 at 19:36 UTC

    Your use of <FILE> is treating the binary file as a text file and returning a series of lines delimited by $/ (the end of line character, aka \n) which is not what you want. See readline for the details.

    Review Apache::MP3 for one way to serve up MP3 streams.

    This is not as trivial a task as you're imagining.


    Be Appropriate && Follow Your Curiosity
Re: Perl, CGI, and streaming files
by jupe (Beadle) on May 05, 2009 at 19:27 UTC
    The moment I post this I solve the problem. Apache was configured to compress the response from the directory this was running in. Commenting out the SetOutputFilter DEFLATE in the apache config seems to have fixed this.