def handle_request():
    server = os.environ["SERVER_NAME"].split(":")[0]
    path = os.environ["REQUEST_URI"]

    # Acquire lockfile
    lock = '/tmp/%s' % sha_constructor(path).hexdigest()
    if os.path.isfile(lock):
        doRedirect(path)
        return 
    
    with lock_file(lock):
        if DEBUG:
            import cgitb
            cgitb.enable()

        try:
            url_parts = process_url(path, server)
        except Http404, e:
            do404(e.message, DEBUG)
            return

        new_file = Transmogrify(url_parts['original_file'], url_parts['actions'])
        new_file.save()

        doRedirect(path)
예제 #2
0
def create_securityhash(action_tuples):
    """
    Create a SHA1 hash based on the KEY and action string
    """
    action_string = "".join(["_%s%s" % a for a in action_tuples])
    security_hash = sha_constructor(action_string + SECRET_KEY).hexdigest()
    return security_hash
def handle_request():
    # Acquire lockfile
    lock = '/tmp/%s' % sha_constructor(os.environ["REQUEST_URI"]).hexdigest()
    if os.path.isfile(lock):
        print "Location: %s" % os.environ["REQUEST_URI"]
        print
        return
    open(lock, 'w')
    
    if DEBUG:
        import cgitb
        cgitb.enable()
    
    try:
        server = os.environ["SERVER_NAME"].split(":")[0]
        url_parts = process_url(os.environ['REQUEST_URI'], server)
    except Http404, e:
        do404(e.message, DEBUG)
예제 #4
0
def app(environ, start_response):
    cropname = None
    server = environ['SERVER_NAME']
    quality = 80

    if "path=" in environ.get("QUERY_STRING", ""):
        # I should probably require a POST for this, but meh, let's not
        # rock the boat.

        # transmogrify is being used directly and not as a 404 handler
        query_dict = urlparse.parse_qs(environ['QUERY_STRING'])

        path = query_dict.get("path", [""])[0]
        key = query_dict.get("key", [""])[0]

        # validate the query params
        if not (path and key):
            # The required parameters were not given
            start_response("400 Bad Response",
                           [("Content-Type", "text/plain")])
            return ["path and key are required query parameters"]

        cropname = query_dict.get("cropname", [None])[0]
        quality = 100

        # rewrite the environ to look like a 404 handler
        environ['REQUEST_URI'] = path + "?" + key


    request_uri = environ['REQUEST_URI']
    path_and_query = request_uri.lstrip("/")
    requested_path = urlparse.urlparse(path_and_query).path

    if path_and_query is "":
        return do404(environ, start_response, "Not Found", DEBUG)

    # Acquire lockfile
    lock = '/tmp/%s' % sha_constructor(path_and_query).hexdigest()

    if os.path.isfile(lock):
        return doRedirect(environ, start_response, request_uri)

    with lock_file(lock):

        if FALLBACK_SERVERS:
            result = do_fallback(FALLBACK_SERVERS, BASE_PATH, requested_path)
            if result == (False, "bad path"):
                start_response("403 Forbidden", [])
                return []

        try:
            url_parts = process_url(path_and_query, server)
        except Http404, e:
            return do404(environ, start_response, e.message, DEBUG)

        new_file = Transmogrify(
            url_parts['original_file'],
            url_parts['actions'],
            quality=quality
        )
        new_file.cropname = cropname
        new_file.save()

        if cropname:
            # Rewrite the request_uri to use the new file with the
            # cropname
            urlbits = list(urlparse.urlsplit(request_uri))
            output_filename = new_file.get_processed_filename()
            filename = os.path.basename(output_filename)
            requested_dir = os.path.dirname(urlbits[2])
            urlbits[2] = os.path.join(requested_dir, filename)
            request_uri = urlparse.urlunsplit(urlbits)

        return doRedirect(environ, start_response, request_uri)
예제 #5
0
def generate_url(url, action_string):
    security_hash = sha_constructor(action_string + SECRET_KEY).hexdigest()
    base_url, ext = os.path.splitext(url)

    return "%s%s%s?%s" % (base_url, action_string, ext, security_hash)
예제 #6
0
 def get_security_hash(self):
     action_string = self.get_action_string()
     return sha_constructor(action_string + SECRET_KEY).hexdigest()