From bbe0fc5303098c5bf3833477aec50d95b60d935b Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 15 Mar 2015 14:12:11 -0700 Subject: [PATCH] Add key and bucket name handling for s3 properly --- pkg/s3/auth.go | 5 ++--- pkg/s3/client.go | 10 +++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/s3/auth.go b/pkg/s3/auth.go index c4a09c1c..c56e08d6 100644 --- a/pkg/s3/auth.go +++ b/pkg/s3/auth.go @@ -223,7 +223,7 @@ var subResList = []string{"acl", "lifecycle", "location", "logging", "notificati // + // [ sub-resource, if present. For example "?acl", "?location", "?logging", or "?torrent"]; func (a *Auth) writeCanonicalizedResource(buf *bytes.Buffer, req *http.Request, host string) { - bucket := a.bucketFromEndpoint(req, host) + bucket := a.bucketFromHost(req, host) if bucket != "" { buf.WriteByte('/') buf.WriteString(bucket) @@ -255,7 +255,7 @@ func hasDotSuffix(s string, suffix string) bool { return len(s) >= len(suffix)+1 && strings.HasSuffix(s, suffix) && s[len(s)-len(suffix)-1] == '.' } -func (a *Auth) bucketFromEndpoint(req *http.Request, host string) string { +func (a *Auth) bucketFromHost(req *http.Request, host string) string { reqHost := req.Host if reqHost == "" { host = req.URL.Host @@ -274,6 +274,5 @@ func (a *Auth) bucketFromEndpoint(req *http.Request, host string) string { } reqHost, _, _ = net.SplitHostPort(reqHost) - return reqHost } diff --git a/pkg/s3/client.go b/pkg/s3/client.go index 499bcb8a..e6348a0b 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -124,7 +124,15 @@ func (c *Client) bucketURL(bucket string) string { } func (c *Client) keyURL(bucket, key string) string { - return c.bucketURL(bucket) + "/" + key + url := c.bucketURL(bucket) + if strings.Contains(c.Host, "localhost") || strings.Contains(c.Host, "127.0.0.1") { + return url + "/" + key + } + host, _, _ := net.SplitHostPort(c.Host) + if net.ParseIP(host) != nil { + return url + "/" + key + } + return url + key } func newReq(url string) *http.Request {