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.



39
40
41
42
43
44
45
# File 'lib/async/http/server.rb', line 39

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.



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

def endpoint
  @endpoint
end

#protocolObject (readonly)

Returns the value of attribute protocol.



48
49
50
# File 'lib/async/http/server.rb', line 48

def protocol
  @protocol
end

#schemeObject (readonly)

Returns the value of attribute scheme.



49
50
51
# File 'lib/async/http/server.rb', line 49

def scheme
  @scheme
end

Class Method Details

.for(*arguments, **options, &block) ⇒ Object



35
36
37
# File 'lib/async/http/server.rb', line 35

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

Instance Method Details

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



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

def accept(peer, address, task: Task.current)
  connection = @protocol.server(peer)
  
  Console.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
    
    # Console.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



73
74
75
# File 'lib/async/http/server.rb', line 73

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