Class: Yarn::RackHandler

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

Overview

handler for Rack applications.

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, #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.



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

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.



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

def env
  @env
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#has_body?Boolean

Proxy for whether the request has body data.

Returns:

  • (Boolean)


56
57
58
# File 'lib/yarn/rack_handler.rb', line 56

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

#make_envObject

Creates and formats the Rack environment.



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/yarn/rack_handler.rb', line 30

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"  => false,
    "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

Prepares the response by setting the environment and calling the Rack app.



18
19
20
21
22
23
24
25
26
27
# File 'lib/yarn/rack_handler.rb', line 18

def prepare_response
  begin
    make_env
    @response.content = @app.call(@env)
    @response.body.close if @response.body.respond_to?("close")
  rescue Exception => e
    log e.message
    log e.backtrace
  end
end