lib/async/http/protocol/http11.rb in async-http-0.27.3 vs lib/async/http/protocol/http11.rb in async-http-0.27.4

- old
+ new

@@ -83,10 +83,11 @@ else return true end end + # @return [Async::Wrapper] the underlying non-blocking IO. def hijack @persistent = false @stream.flush @@ -128,18 +129,25 @@ end # Server loop. def receive_requests(task: Task.current) while request = next_request - if response = yield(request, self) + response = yield(request, self) + + return if @stream.closed? + + if response write_response(self.version, response.status, response.headers, response.body) - request.finish - - # This ensures we yield at least once every iteration of the loop and allow other fibers to execute. - task.yield else - break + # If the request failed to generate a response, it was an internal server error: + write_response(self.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 class Response < Protocol::Response def initialize(protocol, request)