What is the “env” variable in Rack middleware?

寵の児 提交于 2019-12-18 14:04:45

问题


I know a Rack middleware filter is a Ruby class with an initialize and a call method. I know the call method takes an "env" argument. Something like this:

class MyFilter
  def initialize(app)
  end

  def call(env)
  end
end

My question is: what exactly is the "env" argument sent to "call"? Is this the same as the Rails ENV environment (ie. development, testing, production).

Thanks!


回答1:


env is just a hash. Rack itself and various middlewares add values into it.

To understand what the various keys are in the hash, check out the Rack Specification.

And here is a sample env hash:

{
  "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"
}

And to make it easier to use, checkout Rack::Request which makes it easier to access the values inside the env hash.




回答2:


I suggest that you can try to print the 'env' variable with writing a simple programming.

require "rubygems"
require "rack" 
def pp(hash)
  hash.map {|key,value| "#{key} => #{value}"}.sort.join("<br/>") 
end
Rack::Handler::WEBrick.run lambda {|env| [200,{},[pp(env)]]} , :Port=>3000

enter the link localhost:3000




回答3:


The env variable is a hash, which contains a lot of useful information including request headers and body, and run-time environment data that may have been added by upstream middleware.




回答4:


Looking at the source code depending on which web server you have you can get different values in your env hash. I recommend only using what's in the documentation.

Basically the env is a hash version of the request object specific to the web server. Rack does some work to give a normalized env so the middleware can behave consistently across web servers.



来源:https://stackoverflow.com/questions/17396611/what-is-the-env-variable-in-rack-middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!