diff --git a/pkg/client/url.go b/pkg/client/url.go index 168e660c..f80bcd0b 100644 --- a/pkg/client/url.go +++ b/pkg/client/url.go @@ -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 diff --git a/url.go b/url.go index 7d84fcad..c0c48e8f 100644 --- a/url.go +++ b/url.go @@ -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. diff --git a/url_test.go b/url_test.go new file mode 100644 index 00000000..9106ffe0 --- /dev/null +++ b/url_test.go @@ -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") +}