David Steuber on Mon, 30 Sep 2002 20:09:37 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Strange Perl Script Behavior


Hi, folks!

I have written a dirt simple script for producing random images on any
given website that uses the url for the script cgi.  Well, this thing
is apparantly not quite so simple.  Somewhere I have a bug that causes
the script to produce different results for no apparant reason.

Included below is the entire script (in testing form) and sample
output from two consecutive runs.  This has me baffled.  I don't
have the T option on because that causes an internal server error.  In
normal operation, the script would have <FILE> in the print statement
rather than the $file_type variable.

Can anyone find what I am doing wrong here?  I'm also thinking of
ditching the use of the file utility and just going by the file
extension to choose the mime type.

tia,

David Steuber

david@apostrophe:/websites/david-steuber.com/pix/images-cgi
$ cat mesg-sig.cgi
#!/usr/local/bin/perl -w
use strict;
use CGI;
use File::Find;

# look in /websites/david-steuber.com/pix/mesg-board/sig-images
# for image files and return one of them at random.  this should
# be interesting for mesg board viewers.  just to speed things
# up and avoid smacking this server, set the expiration header to
# one hour from time of request.

use constant Expires => '+1h';
use constant Dir => '/websites/david-steuber.com/pix/mesg-board/sig-images';

my @image_files = (); # the list of image files we will pick from
sub wanted {
  if (/^([-\@\w.]+)$/) {
    push @image_files, $1; # this untaints the file names
  }
}
find({ wanted => \&wanted, untaint => 1 }, Dir());
my $chosen_file = Dir() . '/' . $image_files[int(rand(scalar(@image_files)))];
my $file_type = `/usr/bin/file $chosen_file`;

my $content_type = 'text/plain';
if ($file_type =~ / GIF /) {
  $content_type = 'image/gif';
} elsif ($file_type =~ / JPEG /) {
  $content_type = 'image/jpeg';
} elsif ($file_type =~ / PNG /) {
  $content_type = 'image/png';
}

my $query = new CGI();

open FILE, "<$chosen_file";
print $query->header(-type => $content_type, -expires => Expires()), "$file_type\n";
close FILE;

exit(0); # end of script

david@apostrophe:/websites/david-steuber.com/pix/images-cgi
$ perl --version

This is perl, v5.6.0 built for i686-linux

Copyright 1987-2000, Larry Wall

david@apostrophe:/websites/david-steuber.com/pix/images-cgi
$ ./mesg-sig.cgi
Expires: Tue, 01 Oct 2002 00:33:24 GMT
Date: Mon, 30 Sep 2002 23:33:24 GMT
Content-Type: image/gif

/websites/david-steuber.com/pix/mesg-board/sig-images/working-sig-1.gif: GIF image data, version 89a, 640 x 64,


david@apostrophe:/websites/david-steuber.com/pix/images-cgi
$ ./mesg-sig.cgi
Expires: Tue, 01 Oct 2002 00:33:30 GMT
Date: Mon, 30 Sep 2002 23:33:30 GMT
Content-Type: text/plain; charset=ISO-8859-1

/websites/david-steuber.com/pix/mesg-board/sig-images/.: directory


david@apostrophe:/websites/david-steuber.com/pix/images-cgi
$
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**