Class: Hoof::HttpServer

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

Direct Known Subclasses

HttpsServer

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
34
35
36
37
38
39
40
# 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$/, '')
    path = parser.path.split('?', 2)[0]
    application = Hoof.find name

    if application
      if application.static_file? path
        puts "Serve static #{host}#{parser.path}"
        send_data application.serve_static(path)
        close_connection_after_writing
      else
        application.start
        puts "Serve #{host}#{parser.path}"
        EventMachine.defer(proc {
          application.serve data
        }, proc { |result|
          send_data result
          close_connection_after_writing
        })
      end
    else
      close_connection
    end
  rescue => e
    puts e.message
    puts e.backtrace.join("\n")
    close_connection
  end
end