Class: Async::HTTP::Protocol::HTTP1::Server

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

Constant Summary

Constants inherited from Connection

Connection::CRLF

Instance Attribute Summary

Attributes inherited from Connection

#count, #stream, #version

Instance Method Summary collapse

Methods inherited from Connection

#close, #connected?, #hijack, #initialize, #multiplex, #peer, #read_chunked_body, #read_fixed_body, #read_line, #read_remainder_body, #read_tunnel_body, #reusable?

Constructor Details

This class inherits a constructor from Async::HTTP::Protocol::HTTP1::Connection

Instance Method Details

#each(task: Task.current) ⇒ Object

Server loop.



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

def each(task: Task.current)
  while request = next_request
    response = yield(request, self)
    
    return if @stream.closed?
    
    if response
      write_response(@version, response.status, response.headers, response.body, request.head?)
    else
      # If the request failed to generate a response, it was an internal server error:
      write_response(@version, 500, {}, nil)
    end
    
    # Gracefully finish reading the request body if it was not already done so.
    request.finish
    
    # This ensures we yield at least once every iteration of the loop and allow other fibers to execute.
    task.yield
  end
end

#next_requestObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/http/protocol/http1/server.rb', line 28

def next_request
  # The default is true.
  return nil unless @persistent
  
  request = Request.new(self)
  
  unless persistent?(request.version, request.headers)
    @persistent = false
  end
  
  return request
rescue
  # Bad Request
  write_response(@version, 400, {}, nil)
  
  raise
end