File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -93,9 +93,34 @@ def read(socket)
9393 e [ 'pitchfork.socket' ] = socket
9494 e [ 'rack.hijack' ] = self
9595
96+ # We don't support connection upgrade:
97+ remove_connection_upgrade ( e )
98+
9699 e . merge! ( DEFAULTS )
97100 end
98101
102+ def remove_connection_upgrade ( env )
103+ connection_header = env [ 'HTTP_CONNECTION' ]
104+ return unless connection_header
105+
106+ # Split the header value by comma to handle cases where there are multiple connection options:
107+ connection_values = connection_header . split ( ',' ) . map ( &:strip )
108+
109+ # Remove the "upgrade" value
110+ connection_values . reject! { |value | value . downcase == 'upgrade' }
111+
112+ if connection_values . empty?
113+ # If no other values are left, delete the header
114+ env . delete ( 'HTTP_CONNECTION' )
115+ else
116+ # Otherwise, update the header with the remaining values
117+ env [ 'HTTP_CONNECTION' ] = connection_values . join ( ', ' )
118+ end
119+
120+ # Delete the HTTP_UPGRADE header if it exists
121+ env . delete ( 'HTTP_UPGRADE' )
122+ end
123+
99124 # for rack.hijack, we respond to this method so no extra allocation
100125 # of a proc object
101126 def call
Original file line number Diff line number Diff line change @@ -136,4 +136,28 @@ def test_write_on_close
136136
137137 assert_clean_shutdown ( pid )
138138 end
139+
140+ def test_http_upgrade
141+ addr , port = unused_port
142+
143+ pid = spawn_server ( app : File . join ( ROOT , "test/integration/upgrade.ru" ) , config : <<~CONFIG )
144+ listen "#{ addr } :#{ port } "
145+ worker_processes 1
146+ CONFIG
147+
148+ assert_healthy ( "http://#{ addr } :#{ port } " )
149+
150+ Net ::HTTP . start ( addr , port ) do |http |
151+ request = Net ::HTTP ::Get . new ( "/" )
152+ request [ "Connection" ] = "Upgrade"
153+ request [ "Upgrade" ] = "websocket"
154+
155+ # It should not be connection upgrade:
156+ response = http . request ( request )
157+ assert_equal "200" , response . code
158+ assert_equal "Normal response" , response . body
159+ end
160+
161+ assert_clean_shutdown ( pid )
162+ end
139163end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+ run lambda { |env |
3+ if env [ 'HTTP_UPGRADE' ]
4+ [ 404 , { } , [ "Upgrade not supported" ] ]
5+ else
6+ [ 200 , { } , [ "Normal response" ] ]
7+ end
8+ }
You can’t perform that action at this time.
0 commit comments