Class: Rack::Handler::Trusted

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/handler/trusted.rb

Class Method Summary collapse

Class Method Details

.run(app, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/rack/handler/trusted.rb', line 8

def self.run(app, options = {})
  config = options[:trusted_config] || ::Trusted::Config::Builder.new.build

  ::Trusted::Server.new(config).listen do |request, response|
    puts "REQUEST: [#{request.method}] #{request.uri}"

    rack_input = StringIO::new(request.body)

    env = {
      'REQUEST_METHOD' => request.method,
      'REQUEST_URI' => request.uri,
      'PATH_INFO' => request.path_info,
      'QUERY_STRING' => request.query_string,
      'REMOTE_ADDR' => request.remote_addr,
      'SERVER_PORT' => '3000',
      'SERVER_NAME' => 'localhost',
      'SCRIPT_NAME' => '',
      'rack.version' => Rack::VERSION,
      'rack.input' => rack_input,
      'rack.errors' => $stderr,
      'rack.multithread' => true,
      'rack.multiprocess' => false,
      'rack.run_once' => false,
      'rack.url_scheme' => 'http',
      'rack.hijack?' => false
    }

    env.merge!(request.headers)

    status, headers, body = app.call(env)

    response.status = status
    response.headers = headers

    response.body = ''

    body.each { |b| response.body.concat(b) }

    body.close if body.respond_to?(:close)

    response
  end
end