22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0.
44 */
5-
5+ import struct Foundation . CharacterSet
66import struct Foundation. URLQueryItem
77import struct Foundation. URLComponents
88import AwsCommonRuntimeKit
@@ -29,12 +29,22 @@ public class SdkHttpRequest {
2929 }
3030}
3131
32+ // Create a `CharacterSet` of the characters that need not be percent encoded in the
33+ // resulting URL. This set consists of alphanumerics plus underscore, dash, tilde, and
34+ // period. Any other character should be percent-encoded when used in a path segment.
35+ // Forward-slash is added as well because the segments have already been joined into a path.
36+ //
37+ // See, for URL-allowed characters:
38+ // https://www.rfc-editor.org/rfc/rfc3986#section-2.3
39+ private let allowed = CharacterSet . alphanumerics. union ( CharacterSet ( charactersIn: " /_-.~ " ) )
40+
3241extension SdkHttpRequest {
3342 public func toHttpRequest( bufferSize: Int = 1024 ) -> HttpRequest {
3443 let httpHeaders = headers. toHttpHeaders ( )
3544 let httpRequest = HttpRequest ( )
3645 httpRequest. method = method. rawValue
37- httpRequest. path = " \( endpoint. path) \( endpoint. queryItemString) "
46+ let encodedPath = endpoint. path. addingPercentEncoding ( withAllowedCharacters: allowed) ?? endpoint. path
47+ httpRequest. path = " \( encodedPath) \( endpoint. queryItemString) "
3848 httpRequest. addHeaders ( headers: httpHeaders)
3949 httpRequest. body = body. toAwsInputStream ( )
4050 return httpRequest
0 commit comments