Skip to content

Commit

Permalink
Expose HTTP_UPGRADE header when translating request to Rack environment.
Browse files Browse the repository at this point in the history
Potential fix for <socketry/falcon#202>.
  • Loading branch information
ioquatix committed Dec 9, 2023
1 parent 708582d commit d6dee6b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/protocol/rack/adapter/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/protocol/rack/adapter/rack2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions lib/protocol/rack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Rack
# CGI keys <https://tools.ietf.org/html/rfc3875#section-4.1>:
module CGI
HTTP_HOST = 'HTTP_HOST'
HTTP_UPGRADE = "HTTP_UPGRADE"
PATH_INFO = 'PATH_INFO'
REQUEST_METHOD = 'REQUEST_METHOD'
REQUEST_PATH = 'REQUEST_PATH'
Expand Down
5 changes: 2 additions & 3 deletions lib/protocol/rack/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'protocol/http/request'
require 'protocol/http/headers'

require_relative 'constants'
require_relative 'body/input_wrapper'

module Protocol
Expand All @@ -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
Expand Down

0 comments on commit d6dee6b

Please sign in to comment.