Class: Async::HTTP::Server

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

Instance Attribute Summary collapse

Attributes inherited from Middleware

#delegate

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

build, #call, #close

Constructor Details

#initialize(app, endpoint, protocol = endpoint.protocol, scheme = endpoint.scheme) ⇒ Server

Returns a new instance of Server.



34
35
36
37
38
39
40
# File 'lib/async/http/server.rb', line 34

def initialize(app, endpoint, protocol = endpoint.protocol, scheme = endpoint.scheme)
	super(app)
	
	@endpoint = endpoint
	@protocol = protocol
	@scheme = scheme
end

Instance Attribute Details

#schemeObject (readonly)

Returns the value of attribute scheme.



42
43
44
# File 'lib/async/http/server.rb', line 42

def scheme
  @scheme
end

Class Method Details

.for(*args, &block) ⇒ Object



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

def self.for(*args, &block)
	self.new(block, *args)
end

Instance Method Details

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/async/http/server.rb', line 44

def accept(peer, address, task: Task.current)
	stream = Async::IO::Stream.new(peer)
	protocol = @protocol.server(stream)
	
	Async.logger.debug(self) {"Incoming connnection from #{address.inspect} to #{protocol}"}
	
	protocol.each do |request|
		# We set the default scheme unless it was otherwise specified.
		# https://tools.ietf.org/html/rfc7230#section-5.5
		request.scheme ||= self.scheme
		
		# This is a slight optimization to avoid having to get the address from the socket.
		request.remote_address = address
		
		# 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.
		self.call(request)
	end
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EPROTOTYPE
	# Sometimes client will disconnect without completing a result or reading the entire buffer. That means we are done.
	# Errno::EPROTOTYPE is a bug with Darwin. It happens because the socket is lazily created (in Darwin).
end

#runObject



68
69
70
# File 'lib/async/http/server.rb', line 68

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