Skip to content

Commit 805a957

Browse files
committed
Add support for WebSockets
1 parent b309c53 commit 805a957

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

lib/uri/ws.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# frozen_string_literal: false
2+
# = uri/ws.rb
3+
#
4+
# Author:: Matt Muller <mamuller@amazon.com>
5+
# License:: You can redistribute it and/or modify it under the same term as Ruby.
6+
# Revision:: $Id$
7+
#
8+
# See URI for general documentation
9+
#
10+
11+
require_relative 'generic'
12+
13+
module URI
14+
15+
#
16+
# The syntax of WS URIs is defined in RFC6455 section 3.
17+
#
18+
# Note that the Ruby URI library allows WS URLs containing usernames and
19+
# passwords. This is not legal as per the RFC, but used to be
20+
# supported in Internet Explorer 5 and 6, before the MS04-004 security
21+
# update. See <URL:http://support.microsoft.com/kb/834489>.
22+
#
23+
class WS < Generic
24+
# A Default port of 80 for URI::WS.
25+
DEFAULT_PORT = 80
26+
27+
# An Array of the available components for URI::WS.
28+
COMPONENT = %i[
29+
scheme
30+
userinfo host port
31+
path
32+
query
33+
].freeze
34+
35+
#
36+
# == Description
37+
#
38+
# Creates a new URI::WS object from components, with syntax checking.
39+
#
40+
# The components accepted are userinfo, host, port, path, and query.
41+
#
42+
# The components should be provided either as an Array, or as a Hash
43+
# with keys formed by preceding the component names with a colon.
44+
#
45+
# If an Array is used, the components must be passed in the
46+
# order <code>[userinfo, host, port, path, query]</code>.
47+
#
48+
# Example:
49+
#
50+
# uri = URI::WS.build(host: 'www.example.com', path: '/foo/bar')
51+
#
52+
# uri = URI::WS.build([nil, "www.example.com", nil, "/path", "query"])
53+
#
54+
# Currently, if passed userinfo components this method generates
55+
# invalid WS URIs as per RFC 1738.
56+
#
57+
def self.build(args)
58+
tmp = Util.make_components_hash(self, args)
59+
super(tmp)
60+
end
61+
62+
#
63+
# == Description
64+
#
65+
# Returns the full path for a WS URI, as required by Net::HTTP::Get.
66+
#
67+
# If the URI contains a query, the full path is URI#path + '?' + URI#query.
68+
# Otherwise, the path is simply URI#path.
69+
#
70+
# Example:
71+
#
72+
# uri = URI::WS.build(path: '/foo/bar', query: 'test=true')
73+
# uri.request_uri # => "/foo/bar?test=true"
74+
#
75+
def request_uri
76+
return unless @path
77+
78+
url = @query ? "#@path?#@query" : @path.dup
79+
url.start_with?(?/.freeze) ? url : ?/ + url
80+
end
81+
end
82+
83+
@@schemes['WS'] = WS
84+
85+
end

lib/uri/wss.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: false
2+
# = uri/wss.rb
3+
#
4+
# Author:: Matt Muller <mamuller@amazon.com>
5+
# License:: You can redistribute it and/or modify it under the same term as Ruby.
6+
# Revision:: $Id$
7+
#
8+
# See URI for general documentation
9+
#
10+
11+
require_relative 'ws'
12+
13+
module URI
14+
15+
# The default port for WSS URIs is 443, and the scheme is 'wss:' rather
16+
# than 'ws:'. Other than that, WSS URIs are identical to WS URIs;
17+
# see URI::WS.
18+
class WSS < WS
19+
# A Default port of 443 for URI::WSS
20+
DEFAULT_PORT = 443
21+
end
22+
@@schemes['WSS'] = WSS
23+
end

test/uri/test_ws.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: false
2+
require 'test/unit'
3+
require 'uri/ws'
4+
5+
module URI
6+
7+
8+
class TestWS < Test::Unit::TestCase
9+
def setup
10+
end
11+
12+
def teardown
13+
end
14+
15+
def uri_to_ary(uri)
16+
uri.class.component.collect {|c| uri.send(c)}
17+
end
18+
19+
def test_build
20+
u = URI::WS.build(host: 'www.example.com', path: '/foo/bar')
21+
assert_kind_of(URI::WS, u)
22+
end
23+
24+
def test_parse
25+
u = URI.parse('ws://a')
26+
assert_kind_of(URI::WS, u)
27+
assert_equal(['ws',
28+
nil, 'a', URI::HTTP.default_port,
29+
'', nil], uri_to_ary(u))
30+
end
31+
32+
def test_normalize
33+
host = 'aBcD'
34+
u1 = URI.parse('ws://' + host + '/eFg?HiJ')
35+
u2 = URI.parse('ws://' + host.downcase + '/eFg?HiJ')
36+
assert(u1.normalize.host == 'abcd')
37+
assert(u1.normalize.path == u1.path)
38+
assert(u1.normalize == u2.normalize)
39+
assert(!u1.normalize.host.equal?(u1.host))
40+
assert( u2.normalize.host.equal?(u2.host))
41+
42+
assert_equal('ws://abc/', URI.parse('ws://abc').normalize.to_s)
43+
end
44+
45+
def test_equal
46+
assert(URI.parse('ws://abc') == URI.parse('ws://ABC'))
47+
assert(URI.parse('ws://abc/def') == URI.parse('ws://ABC/def'))
48+
assert(URI.parse('ws://abc/def') != URI.parse('ws://ABC/DEF'))
49+
end
50+
51+
def test_request_uri
52+
assert_equal('/', URI.parse('ws://a.b.c/').request_uri)
53+
assert_equal('/?abc=def', URI.parse('ws://a.b.c/?abc=def').request_uri)
54+
assert_equal('/', URI.parse('ws://a.b.c').request_uri)
55+
assert_equal('/?abc=def', URI.parse('ws://a.b.c?abc=def').request_uri)
56+
assert_equal(nil, URI.parse('ws:foo').request_uri)
57+
end
58+
59+
def test_select
60+
assert_equal(['ws', 'a.b.c', 80], URI.parse('ws://a.b.c/').select(:scheme, :host, :port))
61+
u = URI.parse('ws://a.b.c/')
62+
assert_equal(uri_to_ary(u), u.select(*u.component))
63+
assert_raise(ArgumentError) do
64+
u.select(:scheme, :host, :not_exist, :port)
65+
end
66+
end
67+
end
68+
69+
70+
end

0 commit comments

Comments
 (0)