in reply to How to display an image on a webpage with minimal code

merlyn's comment about the Location header is spot on, the RFC says that a Location header should use an absolute URI. That's no disaster, with the module URI (that comes with LWP, AFAICT, thus, I think, most perl installs should have it — at least, Bundle::LWP lists it), you can solve several problems at once, such as not having to have the images under the current directory (perhaps even chdir to it before you glob, if you have to). For example:
use URI; print URI->new_abs('../images/t-rex.jpg', 'http://www.example.com/home +/');
produces:
http://www.example.com/images/t-rex.jpg

You must have noticed another problem with redirection: caching. The browser thinks, because that's what the server told it, that the location of the image is permanently moved to the URL it got back once. So next time, the browser won't ask again.

You can remedy that, at least on HTTP1.1, with a "moved temporarily" status header, status code 307. That way, next time, the browser will repeat the request.

You might have to find other solutions for browsers that only understand pre-1.1 HTTP, if those still exist.

Replies are listed 'Best First'.
Re^2: How to display an image on a webpage with minimal code
by Melly (Chaplain) on Nov 15, 2006 at 12:59 UTC

    Hmm, URI works v. nicely, but Ihad some problems implementing a 307.

    My code was:

    print "HTTP/1.1 307 temporary redirect\n"; print "Location: " . URI->new_abs($files[int(rand @files)], "http://bu +gzilla") . "\n\n";

    But this didn't redirect me unless I removed the 307 line...

    Tom Melly, tom@tomandlu.co.uk
      n.b. I finally got around to test this. Hence the belated reply.

      Wow, hold it, you're doing this too low level. You're not supposed to be doing this the NPH way, there's still a webserver between the CGI scripts and the web browser, and that one is still postprocessing the headers that the CGI script sent to it — for example, adding a content-length header for plain pages. And there's a special pseudo-header for status codes: Status:.

      When I did this on Apache 2 on my laptop:

      print "Status: 307 Temporary redirect\n"; print "Location: " . URI->new_abs($files[int(rand @files)], "http://lo +calhost/") . "\n\n";
      then the headers that Live HTTP Headers displayed were:
      HTTP/1.x 307 Temporary redirect Date: Thu, 16 Nov 2006 19:55:06 GMT Server: Apache/2.0.50 (Win32) mod_perl/1.99_16 Perl/v5.8.8 PHP/4.3.8 Location: http://localhost/images/060210-leaf.jpg Content-Length: 0 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/plain; charset=ISO-8859-1
      So the headers that got sent to the browser, are looking fine to me.

        Ah, many thanks, that makes sense... btw even with the temporary redirect, I still see some caching (either that, or perls rand is hosed;). Still, I guess that could be any number of misbehaving services (and, given that this is all taking place within an intranet, I suppose there may be any number of factors).

        Many thanks again.

        Tom Melly, tom@tomandlu.co.uk