From 082bb23a71004fa03d79c09b8cf4757bc908dcef Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 18 May 2015 22:40:32 -0700 Subject: [PATCH] Implement our own url.Parse with minimal functionality --- aliases.go | 4 +- common-methods.go | 4 +- cp-url.go | 32 ++++++++++------ hostconfig.go | 4 +- pkg/client/url.go | 96 ++++++++++++++++++++++++++++++++++------------- url.go | 4 +- 6 files changed, 97 insertions(+), 47 deletions(-) diff --git a/aliases.go b/aliases.go index 7a5a06f7..a8c8efbe 100644 --- a/aliases.go +++ b/aliases.go @@ -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 := client.Parse(aliasedURL) - if u == nil { + u, err := client.Parse(aliasedURL) + if err != nil { return aliasedURL, iodine.New(errInvalidURL{url: aliasedURL}, nil) } // proper URL diff --git a/common-methods.go b/common-methods.go index 86709d11..655d3507 100644 --- a/common-methods.go +++ b/common-methods.go @@ -58,8 +58,8 @@ 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) { - url := client.Parse(urlStr) - if url == nil { + url, err := client.Parse(urlStr) + if err != nil { return nil, iodine.New(errInvalidURL{url: urlStr}, nil) } switch url.Type { diff --git a/cp-url.go b/cp-url.go index e6eb4005..3cde3a34 100644 --- a/cp-url.go +++ b/cp-url.go @@ -68,7 +68,11 @@ const ( // source2Client returns client and hostconfig objects from the source URL. func source2Client(sourceURL string) (client.Client, error) { // Empty source arg? - sourceURLParse := client.Parse(sourceURL) + sourceURLParse, err := client.Parse(sourceURL) + if err != nil { + return nil, iodine.New(errInvalidSource{path: sourceURL}, nil) + } + if sourceURLParse.Path == "" { return nil, iodine.New(errInvalidSource{path: sourceURL}, nil) } @@ -88,7 +92,11 @@ 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 := client.Parse(targetURL) + targetURLParse, err := client.Parse(targetURL) + if err != nil { + return nil, iodine.New(errInvalidTarget{path: targetURL}, nil) + } + if targetURLParse.Path == "" { return nil, iodine.New(errInvalidTarget{path: targetURL}, nil) } @@ -227,13 +235,13 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) *cpURLs { } // Else name is available to create. // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. - sourceURLParse := client.Parse(sourceURL) - if sourceURLParse == nil { + sourceURLParse, err := client.Parse(sourceURL) + if err != nil { return &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} } - targetURLParse := client.Parse(targetURL) - if targetURLParse == nil { + targetURLParse, err := client.Parse(targetURL) + if err != nil { return &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} } @@ -307,20 +315,20 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { } // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. - sourceURLParse := client.Parse(sourceURL) - if sourceURLParse == nil { + sourceURLParse, err := client.Parse(sourceURL) + if err != nil { cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} continue } - targetURLParse := client.Parse(targetURL) - if targetURLParse == nil { + targetURLParse, err := client.Parse(targetURL) + if err != nil { cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} continue } - sourceContentParse := client.Parse(sourceContent.Content.Name) - if sourceContentParse == nil { + sourceContentParse, err := client.Parse(sourceContent.Content.Name) + if err != nil { cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{path: sourceContent.Content.Name}, nil)} continue } diff --git a/hostconfig.go b/hostconfig.go index 6c20290e..4d19f2e5 100644 --- a/hostconfig.go +++ b/hostconfig.go @@ -47,8 +47,8 @@ func getHostConfig(URL string) (*hostConfig, error) { if err != nil { return nil, iodine.New(err, nil) } - url := client.Parse(URL) - if url == nil { + url, err := client.Parse(URL) + if err != nil { return nil, iodine.New(errInvalidURL{url: URL}, nil) } // No host matching or keys needed for filesystem requests diff --git a/pkg/client/url.go b/pkg/client/url.go index b3f1bf7a..29e70390 100644 --- a/pkg/client/url.go +++ b/pkg/client/url.go @@ -18,9 +18,11 @@ package client import ( "bytes" - "net/url" + "errors" "runtime" "strings" + + "github.com/minio/minio/pkg/iodine" ) // URL client url structure @@ -40,40 +42,80 @@ const ( Filesystem // POSIX compatible file systems ) -// Parse url parse -func Parse(urlStr string) *URL { - u, err := url.Parse(urlStr) - if err != nil { - return nil - } - if u.Opaque != "" { - path, err := url.QueryUnescape(u.Opaque) - if err != nil { - return nil - } - // if Opaque defaulting to filesystem - return &URL{ - Type: Filesystem, - Path: path, +// Maybe rawurl is of the form scheme:path. (Scheme must be [a-zA-Z][a-zA-Z0-9+-.]*) +// If so, return scheme, path; else return "", rawurl. +func getScheme(rawurl string) (scheme, path string, err error) { + for i := 0; i < len(rawurl); i++ { + c := rawurl[i] + switch { + // valid characters, do nothing + case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z': + // invalid characters, return raw url + case '0' <= c && c <= '9' || c == '+' || c == '-' || c == '.': + if i == 0 { + return "", rawurl, nil + } + // check if the scheme delimiter is a first character, return missing protocol scheme + case c == ':': + if i == 0 { + return "", "", iodine.New(errors.New("missing protocol scheme"), nil) + } + // if not separate them properly + return rawurl[0:i], rawurl[i+1:], nil + default: + // we have encountered an unexpected character, so there is no valid scheme + return "", rawurl, nil } } + return "", rawurl, nil +} - if u.Scheme == "http" || u.Scheme == "https" { - return &URL{ - Scheme: u.Scheme, - Type: Object, - Host: u.Host, - Path: u.Path, - } +// Maybe s is of the form s d s. If so, return s, ds (or s, s if cutd == true). +// If not, return s, "". +func split(s string, d string, cutd bool) (string, string) { + i := strings.Index(s, d) + if i < 0 { + return s, "" } - path, err := url.QueryUnescape(u.Path) + if cutd { + return s[0:i], s[i+len(d):] + } + return s[0:i], s[i:] +} + +func getHost(rest string) (host string) { + i := strings.LastIndex(rest, "@") + if i < 0 { + host = rest + return + } + return +} + +// Parse url parse +func Parse(urlStr string) (*URL, error) { + scheme, rest, err := getScheme(urlStr) if err != nil { - return nil + return nil, iodine.New(err, nil) + } + rest, _ = split(rest, "?", true) + if strings.HasPrefix(rest, "//") { + // if rest has '//' prefix, skip them + authority, rest := split(rest[2:], "/", false) + host := getHost(authority) + if host != "" && (scheme == "http" || scheme == "https") { + return &URL{ + Scheme: scheme, + Type: Object, + Host: host, + Path: rest, + }, nil + } } return &URL{ Type: Filesystem, - Path: path, - } + Path: rest, + }, nil } func (u *URL) String() string { diff --git a/url.go b/url.go index 2c82a551..cc3a3dff 100644 --- a/url.go +++ b/url.go @@ -47,7 +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) { - if url := client.Parse(urlStr); url == nil { + if _, err := client.Parse(urlStr); err != nil { // Not a valid URL. Return error return "", iodine.New(errInvalidURL{arg}, nil) } @@ -56,7 +56,7 @@ func getExpandedURL(arg string, aliases map[string]string) (urlStr string, err e if err != nil { return "", iodine.New(err, nil) } - if url := client.Parse(urlStr); url == nil { + if _, err := client.Parse(urlStr); err != nil { // Not a valid URL. Return error return "", iodine.New(errInvalidURL{urlStr}, nil) }