|
| 1 | +https://github.com/imanel/websocket-ruby/commit/736a7515aff808a5c268b87066e928b59aed769e |
| 2 | + |
| 3 | +From 736a7515aff808a5c268b87066e928b59aed769e Mon Sep 17 00:00:00 2001 |
| 4 | +From: Bernard Potocki < [email protected]> |
| 5 | +Date: Thu, 17 Feb 2022 20:02:21 +0100 |
| 6 | +Subject: [PATCH] Ensure correct port is always specified (#48) |
| 7 | + |
| 8 | +--- a/lib/websocket/handshake/base.rb |
| 9 | ++++ b/lib/websocket/handshake/base.rb |
| 10 | +@@ -7,7 +7,7 @@ class Base |
| 11 | + include ExceptionHandler |
| 12 | + include NiceInspect |
| 13 | + |
| 14 | +- attr_reader :host, :port, :path, :query, |
| 15 | ++ attr_reader :host, :path, :query, |
| 16 | + :state, :version, :secure, |
| 17 | + :headers, :protocols |
| 18 | + |
| 19 | +@@ -66,6 +66,20 @@ def leftovers |
| 20 | + (@leftovers.to_s.split("\n", reserved_leftover_lines + 1)[reserved_leftover_lines] || '').strip |
| 21 | + end |
| 22 | + |
| 23 | ++ # Return default port for protocol (80 for ws, 443 for wss) |
| 24 | ++ def default_port |
| 25 | ++ secure ? 443 : 80 |
| 26 | ++ end |
| 27 | ++ |
| 28 | ++ # Check if provided port is a default one |
| 29 | ++ def default_port? |
| 30 | ++ port == default_port |
| 31 | ++ end |
| 32 | ++ |
| 33 | ++ def port |
| 34 | ++ @port || default_port |
| 35 | ++ end |
| 36 | ++ |
| 37 | + # URI of request. |
| 38 | + # @return [String] Full URI with protocol |
| 39 | + # @example |
| 40 | +@@ -73,7 +87,7 @@ def leftovers |
| 41 | + def uri |
| 42 | + uri = String.new(secure ? 'wss://' : 'ws://') |
| 43 | + uri << host |
| 44 | +- uri << ":#{port}" if port |
| 45 | ++ uri << ":#{port}" unless default_port? |
| 46 | + uri << path |
| 47 | + uri << "?#{query}" if query |
| 48 | + uri |
| 49 | +--- a/lib/websocket/handshake/client.rb |
| 50 | ++++ b/lib/websocket/handshake/client.rb |
| 51 | +@@ -61,7 +61,7 @@ def initialize(args = {}) |
| 52 | + uri = URI.parse(@url || @uri) |
| 53 | + @secure ||= (uri.scheme == 'wss') |
| 54 | + @host ||= uri.host |
| 55 | +- @port ||= uri.port |
| 56 | ++ @port ||= uri.port || default_port |
| 57 | + @path ||= uri.path |
| 58 | + @query ||= uri.query |
| 59 | + end |
| 60 | +--- a/lib/websocket/handshake/handler/client04.rb |
| 61 | ++++ b/lib/websocket/handshake/handler/client04.rb |
| 62 | +@@ -21,7 +21,7 @@ def handshake_keys |
| 63 | + %w[Connection Upgrade] |
| 64 | + ] |
| 65 | + host = @handshake.host |
| 66 | +- host += ":#{@handshake.port}" if @handshake.port |
| 67 | ++ host += ":#{@handshake.port}" unless @handshake.default_port? |
| 68 | + keys << ['Host', host] |
| 69 | + keys += super |
| 70 | + keys << ['Sec-WebSocket-Origin', @handshake.origin] if @handshake.origin |
| 71 | +--- a/lib/websocket/handshake/handler/client75.rb |
| 72 | ++++ b/lib/websocket/handshake/handler/client75.rb |
| 73 | +@@ -18,7 +18,7 @@ def handshake_keys |
| 74 | + %w[Connection Upgrade] |
| 75 | + ] |
| 76 | + host = @handshake.host |
| 77 | +- host += ":#{@handshake.port}" if @handshake.port |
| 78 | ++ host += ":#{@handshake.port}" unless @handshake.default_port? |
| 79 | + keys << ['Host', host] |
| 80 | + keys << ['Origin', @handshake.origin] if @handshake.origin |
| 81 | + keys << ['WebSocket-Protocol', @handshake.protocols.first] if @handshake.protocols.any? |
| 82 | +--- a/lib/websocket/handshake/server.rb |
| 83 | ++++ b/lib/websocket/handshake/server.rb |
| 84 | +@@ -129,13 +129,13 @@ def should_respond? |
| 85 | + # Host of server according to client header |
| 86 | + # @return [String] host |
| 87 | + def host |
| 88 | +- @headers['host'].to_s.split(':')[0].to_s |
| 89 | ++ @host || @headers['host'].to_s.split(':')[0].to_s |
| 90 | + end |
| 91 | + |
| 92 | + # Port of server according to client header |
| 93 | +- # @return [String] port |
| 94 | ++ # @return [Integer] port |
| 95 | + def port |
| 96 | +- @headers['host'].to_s.split(':')[1] |
| 97 | ++ (@port || @headers['host'].to_s.split(':')[1] || default_port).to_i |
| 98 | + end |
| 99 | + |
| 100 | + private |
| 101 | +--- a/spec/support/all_client_drafts.rb |
| 102 | ++++ b/spec/support/all_client_drafts.rb |
| 103 | +@@ -38,6 +38,10 @@ def validate_request |
| 104 | + expect(handshake.query).to eql('aaa=bbb') |
| 105 | + end |
| 106 | + |
| 107 | ++ it 'returns default port' do |
| 108 | ++ expect(handshake.port).to be(80) |
| 109 | ++ end |
| 110 | ++ |
| 111 | + it 'returns valid port' do |
| 112 | + @request_params = { port: 123 } |
| 113 | + expect(handshake.port).to be(123) |
| 114 | +--- a/spec/support/all_server_drafts.rb |
| 115 | ++++ b/spec/support/all_server_drafts.rb |
| 116 | +@@ -47,11 +47,17 @@ def validate_request |
| 117 | + expect(handshake.query).to eql('aaa=bbb') |
| 118 | + end |
| 119 | + |
| 120 | ++ it 'returns default port' do |
| 121 | ++ handshake << client_request |
| 122 | ++ |
| 123 | ++ expect(handshake.port).to be(80) |
| 124 | ++ end |
| 125 | ++ |
| 126 | + it 'returns valid port' do |
| 127 | + @request_params = { port: 123 } |
| 128 | + handshake << client_request |
| 129 | + |
| 130 | +- expect(handshake.port).to eql('123') |
| 131 | ++ expect(handshake.port).to be(123) |
| 132 | + end |
| 133 | + |
| 134 | + it 'returns valid response' do |
0 commit comments