From e190ccb0a9afe537918e24941fffef5504bee2ac Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Mon, 20 Sep 2021 18:06:59 +0100 Subject: [PATCH 1/3] added URI#HTTP#origin and URI#HTTP#authority --- lib/uri/http.rb | 36 ++++++++++++++++++++++++++++++++++++ test/uri/test_http.rb | 14 ++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/uri/http.rb b/lib/uri/http.rb index 6e9c963..8b5c220 100644 --- a/lib/uri/http.rb +++ b/lib/uri/http.rb @@ -80,6 +80,42 @@ def request_uri url = @query ? "#@path?#@query" : @path.dup url.start_with?(?/.freeze) ? url : ?/ + url end + + # + # == Description + # + # Returns the authority for an HTTP uri, as defined in + # https://datatracker.ietf.org/doc/html/rfc3986/#section-3.2. + # + # + # Example: + # + # URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority #=> "www.example.com" + # URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority #=> "www.example.com:8000" + # URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority #=> "www.example.com" + # + def authority + port_string = port == default_port ? nil : ":#{port}" + "#{host}#{port_string}" + end + + # + # == Description + # + # Returns the origin for an HTTP uri, as defined in + # https://datatracker.ietf.org/doc/html/rfc6454. + # + # + # Example: + # + # URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').origin #=> "http://www.example.com" + # URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').origin #=> "http://www.example.com:8000" + # URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').origin #=> "http://www.example.com" + # URI::HTTPS.build(host: 'www.example.com', path: '/foo/bar').origin #=> "https://www.example.com" + # + def origin + "#{scheme}://#{authority}" + end end register_scheme 'HTTP', HTTP diff --git a/test/uri/test_http.rb b/test/uri/test_http.rb index cc19046..bbb2487 100644 --- a/test/uri/test_http.rb +++ b/test/uri/test_http.rb @@ -64,6 +64,20 @@ def test_select u.select(:scheme, :host, :not_exist, :port) end end + + def test_authority + assert_equal('a.b.c', URI.parse('http://a.b.c/').authority) + assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority) + assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority) + end + + + def test_origin + assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin) + assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin) + assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin) + assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin) + end end From b1c925c11f69985fa3bd6dd3be609758091e0570 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 21 Sep 2021 09:47:25 +1200 Subject: [PATCH 2/3] Avoid string allocations on fast path. --- lib/uri/http.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/uri/http.rb b/lib/uri/http.rb index 8b5c220..306daf1 100644 --- a/lib/uri/http.rb +++ b/lib/uri/http.rb @@ -95,8 +95,11 @@ def request_uri # URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority #=> "www.example.com" # def authority - port_string = port == default_port ? nil : ":#{port}" - "#{host}#{port_string}" + if port == default_port + host + else + "#{host}:#{port}" + end end # From c057c39bbf1b12054493b044c9fd28a99f0cc52a Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 21 Sep 2021 09:50:44 +1200 Subject: [PATCH 3/3] Tidy up indentation/alignment. --- test/uri/test_http.rb | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/test/uri/test_http.rb b/test/uri/test_http.rb index bbb2487..a52b4cf 100644 --- a/test/uri/test_http.rb +++ b/test/uri/test_http.rb @@ -24,14 +24,16 @@ def test_build def test_parse u = URI.parse('http://a') assert_kind_of(URI::HTTP, u) - assert_equal(['http', - nil, 'a', URI::HTTP.default_port, - '', nil, nil], uri_to_ary(u)) + assert_equal([ + 'http', + nil, 'a', URI::HTTP.default_port, + '', nil, nil + ], uri_to_ary(u)) end def test_normalize host = 'aBcD' - u1 = URI.parse('http://' + host + '/eFg?HiJ') + u1 = URI.parse('http://' + host + '/eFg?HiJ') u2 = URI.parse('http://' + host.downcase + '/eFg?HiJ') assert(u1.normalize.host == 'abcd') assert(u1.normalize.path == u1.path) @@ -49,11 +51,11 @@ def test_equal end def test_request_uri - assert_equal('/', URI.parse('http://a.b.c/').request_uri) + assert_equal('/', URI.parse('http://a.b.c/').request_uri) assert_equal('/?abc=def', URI.parse('http://a.b.c/?abc=def').request_uri) - assert_equal('/', URI.parse('http://a.b.c').request_uri) + assert_equal('/', URI.parse('http://a.b.c').request_uri) assert_equal('/?abc=def', URI.parse('http://a.b.c?abc=def').request_uri) - assert_equal(nil, URI.parse('http:foo').request_uri) + assert_equal(nil, URI.parse('http:foo').request_uri) end def test_select @@ -66,17 +68,17 @@ def test_select end def test_authority - assert_equal('a.b.c', URI.parse('http://a.b.c/').authority) - assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority) - assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority) + assert_equal('a.b.c', URI.parse('http://a.b.c/').authority) + assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority) + assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority) end def test_origin - assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin) - assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin) - assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin) - assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin) + assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin) + assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin) + assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin) + assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin) end end