Merge pull request #640 from harshavardhana/pr_out_remove_dependency_on_url_parse_use_client_parse_implementation
This commit is contained in:
+3
-3
@@ -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
|
||||
|
||||
+8
-12
@@ -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)
|
||||
}
|
||||
|
||||
+17
-20
@@ -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.
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
+5
-7
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+48
-6
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user