From 165ae8de4151f3ed50b0487b1aa6d197b076807c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Tue, 3 Jul 2018 18:01:45 +0200 Subject: [PATCH] Fix URI#to_s path starting with `/` --- spec/std/uri_spec.cr | 3 +++ src/uri.cr | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/std/uri_spec.cr b/spec/std/uri_spec.cr index 730800a02c46..ce34583d0d65 100644 --- a/spec/std/uri_spec.cr +++ b/spec/std/uri_spec.cr @@ -211,6 +211,9 @@ describe "URI" do it { URI.new("mailto", path: "foo@example.com").to_s.should eq("mailto:foo@example.com") } it { URI.new("file", path: "/foo.html").to_s.should eq("file:/foo.html") } it { URI.new("file", path: "foo.html").to_s.should eq("file:foo.html") } + it { URI.new("file", host: "host", path: "foo.html").to_s.should eq("file://host/foo.html") } + it { URI.new(path: "//foo").to_s.should eq("/.//foo") } + it { URI.new(host: "host", path: "//foo").to_s.should eq("//host//foo") } it "preserves non-default port" do URI.new("http", "www.example.com", 1234).to_s.should eq("http://www.example.com:1234") diff --git a/src/uri.cr b/src/uri.cr index fa0909f6f9f4..761d5b8ed9fa 100644 --- a/src/uri.cr +++ b/src/uri.cr @@ -147,7 +147,9 @@ class URI io << scheme io << ':' end - io << "//" if @user || host || port + + authority = @user || @host || @port + io << "//" if authority if user = @user userinfo(user, io) io << '@' @@ -160,7 +162,16 @@ class URI if port = @port io << ':' << port end + + if authority + if !@path.empty? && !@path.starts_with?('/') + io << '/' + end + elsif @path.starts_with?("//") + io << "/." + end io << @path + if query io << '?' io << query