Module: Oaf::HTTPServer
Instance Method Summary collapse
-
#get_request_body(req) ⇒ Object
Safely retrieves the request body, and assumes an empty string if it cannot be retrieved.
-
#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(options) ⇒ Object
Invokes the Webrick web server library to handle incoming requests, and routes them to the appropriate scripts if they exist on the filesystem.
-
#set_response!(res, headers, body, status) ⇒ Object
Consume HTTP response details and set them into a response object.
Instance Method Details
#get_request_body(req) ⇒ Object
Safely retrieves the request body, and assumes an empty string if it cannot be retrieved. This helps get around a nasty exception in WEBrick.
Parameters:
- req
-
A WEBrick::HTTPRequest object
Returns:
A string containing the request body
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/oaf/httpserver.rb', line 38 def get_request_body req if ['POST', 'PUT'].member? req.request_method begin result = req.body rescue WEBrick::HTTPStatus::LengthRequired result = '' # needs to be in rescue for coverage end end result end |
#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).
18 19 20 21 22 23 24 25 26 |
# File 'lib/oaf/httpserver.rb', line 18 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(options) ⇒ 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:
- options
-
A hash of Oaf configuration options
76 77 78 79 80 81 |
# File 'lib/oaf/httpserver.rb', line 76 def serve server = WEBrick::HTTPServer.new :Port => [:port] server.mount '/', Oaf::HTTPHandler, trap 'INT' do server.shutdown end server.start end |
#set_response!(res, headers, body, status) ⇒ Object
Consume HTTP response details and set them into a response object.
Parameters:
- res
-
A WEBrick::HTTPResponse object
- headers
-
A hash containing HTTP response headers
- body
-
A string containing the HTTP response body
- status
-
An integer indicating the response status
61 62 63 64 65 66 67 |
# File 'lib/oaf/httpserver.rb', line 61 def set_response! res, headers, body, status headers.each do |name, value| res.header[name] = value end res.body = body res.status = status end |