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
31
32
33
# File 'lib/hoof/http_server.rb', line 4

def receive_data data
  begin
    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
      application.start
      puts "Serve #{host}#{parser.path}"
      EventMachine.defer(proc {
        application.serve data
      }, proc { |result|
        send_data result
        close_connection_after_writing
      })
    else
      close_connection
    end
  rescue => e
    puts e.message
    puts e.backtrace.join("\n")
    close_connection
  end
end