Class: Async::HTTP::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

build, #call, #close

Constructor Details

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

Returns a new instance of Server.



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

def initialize(app, endpoint, protocol_class = Protocol::HTTP1)
  super(app)
  
  @endpoint = endpoint
  @protocol_class = protocol_class || endpoint.protocol
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



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

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|
    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
  # Sometimes client will disconnect without completing a result or reading the entire buffer. That means we are done.
end

#runObject



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

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