From 8a8fd8285330a5632346676ab717fe6e39386e0f Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 18 May 2015 16:28:15 -0700 Subject: [PATCH] Remove dependency on url.Parse() use client.Parse implementation --- aliases.go | 6 ++--- common-methods.go | 20 +++++++---------- copy-url.go | 37 ++++++++++++++----------------- globals.go | 8 ------- hostconfig.go | 12 +++++----- pkg/client/s3/s3.go | 2 -- pkg/client/url.go | 54 ++++++++++++++++++++++++++++++++++++++++----- url.go | 12 ++++------ 8 files changed, 85 insertions(+), 66 deletions(-) diff --git a/aliases.go b/aliases.go index 74dd4ba9..7a5a06f7 100644 --- a/aliases.go +++ b/aliases.go @@ -17,10 +17,10 @@ package main import ( - "net/url" "regexp" "strings" + "github.com/minio/mc/pkg/client" "github.com/minio/minio/pkg/iodine" ) @@ -47,8 +47,8 @@ func isValidAliasName(aliasName string) bool { // aliasExpand expands aliased (name:/path) to full URL, used by url-parser func aliasExpand(aliasedURL string, aliases map[string]string) (newURL string, err error) { - u, err := url.Parse(aliasedURL) - if err != nil { + u := client.Parse(aliasedURL) + if u == nil { return aliasedURL, iodine.New(errInvalidURL{url: aliasedURL}, nil) } // proper URL diff --git a/common-methods.go b/common-methods.go index 009ecc17..8ef9fe2f 100644 --- a/common-methods.go +++ b/common-methods.go @@ -56,8 +56,11 @@ func putTarget(targetURL string, targetConfig *hostConfig, md5hex string, length // getNewClient gives a new client interface func getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Client, err error) { - t := client.GetType(urlStr) - switch t { + url := client.Parse(urlStr) + if url == nil { + return nil, iodine.New(errInvalidURL{url: urlStr}, nil) + } + switch url.Type { case client.Object: // Minio and S3 compatible object storage if auth == nil { return nil, iodine.New(errInvalidArgument{}, nil) @@ -65,18 +68,11 @@ func getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Clie s3Config := new(s3.Config) s3Config.AccessKeyID = auth.AccessKeyID s3Config.SecretAccessKey = auth.SecretAccessKey - s3Config.UserAgent = mcUserAgent s3Config.HostURL = urlStr s3Config.Debug = debug - clnt = s3.New(s3Config) - return clnt, nil + return s3.New(s3Config), nil case client.Filesystem: - clnt, err = fs.New(urlStr) - return clnt, err - default: - return nil, iodine.New(errUnsupportedScheme{ - scheme: t.String(), - url: urlStr, - }, nil) + return fs.New(urlStr) } + return nil, iodine.New(errInvalidURL{url: urlStr}, nil) } diff --git a/copy-url.go b/copy-url.go index c9024a30..4a7048f2 100644 --- a/copy-url.go +++ b/copy-url.go @@ -19,7 +19,6 @@ package main import ( "errors" "fmt" - "net/url" "path/filepath" "strings" @@ -69,8 +68,8 @@ const ( // source2Client returns client and hostconfig objects from the source URL. func source2Client(sourceURL string) (client.Client, error) { // Empty source arg? - sourceURLParse, err := url.Parse(sourceURL) - if err != nil || sourceURLParse.Path == "" { + sourceURLParse := client.Parse(sourceURL) + if sourceURLParse.Path == "" { return nil, iodine.New(errInvalidSource{path: sourceURL}, nil) } @@ -89,8 +88,8 @@ func source2Client(sourceURL string) (client.Client, error) { // target2Client returns client and hostconfig objects from the target URL. func target2Client(targetURL string) (client.Client, error) { // Empty target arg? - targetURLParse, err := url.Parse(targetURL) - if err != nil || targetURLParse.Path == "" { + targetURLParse := client.Parse(targetURL) + if targetURLParse.Path == "" { return nil, iodine.New(errInvalidTarget{path: targetURL}, nil) } @@ -228,13 +227,13 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) *copyURLs { } // Else name is available to create. // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. - sourceURLParse, err := url.Parse(sourceURL) - if err != nil { + sourceURLParse := client.Parse(sourceURL) + if sourceURLParse == nil { return ©URLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} } - targetURLParse, err := url.Parse(targetURL) - if err != nil { + targetURLParse := client.Parse(targetURL) + if targetURLParse == nil { return ©URLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} } @@ -308,24 +307,22 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *copyURLs { } // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. - sourceURLParse, err := url.Parse(sourceURL) - if err != nil { + sourceURLParse := client.Parse(sourceURL) + if sourceURLParse == nil { copyURLsCh <- ©URLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + continue } - targetURLParse, err := url.Parse(targetURL) - if err != nil { + targetURLParse := client.Parse(targetURL) + if targetURLParse == nil { copyURLsCh <- ©URLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} + continue } - sourceContentName, err := url.QueryUnescape(sourceContent.Content.Name) - if err != nil { - copyURLsCh <- ©URLs{Error: iodine.New(errInvalidSource{path: sourceContent.Content.Name}, nil)} - } - - sourceContentParse, err := url.Parse(sourceContentName) - if err != nil { + sourceContentParse := client.Parse(sourceContent.Content.Name) + if sourceContentParse == nil { copyURLsCh <- ©URLs{Error: iodine.New(errInvalidSource{path: sourceContent.Content.Name}, nil)} + continue } // Construct target path from recursive path of source without its prefix dir. diff --git a/globals.go b/globals.go index f6d32b60..c32a2285 100644 --- a/globals.go +++ b/globals.go @@ -17,19 +17,11 @@ // This package contains all the global variables and constants package main -import ( - "os" - "runtime" -) - var ( globalDebugFlag = false // Debug flag set via command line globalQuietFlag = false // Quiet flag set via command line globalMaxRetryFlag = 5 // Maximum number of retries - mcUserAgent = "Minio/" + - Version + " (" + os.Args[0] + "; " + runtime.GOOS + "; " + runtime.GOARCH + ")" - mcCurrentConfigVersion = "1.0.0" ) diff --git a/hostconfig.go b/hostconfig.go index 5115f2dc..6c20290e 100644 --- a/hostconfig.go +++ b/hostconfig.go @@ -17,7 +17,6 @@ package main import ( - "net/url" "path/filepath" "strings" @@ -48,13 +47,12 @@ func getHostConfig(URL string) (*hostConfig, error) { if err != nil { return nil, iodine.New(err, nil) } - u, err := url.Parse(URL) - if err != nil { + url := client.Parse(URL) + if url == nil { return nil, iodine.New(errInvalidURL{url: URL}, nil) } - // No host matching or keys needed for filesystem requests - if client.GetType(URL) == client.Filesystem { + if url.Type == client.Filesystem { hostCfg := &hostConfig{ AccessKeyID: "", SecretAccessKey: "", @@ -63,7 +61,7 @@ func getHostConfig(URL string) (*hostConfig, error) { } // No host matching or keys needed for localhost and 127.0.0.1 URL's skip them - if strings.Contains(u.Host, "localhost") || strings.Contains(u.Host, "127.0.0.1") { + if strings.Contains(url.Host, "localhost") || strings.Contains(url.Host, "127.0.0.1") { hostCfg := &hostConfig{ AccessKeyID: "", SecretAccessKey: "", @@ -71,7 +69,7 @@ func getHostConfig(URL string) (*hostConfig, error) { return hostCfg, nil } for globURL, hostCfg := range config.Hosts { - match, err := filepath.Match(globURL, u.Host) + match, err := filepath.Match(globURL, url.Host) if err != nil { return nil, iodine.New(errInvalidGlobURL{glob: globURL, request: URL}, nil) } diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index b18c1e2a..bf55ca19 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -34,7 +34,6 @@ type Config struct { AccessKeyID string SecretAccessKey string HostURL string - UserAgent string Debug bool // Used for SSL transport layer @@ -88,7 +87,6 @@ func New(config *Config) client.Client { objectstorageConf := new(objectstorage.Config) objectstorageConf.AccessKeyID = config.AccessKeyID objectstorageConf.SecretAccessKey = config.SecretAccessKey - objectstorageConf.UserAgent = config.UserAgent objectstorageConf.Transport = transport objectstorageConf.Region = getRegion(u.Host) objectstorageConf.Endpoint = u.Scheme + "://" + u.Host diff --git a/pkg/client/url.go b/pkg/client/url.go index 3db47f0c..fcd3413d 100644 --- a/pkg/client/url.go +++ b/pkg/client/url.go @@ -16,7 +16,19 @@ package client -import "net/url" +import ( + "bytes" + "net/url" + "os" +) + +// URL client url structure +type URL struct { + Type URLType + Scheme string + Host string + Path string +} // URLType - enum of different url types type URLType int @@ -40,16 +52,46 @@ func (t URLType) String() string { } } -// GetType returns the type of URL -func GetType(urlStr string) URLType { +// Parse url parse +func Parse(urlStr string) *URL { u, err := url.Parse(urlStr) if err != nil { - return Unknown + return nil } if u.Scheme == "http" || u.Scheme == "https" { - return Object + return &URL{ + Scheme: u.Scheme, + Type: Object, + Host: u.Host, + Path: u.Path, + } + } + path, err := url.QueryUnescape(u.Path) + if err != nil { + return nil + } + return &URL{ + Type: Filesystem, + Path: path, } +} - return Filesystem +func (u *URL) String() string { + var buf bytes.Buffer + if u.Scheme != "" { + buf.WriteString(u.Scheme) + buf.WriteByte(':') + } + if u.Scheme != "" || u.Host != "" { + buf.WriteString("//") + if h := u.Host; h != "" { + buf.WriteString(h) + } + } + if u.Path != "" && u.Path[0] != '/' && u.Host != "" { + buf.WriteByte(os.PathSeparator) + } + buf.WriteString(u.Path) + return buf.String() } diff --git a/url.go b/url.go index 454c1a9c..2c82a551 100644 --- a/url.go +++ b/url.go @@ -17,7 +17,6 @@ package main import ( - "net/url" "strings" "github.com/minio/mc/pkg/client" @@ -48,8 +47,7 @@ func stripRecursiveURL(urlStr string) string { // getExpandedURL - extracts URL string from a single cmd-line argument func getExpandedURL(arg string, aliases map[string]string) (urlStr string, err error) { - _, err = url.Parse(arg) - if err != nil { + if url := client.Parse(urlStr); url == nil { // Not a valid URL. Return error return "", iodine.New(errInvalidURL{arg}, nil) } @@ -58,11 +56,9 @@ func getExpandedURL(arg string, aliases map[string]string) (urlStr string, err e if err != nil { return "", iodine.New(err, nil) } - if client.GetType(urlStr) == client.Unknown { - return "", iodine.New(errUnsupportedScheme{ - scheme: client.Unknown.String(), - url: urlStr, - }, nil) + if url := client.Parse(urlStr); url == nil { + // Not a valid URL. Return error + return "", iodine.New(errInvalidURL{urlStr}, nil) } return urlStr, nil }