gpt4 book ai didi

ruby-on-rails - Rack 中间件中的 "env"变量是什么?

转载 作者:行者123 更新时间:2023-12-03 11:33:10 26 4
gpt4 key购买 nike

我知道 Rack 中间件过滤器是一个带有初始化和调用方法的 Ruby 类。我知道 call 方法需要一个“env”参数。像这样的东西:

class MyFilter
def initialize(app)
end

def call(env)
end
end

我的问题是:发送给“call”的“env”参数究竟是什么?这是否与 Rails ENV 环境相同(即开发、测试、生产)。

谢谢!

最佳答案

env只是一个哈希。 Rack 本身和各种中间件为其添加了值(value)。

要了解散列中的各种键是什么,请查看 Rack Specification .

这是一个示例 env 哈希:

{
"GATEWAY_INTERFACE" => "CGI/1.1",
"PATH_INFO" => "/index.html",
"QUERY_STRING" => "",
"REMOTE_ADDR" => "::1",
"REMOTE_HOST" => "localhost",
"REQUEST_METHOD" => "GET",
"REQUEST_URI" => "http://localhost:3000/index.html",
"SCRIPT_NAME" => "",
"SERVER_NAME" => "localhost",
"SERVER_PORT" => "3000",
"SERVER_PROTOCOL" => "HTTP/1.1",
"SERVER_SOFTWARE" => "WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22)",
"HTTP_HOST" => "localhost:3000",
"HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0",
"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"HTTP_ACCEPT_LANGUAGE" => "zh-tw,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"HTTP_ACCEPT_ENCODING" => "gzip, deflate",
"HTTP_COOKIE" => "jsonrpc.session=3iqp3ydRwFyqjcfO0GT2bzUh.bacc2786c7a81df0d0e950bec8fa1a9b1ba0bb61",
"HTTP_CONNECTION" => "keep-alive",
"HTTP_CACHE_CONTROL" => "max-age=0",
"rack.version" => [1, 2],
"rack.input" => #<StringIO:0x007fa1bce039f8>,
"rack.errors" => #<IO:<STDERR>>,
"rack.multithread" => true,
"rack.multiprocess" => false,
"rack.run_once" => false,
"rack.url_scheme" => "http",
"HTTP_VERSION" => "HTTP/1.1",
"REQUEST_PATH" => "/index.html"
}

为了更易于使用,请查看 Rack::Request这使得访问 env 哈希中的值变得更加容易。

关于ruby-on-rails - Rack 中间件中的 "env"变量是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17396611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com