Class: Up::UWebSocket::RackEnv

Inherits:
Hash
  • Object
show all
Defined in:
lib/up/u_web_socket/rack_env.rb

Constant Summary collapse

RACK_VARS =
%w[rack.errors rack.hijack rack.hijack? rack.input rack.logger
rack.multipart.buffer_size rack.multipart.tempfile_factory
rack.response_finished
rack.session rack.upgrade rack.upgrade? rack.url_scheme
HTTP_ACCEPT HTTP_ACCEPT_ENCODING HTTP_ACCEPT_LANGUAGE
HTTP_CONNECTION HTTP_HOST HTTP_USER_AGENT PATH_INFO QUERY_STRING REQUEST_METHOD
SCRIPT_NAME SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE]

Instance Method Summary collapse

Constructor Details

#initialize(req, config) ⇒ RackEnv

Returns a new instance of RackEnv.



16
17
18
19
# File 'lib/up/u_web_socket/rack_env.rb', line 16

def initialize(req, config)
  @req = req
  @config = config
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/up/u_web_socket/rack_env.rb', line 21

def [](key)
  return super(key) if key?(key)
  self[key] = case key
              when 'rack.errors'
                STDERR
              when 'rack.hijack'
                nil
              when 'rack.hijack?'
                false
              when 'rack.input'
                ::IO.new
              when 'rack.logger'
                ::Logger.new(self['rack.errors'])
              when 'rack.multipart.buffer_size'
                4096
              when 'rack.multipart.tempfile_factory'
                proc { |_filename, _content_type| File.new }
              when 'rack.response_finished'
                []
              when 'rack.session'
                {}
              when 'rack.upgrade'
                nil
              when 'rack.upgrade?'
                nil
              when 'rack.url_scheme'
                @config[:scheme]
              when 'PATH_INFO'
                `#@req.getUrl()`
              when 'QUERY_STRING'
                `#@req.getQuery()`
              when 'RACK_ERRORS'
                self['rack.errors']
              when 'RACK_LOGGER'
                self['rack.logger']
              when 'REQUEST_METHOD'
                `#@req.getMethod().toUpperCase()`
              when 'SCRIPT_NAME'
                ""
              when 'SERVER_NAME'
                @config[:host]
              when 'SERVER_PORT'
                @config[:port].to_s
              when 'SERVER_PROTOCOL'
                `#@req.getHeader('protocol')`
              when 'SERVER_SOFTWARE'
                "#{@config[:handler]}/#{Up::VERSION} #{@config[:engine]}"
              else
                if key.start_with?('HTTP_')
                  key = key[5..].gsub(/_/, '-')
                  `#@req.getHeader(key.toLowerCase())`
                else
                  nil
                end
              end
end

#eachObject



84
85
86
87
88
89
90
# File 'lib/up/u_web_socket/rack_env.rb', line 84

def each
  unless @got_them_all
    RACK_VARS.each { |k| self[k] unless self.key?(k) }
    @got_them_all = true
  end
  super
end

#req_headersObject



78
79
80
81
82
# File 'lib/up/u_web_socket/rack_env.rb', line 78

def req_headers
  h = {}
  `#@req.forEach((k, v) => { h.set(k, v); })`
  h
end

#to_sObject



92
93
94
95
96
97
98
# File 'lib/up/u_web_socket/rack_env.rb', line 92

def to_s
  unless @got_them_all
    RACK_VARS.each { |k| self[k] unless self.key?(k) }
    @got_them_all = true
  end
  super
end