Beispiel #1
0
    def response(self):
        log = logging.getLogger("outpost.files")
        settings = self.request.registry.settings
        url = self.request.url

        # run pre proxy request hooked filters
        # if the filter returns a response and not None. The response is returned
        # immediately
        try:
            file = filtermanager.runPreHook(filtermanager.EmptyFileResponse(), self.request, self.url)
        except filtermanager.ResponseFinished, e:
            return e.response
Beispiel #2
0
    def response(self):
        request = self.request
        settings = request.registry.settings
        request.environ["proxy"] = self

        # Run pre proxy request hooked filters
        # If the filter raises `ResponseFinished` processing is stopped and the response returned.
        # If runPreHook returns a response and not None the proxy call is not triggered and and
        # post hooks are processed for the returned response.
        try:
            response = filtermanager.runPreHook(filtermanager.EmptyProxyResponse(), request, self.url)
        except filtermanager.ResponseFinished, e:
            return e.response
Beispiel #3
0
    def response(self):
        request = self.request
        settings = request.registry.settings
        request.environ["proxy"] = self

        # Run pre proxy request hooked filters
        # If the filter raises `ResponseFinished` processing is stopped and the response returned.
        # If runPreHook returns a response and not None the proxy call is not triggered and and
        # post hooks are processed for the returned response.
        try:
            response = filtermanager.runPreHook(
                filtermanager.EmptyProxyResponse(), request, self.url)
        except filtermanager.ResponseFinished as e:
            return e.response

        if response is None:
            response, body = self.proxy(self.url, request)

        else:
            # pre hook returned response
            body = response.body

        def removeHeader(key, values):
            # the default header key should be the standard capitilized version e.g 'Content-Length'
            try:
                del headers[key]
            except KeyError:
                try:
                    del headers[key.lower()]
                except KeyError:
                    try:
                        del headers[key.upper()]
                    except KeyError:
                        pass

        # clean up headers
        headers = dict(response.headers)
        keys = [k.lower() for k in list(headers)]
        if 'content-length' in keys:
            removeHeader('Content-Length', headers)
        if 'transfer-encoding' in keys:
            removeHeader('Transfer-Encoding', headers)
        if 'content-encoding' in keys:
            removeHeader('Content-Encoding', headers)
        if 'connection' in keys:
            removeHeader('Connection', headers)
        if 'keep-alive' in keys:
            removeHeader('Keep-Alive', headers)

        # cookies
        if 'set-cookie' in keys:
            try:
                cookie = headers['Set-Cookie']
            except KeyError:
                try:
                    cookie = headers['set-cookie']
                except KeyError:
                    cookie = headers['SET-COOKIE']
            host = request.host.split(":")[0]
            local = str(self.url.host).split(":")[0]
            cookie = cookie.replace(local, host)
            headers['set-cookie'] = cookie

        proxy_response = Response(body=body, status=response.status_code)
        proxy_response.headers.update(headers)
        alsoProvides(proxy_response, filtermanager.IProxyRequest)

        # run post proxy request hooked filters
        proxy_response = filtermanager.runPostHook(proxy_response, request,
                                                   self.url)

        return proxy_response
Beispiel #4
0
    def response(self):
        log = logging.getLogger("outpost.files")
        settings = self.request.registry.settings
        url = self.request.url

        # run pre proxy request hooked filters
        # if the filter returns a response and not None. The response is returned
        # immediately
        try:
            file = filtermanager.runPreHook(filtermanager.EmptyFileResponse(), self.request, self.url)
        except filtermanager.ResponseFinished as e:
            return e.response

        if file is None:
            df = settings.get("server.default_path")
            # bw 0.2.6
            if df is None:
                df = settings.get("server.defaultfile")
            if df:
                static = static_view(root_dir=settings["files.directory"],
                                     use_subpath=True,
                                     index=df)
            else:
                static = static_view(root_dir=settings["files.directory"],
                                     use_subpath=True)
            file = static(self.context, self.request)
            alsoProvides(file, filtermanager.IFileRequest)

            # adjust headers
            #file.headers["Cache-control"] = "no-cache"
            #file.headers["Pragma"] = "no-cache"
            #file.headers["Expires"] = "0"
            #if "Last-Modified" in file.headers:
            #    del file.headers["Last-Modified"]
            # set default mime type to text/html
            if len(self.request.subpath):
                name = self.request.subpath[-1]
            else:
                name = df

            # cache content type
            global __ct_cache__
            ext = ".".join(name.split(".")[1:])
            if ext in __ct_cache__:
                ct = __ct_cache__[ext]
            else:
                ct = guess_type(name, strict=False)[0] or settings.get("server.content_type")
                __ct_cache__[ext] = ct

            file.headers["Content-Type"] = ct
            file.content_type = ct
            file.charset = settings.get("files.charset") or "utf-8"

        if self.debug:
            server_trace = settings.get("files.trace")
            # bw 0.2.6 renamed setting
            if server_trace is None:
                server_trace = settings.get("server.trace")
            # trace in debugger
            if server_trace and re.search(server_trace, url):
                pdb.set_trace()

        # run post file server hooked filters
        file = filtermanager.runPostHook(file, self.request, self.url) #=> Ready to filter and return the current file. Step once (n) to apply filters.
        return file
