From 8b77f918b3c3217125c0a02455ca183e7f8a427f Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 28 Jan 2016 11:18:53 -0800 Subject: [PATCH] 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. --- common-methods.go | 15 ++++++++++++--- typed-errors.go | 4 ++++ url.go | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/common-methods.go b/common-methods.go index dc2aba10..085371e4 100644 --- a/common-methods.go +++ b/common-methods.go @@ -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) } diff --git a/typed-errors.go b/typed-errors.go index 1f8bfdef..248a2ac1 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -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() } diff --git a/url.go b/url.go index 7997af52..09bb9889 100644 --- a/url.go +++ b/url.go @@ -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.