Class: Miniserver::Connection
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Miniserver::Connection
- Defined in:
- lib/miniserver/connection.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
Instance Method Summary collapse
- #on_body(chunk) ⇒ Object
- #on_headers_complete(headers) ⇒ Object
- #on_message_complete ⇒ Object
- #post_init ⇒ Object
- #receive_data(data) ⇒ Object
- #send_body ⇒ Object
- #send_headers ⇒ Object
- #send_response! ⇒ Object
- #send_status_line ⇒ Object
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
8 9 10 |
# File 'lib/miniserver/connection.rb', line 8 def app @app end |
Instance Method Details
#on_body(chunk) ⇒ Object
21 22 23 |
# File 'lib/miniserver/connection.rb', line 21 def on_body(chunk) @request.body << chunk end |
#on_headers_complete(headers) ⇒ Object
16 17 18 19 |
# File 'lib/miniserver/connection.rb', line 16 def on_headers_complete(headers) @request.headers = headers @request.parse_header(@parser, self) end |
#on_message_complete ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/miniserver/connection.rb', line 25 def begin Rack::Handler::Miniserver.new(app).process(@request, @response) send_response! rescue Miniserver.logger.error("!! Unexpected error while processing request: #{$!.}") ensure close_connection_after_writing end end |
#post_init ⇒ Object
10 11 12 13 14 |
# File 'lib/miniserver/connection.rb', line 10 def post_init @request = Request.new @response = Response.new @parser = Http::Parser.new(self) end |
#receive_data(data) ⇒ Object
36 37 38 |
# File 'lib/miniserver/connection.rb', line 36 def receive_data(data) @parser << data end |
#send_body ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/miniserver/connection.rb', line 70 def send_body begin @response.body.each { |chunk| send_data(chunk) } ensure @response.body.close if @response.body.respond_to?(:close) end end |
#send_headers ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/miniserver/connection.rb', line 51 def send_headers headers = String.new @response.headers[SERVER] = MINISERVER @response.headers[DATE] = Time.now.httpdate @response.headers.each do |name, value| if value.is_a?(String) value.split(NEWLINE).each do |v| headers.concat("#{name}#{COLON} #{v}#{BLANK_LINE}") end else headers.concat("#{name}#{COLON} #{value}#{BLANK_LINE}") end end send_data(headers) end |
#send_response! ⇒ Object
40 41 42 43 44 45 |
# File 'lib/miniserver/connection.rb', line 40 def send_response! send_status_line send_headers send_data(BLANK_LINE) send_body end |
#send_status_line ⇒ Object
47 48 49 |
# File 'lib/miniserver/connection.rb', line 47 def send_status_line send_data("#{HTTP_VERSION} #{@response.status} #{HTTP_CODE[@response.status]}#{BLANK_LINE}") end |