Class: Yarn::RackHandler

Inherits:
AbstractHandler show all
Defined in:
lib/yarn/rack_handler.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractHandler

#parser, #request, #response, #session

Instance Method Summary collapse

Methods inherited from AbstractHandler

#client_address, #extract_path, #parse_request, #persistent?, #post_body, #read_request, #request_path, #return_response, #run, #set_common_headers

Methods included from ErrorPage

#serve_404_page, #serve_500_page

Methods included from Logging

#debug, #log, #output, #timestamp

Constructor Details

#initialize(app, opts = {}) ⇒ RackHandler

Returns a new instance of RackHandler.



8
9
10
11
12
13
14
# File 'lib/yarn/rack_handler.rb', line 8

def initialize(app,opts={})
  @parser = Parser.new
  @response = Response.new
  @app = app
  @host = opts[:host]
  @port = opts[:port].to_s
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/yarn/rack_handler.rb', line 6

def env
  @env
end

#optsObject

Returns the value of attribute opts.



6
7
8
# File 'lib/yarn/rack_handler.rb', line 6

def opts
  @opts
end

Instance Method Details

#has_body?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/yarn/rack_handler.rb', line 51

def has_body?
  value ||= !! @request[:body]
  value
end

#make_envObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/yarn/rack_handler.rb', line 26

def make_env
  input = StringIO.new("").set_encoding(Encoding::ASCII_8BIT)
  if has_body?
    input.string = @request[:body]
  end
  @env = {
    "REQUEST_METHOD"    => @request[:method].to_s,
    "PATH_INFO"         => @request[:uri][:path].to_s,
    "QUERY_STRING"      => @request[:uri][:query].to_s,
    "SERVER_NAME"       => @host,
    "SERVER_PORT"       => @port,
    "SCRIPT_NAME"       => "",
    "rack.input"        => input,
    "rack.version"      => Rack::VERSION,
    "rack.errors"       => $output,
    "rack.multithread"  => true,
    "rack.multiprocess" => true,
    "rack.run_once"     => false,
    "rack.url_scheme"   => "http"
  }
  @env["CONTENT_LENGTH"] = @request[:body].size.to_i if has_body?

  return @env
end

#prepare_responseObject



16
17
18
19
20
21
22
23
24
# File 'lib/yarn/rack_handler.rb', line 16

def prepare_response
  begin
    make_env
    @response.content = @app.call(@env)
  rescue Exception => e
    log e.message
    log e.backtrace
  end
end