sinatra的send_file决定使用哪种内容类型的过程是什么?

问题描述 投票:0回答:1

sinatra的send_file决定使用哪种内容类型的过程是什么?

例如,似乎它通过传递给send_file的文件的扩展名来工作,所以如果它是send_file blah.txt。然后当我http到路由时,我会得到/响应头,content-type: text/plain,所以txt文件中的任何html都将被Web浏览器解释为纯文本。如果文件是blah.html,那么服务器将使用content-type: text/html进行响应。(并且文件中的任何html都是这样呈现的)

当然路线名称是无关紧要的,所以你可以去http://127.0.0.1:4567/zzz.html它可能导致send_file a.txt和a.txt可能包含html标签,但因为它是一个.txt文件send_file将导致sinatra响应content-type: text/plain并且浏览器赢了' t渲染任何发送的HTML,并将其显示为纯文本。我可能错了,但这似乎是我的快速测试所表明的。在哪里我尝试了不同的路由,不同的文件扩展名(.txt和.html),有时候文件中包含html,看看浏览器是否呈现html,并查看内容类型标题是什么,使用wget - d。

那么我的问题是,是否有一个sinatra的send_file函数使用的列表,它将文件扩展名与内容类型相关联?我想看看那份清单。如果没有,那么它正在使用的是什么过程。

注意 - 我知道有一种传递内容类型Sinatra: How to respond with an image with headers "content-type" => "image/jpeg"的方法,但我问的是/当没有传入内容类型时,send_file如何/通过什么方法确定内容类型。

http sinatra content-type sendfile
1个回答
1
投票

在Sinatra框架中的This is the send_file method(目前是v2.0.5),请注意如果没有设置,它会立即找到内容类型:

if opts[:type] or not response['Content-Type']
  content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
end

The content_type method将立即返回或交给mime_typeRack's mime_type methoddef mime_type(ext, fallback='application/octet-stream') MIME_TYPES.fetch(ext.to_s.downcase, fallback) end 的代表(目前为v2.0.7)。这使用众所周知的扩展列表进行检查。

on line 49

该列表开始MIME_TYPES = { ".123" => "application/vnd.lotus-1-2-3", ".3dml" => "text/vnd.in3d.3dml", ".3g2" => "video/3gpp2", ".3gp" => "video/3gpp", # <snip>

content_type

正如您从application/octet-stream片段中看到的那样,它可以依赖的默认值是qazxswpoi。

© www.soinside.com 2019 - 2024. All rights reserved.