Class: Messiah::RackCGIApp

Inherits:
Object
  • Object
show all
Defined in:
lib/messiah/rack_cgi_app.rb

Instance Method Summary collapse

Instance Method Details

#add_to_environment!(env) ⇒ Object



9
10
11
12
13
14
# File 'lib/messiah/rack_cgi_app.rb', line 9

def add_to_environment!(env)
  env['HTTP_HOST'] = Messiah.host
  env['DOCUMENT_ROOT'] = Messiah.root
  env['SCRIPT_NAME'] = (Messiah.script || env['PATH_INFO']).gsub(/^\//, '')
  env['SCRIPT_FILENAME'] = env['PATH_TRANSLATED'] = File.join(env['DOCUMENT_ROOT'], env['SCRIPT_NAME'])
end

#build_environment_string(env) ⇒ Object



16
17
18
19
20
21
# File 'lib/messiah/rack_cgi_app.rb', line 16

def build_environment_string(env)
  env.inject([]) do |a, item|
    a << "#{item.first}=\"#{item.last}\""
    a
  end.join(' ')
end

#call(env) ⇒ Object



2
3
4
5
6
7
# File 'lib/messiah/rack_cgi_app.rb', line 2

def call(env)
  add_to_environment!(env)
  header_string, body = call_cgi(env)
  headers, status = parse_headers(header_string)
  [status, headers, [body]]
end

#call_cgi(env) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/messiah/rack_cgi_app.rb', line 23

def call_cgi(env)
  env_string = build_environment_string(env)

  stdin, stdout, stderr = Open3.popen3("env #{env_string} #{Messiah.command}")
  stdin.write env['rack.input'].read if env['REQUEST_METHOD'] == 'POST'
  stdin.close
  
  stdout.read.split("\n\r", 2)
end

#parse_headers(header) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/messiah/rack_cgi_app.rb', line 33

def parse_headers(header)
  headers = header.split("\n").inject({}) do |h, line|
    key, val = line.split(':').map(&:strip)
    h[key] = val
    h
  end
  status = headers.delete('Status') || 200
  
  [headers, status]
end