Class: Async::HTTP::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoints, protocol_class = Protocol::HTTP1x) ⇒ Server

Returns a new instance of Server.



28
29
30
31
# File 'lib/async/http/server.rb', line 28

def initialize(endpoints, protocol_class = Protocol::HTTP1x)
	@endpoints = endpoints
	@protocol_class = protocol_class
end

Instance Method Details

#accept(peer, address) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/async/http/server.rb', line 37

def accept(peer, address)
	stream = Async::IO::Stream.new(peer)
	
	protocol = @protocol_class.new(stream)
	
	# puts "Opening session on child pid #{Process.pid}"
	
	hijack = catch(:hijack) do
		protocol.receive_requests do |request|
			handle_request(request, peer, address)
		end
	end

	if hijack
		hijack.call
	end

	# puts "Closing session"
	
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE
	# Sometimes client will disconnect without completing a result or reading the entire buffer.
	return nil
end

#handle_request(request, peer, address) ⇒ Object



33
34
35
# File 'lib/async/http/server.rb', line 33

def handle_request(request, peer, address)
	[200, {}, []]
end

#runObject



61
62
63
64
65
66
67
# File 'lib/async/http/server.rb', line 61

def run
	Async::IO::Endpoint.each(@endpoints) do |endpoint|
		# puts "Binding to #{endpoint} on process #{Process.pid}"
		
		endpoint.accept(&self.method(:accept))
	end
end