# Perl CGI script to respond to erroneous Base64 image requests

I often get lines like this in my Apache error log:

Obviously, this is due to somebody using an outdated browser (or a robot) which doesn't handle inline images (Data URI/base64 encoded).

I am thinking it must be easy to redirect requests starting with "data:image/gif;base64," to a short perl CGI script which returns the requested value as a binary image, with the correct content header (both GIFs and PNGs are used), and without having to read any image files.

I am thinking something along the lines of the following CGI (after a rewrite places the request URL in the query string):

#!/usr/bin/perl
use strict;
use warnings;
use MIME::Base64 ();
$ENV{QUERY_STRING} =~ m{^data:(\w+/\w+);base64,(.+)$} or die "bad input";
print "Content-Type: $1\n\n"; print MIME::Base64::decode ($2);


Would that work and would there be any downsides to this approach (apart from accommodating people with last-century browsers)?

## migrated from stackoverflow.comAug 25 '12 at 18:24

This question came from our site for professional and enthusiast programmers.

The only real problem I see is that if the browser is really "last century" it probably won't support a long enough query string to contain the full image. So your returned image might be incomplete. Another thing is that this would waste a lot of bandwidth.

I see two options

• To suppress those error messages you could just handle those URLs like you and make a header redirect (to use the browser cache!) to an error/empty image.

• Use conditional comments to load inline images on IE < 8 with a normal img:src attribute. Since your culprit is most likely an Internet Explorer (people who install alternate browsers usually keep them up to date)

I would use the first actually, as it does not involve changing code.

To test whether your solution works, start Internet Explorer (9), open the Web Development tools, and dial it back to IE 6 or 7 (best try both)

• Good answer, will accept it within a couple of weeks if nothing else comes up. Thanks. – W3Coder Aug 29 '12 at 9:02