Class: Async::HTTP::Protocol::HTTP1::Server
- Inherits:
-
Connection
- Object
- Protocol::HTTP1::Connection
- Connection
- Async::HTTP::Protocol::HTTP1::Server
- Defined in:
- lib/async/http/protocol/http1/server.rb
Instance Attribute Summary
Attributes inherited from Connection
Instance Method Summary collapse
-
#each(task: Task.current) ⇒ Object
Server loop.
- #fail_request(status) ⇒ Object
- #next_request ⇒ Object
Methods inherited from Connection
#close, #connected?, #http1?, #http2?, #initialize, #multiplex, #peer, #read_line, #read_line?, #reusable?
Constructor Details
This class inherits a constructor from Async::HTTP::Protocol::HTTP1::Connection
Instance Method Details
#each(task: Task.current) ⇒ Object
Server loop.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/async/http/protocol/http1/server.rb', line 54 def each(task: Task.current) while request = next_request Async.logger.debug(self) do |buffer| buffer.puts "Incoming request: #{request.} #{request.method} #{request.path} #{request.version}" buffer.puts "Incoming headers: #{request.headers}" end response = yield(request, self) return if @stream.nil? or @stream.closed? if response # Try to avoid holding on to request, to minimse GC overhead: head = request.head? unless request.body? # If there is no body, #finish is a no-op. request = nil end write_response(@version, response.status, response.headers) if body = response.body and protocol = response.protocol stream = write_upgrade_body(protocol) # At this point, the request body is hijacked, so we don't want to call #finish below. request = nil body.call(stream) else write_body(@version, body, head) end else # If the request failed to generate a response, it was an internal server error: write_response(@version, 500, {}) write_body(@version, 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 |
#fail_request(status) ⇒ Object
28 29 30 31 |
# File 'lib/async/http/protocol/http1/server.rb', line 28 def fail_request(status) @persistent = false write_response(@version, status, {}, nil) end |
#next_request ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/async/http/protocol/http1/server.rb', line 33 def next_request # The default is true. return unless @persistent # Read an incoming request: return unless request = Request.read(self) unless persistent?(request.version, request.headers) @persistent = false end return request rescue Async::TimeoutError fail_request(408) raise rescue fail_request(400) raise end |