Module: Oaf::HTTP
Constant Summary
Constants included from Oaf
Instance Method Summary collapse
-
#parse_response(output) ⇒ Object
Given output from a script, parse HTTP response details and return them as a hash, including body, status, and headers.
-
#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.
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 = false headers = {'content-type' => 'text/plain'} status = 200 headers, status, = Oaf::Util. output lines = output.split("\n") body = lines.take(lines.length - ).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 |