# frozen_string_literal: true## Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com># # Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:# # The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.require_relative'connection'moduleAsyncmoduleHTTPmoduleProtocolmoduleHTTP1classServer<Connectiondeffail_request(status)@persistent=falsewrite_response(@version,status,{},nil)enddefnext_request# The default is true.returnunless@persistent# Read an incoming request:returnunlessrequest=Request.read(self)unlesspersistent?(request.version,request.method,request.headers)@persistent=falseendreturnrequestrescueAsync::TimeoutError# For an interesting discussion about this behaviour, see https://trac.nginx.org/nginx/ticket/1005# If you enable this, you will see some spec failures...# fail_request(408)raiserescuefail_request(400)raiseend# Server loop.defeach(task: Task.current)task.annotate("Reading #{self.version} requests for #{self.class}.")whilerequest=next_requestresponse=yield(request,self)body=response&.bodyif@stream.nil?andbody.nil?# Full hijack.returnendbegin# If a response was generated, send it:ifresponsetrailer=response.headers.trailer!write_response(@version,response.status,response.headers)# 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.protocolstream=write_upgrade_body(protocol)# At this point, the request body is hijacked, so we don't want to call #finish below.request=response=nilbody.call(stream)elsifrequest.connect?andresponse.success?stream=write_tunnel_body(request.version)# Same as above:request=response=nilbody.call(stream)elsehead=request.head?version=request.version# Same as above:request=nilunlessbodyresponse=nilwrite_body(version,body,head,trailer)end# 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&.finish# This ensures we yield at least once every iteration of the loop and allow other fibers to execute.task.yieldrescue=>errorraiseensurebody&.close(error)endendendendendendendend