Class: Hoof::HttpServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ HttpServer

Returns a new instance of HttpServer.



5
6
7
8
# File 'lib/hoof/http_server.rb', line 5

def initialize *args
  super
  @applications = {}
end

Instance Attribute Details

#applicationsObject

Returns the value of attribute applications.



3
4
5
# File 'lib/hoof/http_server.rb', line 3

def applications
  @applications
end

Instance Method Details

#receive_data(data) ⇒ Object



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 10

def receive_data data
  parser = Http::Parser.new
  parser.parse data

  host = parser.headers["HOST"].gsub(/:\d+$/, '')
  if host =~ /.dev$/
    name = host.gsub(/.dev$/, '')
    application = Hoof.application name

    if application.static? parser.path.split('?', 2)[0]
      p "serve static #{host}#{parser.path}"
      send_data application.serve_static parser.path.split('?', 2)[0]
      close_connection_after_writing
    else
      p "serve #{host}#{parser.path}"
      EventMachine.defer(proc {
        application.serve(data)
      }, proc { |result|
        send_data result
        close_connection_after_writing
      })
    end
  end
end