Class: Hoof::HttpServer

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/hoof/http_server.rb

Instance Method Summary collapse

Instance Method Details

#receive_data(data) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hoof/http_server.rb', line 4

def receive_data data
  parser = Http::Parser.new
  parser.parse data
  host = parser.headers["HOST"].gsub(/:\d+$/, '')

  close_connection and return unless host =~ /.dev$/

  name = host.gsub(/.dev$/, '')
  application = Hoof.find name

  if application
    begin
      application.start
      puts "Serve #{host}#{parser.path}"
      EventMachine.defer(proc {
        application.serve data
      }, proc { |result|
        send_data result
        close_connection_after_writing
      })
    rescue
      puts "Failed to serve #{name}"
    end
  else
    close_connection
  end
end