Class: Async::HTTP::Server

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Server.



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

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



44
45
46
# File 'lib/async/http/server.rb', line 44

def endpoint
  @endpoint
end

#protocolObject (readonly)

Returns the value of attribute protocol.



45
46
47
# File 'lib/async/http/server.rb', line 45

def protocol
  @protocol
end

#schemeObject (readonly)

Returns the value of attribute scheme.



46
47
48
# File 'lib/async/http/server.rb', line 46

def scheme
  @scheme
end

Class Method Details

.for(*arguments, &block) ⇒ Object



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

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

Instance Method Details

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



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

def accept(peer, address, task: Task.current)
	connection = @protocol.server(peer)
	
	Async.logger.debug(self) {"Incoming connnection from #{address.inspect} to #{@protocol}"}
	
	connection.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
ensure
	connection&.close
end

#runObject



70
71
72
# File 'lib/async/http/server.rb', line 70

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