标题是不言自明的.有什么方法可以获取标头(Rack::Request.env[] 除外)?
The title is pretty self-explanatory. Is there any way to get the headers (except for Rack::Request.env[])?
推荐答案Rack 环境中提供 HTTP 标头 传递给您的应用:
HTTP_ 变量:对应于客户端提供的 HTTP 请求标头的变量(即名称以 HTTP_ 开头的变量).这些变量的存在与否应与请求中相应 HTTP 标头的存在或不存在相对应.
HTTP_ Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request.
因此 HTTP 标头以HTTP_"为前缀并添加到哈希中.
So the HTTP headers are prefixed with "HTTP_" and added to the hash.
这是一个提取并显示它们的小程序:
Here's a little program that extracts and displays them:
require 'rack' app = Proc.new do |env| headers = env.select {|k,v| k.start_with? 'HTTP_'} .collect {|key, val| [key.sub(/^HTTP_/, ''), val]} .collect {|key, val| "#{key}: #{val}<br>"} .sort [200, {'Content-Type' => 'text/html'}, headers] end Rack::Server.start :app => app, :Port => 8080当我运行它时,除了 Chrome 或 Firefox 显示的 HTTP 标头之外,还有一个VERSION:HTPP/1.1"(即一个带有键HTTP_VERSION"和值HTTP/1.1"的条目正在添加到 env 哈希).
When I run this, in addition to the HTTP headers as shown by Chrome or Firefox, there is a "VERSION: HTPP/1.1" (i.e. an entry with key "HTTP_VERSION" and value "HTTP/1.1" is being added to the env hash).
更多推荐
Rack::Request
发布评论