Class: WAB::Impl::RackHandler

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/wab/impl/rack_handler.rb

Overview

Handler for requests that fall under the path assigned to the rack Controller. This is used only with the WAB::Impl::Shell.

Instance Method Summary collapse

Constructor Details

#initialize(server, shell, handler) ⇒ RackHandler

Returns a new instance of RackHandler.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wab/impl/rack_handler.rb', line 11

def initialize(server, shell, handler)
  super(server)
  @shell = shell
  case handler
  when String
    handler = Object.const_get(handler).new(self)
  when Class
    handler = handler.new(self)
  end
  handler.shell = self
	@handler = handler
end

Instance Method Details

#service(req, res) ⇒ Object



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
# File 'lib/wab/impl/rack_handler.rb', line 24

def service(req, res)
	env = {
	  'REQUEST_METHOD' => req.request_method,
	  'SCRIPT_NAME' => req.script_name,
	  'PATH_INFO' => req.path_info,
	  'QUERY_STRING' => req.query_string,
	  'SERVER_NAME' => req.server_name,
	  'SERVER_PORT' => req.port,
	  'rack.version' => '1.2',
	  'rack.url_scheme' => req.ssl? ? 'https' : 'http',
	  'rack.errors' => '', ## TBD
	  'rack.multithread' => false,
	  'rack.multiprocess' => false,
	  'rack.run_once' => false,
	}
	req.each { |k| env['HTTP_' + k] = req[k] }
	unless req.body.nil?
	  env['rack.input'] = StringIO.new(req.body)
	end
	rres = @handler.call(env)
  res.status = rres[0]
	rres[1].each { |a| res[a[0]] = a[1] }
	unless rres[2].empty?
	  res.body = ''
	  rres[2].each { |s| res.body << s }
	end
  @shell.logger.debug("reply to #{path.join('/')}#{query}: #{res.body}") if @shell.logger.debug?
rescue StandardError => e
  send_error(e, res)
end