如何为 pylons 静态(公共)文件设置自定义响应标头?

发布于 2024-07-19 07:21:25 字数 33 浏览 2 评论 0原文

如何向 pylons 公开提供的文件添加自定义标头?

How do I add a custom header to files pylons is serving from public?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉鱼一梦 2024-07-26 07:21:25

a) 让您的网络服务器从 /public 而不是 Paster 提供文件,并将其配置为传递一些特殊标头。

b) 添加一个特殊的路由并自己提供文件

class FilesController(BaseController):
    def download(self, path)
        fapp = FileApp( path, headers=self.get_headers(path) )
        return fapp(request.environ, self.start_response)

c) 也许有一种方法覆盖标题,我只是不知道如何。

a) Let your webserver serve files from /public instead of paster and configure it to pass some special headers.

b) Add a special route and serve the files yourself ala

class FilesController(BaseController):
    def download(self, path)
        fapp = FileApp( path, headers=self.get_headers(path) )
        return fapp(request.environ, self.start_response)

c) maybe there is a way to overwrite headers and i just dont know how.

ぺ禁宫浮华殁 2024-07-26 07:21:25

对于最新版本的路线,您可以使用“Magic path_info ' 功能,并按照此处的文档编写控制器,以便它调用 Paster.DirectoryApp。

在我的项目中,我想提供公共目录中的任何文件(包括子目录),并以此作为控制器结束,以便能够覆盖 content_type :

import logging
from paste.fileapp import FileApp

from paste.urlparser import StaticURLParser
from pylons import config
from os.path import basename

class ForceDownloadController(StaticURLParser):

    def __init__(self, directory=None, root_directory=None, cache_max_age=None):
        if not directory:
            directory = config['pylons.paths']['static_files']
        StaticURLParser.__init__(self, directory, root_directory, cache_max_age)

    def make_app(self, filename):
        headers = [('Content-Disposition', 'filename=%s' % (basename(filename)))]
        return FileApp(filename, headers, content_type='application/octetstream')

With a recent version of route, you can use the 'Magic path_info' feature, and follow the documentation from here to write your controller so it calls paster.DirectoryApp.

In my project, I wanted to serve any file in the public directory, including subdirs, and ended with this as controller, to be able to override content_type :

import logging
from paste.fileapp import FileApp

from paste.urlparser import StaticURLParser
from pylons import config
from os.path import basename

class ForceDownloadController(StaticURLParser):

    def __init__(self, directory=None, root_directory=None, cache_max_age=None):
        if not directory:
            directory = config['pylons.paths']['static_files']
        StaticURLParser.__init__(self, directory, root_directory, cache_max_age)

    def make_app(self, filename):
        headers = [('Content-Disposition', 'filename=%s' % (basename(filename)))]
        return FileApp(filename, headers, content_type='application/octetstream')
握住你手 2024-07-26 07:21:25

在标准 Pylons 设置中,公共文件由 StaticUrlParser 提供。 这通常是在 config/middleware.py:make_app() 函数中设置的。

您需要像 Antonin ENFRUN 描述的那样对 StaticUrlParser 进行子类化,尽管将其称为控制器会令人困惑,因为它的用途不同。 在 config/middleware.py 的顶部添加类似以下内容:

from paste.fileapp import FileApp
from paste.urlparser import StaticURLParser

class HeaderUrlParser(StaticURLParser):
    def make_app(self, filename):
        headers = # your headers here
        return FileApp(filename, headers, content_type='application/octetstream')

然后将 config/middleware.py:make_app() 中的 StaticUrlParser 替换为 HeaderUrlParser

    static_app = StaticURLParser(config['pylons.paths']['static_files'])

变为

    static_app = HeaderURLParser(config['pylons.paths']['static_files'])

In a standard Pylons setup, the public files are served from a StaticUrlParser. This is typically setup in your config/middleware.py:make_app() function

You need to subclass the StaticUrlParser like Antonin ENFRUN describes, though calling it a Controller is confusing because it's doing a different purpose. Add something like the following to the top of the config/middleware.py:

from paste.fileapp import FileApp
from paste.urlparser import StaticURLParser

class HeaderUrlParser(StaticURLParser):
    def make_app(self, filename):
        headers = # your headers here
        return FileApp(filename, headers, content_type='application/octetstream')

then replace StaticUrlParser in config/middleware.py:make_app() with HeaderUrlParser

    static_app = StaticURLParser(config['pylons.paths']['static_files'])

becomes

    static_app = HeaderURLParser(config['pylons.paths']['static_files'])
痴骨ら 2024-07-26 07:21:25

使用 FileApp 进行流式传输的更简单方法,基于 pylon书。 下面的代码假设您的路由提供了 some_file_identifier,但其他两个变量是“神奇的”(请参阅​​代码后的说明)。

class MyFileController(BaseController):
  def serve(self, environ, start_response, some_file_identifier):
      path = self._convert_id_to_path(some_file_identifier)
      app = FileApp(path)
      return app(environ, start_response)

如果方法签名中有这些名称的变量,Pylons 会自动为您提供 wsgi environstart_response 变量。 否则,您不需要设置或修改标头,但如果您这样做,您可以使用 FileApp 中内置的功能 < /a> 来实现这一点。

A simpler way to use FileApp for streaming, based on the pylons book. The code below assumes your route provides some_file_identifier, but the other two variables are "magic" (see explanation after code).

class MyFileController(BaseController):
  def serve(self, environ, start_response, some_file_identifier):
      path = self._convert_id_to_path(some_file_identifier)
      app = FileApp(path)
      return app(environ, start_response)

Pylons automatically gives you the wsgi environ and start_response variables if you have variables of those names in your method signature. You should not need to set or munge headers otherwise, but if you do you can use the abilities built in to FileApp to achieve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文