helpers: Use net/url for URL parsing in AbsURL
authorCameron Moore <moorereason@gmail.com>
Wed, 27 Apr 2016 14:54:44 +0000 (09:54 -0500)
committerCameron Moore <moorereason@gmail.com>
Wed, 27 Apr 2016 15:29:46 +0000 (10:29 -0500)
Fixes #2112

helpers/url.go
helpers/url_test.go

index 9d3db80043df8f9b75be0a555d7da9c80eea8d1f..00bcd67c9f5502a47ffa8c0cd59e1bcedad686dd 100644 (file)
@@ -148,7 +148,12 @@ func MakePermalink(host, plink string) *url.URL {
 
 // AbsURL creates a absolute URL from the relative path given and the BaseURL set in config.
 func AbsURL(path string) string {
-       if strings.HasPrefix(path, "http") || strings.HasPrefix(path, "//") {
+       url, err := url.Parse(path)
+       if err != nil {
+               return path
+       }
+
+       if url.IsAbs() || strings.HasPrefix(path, "//") {
                return path
        }
 
index 4774accbb9b4be5915d95b8f04462c0f0b571924..fd8cd5137f59d8e670adf4e5975cb572e7cba995 100644 (file)
@@ -52,9 +52,11 @@ func TestAbsURL(t *testing.T) {
                {"", "http://base/ace/", "http://base/ace/"},
                {"/test/2/foo/", "http://base", "http://base/test/2/foo/"},
                {"http://abs", "http://base/", "http://abs"},
+               {"schema://abs", "http://base/", "schema://abs"},
                {"//schemaless", "http://base/", "//schemaless"},
                {"test/2/foo/", "http://base/path", "http://base/path/test/2/foo/"},
                {"/test/2/foo/", "http://base/path", "http://base/test/2/foo/"},
+               {"http//foo", "http://base/path", "http://base/path/http/foo"},
        }
 
        for _, test := range tests {