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

     I have run across a rather interesting problem... I have written a script to be used as the 404 handler for a website I am working on. When a user requests pages or images that are broken with certain characteristics I want do display them different pages/images. Reading a webpage off the disk and outputting it to the user is working great. But when I read an image off the disk all hell breaks loose! Below is basically what I am doing:
open(INFILE, "$filename"); binmode(INFILE); sysread INFILE, $filedata, $filesize; close(INFILE); #unbuffer STDOUT and print the file to the user $| = 1; print "Content-Type: image/jpeg Content-Length: $filesize\n\n"; print $filedata;
The above prints out a very distorted image, any help would be appreciated!

Replies are listed 'Best First'.
Re: reading binary files in windows
by merlyn (Sage) on Oct 27, 2000 at 03:49 UTC
      I did add binmode on STDOUT and don't remember what that did to the output but it wasn't correct :) I am pretty sure it just displayed a completely different distorted image
RE: reading binary files in windows
by extremely (Priest) on Oct 27, 2000 at 04:24 UTC
    Wouldn't it be easier to do a redirect? You are in fact just writing your own webserver here. I suppose you have a good reason to give them the correct data from the wrong URL tho. I've seen a site where half the content was at /error.pl before. Plays havok with caching =) Your webserver at least TRYS to give good header...

    --
    $you = new YOU;
    honk() if $you->love(perl)

      A redirect implies that the URL they requested does have valid content. If you're returning something to the browser in a 404 status, it makes sense not to cache it, since a) there is no "url" associated with the content (we have to let the browser know it's a bad url, right? 404!) and b) what happens when content appears at this URL? Do we really want to cache a '404' mesage?

      Many browsers also disregard the content to pages delivered in 404 status, opting for their own simple message instead.

        But by writing my own handler it doesn't show up as a 404. This is a "temporary" solution to a problem on a site that just rolled out with 6000+ products that have about 30 images each... Any image that is requested under a product directory with a certain name I display a certain "art is missing" image. It's just an interum solution till they art department can catch up.
      You can't redirect to a URL that is outside the docroot though. So this method permits the dumping of an image that lives outside the tree.

      -- Randal L. Schwartz, Perl hacker