Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 59543bc

Browse files
committed
Fix bug in #normalize_uri causing duplicate slashes
1 parent 9361ef7 commit 59543bc

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/wpxf/net/http_client.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'uri'
2+
13
module Wpxf
24
module Net
35
# Provides HTTP client functionality.
@@ -62,13 +64,11 @@ def target_host
6264
# @param parts the URI parts to join and normalize.
6365
# @return [String] a normalized URI.
6466
def normalize_uri(*parts)
65-
uri = parts * '/'
66-
uri = uri.gsub(%r{(?<!:)//}, '/')
67-
unless uri.start_with?('/') || uri.start_with?('http://') ||
68-
uri.start_with?('https://')
69-
uri = '/' + uri
70-
end
71-
uri
67+
path = parts * '/'
68+
path = '/' + path unless path.start_with?('/', 'http://', 'https://')
69+
url = URI.parse(path)
70+
url.path.squeeze!('/')
71+
url.to_s
7272
end
7373

7474
# Returns the base URI string.

spec/net/http_client_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@
268268
it 'joins each part specified with a forward slash' do
269269
expect(subject.normalize_uri('a', 'uri', 'path')).to eq '/a/uri/path'
270270
end
271+
272+
it 'removes duplicate forward slashes' do
273+
expect(subject.normalize_uri('http://localhost/', '/path')).to eq 'http://localhost/path'
274+
end
271275
end
272276

273277
describe '#target_port' do

0 commit comments

Comments
 (0)