Class: Hoof::HttpServer

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

Direct Known Subclasses

HttpsServer

Instance Method Summary collapse

Instance Method Details

#post_initObject



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

def post_init
  super
  no_environment_strings
  @buffer = ''
end

#process_http_requestObject



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
41
42
43
44
45
46
47
48
49
# File 'lib/hoof/http_server.rb', line 16

def process_http_request
  p 'DATA'
  p @buffer
  begin
    host = @http_headers.scan(/Host:\s*([-a-zA-z.]*)\000/)[0][0].gsub(/:\d+$/, '')
    close_connection and return unless host =~ /.dev$/

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

    if application
      if application.static_file? @http_path_info
        puts "Serve static #{host}#{@http_request_uri}"
        send_data application.serve_static(@http_path_info)
        close_connection_after_writing
      else
        application.start
        puts "Serve #{host}#{@http_request_uri}"
        EventMachine.defer(proc {
          application.serve @buffer
        }, 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

#receive_data(data) ⇒ Object



11
12
13
14
# File 'lib/hoof/http_server.rb', line 11

def receive_data data
  @buffer << data
  super
end