Apache::MP3 Dispatch

This is the code that dispatches on various URLs:
package Apache::MP3;
 
use strict;
use Apache::Constants qw(:common REDIRECT HTTP_NO_CONTENT DIR_MAGIC_TYPE);
use MP3::Info;
use CGI qw/:standard escape *table *TR/;
use vars '$VERSION';

$VERSION = '2.02';

# defaults:
use constant ICON_DIR   => '/apache_mp3';
use constant STYLESHEET => 'apache_mp3.css';
use constant PARENTICON => 'back.gif';
use constant CDICON     => 'cd_icon.gif';
use constant CDLISTICON => 'cd_icon_small.gif';
use constant SONGICON   => 'sound.gif';

my $NO  = '^(no|false)$';  # regular expression
my $YES = '^(yes|true)$';  # regular expression

sub handler {
  my $r = shift;
  if (-d $r->filename) { #  should use $r->finfo here, but causes a crash
    unless ($r->path_info){
      #Issue an external redirect if the dir isn't tailed with a '/'
      my $uri = $r->uri;
      my $query = $r->args;
      $query = "?" . $query if defined $query;
      $r->header_out(Location => "$uri/$query");
      return REDIRECT;
    }
  }

  unless (param) { # simple download
    if ($r->dir_config('AllowDownload') =~ /$NO/oi) {
      $r->log_reason('AllowDownload set to false');
      return FORBIDDEN;
    }
    return DECLINED;
  }

  if (param('stream')) {
    return DECLINED unless -e _;
    if ($r->dir_config('AllowStream') =~ /$NO/oi) {
      $r->log_reason('AllowStream set to false');
      return FORBIDDEN;
    }
    if ($r->dir_config('CheckStreamClient') =~ /$YES/oi
	&& !( $r->header_in('Icy-MetaData')   # winamp/xmms
	      || $r->header_in('Bandwidth')   # realplayer
	      || $r->header_in('Accept') =~ m!\baudio/mpeg\b!  # mpg123 and others
	   )) {  
      my $useragent = $r->header_in('User-Agent');
      $r->log_reason("CheckStreamClient is true and $useragent is not a streaming client");
      return FORBIDDEN;
    }
    return send_stream($r,$r->filename,$r->uri);
  }

  if (param('Play Selected')) {
    return HTTP_NO_CONTENT unless my @files = param('file');
    send_playlist($r,\@files);
    return OK;
  }

  (my $dir = $r->filename) =~ s![^/]+$!!;
  if (param('Play All')) {
    my ($d,$mp3s) = read_directory($r,$dir,'skip info');
    return HTTP_NO_CONTENT unless %$mp3s;
    send_playlist($r,[sort keys %$mp3s]);
    return OK;
  }

  if (param('play')) {
    my($basename) = $r->uri =~ m!([^/]+)$!;
    $basename =~ s/\.m3u$//;
    $basename = "$basename.mp3" if -e "$dir/$basename.mp3";
    $basename = "$basename.MP3" if -e "$dir/$basename.MP3";
    send_playlist($r,[$basename]);
    return OK;
  }

  return DECLINED;  # otherwise don't know how to deal with this
}


<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Sun Jun 4 13:22:53 PDT 2000