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
#as_json, #concurrency, #http1?, #http2?, #initialize, #peer, #read_line, #read_line?, #reusable?, #to_json, #to_s, #viable?
Constructor Details
This class inherits a constructor from Async::HTTP::Protocol::HTTP1::Connection
Instance Method Details
#each(task: Task.current) ⇒ Object
Server loop.
45 46 47 48 49 50 51 52 53 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/async/http/protocol/http1/server.rb', line 45 def each(task: Task.current) task.annotate("Reading #{self.version} requests for #{self.class}.") while request = next_request response = yield(request, self) body = response&.body if hijacked? body&.close return end task.defer_stop do # If a response was generated, send it: if response trailer = response.headers.trailer! # Some operations in this method are long running, that is, it's expected that `body.call(stream)` could literally run indefinitely. In order to facilitate garbage collection, we want to nullify as many local variables before calling the streaming body. This ensures that the garbage collection can clean up as much state as possible during the long running operation, so we don't retain objects that are no longer needed. if body and protocol = response.protocol # We force a 101 response if the protocol is upgraded - HTTP/2 CONNECT will return 200 for success, but this won't be understood by HTTP/1 clients: write_response(@version, 101, response.headers) stream = write_upgrade_body(protocol) # At this point, the request body is hijacked, so we don't want to call #finish below. request = nil unless request.body response = nil # We must return here as no further request processing can be done: return body.call(stream) else write_response(@version, response.status, response.headers) if request.connect? and response.success? stream = write_tunnel_body(request.version) # Same as above: request = nil unless request.body response = nil # We must return here as no further request processing can be done: return body.call(stream) else head = request.head? version = request.version # Same as above: request = nil unless request.body response = nil write_body(version, body, head, trailer) end end # We are done with the body, you shouldn't need to call close on it: body = nil else # If the request failed to generate a response, it was an internal server error: write_response(@version, 500, {}) write_body(request.version, nil) end # Gracefully finish reading the request body if it was not already done so. request&.each{} # This ensures we yield at least once every iteration of the loop and allow other fibers to execute. task.yield rescue => error raise ensure body&.close(error) end end end |
#fail_request(status) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/async/http/protocol/http1/server.rb', line 17 def fail_request(status) @persistent = false write_response(@version, status, {}) write_body(@version, nil) rescue => error # At this point, there is very little we can do to recover: Console::Event::Failure.for(error).emit(self, "Failed to write failure response.", severity: :debug) end |
#next_request ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/async/http/protocol/http1/server.rb', line 26 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.method, request.headers) @persistent = false end return request rescue ::Protocol::HTTP1::BadRequest fail_request(400) # Conceivably we could retry here, but we don't really know how bad the error is, so it's better to just fail: raise end |