Module: Chase::Server

Includes:
HttpParser
Defined in:
lib/chase/server.rb

Overview

HTTP Server Module

Constant Summary

Constants included from HttpParser

HttpParser::MAPPED_HEADERS, HttpParser::VALID_METHODS

Instance Method Summary collapse

Methods included from HttpParser

#http_method, #http_parser, #set_env

Instance Method Details

#prepare_requestObject



30
31
32
33
34
35
36
37
38
# File 'lib/chase/server.rb', line 30

def prepare_request
  Request.new.tap do |request|
    request.on(:created) do
      raise ServerError unless request.env['REQUEST_METHOD']
      prepare_response
      handle
    end
  end
end

#prepare_responseObject



40
41
42
43
# File 'lib/chase/server.rb', line 40

def prepare_response
  response.on(:flushed) { close_connection_after_writing }
  response.on(:write) { |data| send_data(data) }
end

#receive_data(data) ⇒ Object



11
12
13
14
15
# File 'lib/chase/server.rb', line 11

def receive_data(data)
  http_parser << data
rescue ServerError, HTTP::Parser::Error
  send_error('400 Bad Request')
end

#requestObject



17
18
19
# File 'lib/chase/server.rb', line 17

def request
  @request ||= prepare_request
end

#responseObject



21
22
23
# File 'lib/chase/server.rb', line 21

def response
  @response ||= Response.new
end

#send_error(status_code) ⇒ Object



25
26
27
28
# File 'lib/chase/server.rb', line 25

def send_error(status_code)
  send_data "HTTP/1.1 #{status_code}\r\nConnection: close\r\nContent-Type: text/plain\r\n"
  close_connection_after_writing
end