From d6dee6b89004077e91d4b1efc686c181e9186096 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 9 Dec 2023 20:17:16 +1300 Subject: [PATCH] Expose HTTP_UPGRADE header when translating request to Rack environment. Potential fix for . --- lib/protocol/rack/adapter/generic.rb | 5 +++++ lib/protocol/rack/adapter/rack2.rb | 1 - lib/protocol/rack/constants.rb | 1 + lib/protocol/rack/request.rb | 5 ++--- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/protocol/rack/adapter/generic.rb b/lib/protocol/rack/adapter/generic.rb index 61392f4..0bdb5ef 100644 --- a/lib/protocol/rack/adapter/generic.rb +++ b/lib/protocol/rack/adapter/generic.rb @@ -68,6 +68,11 @@ def unwrap_request(request, env) self.unwrap_headers(request.headers, env) + # For the sake of compatibility, we set the `HTTP_UPGRADE` header to the requested protocol. + if protocol = request.protocol and request.version.start_with?('http/1') + env[CGI::HTTP_UPGRADE] = Array(protocol).join(",") + end + # HTTP/2 prefers `:authority` over `host`, so we do this for backwards compatibility. env[CGI::HTTP_HOST] ||= request.authority diff --git a/lib/protocol/rack/adapter/rack2.rb b/lib/protocol/rack/adapter/rack2.rb index cf5b16d..de95506 100644 --- a/lib/protocol/rack/adapter/rack2.rb +++ b/lib/protocol/rack/adapter/rack2.rb @@ -124,7 +124,6 @@ def self.make_response(env, response) if protocol = response.protocol headers['rack.protocol'] = protocol - # headers['upgrade'] = protocol end if body = response.body and body.stream? diff --git a/lib/protocol/rack/constants.rb b/lib/protocol/rack/constants.rb index c8a2a7c..b55c075 100644 --- a/lib/protocol/rack/constants.rb +++ b/lib/protocol/rack/constants.rb @@ -11,6 +11,7 @@ module Rack # CGI keys : module CGI HTTP_HOST = 'HTTP_HOST' + HTTP_UPGRADE = "HTTP_UPGRADE" PATH_INFO = 'PATH_INFO' REQUEST_METHOD = 'REQUEST_METHOD' REQUEST_PATH = 'REQUEST_PATH' diff --git a/lib/protocol/rack/request.rb b/lib/protocol/rack/request.rb index cd4ab72..f92ed28 100644 --- a/lib/protocol/rack/request.rb +++ b/lib/protocol/rack/request.rb @@ -6,6 +6,7 @@ require 'protocol/http/request' require 'protocol/http/headers' +require_relative 'constants' require_relative 'body/input_wrapper' module Protocol @@ -30,12 +31,10 @@ def initialize(env) ) end - HTTP_UPGRADE = 'HTTP_UPGRADE' - def self.protocol(env) if protocols = env['rack.protocol'] return Array(protocols) - elsif protocols = env[HTTP_UPGRADE] + elsif protocols = env[CGI::HTTP_UPGRADE] return protocols.split(/\s*,\s*/) end end