htsql-firebird / src / htsql / tweak / resource / wsgi.py

#
# Copyright (c) 2006-2012, Prometheus Research, LLC
# See `LICENSE` for license information, `AUTHORS` for the list of authors.
#


from ...core.context import context
from ...core.adapter import weigh
from ...core.wsgi import WSGI
from .locate import locate


class ResourceWSGI(WSGI):

    weigh(100)

    def __call__(self, environ, start_response):
        path = environ.get('PATH_INFO')
        indicator = context.app.tweak.resource.indicator
        if not path.startswith('/'+indicator+'/'):
            return super(ResourceWSGI, self).__call__(environ, start_response)
        method = environ['REQUEST_METHOD']
        if method not in ['HEAD', 'GET']:
            start_response('400 Bad Request', [('Content-Type', 'text/plain')])
            return ["Expected a %r request, got %r.\n" % method]
        path = path[len(indicator)+1:]
        resource = None
        if not (path.endswith('/') or '/.' in path or '\\' in path):
            resource = locate(path)
        if resource is None:
            start_response("404 Not Found", [('Content-Type', 'text/plain')])
            return ["Resourse does not exist: %r.\n" % path]
        request_etag = environ.get('HTTP_IF_NONE_MATCH')
        if resource.etag == request_etag:
            start_response("304 Not Modified", [('ETag', resource.etag)])
            return []
        status = "200 OK"
        headers = [('Content-Type', resource.mimetype),
                   ('Content-Length', str(len(resource.data))),
                   ('ETag', resource.etag),
                   ('Content-Disposition', '%s; filename="%s"'
                       % (resource.disposition,
                          resource.name.replace('\\', '\\\\')
                                       .replace('"', '\\"')))]
        start_response(status, headers)
        if method == 'HEAD':
            return []
        return [resource.data]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.