如何设置内容类型标头以响应 Pyramid Web 框架中的特定文件类型 [英] How to set the content type header in response for a particular file type in Pyramid web framework
问题描述
我正在使用金字塔网络框架来构建网站.我一直在 Chrome 控制台中收到此警告:
I am using pyramid web framework to build a website. I keep getting this warning in chrome console:
资源被解释为字体但使用 MIME 类型 application/octet-stream: "http:static/images/fonts/font.woff".
Resource interpreted as Font but transferred with MIME type application/octet-stream: "http:static/images/fonts/font.woff".
如何消除此警告消息?
我已经配置了使用 add_static_view
我可以想到一种方法,通过为响应添加一个订阅者函数来检查路径是否以 .woff
结尾,并将响应头设置为 application/x-font-woff
.但它看起来不像一个干净的解决方案.有没有办法通过一些设置告诉 Pyramid 这样做.
I can think of a way to do this by adding a subscriber function for responses that checks if the path ends in .woff
and setting the response header to application/x-font-woff
. But it does not look like a clean solution. Is there a way to tell Pyramid to do it through some setting.
推荐答案
Pyramid 使用标准 mimetypes
模块 根据扩展猜测 mimetype.它调用:
Pyramid uses the standard mimetypes
module to guess the mimetype based on the extension. It calls:
mimetypes.guess_type(path, strict=False)
如果在该平台上,模块会在 Windows 注册表中查找,并在以下位置查找 mimetype 列表:
The module looks in the Windows registry if on that platform, and in the following locations for mimetype lists:
knownfiles = [
"/etc/mime.types",
"/etc/httpd/mime.types", # Mac OS X
"/etc/httpd/conf/mime.types", # Apache
"/etc/apache/mime.types", # Apache 1
"/etc/apache2/mime.types", # Apache 2
"/usr/local/etc/httpd/conf/mime.types",
"/usr/local/lib/netscape/mime.types",
"/usr/local/etc/httpd/conf/mime.types", # Apache 1.2
"/usr/local/etc/mime.types", # Apache 1.3
]
您可以扩展这些文件之一,也可以创建自己的文件并使用 .init()
函数将其添加到模块中.
You can either extend one of those files, or create your own file and add it to the module using the .init()
function.
文件格式很简单,只需列出 mimetype,然后是一些空格,然后是空格分隔的扩展名列表:
The file format is simple, just list the mimetype, then some whitespace, then a space-separated list of extensions:
application/x-font-woff woff
这篇关于如何设置内容类型标头以响应 Pyramid Web 框架中的特定文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!