url: Error out if the aliased URL has host entry.

```
mc ls http://localhost:9000/
```

ends up listing `mc ls /`, This is confusing and should be
treated as an error, allow only aliased operations.
This commit is contained in:
Harshavardhana
2016-02-01 02:41:04 -08:00
parent c5453d1528
commit 8b77f918b3
3 changed files with 17 additions and 3 deletions
+12 -3
View File
@@ -19,6 +19,7 @@ package main
import (
"io"
"os"
"regexp"
"runtime"
"strings"
@@ -131,11 +132,19 @@ func newClientFromAlias(alias string, urlStr string) (client.Client, *probe.Erro
return s3Client, nil
}
// urlRgx - verify if aliased url is real URL.
var urlRgx = regexp.MustCompile("^https?://")
// newClient gives a new client interface
func newClient(urlStr string) (client.Client, *probe.Error) {
alias, urlStrFull, _, err := expandAlias(urlStr)
func newClient(aliasedURL string) (client.Client, *probe.Error) {
alias, urlStrFull, hostCfg, err := expandAlias(aliasedURL)
if err != nil {
return nil, err.Trace(urlStr)
return nil, err.Trace(aliasedURL)
}
// Verify if the aliasedURL is a real URL, fail in those cases
// indicating the user to add alias.
if hostCfg == nil && urlRgx.MatchString(aliasedURL) {
return nil, errInvalidAliasedURL(aliasedURL).Trace(aliasedURL)
}
return newClientFromAlias(alias, urlStrFull)
}
+4
View File
@@ -31,6 +31,10 @@ var (
return probe.NewError(errors.New("Invalid arguments provided, cannot proceed.")).Untrace()
}
errInvalidAliasedURL = func(URL string) *probe.Error {
return probe.NewError(errors.New("Use ‘mc config host add mycloud " + URL + " ...’ to add an alias. Use the alias for S3 operations.")).Untrace()
}
errNoMatchingHost = func(URL string) *probe.Error {
return probe.NewError(errors.New("No matching host found for the given URL ‘" + URL + "’.")).Untrace()
}
+1
View File
@@ -54,6 +54,7 @@ func url2Stat(urlStr string) (client client.Client, content *client.Content, err
// url2Alias separates alias and path from the URL. Aliased URL is of
// the form [/]alias/path/to/blah.
func url2Alias(aliasedURL string) (alias, path string) {
// Save aliased url.
urlStr := aliasedURL
// Convert '/' on windows to filepath.Separator.