Merge pull request #1321 from harshavardhana/url-join

url: urlJoinPath should work on trailing slashes now.
This commit is contained in:
Harshavardhana
2015-11-15 12:51:57 -08:00
3 changed files with 79 additions and 7 deletions
+29
View File
@@ -117,6 +117,35 @@ func NewURL(urlStr string) *URL {
}
}
// JoinURLs join two input urls and returns a url
func JoinURLs(url1, url2 *URL) *URL {
var url1Path, url2Path string
url1Path = url1.Path
url2Path = url2.Path
if runtime.GOOS == "windows" {
url1Path = strings.Replace(url1.Path, "\\", "/", -1)
url2Path = strings.Replace(url2.Path, "\\", "/", -1)
}
if url1.Type == Object {
if strings.HasSuffix(url1Path, "/") {
url1.Path = url1Path + strings.TrimPrefix(url2Path, "/")
} else {
url1.Path = url1Path + "/" + strings.TrimPrefix(url2Path, "/")
}
}
if url1.Type == Filesystem {
if strings.HasSuffix(url1Path, "/") {
url1.Path = url1Path + strings.TrimPrefix(url2Path, "/")
} else {
url1.Path = url1Path + "/" + strings.TrimPrefix(url2Path, "/")
}
if runtime.GOOS == "windows" {
url1.Path = strings.Replace(url1.Path, "/", "\\", -1)
}
}
return url1
}
// String convert URL into its canonical form.
func (u URL) String() string {
var buf bytes.Buffer
+1 -7
View File
@@ -18,7 +18,6 @@ package main
import (
"os"
"path/filepath"
"strings"
"github.com/minio/mc/pkg/client"
@@ -48,12 +47,7 @@ func url2Dir(urlStr string) string {
func urlJoinPath(url1, url2 string) string {
u1 := client.NewURL(url1)
u2 := client.NewURL(url2)
if u1.Path != string(u1.Separator) {
u1.Path = filepath.Join(u1.Path, u2.Path)
} else {
u1.Path = u2.Path
}
return u1.String()
return client.JoinURLs(u1, u2).String()
}
// isURLRecursive - find out if requested url is recursive.
+49
View File
@@ -0,0 +1,49 @@
/*
* Minio Client (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import . "gopkg.in/check.v1"
func (s *TestSuite) TestURLJoinPath(c *C) {
// Join two URLs
url1 := "http://s3.mycompany.io/dev"
url2 := "http://s3.aws.amazon.com/mybucket/bin/zgrep"
url := urlJoinPath(url1, url2)
c.Assert(url, Equals, "http://s3.mycompany.io/dev/mybucket/bin/zgrep")
// Join URL and a path
url1 = "http://s3.mycompany.io/dev"
url2 = "mybucket/bin/zgrep"
url = urlJoinPath(url1, url2)
c.Assert(url, Equals, "http://s3.mycompany.io/dev/mybucket/bin/zgrep")
// Check if it strips URL2's tailing ‘/’
url1 = "http://s3.mycompany.io/dev"
url2 = "mybucket/bin/"
url = urlJoinPath(url1, url2)
c.Assert(url, Equals, "http://s3.mycompany.io/dev/mybucket/bin/")
}
func (s *TestSuite) TestStripRecursiveURL(c *C) {
// Strip ‘...’ excluding trailing ‘/’
url := stripRecursiveURL("http://s3.mycompany.io/dev/...")
c.Assert(url, Equals, "http://s3.mycompany.io/dev/")
// Strip ‘...‘
url = stripRecursiveURL("http://s3.mycompany.io/test...")
c.Assert(url, Equals, "http://s3.mycompany.io/test")
}