cgi-dev

Repository that serves as my CGI "scratchpad" to try things out.
git clone git://git.samirparikh.com/cgi-dev
Log | Files | Refs | README

image_fetch.cgi (903B) - raw


      1 #!/usr/bin/perl -wT
      2 
      3 use strict;
      4 
      5 my $image_type = $ENV{HTTP_ACCEPT} =~ m|image/png| ? "png" : "jpeg";
      6 my( $basename ) = $ENV{PATH_INFO} =~ /\/(\w+)/;
      7 my $image_path = "$ENV{DOCUMENT_ROOT}/images/$basename.$image_type";
      8 
      9 #print <<END_OF_HTML;
     10 #Content-type: text/html
     11 #
     12 #<HTML>
     13 #<HEAD>
     14 #    <TITLE>Here is what I found</TITLE>
     15 #</HEAD>
     16 #<BODY>
     17 #<H1>Parameter Values</H1>
     18 #<HR>
     19 #<PRE>
     20 #  HTTP_ACCEPT:		$ENV{HTTP_ACCEPT}
     21 #  image_type:		$image_type
     22 #  PATH_INFO:		$ENV{PATH_INFO}
     23 #  basename:		$basename
     24 #  DOCUMENT_ROOT:	$ENV{DOCUMENT_ROOT}
     25 #  image_path:		$image_path
     26 #</PRE>
     27 #<HR>
     28 #</BODY>
     29 #</HTML>
     30 #END_OF_HTML
     31 
     32 my $image;
     33 unless ( $basename and -B $image_path and open $image, '<', $image_path ) {
     34     print "Location: /errors/not_found.html\n\n";
     35     exit;
     36 }
     37 
     38 my $buffer;
     39 print "Content-type: image/$image_type\n\n";
     40 binmode $image;
     41 
     42 while ( read( $image, $buffer, 16_384 ) ) {
     43     print $buffer;
     44 }