import urllib
class FCGIConvertAccessChecker(object):
    '''used to convert special AccessChecker requests to something django can deal with'''

    def process_request(self, request):
        environ = request.environ
        role = environ.get('FCGI_APACHE_ROLE',None)
        if role!='ACCESS_CHECKER': return
        script_name = environ.get('SCRIPT_NAME','')
        script_filename = environ.get('SCRIPT_FILENAME','')
        script_url = environ.get('SCRIPT_URL','')
        request_uri = environ.get('REQUEST_URI','')
        script_uri = environ.get('SCRIPT_URI','')
        new_script_name = environ['SCRIPT_NAME'] = environ['SCRIPT_URL']
        environ['SCRIPT_FILENAME'] = script_filename.replace(script_name,'')
        new_script_name = environ['SCRIPT_NAME'] = environ['SCRIPT_URL'] = environ['PATH_INFO'] = '/fcgi_authorizer/'
        environ['SCRIPT_URI'] = script_filename.replace(script_name,new_script_name)
        environ['REQUEST_URI'] = new_script_name+'?'+urllib.urlencode(
                                dict(
                                     fcgi_script_name=script_name,
                                     fcgi_script_filename=script_filename,
                                     fcgi_script_url=script_url,
                                     fcgi_request_uri=request_uri,
                                     fcgi_script_uri=script_uri,
                                     ))
        req = request.__class__(environ)
        request.__dict__ = req.__dict__
