Merge pull request #583 from harshavardhana/pr_out_bucketfromhost_was_buggy_remove_it_we_have_better_ways_to_grab_bucketnames
This commit is contained in:
+1
-28
@@ -195,7 +195,7 @@ var subResList = []string{
|
||||
// <HTTP-Request-URI, from the protocol name up to the query string> +
|
||||
// [ sub-resource, if present. For example "?acl", "?location", "?logging", or "?torrent"];
|
||||
func (a *s3Client) writeCanonicalizedResource(buf *bytes.Buffer, req *http.Request, host string) {
|
||||
bucket := a.bucketFromHost(req, host)
|
||||
bucket, _ := a.url2BucketAndObject()
|
||||
if bucket != "" {
|
||||
buf.WriteByte('/')
|
||||
buf.WriteString(bucket)
|
||||
@@ -222,30 +222,3 @@ func (a *s3Client) writeCanonicalizedResource(buf *bytes.Buffer, req *http.Reque
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hasDotSuffix reports whether s ends with "." + suffix.
|
||||
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 *s3Client) bucketFromHost(req *http.Request, host string) string {
|
||||
reqHost := req.Host
|
||||
if reqHost == "" {
|
||||
host = req.URL.Host
|
||||
}
|
||||
|
||||
if reqHost == strings.TrimPrefix(host, "http://") {
|
||||
return ""
|
||||
}
|
||||
|
||||
if reqHost == strings.TrimPrefix(host, "https://") {
|
||||
return ""
|
||||
}
|
||||
|
||||
if reqHostSuffix := strings.TrimPrefix(host, "https://"); hasDotSuffix(reqHost, reqHostSuffix) {
|
||||
return reqHost[:len(reqHost)-len(reqHostSuffix)-1]
|
||||
}
|
||||
|
||||
reqHost, _, _ = net.SplitHostPort(reqHost)
|
||||
return reqHost
|
||||
}
|
||||
|
||||
@@ -38,77 +38,6 @@ func req(s string) *http.Request {
|
||||
return req
|
||||
}
|
||||
|
||||
func (s *MySuite) TestStringToSign(c *C) {
|
||||
var a s3Client
|
||||
tests := []reqAndExpected{
|
||||
{`GET /photos/puppy.jpg HTTP/1.1
|
||||
Host: johnsmith.s3.amazonaws.com
|
||||
Date: Tue, 27 Mar 2007 19:36:42 +0000
|
||||
|
||||
`,
|
||||
"GET\n\n\nTue, 27 Mar 2007 19:36:42 +0000\n/johnsmith/photos/puppy.jpg", "s3.amazonaws.com"},
|
||||
{`PUT /photos/puppy.jpg HTTP/1.1
|
||||
Content-Type: image/jpeg
|
||||
Content-Length: 94328
|
||||
Host: johnsmith.s3.amazonaws.com
|
||||
Date: Tue, 27 Mar 2007 21:15:45 +0000
|
||||
|
||||
`,
|
||||
"PUT\n\nimage/jpeg\nTue, 27 Mar 2007 21:15:45 +0000\n/johnsmith/photos/puppy.jpg", "s3.amazonaws.com"},
|
||||
{`GET /?prefix=photos&max-keys=50&marker=puppy HTTP/1.1
|
||||
User-Agent: Mozilla/5.0
|
||||
Host: johnsmith.s3.amazonaws.com
|
||||
Date: Tue, 27 Mar 2007 19:42:41 +0000
|
||||
|
||||
`,
|
||||
"GET\n\n\nTue, 27 Mar 2007 19:42:41 +0000\n/johnsmith/", "s3.amazonaws.com"},
|
||||
{`DELETE /johnsmith/photos/puppy.jpg HTTP/1.1
|
||||
User-Agent: dotnet
|
||||
Host: s3.amazonaws.com
|
||||
Date: Tue, 27 Mar 2007 21:20:27 +0000
|
||||
x-amz-date: Tue, 27 Mar 2007 21:20:26 +0000
|
||||
|
||||
`,
|
||||
"DELETE\n\n\n\nx-amz-date:Tue, 27 Mar 2007 21:20:26 +0000\n/johnsmith/photos/puppy.jpg", "s3.amazonaws.com"},
|
||||
{`PUT /db-backup.dat.gz HTTP/1.1
|
||||
User-Agent: curl/7.15.5
|
||||
Host: static.johnsmith.net:8080
|
||||
Date: Tue, 27 Mar 2007 21:06:08 +0000
|
||||
x-amz-acl: public-read
|
||||
content-type: application/x-download
|
||||
Content-MD5: 4gJE4saaMU4BqNR0kLY+lw==
|
||||
X-Amz-Meta-ReviewedBy: joe@johnsmith.net
|
||||
X-Amz-Meta-ReviewedBy: jane@johnsmith.net
|
||||
X-Amz-Meta-FileChecksum: 0x02661779
|
||||
X-Amz-Meta-ChecksumAlgorithm: crc32
|
||||
Content-Disposition: attachment; filename=database.dat
|
||||
Content-Encoding: gzip
|
||||
Content-Length: 5913339
|
||||
|
||||
`,
|
||||
"PUT\n4gJE4saaMU4BqNR0kLY+lw==\napplication/x-download\nTue, 27 Mar 2007 21:06:08 +0000\nx-amz-acl:public-read\nx-amz-meta-checksumalgorithm:crc32\nx-amz-meta-filechecksum:0x02661779\nx-amz-meta-reviewedby:joe@johnsmith.net,jane@johnsmith.net\n/db-backup.dat.gz", "static.johnsmith.net:8080"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
got := a.stringToSign(req(test.req), test.host)
|
||||
c.Assert(got, Equals, test.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MySuite) TestBucketFromHostname(c *C) {
|
||||
var a s3Client
|
||||
tests := []reqAndExpected{
|
||||
{"GET / HTTP/1.0\n\n", "", ""},
|
||||
{"GET / HTTP/1.0\nHost: s3.amazonaws.com\n\n", "", "s3.amazonaws.com"},
|
||||
{"GET / HTTP/1.0\nHost: foo.s3.amazonaws.com\n\n", "foo", "s3.amazonaws.com"},
|
||||
{"GET / HTTP/1.0\nHost: foo.com:123\n\n", "foo.com", "foo.com"},
|
||||
{"GET / HTTP/1.0\nHost: bar.com\n\n", "", "bar.com"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
got := a.bucketFromHost(req(test.req), test.host)
|
||||
c.Assert(got, Equals, test.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MySuite) TestsignRequest(c *C) {
|
||||
r := req("GET /foo HTTP/1.1\n\n")
|
||||
config := &Config{
|
||||
@@ -130,9 +59,3 @@ func (s *MySuite) TestsignRequest(c *C) {
|
||||
cl.signRequest(r, "localhost:9000")
|
||||
c.Assert(r.Header.Get("Authorization"), Equals, "AWS key:kHpCR/N7Rw3PwRlDd8+5X40CFVc=")
|
||||
}
|
||||
|
||||
func (s *MySuite) TestHasDotSuffix(c *C) {
|
||||
c.Assert(hasDotSuffix("foo.com", "com"), Equals, true)
|
||||
c.Assert(hasDotSuffix("foocom", "com"), Equals, false)
|
||||
c.Assert(hasDotSuffix("com", "com"), Equals, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user