Skip to content

Commit

Permalink
Ensure correct port is always specified (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
imanel committed Feb 17, 2022
1 parent df3e892 commit 736a751
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Edge

- ensure correct port is always specified for handshake

## 1.2.9

- avoid ruby -w warnings
Expand Down
18 changes: 16 additions & 2 deletions lib/websocket/handshake/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Base
include ExceptionHandler
include NiceInspect

attr_reader :host, :port, :path, :query,
attr_reader :host, :path, :query,
:state, :version, :secure,
:headers, :protocols

Expand Down Expand Up @@ -66,14 +66,28 @@ def leftovers
(@leftovers.to_s.split("\n", reserved_leftover_lines + 1)[reserved_leftover_lines] || '').strip
end

# Return default port for protocol (80 for ws, 443 for wss)
def default_port
secure ? 443 : 80
end

# Check if provided port is a default one
def default_port?
port == default_port
end

def port
@port || default_port
end

# URI of request.
# @return [String] Full URI with protocol
# @example
# @handshake.uri #=> "ws://example.com/path?query=true"
def uri
uri = String.new(secure ? 'wss://' : 'ws://')
uri << host
uri << ":#{port}" if port
uri << ":#{port}" unless default_port?
uri << path
uri << "?#{query}" if query
uri
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket/handshake/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def initialize(args = {})
uri = URI.parse(@url || @uri)
@secure ||= (uri.scheme == 'wss')
@host ||= uri.host
@port ||= uri.port
@port ||= uri.port || default_port
@path ||= uri.path
@query ||= uri.query
end
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket/handshake/handler/client04.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def handshake_keys
%w[Connection Upgrade]
]
host = @handshake.host
host += ":#{@handshake.port}" if @handshake.port
host += ":#{@handshake.port}" unless @handshake.default_port?
keys << ['Host', host]
keys += super
keys << ['Sec-WebSocket-Origin', @handshake.origin] if @handshake.origin
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket/handshake/handler/client75.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def handshake_keys
%w[Connection Upgrade]
]
host = @handshake.host
host += ":#{@handshake.port}" if @handshake.port
host += ":#{@handshake.port}" unless @handshake.default_port?
keys << ['Host', host]
keys << ['Origin', @handshake.origin] if @handshake.origin
keys << ['WebSocket-Protocol', @handshake.protocols.first] if @handshake.protocols.any?
Expand Down
6 changes: 3 additions & 3 deletions lib/websocket/handshake/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ def should_respond?
# Host of server according to client header
# @return [String] host
def host
@headers['host'].to_s.split(':')[0].to_s
@host || @headers['host'].to_s.split(':')[0].to_s
end

# Port of server according to client header
# @return [String] port
# @return [Integer] port
def port
@headers['host'].to_s.split(':')[1]
(@port || @headers['host'].to_s.split(':')[1] || default_port).to_i
end

private
Expand Down
4 changes: 4 additions & 0 deletions spec/support/all_client_drafts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def validate_request
expect(handshake.query).to eql('aaa=bbb')
end

it 'returns default port' do
expect(handshake.port).to be(80)
end

it 'returns valid port' do
@request_params = { port: 123 }
expect(handshake.port).to be(123)
Expand Down
8 changes: 7 additions & 1 deletion spec/support/all_server_drafts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ def validate_request
expect(handshake.query).to eql('aaa=bbb')
end

it 'returns default port' do
handshake << client_request

expect(handshake.port).to be(80)
end

it 'returns valid port' do
@request_params = { port: 123 }
handshake << client_request

expect(handshake.port).to eql('123')
expect(handshake.port).to be(123)
end

it 'returns valid response' do
Expand Down

0 comments on commit 736a751

Please sign in to comment.