...

Source file src/go.mongodb.org/mongo-driver/internal/aws/signer/v4/uri_path.go

Documentation: go.mongodb.org/mongo-driver/internal/aws/signer/v4

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Based on github.com/aws/aws-sdk-go by Amazon.com, Inc. with code from:
     8  // - github.com/aws/aws-sdk-go/blob/v1.44.225/aws/signer/v4/uri_path.go
     9  // - github.com/aws/aws-sdk-go/blob/v1.44.225/private/protocol/rest/build.go
    10  // See THIRD-PARTY-NOTICES for original license terms
    11  
    12  package v4
    13  
    14  import (
    15  	"bytes"
    16  	"fmt"
    17  	"net/url"
    18  	"strings"
    19  )
    20  
    21  // Whether the byte value can be sent without escaping in AWS URLs
    22  var noEscape [256]bool
    23  
    24  func init() {
    25  	for i := 0; i < len(noEscape); i++ {
    26  		// AWS expects every character except these to be escaped
    27  		noEscape[i] = (i >= 'A' && i <= 'Z') ||
    28  			(i >= 'a' && i <= 'z') ||
    29  			(i >= '0' && i <= '9') ||
    30  			i == '-' ||
    31  			i == '.' ||
    32  			i == '_' ||
    33  			i == '~'
    34  	}
    35  }
    36  
    37  func getURIPath(u *url.URL) string {
    38  	var uri string
    39  
    40  	if len(u.Opaque) > 0 {
    41  		uri = "/" + strings.Join(strings.Split(u.Opaque, "/")[3:], "/")
    42  	} else {
    43  		uri = u.EscapedPath()
    44  	}
    45  
    46  	if len(uri) == 0 {
    47  		uri = "/"
    48  	}
    49  
    50  	return uri
    51  }
    52  
    53  // EscapePath escapes part of a URL path in Amazon style
    54  func EscapePath(path string, encodeSep bool) string {
    55  	var buf bytes.Buffer
    56  	for i := 0; i < len(path); i++ {
    57  		c := path[i]
    58  		if noEscape[c] || (c == '/' && !encodeSep) {
    59  			buf.WriteByte(c)
    60  		} else {
    61  			fmt.Fprintf(&buf, "%%%02X", c)
    62  		}
    63  	}
    64  	return buf.String()
    65  }
    66  

View as plain text