66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
// Copyright (C) MongoDB, Inc. 2017-present.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
// not use this file except in compliance with the License. You may obtain
|
|
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Based on github.com/aws/aws-sdk-go by Amazon.com, Inc. with code from:
|
|
// - github.com/aws/aws-sdk-go/blob/v1.44.225/aws/signer/v4/uri_path.go
|
|
// - github.com/aws/aws-sdk-go/blob/v1.44.225/private/protocol/rest/build.go
|
|
// See THIRD-PARTY-NOTICES for original license terms
|
|
|
|
package v4
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// Whether the byte value can be sent without escaping in AWS URLs
|
|
var noEscape [256]bool
|
|
|
|
func init() {
|
|
for i := 0; i < len(noEscape); i++ {
|
|
// AWS expects every character except these to be escaped
|
|
noEscape[i] = (i >= 'A' && i <= 'Z') ||
|
|
(i >= 'a' && i <= 'z') ||
|
|
(i >= '0' && i <= '9') ||
|
|
i == '-' ||
|
|
i == '.' ||
|
|
i == '_' ||
|
|
i == '~'
|
|
}
|
|
}
|
|
|
|
func getURIPath(u *url.URL) string {
|
|
var uri string
|
|
|
|
if len(u.Opaque) > 0 {
|
|
uri = "/" + strings.Join(strings.Split(u.Opaque, "/")[3:], "/")
|
|
} else {
|
|
uri = u.EscapedPath()
|
|
}
|
|
|
|
if len(uri) == 0 {
|
|
uri = "/"
|
|
}
|
|
|
|
return uri
|
|
}
|
|
|
|
// EscapePath escapes part of a URL path in Amazon style
|
|
func EscapePath(path string, encodeSep bool) string {
|
|
var buf bytes.Buffer
|
|
for i := 0; i < len(path); i++ {
|
|
c := path[i]
|
|
if noEscape[c] || (c == '/' && !encodeSep) {
|
|
buf.WriteByte(c)
|
|
} else {
|
|
fmt.Fprintf(&buf, "%%%02X", c)
|
|
}
|
|
}
|
|
return buf.String()
|
|
}
|