summaryrefslogtreecommitdiff
path: root/dev-ruby/websocket-driver/files/websocket-driver-0.7.5-ruby32.patch
blob: e4f58e495cfdda38e2f8f5564822f11866c07272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
https://github.com/faye/websocket-driver-ruby/commit/3a2931751c6893e312ee24d9c6392bd096a798fd

From 3a2931751c6893e312ee24d9c6392bd096a798fd Mon Sep 17 00:00:00 2001
From: James Coglan <jcoglan@gmail.com>
Date: Sat, 10 Sep 2022 15:37:55 +0100
Subject: [PATCH] Fix handling of default ports on Ruby 3.1

--- a/lib/websocket/driver.rb
+++ b/lib/websocket/driver.rb
@@ -42,6 +42,7 @@ def Mask.mask(payload, mask)
     end
 
     MAX_LENGTH = 0x3ffffff
+    PORTS      = { 'ws' => 80, 'wss' => 443 }
     STATES     = [:connecting, :open, :closing, :closed]
 
     ConnectEvent = Struct.new(nil)
@@ -209,6 +210,14 @@ def self.encode(data, encoding = nil)
       data.force_encoding(encoding)
     end
 
+    def self.host_header(uri)
+      host = uri.host
+      if uri.port and uri.port != PORTS[uri.scheme]
+        host += ":#{uri.port}"
+      end
+      host
+    end
+
     def self.validate_options(options, valid_keys)
       options.keys.each do |key|
         unless valid_keys.include?(key)
--- a/lib/websocket/driver/client.rb
+++ b/lib/websocket/driver/client.rb
@@ -23,11 +23,10 @@ def initialize(socket, options = {})
           raise URIError, "#{ socket.url } is not a valid WebSocket URL"
         end
 
-        host      = uri.host + (uri.port ? ":#{ uri.port }" : '')
         path      = (uri.path == '') ? '/' : uri.path
         @pathname = path + (uri.query ? '?' + uri.query : '')
 
-        @headers['Host']                  = host
+        @headers['Host']                  = Driver.host_header(uri)
         @headers['Upgrade']               = 'websocket'
         @headers['Connection']            = 'Upgrade'
         @headers['Sec-WebSocket-Key']     = @key
--- a/lib/websocket/driver/proxy.rb
+++ b/lib/websocket/driver/proxy.rb
@@ -4,8 +4,6 @@ class Driver
     class Proxy
       include EventEmitter
 
-      PORTS = { 'ws' => 80, 'wss' => 443 }
-
       attr_reader :status, :headers
 
       def initialize(client, origin, options)
@@ -20,7 +18,7 @@ def initialize(client, origin, options)
         @state   = 0
 
         @headers = Headers.new
-        @headers['Host'] = @origin.host + (@origin.port ? ":#{ @origin.port }" : '')
+        @headers['Host'] = Driver.host_header(@origin)
         @headers['Connection'] = 'keep-alive'
         @headers['Proxy-Connection'] = 'keep-alive'
 
--- a/spec/websocket/driver/client_spec.rb
+++ b/spec/websocket/driver/client_spec.rb
@@ -121,6 +121,54 @@
         end
       end
 
+      describe "with an explicit port" do
+        let(:url) { "ws://www.example.com:3000/socket" }
+
+        it "includes the port in the Host header" do
+          expect(socket).to receive(:write).with(
+              "GET /socket HTTP/1.1\r\n" +
+              "Host: www.example.com:3000\r\n" +
+              "Upgrade: websocket\r\n" +
+              "Connection: Upgrade\r\n" +
+              "Sec-WebSocket-Key: 2vBVWg4Qyk3ZoM/5d3QD9Q==\r\n" +
+              "Sec-WebSocket-Version: 13\r\n" +
+              "\r\n")
+          driver.start
+        end
+      end
+
+      describe "with a wss: URL" do
+        let(:url) { "wss://www.example.com/socket" }
+
+        it "does not include the port in the Host header" do
+          expect(socket).to receive(:write).with(
+              "GET /socket HTTP/1.1\r\n" +
+              "Host: www.example.com\r\n" +
+              "Upgrade: websocket\r\n" +
+              "Connection: Upgrade\r\n" +
+              "Sec-WebSocket-Key: 2vBVWg4Qyk3ZoM/5d3QD9Q==\r\n" +
+              "Sec-WebSocket-Version: 13\r\n" +
+              "\r\n")
+          driver.start
+        end
+      end
+
+      describe "with a wss: URL and explicit port" do
+        let(:url) { "wss://www.example.com:3000/socket" }
+
+        it "includes the port in the Host header" do
+          expect(socket).to receive(:write).with(
+              "GET /socket HTTP/1.1\r\n" +
+              "Host: www.example.com:3000\r\n" +
+              "Upgrade: websocket\r\n" +
+              "Connection: Upgrade\r\n" +
+              "Sec-WebSocket-Key: 2vBVWg4Qyk3ZoM/5d3QD9Q==\r\n" +
+              "Sec-WebSocket-Version: 13\r\n" +
+              "\r\n")
+          driver.start
+        end
+      end
+
       describe "with custom headers" do
         before do
           driver.set_header "User-Agent", "Chrome"