Module: Oaf::HTTP

Extended by:
Oaf, HTTP
Included in:
HTTP
Defined in:
lib/oaf/http.rb

Constant Summary

Constants included from Oaf

VERSION

Instance Method Summary collapse

Instance Method Details

#parse_response(output) ⇒ Object

Given output from a script, parse HTTP response details and return them as a hash, including body, status, and headers.

Parameters:

output

The output text data returned by a script.

Returns:

A hash containing the HTTP response details (body, status, and headers).



41
42
43
44
45
46
47
48
49
# File 'lib/oaf/http.rb', line 41

def parse_response output
  has_meta = false
  headers = {'content-type' => 'text/plain'}
  status = 200
  headers, status, meta_size = Oaf::Util.parse_http_meta output
  lines = output.split("\n")
  body = lines.take(lines.length - meta_size).join("\n")+"\n"
  [headers, status, body]
end

#serve(path, port) ⇒ Object

Invokes the Webrick web server library to handle incoming requests, and routes them to the appropriate scripts if they exist on the filesystem.

Parameters:

path

The path in which to search for files

port

The TCP port to listen on



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/oaf/http.rb', line 60

def serve path, port
  server = WEBrick::HTTPServer.new :Port => port
  server.mount_proc '/' do |req, res|
    req_body = ''
    req_headers = req.header
    req_query = req.query
    if ['POST', 'PUT'].member? req.request_method
      begin
        req_body = req.body
      rescue WEBrick::HTTPStatus::LengthRequired
        true  # without this, coverage is not collected.
      end
    end
    file = Oaf::Util.get_request_file path, req.path, req.request_method
    out = Oaf::Util.get_output file, req_headers, req_body, req_query
    res_headers, res_status, res_body = Oaf::HTTP.parse_response out
    res_headers.each do |name, value|
      res.header[name] = value
    end
    res.status = res_status
    res.body = res_body
  end
  trap 'INT' do server.shutdown end
  server.start
end