envの中身

sinatraをサブディレクトリで動かしている時なんかに必要


http://localhost:4567/hello/world
にアクセスしたときのenvをppした

{"HTTP_ACCEPT"=>
  "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
 "HTTP_CACHE_CONTROL"=>"max-age=0",
 "HTTP_HOST"=>"localhost:4567",
 "SERVER_NAME"=>"localhost",
 "rack.url_scheme"=>"http",
 "HTTP_USER_AGENT"=>
  "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4",
 "REQUEST_PATH"=>"/hello/world",
 "SERVER_PROTOCOL"=>"HTTP/1.1",
 "HTTP_ACCEPT_LANGUAGE"=>"ja,en-US;q=0.8,en;q=0.6",
 "rack.errors"=>#<IO:0x2ddc0>,
 "async.callback"=>#<Method: Thin::Connection#post_process>,
 "REMOTE_ADDR"=>"127.0.0.1",
 "PATH_INFO"=>"/hello/world",
 "rack.run_once"=>false,
 "rack.version"=>[1, 0],
 "SERVER_SOFTWARE"=>"thin 1.2.7 codename No Hup",
 "SCRIPT_NAME"=>"",
 "HTTP_VERSION"=>"HTTP/1.1",
 "rack.multithread"=>false,
 "REQUEST_URI"=>"/hello/world",
 "rack.multiprocess"=>false,
 "rack.request.query_hash"=>{},
 "SERVER_PORT"=>"4567",
 "HTTP_ACCEPT_CHARSET"=>"Shift_JIS,utf-8;q=0.7,*;q=0.3",
 "async.close"=>#<EventMachine::DefaultDeferrable:0x1a3e1d8>,
 "REQUEST_METHOD"=>"GET",
 "rack.request.query_string"=>"",
 "QUERY_STRING"=>"",
 "GATEWAY_INTERFACE"=>"CGI/1.2",
 "HTTP_ACCEPT_ENCODING"=>"gzip,deflate,sdch",
 "HTTP_CONNECTION"=>"keep-alive",
 "rack.input"=>#<StringIO:0x1a3e50c>}

http://localhost:4567/hello/world?message=asdfjkl の場合

{"HTTP_ACCEPT"=>
  "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
 "HTTP_CACHE_CONTROL"=>"max-age=0",
 "HTTP_HOST"=>"localhost:4567",
 "SERVER_NAME"=>"localhost",
 "rack.url_scheme"=>"http",
 "HTTP_USER_AGENT"=>
  "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4",
 "REQUEST_PATH"=>"/hello/world",
 "SERVER_PROTOCOL"=>"HTTP/1.1",
 "HTTP_ACCEPT_LANGUAGE"=>"ja,en-US;q=0.8,en;q=0.6",
 "rack.errors"=>#<IO:0x2ddc0>,
 "async.callback"=>#<Method: Thin::Connection#post_process>,
 "REMOTE_ADDR"=>"127.0.0.1",
 "PATH_INFO"=>"/hello/world",
 "rack.run_once"=>false,
 "rack.version"=>[1, 0],
 "SERVER_SOFTWARE"=>"thin 1.2.7 codename No Hup",
 "SCRIPT_NAME"=>"",
 "HTTP_VERSION"=>"HTTP/1.1",
 "rack.multithread"=>false,
 "REQUEST_URI"=>"/hello/world?message=asdfjkl",
 "rack.multiprocess"=>false,
 "rack.request.query_hash"=>{"message"=>"asdfjkl"},
 "SERVER_PORT"=>"4567",
 "HTTP_ACCEPT_CHARSET"=>"Shift_JIS,utf-8;q=0.7,*;q=0.3",
 "async.close"=>#<EventMachine::DefaultDeferrable:0x1a435e8>,
 "REQUEST_METHOD"=>"GET",
 "rack.request.query_string"=>"message=asdfjkl",
 "GATEWAY_INTERFACE"=>"CGI/1.2",
 "HTTP_ACCEPT_ENCODING"=>"gzip,deflate,sdch",
 "HTTP_CONNECTION"=>"keep-alive",
 "QUERY_STRING"=>"message=asdfjkl",
 "rack.input"=>#<StringIO:0x1a43930>}
require 'rubygems'
require 'sinatra'
require 'rack'
require 'pp'

get '/hello/world' do
  pp env
end

apache2+passengerでSub URLで動かした場合
http://shokai.mag.keio.ac.jp/testapp/ がアプリのrootで、http://shokai.mag.keio.ac.jp/testapp/hello/world にリクエストした時のenvの中身

SERVER_NAME => shokai.mag.keio.ac.jp
rack.url_scheme => http
rack.run_once => false
rack.input => #
HTTP_ACCEPT_ENCODING => gzip,deflate,sdch
HTTP_USER_AGENT => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4
PATH_INFO => /hello/world
rack.errors => #
HTTP_ACCEPT_LANGUAGE => ja,en-US;q=0.8,en;q=0.6
HTTP_CACHE_CONTROL => max-age=0
HTTP_HOST => shokai.mag.keio.ac.jp
SCRIPT_NAME => /testapp
SERVER_ADDR => 133.27.242.50
SERVER_PROTOCOL => HTTP/1.1
REMOTE_ADDR => ひみつ
SERVER_SOFTWARE => Apache/2.2.8 (Ubuntu) mod_python/3.3.1 Python/2.5.2 PHP/5.2.4-2ubuntu5.10 with Suhosin-Patch proxy_html/3.0.0 mod_ruby/1.2.6 Ruby/1.8.6(2007-09-24) Phusion_Passenger/2.2.11
rack.request.query_hash => 
rack.multithread => false
rack.version => 10
HTTP_COOKIE => ひみつ
HTTP_ACCEPT_CHARSET => Shift_JIS,utf-8;q=0.7,*;q=0.3
rack.multiprocess => true
REQUEST_URI => /testapp/hello/world
DOCUMENT_ROOT => /var/www/
SERVER_PORT => 80
rack.request.query_string => 
QUERY_STRING => 
REMOTE_PORT => 52489
SERVER_ADMIN => webmaster@localhost
_ => _
HTTP_ACCEPT => application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
HTTP_CONNECTION => keep-alive
REQUEST_METHOD => GET

重要そうな値

  • rack.url_scheme => http
  • SCRIPT_NAME => /testapp
  • HTTP_HOST => shokai.mag.keio.ac.jp
  • HTTP_HOST => localhost:4567
  • REQUEST_URI => /testapp/hello/world
  • REQUEST_URI => /hello/world?message=asdfjkl
  • PATH_INFO => /hello/world
  • rack.request.query_string => message=asdfjkl
  • QUERY_STRING => message=asdfjkl
  • rack.request.query_hash => {"message"=>"asdfjkl"}
  • env['HTTP_HOST']にホスト名と接続ポート番号(あれば)が入る
  • env['SCRIPT_NAME']がサブディレクトリで動かしている場合のディレクトリ名になる。サブディレクトリで動かしていない場合は空になる。
  • env['SCRIPT_NAME']+env['PATH_INFO']+'?'+env['QUERY_STRING'] == env['REQUEST_URI']になる
  • env['QUERY_STRING'] は env['rack.request.query_string'] にも同じ値が入っている。env['rack.request.query_hash']にはparseされたHashが入ってる
  • redirectするならparams[:splat]ではなくenv['REQUEST_URI']を使う


URLを復元するならこうでいいのかな

  if env['QUERY_STRING'].to_s.size>0
    url = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}#{env['PATH_INFO']}?#{env['QUERY_STRING']}"
  else
    url = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}#{env['PATH_INFO']}"
  end