Implement our own url.Parse with minimal functionality

This commit is contained in:
Harshavardhana
2015-05-18 22:40:32 -07:00
parent 0987e29242
commit 082bb23a71
6 changed files with 97 additions and 47 deletions
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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 {
+20 -12
View File
@@ -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
}
+2 -2
View File
@@ -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
+69 -27
View File
@@ -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 {
+2 -2
View File
@@ -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)
}