Beispiel #5
0
    def response(self):
        log = logging.getLogger("outpost.files")
        settings = self.request.registry.settings
        url = self.request.url

        # run pre proxy request hooked filters
        # if the filter returns a response and not None. The response is returned
        # immediately
        try:
            file = filtermanager.runPreHook(filtermanager.EmptyFileResponse(),
                                            self.request, self.url)
        except filtermanager.ResponseFinished as e:
            return e.response

        if file is None:
            df = settings.get("server.default_path")
            # bw 0.2.6
            if df is None:
                df = settings.get("server.defaultfile")
            if df:
                static = static_view(root_dir=settings["files.directory"],
                                     use_subpath=True,
                                     index=df)
            else:
                static = static_view(root_dir=settings["files.directory"],
                                     use_subpath=True)
            file = static(self.context, self.request)
            alsoProvides(file, filtermanager.IFileRequest)

            # adjust headers
            #file.headers["Cache-control"] = "no-cache"
            #file.headers["Pragma"] = "no-cache"
            #file.headers["Expires"] = "0"
            #if "Last-Modified" in file.headers:
            #    del file.headers["Last-Modified"]
            # set default mime type to text/html
            if len(self.request.subpath):
                name = self.request.subpath[-1]
            else:
                name = df

            # cache content type
            global __ct_cache__
            ext = ".".join(name.split(".")[1:])
            if ext in __ct_cache__:
                ct = __ct_cache__[ext]
            else:
                ct = guess_type(
                    name,
                    strict=False)[0] or settings.get("server.content_type")
                __ct_cache__[ext] = ct

            file.headers["Content-Type"] = ct
            file.content_type = ct
            file.charset = settings.get("files.charset") or "utf-8"

        if self.debug:
            server_trace = settings.get("files.trace")
            # bw 0.2.6 renamed setting
            if server_trace is None:
                server_trace = settings.get("server.trace")
            # trace in debugger
            if server_trace and re.search(server_trace, url):
                pdb.set_trace()

        # run post file server hooked filters
        file = filtermanager.runPostHook(
            file, self.request, self.url
        )  #=> Ready to filter and return the current file. Step once (n) to apply filters.
        return file
Beispiel #6
0
    def response(self):
        request = self.request
        settings = request.registry.settings
        request.environ["proxy"] = self

        # Run pre proxy request hooked filters
        # If the filter raises `ResponseFinished` processing is stopped and the response returned.
        # If runPreHook returns a response and not None the proxy call is not triggered and and
        # post hooks are processed for the returned response.
        try:
            response = filtermanager.runPreHook(filtermanager.EmptyProxyResponse(), request, self.url)
        except filtermanager.ResponseFinished as e:
            return e.response

        if response is None:
            response, body = self.proxy(self.url, request)

        else:
            # pre hook returned response
            body = response.body

        def removeHeader(key, values):
            # the default header key should be the standard capitilized version e.g 'Content-Length'
            try:
                del headers[key]
            except KeyError:
                try:
                    del headers[key.lower()]
                except KeyError:
                    try:
                        del headers[key.upper()]
                    except KeyError:
                        pass

        # clean up headers
        headers = dict(response.headers)
        keys = [k.lower() for k in list(headers)]
        if 'content-length' in keys:
            removeHeader('Content-Length', headers)
        if 'transfer-encoding' in keys:
            removeHeader('Transfer-Encoding', headers)
        if 'content-encoding' in keys:
            removeHeader('Content-Encoding', headers)
        if 'connection' in keys:
            removeHeader('Connection', headers)
        if 'keep-alive' in keys:
            removeHeader('Keep-Alive', headers)

        # cookies
        if 'set-cookie' in keys:
            try:
                cookie = headers['Set-Cookie']
            except KeyError:
                try:
                    cookie = headers['set-cookie']
                except KeyError:
                    cookie = headers['SET-COOKIE']
            host = request.host.split(":")[0]
            local = str(self.url.host).split(":")[0]
            cookie = cookie.replace(local, host)
            headers['set-cookie'] = cookie
            
        proxy_response = Response(body=body, status=response.status_code)
        proxy_response.headers.update(headers)
        alsoProvides(proxy_response, filtermanager.IProxyRequest)

        # run post proxy request hooked filters
        proxy_response = filtermanager.runPostHook(proxy_response, request, self.url)

        return proxy_response