1
\$\begingroup\$

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

File does not exist: /path/to/www/data:image/gif;base64,R0lGODlhBgAGAIAOAP/yH5BAEACAEALAAAAAAGGAYAAAIJhB0Xi5vOoFwFADs=

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)?

\$\endgroup\$

migrated from stackoverflow.com Aug 25 '12 at 18:24

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

  • \$\begingroup\$ @W3Coder Asking about voting is inappropriate for the comments section. Please refrain from asking about downvotes in the future. \$\endgroup\$ – casperOne Aug 25 '12 at 18:23
2
\$\begingroup\$

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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.