Class: Async::HTTP::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, protocol_class = Protocol::HTTP1, &block) ⇒ Server

Returns a new instance of Server.



30
31
32
33
34
35
36
37
# File 'lib/async/http/server.rb', line 30

def initialize(endpoint, protocol_class = Protocol::HTTP1, &block)
	@endpoint = endpoint
	@protocol_class = protocol_class || endpoint.protocol
	
	if block_given?
		define_singleton_method(:handle_request, block)
	end
end

Instance Method Details

#accept(peer, address, task: Task.current) ⇒ Object



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

def accept(peer, address, task: Task.current)
	peer.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
	
	stream = Async::IO::Stream.new(peer)
	protocol = @protocol_class.server(stream)
	
	Async.logger.debug(self) {"Incoming connnection from #{address.inspect} to #{protocol}"}
	
	protocol.receive_requests do |request|
		# Async.logger.debug(self) {"Incoming request from #{address.inspect}: #{request.method} #{request.path}"}
		
		# If this returns nil, we assume that the connection has been hijacked.
		handle_request(request, peer, address)
	end
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE
	# Sometimes client will disconnect without completing a result or reading the entire buffer. That means we are done.
end

#handle_request(request, peer, address) ⇒ Object



39
40
41
# File 'lib/async/http/server.rb', line 39

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

#runObject



61
62
63
# File 'lib/async/http/server.rb', line 61

def run
	@endpoint.accept(&self.method(:accept))
end