# frozen_string_literal: true# Released under the MIT License.# Copyright, 2018-2024, by Samuel Williams.# Copyright, 2020, by Igor Sidorov.# Copyright, 2023, by Thomas Morgan.# Copyright, 2024, by Anton Zhuravsky.require_relative'connection'require'console/event/failure'moduleAsyncmoduleHTTPmoduleProtocolmoduleHTTP1classServer<Connectiondeffail_request(status)@persistent=falsewrite_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)enddefnext_request# The default is true.returnunless@persistent# Read an incoming request:returnunlessrequest=Request.read(self)unlesspersistent?(request.version,request.method,request.headers)@persistent=falseendreturnrequestrescue::Protocol::HTTP1::BadRequestfail_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:raiseend# Server loop.defeach(task: Task.current)task.annotate("Reading #{self.version} requests for #{self.class}.")whilerequest=next_requestresponse=yield(request,self)body=response&.bodyifhijacked?body&.closereturnendtask.defer_stopdo# If a response was generated, send it:ifresponsetrailer=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.ifbodyandprotocol=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=nilresponse=nil# We must return here as no further request processing can be done:returnbody.call(stream)elsifresponse.status==101# This code path is to support legacy behavior where the response status is set to 101, but the protocol is not upgraded. This may not be a valid use case, but it is supported for compatibility. We expect the response headers to contain the `upgrade` header.write_response(@version,response.status,response.headers)stream=write_tunnel_body(request.version)# Same as above:request=nilresponse=nil# We must return here as no further request processing can be done:returnbody&.call(stream)elsewrite_response(@version,response.status,response.headers)ifrequest.connect?andresponse.success?stream=write_tunnel_body(request.version)# Same as above:request=nilresponse=nil# We must return here as no further request processing can be done:returnbody.call(stream)elsehead=request.head?version=request.version# Same as above:request=nilunlessrequest.bodyresponse=nilwrite_body(version,body,head,trailer)endend# We are done with the body, you shouldn't need to call close on it:body=nilelse# 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.yieldrescue=>errorraiseensurebody&.close(error)endendendendendendendend