#! /usr/bin/python

import base64, os, sys, re, mimetypes, posixpath

good = re.compile(r"^([\w-]{4})*([\w-]{2,3})?$")

def main():
    param = posixpath.basename(os.environ["PATH_INFO"])
    base, ext = posixpath.splitext(param)
    if good.match(base):
        mime, encoding = mimetypes.guess_type(param)
        mime = mime or "application/octet-stream"
        data = base64.urlsafe_b64decode(base + "==")
        print "Content-Type: %s" % mime
        print
        sys.stdout.write(data)
    else:
        print "Status: 400 Bad Request"
        print "Content-Type: text/html"
        print
        print "<h1>400 Bad Request</h1>"
        print "<p>Use urlsafe base64 for the good one"

if __name__ == "__main__":
    main()

