Skip to content

Commit

Permalink
fix: ipv6 support without 'port' add missing brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed Feb 22, 2022
1 parent 98a40db commit 7c2621e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,16 +869,24 @@ func (c *Client) makeTargetURL(bucketName, objectName, bucketLocation string, is
// Save scheme.
scheme := c.endpointURL.Scheme

var isIPV6 bool
// Strip port 80 and 443 so we won't send these ports in Host header.
// The reason is that browsers and curl automatically remove :80 and :443
// with the generated presigned urls, then a signature mismatch error.
if h, p, err := net.SplitHostPort(host); err == nil {
if scheme == "http" && p == "80" || scheme == "https" && p == "443" {
host = h
if ip := net.ParseIP(h); ip != nil {
isIPV6 = ip.To16() != nil
}
}
}

urlStr := scheme + "://" + host + "/"
if isIPV6 {
urlStr = scheme + "://[" + host + "]/"
}

// Make URL only if bucketName is available, otherwise use the
// endpoint URL.
if bucketName != "" {
Expand Down
2 changes: 2 additions & 0 deletions api_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ func TestMakeTargetURL(t *testing.T) {
{"localhost:80", false, "mybucket", "myobject", "", nil, url.URL{Host: "localhost", Scheme: "http", Path: "/mybucket/myobject"}, nil},
// Test 9, testing with port 443
{"localhost:443", true, "mybucket", "myobject", "", nil, url.URL{Host: "localhost", Scheme: "https", Path: "/mybucket/myobject"}, nil},
{"[240b:c0e0:102:54C0:1c05:c2c1:19:5001]:443", true, "mybucket", "myobject", "", nil, url.URL{Host: "[240b:c0e0:102:54C0:1c05:c2c1:19:5001]", Scheme: "https", Path: "/mybucket/myobject"}, nil},
{"[240b:c0e0:102:54C0:1c05:c2c1:19:5001]:9000", true, "mybucket", "myobject", "", nil, url.URL{Host: "[240b:c0e0:102:54C0:1c05:c2c1:19:5001]:9000", Scheme: "https", Path: "/mybucket/myobject"}, nil},
}

for i, testCase := range testCases {
Expand Down

0 comments on commit 7c2621e

Please sign in to comment.