@@ -6,8 +6,11 @@ import (
66 "net/url"
77 "strings"
88 "spectra/ds3_go_sdk/ds3/models"
9+ "sort"
910)
1011
12+ const AmazonMetadataPrefix = "x-amz-meta-"
13+
1114type HttpRequestBuilder struct {
1215 reader io.Reader
1316 contentLength * int64
@@ -116,19 +119,60 @@ func (builder *HttpRequestBuilder) Build(conn *ConnectionInfo) (*http.Request, e
116119
117120 builder .signatureFields .Date = getCurrentTime ()
118121
122+ builder .maybeAddAmazonCanonicalHeaders ()
123+
119124 authHeaderVal := builder .signatureFields .BuildAuthHeaderValue (conn .Credentials )
120125
121126 // Set the http request headers such as authorization and date.
122127 return builder .addHttpRequestHeaders (httpRequest , authHeaderVal )
123128}
124129
125130func (builder * HttpRequestBuilder ) buildUrl (conn * ConnectionInfo ) string {
126- var httpUrl url. URL = * conn .Endpoint
131+ var httpUrl = * conn .Endpoint
127132 httpUrl .Path = builder .signatureFields .Path
128133 httpUrl .RawQuery = encodeQueryParams (builder .queryParams )
129134 return httpUrl .String ()
130135}
131136
137+ func (builder * HttpRequestBuilder ) maybeAddAmazonCanonicalHeaders () {
138+ headerKeys := make ([]string , 0 )
139+
140+ for key , value := range * builder .headers {
141+ lowerCaseKey := strings .ToLower (key )
142+ if strings .HasPrefix (lowerCaseKey , models .AMZ_META_HEADER ) && len (value ) > 0 {
143+ headerKeys = append (headerKeys , key )
144+ }
145+ }
146+
147+ if len (headerKeys ) == 0 {
148+ return
149+ }
150+
151+ sort .Strings (headerKeys )
152+
153+ var stringBuilder strings.Builder
154+
155+ var httpHeaders map [string ][]string = * builder .headers
156+
157+ for _ , headerKey := range headerKeys {
158+ lowerCaseKey := strings .ToLower (headerKey )
159+ headerValue := httpHeaders [headerKey ]
160+
161+ if len (headerValue ) > 0 {
162+ stringBuilder .WriteString (lowerCaseKey )
163+ stringBuilder .WriteString (":" )
164+ stringBuilder .WriteString (strings .Join (headerValue , "," ))
165+ stringBuilder .WriteString ("\n " )
166+ }
167+ }
168+
169+ canonicalAmazonHeaders := stringBuilder .String ()
170+
171+ if len (canonicalAmazonHeaders ) > 0 {
172+ builder .signatureFields .CanonicalizedAmzHeaders = stringBuilder .String ()
173+ }
174+ }
175+
132176func (builder * HttpRequestBuilder ) addHttpRequestHeaders (httpRequest * http.Request , authHeader string ) (* http.Request , error ) {
133177 httpRequest .Header .Add ("Date" , builder .signatureFields .Date )
134178 httpRequest .Header .Add ("Authorization" , authHeader )
@@ -155,3 +199,4 @@ func encodeQueryParams(queryParams *url.Values) string {
155199 // with percent encoding for spaces (%20)
156200 return strings .Replace (queryParams .Encode (), "+" , "%20" , - 1 )
157201}
202+
0 commit comments