This repository was archived by the owner on Apr 20, 2024. It is now read-only.
File tree 2 files changed +39
-14
lines changed
2 files changed +39
-14
lines changed Original file line number Diff line number Diff line change @@ -23,14 +23,6 @@ struct Authentication {
23
23
24
24
//used for unit tests
25
25
var unitTestDate : Date ?
26
-
27
- static let awsQueryAllowed = CharacterSet (
28
- charactersIn: " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~= "
29
- )
30
-
31
- static let awsPathAllowed = CharacterSet (
32
- charactersIn: " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~/ "
33
- )
34
26
35
27
var amzDate : String {
36
28
let dateFormatter = DateFormatter ( )
@@ -54,14 +46,10 @@ struct Authentication {
54
46
self . host = host
55
47
self . region = region
56
48
//TODO(Brett): Proper encoding and error handling.
57
- self . baseURL = baseURL. addingPercentEncoding (
58
- withAllowedCharacters: Authentication . awsPathAllowed
59
- ) !
49
+ self . baseURL = try ! baseURL. percentEncode ( allowing: Byte . awsPathAllowed)
60
50
self . key = key
61
51
self . secret = secret
62
- self . requestParam = requestParam? . addingPercentEncoding (
63
- withAllowedCharacters: Authentication . awsQueryAllowed
64
- )
52
+ self . requestParam = try ! requestParam? . percentEncode ( allowing: Byte . awsQueryAllowed)
65
53
}
66
54
67
55
func getSignature( stringToSign: String ) throws -> String {
Original file line number Diff line number Diff line change
1
+ import Core
2
+
3
+ extension Byte {
4
+ static let awsQueryAllowed = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~= " . bytes
5
+
6
+ static let awsPathAllowed = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~/ " . bytes
7
+ }
8
+
9
+ extension String {
10
+ func percentEncode( allowing allowed: Bytes ) throws -> String {
11
+ let bytes = self . bytes
12
+ let encodedBytes = try percentEncodedUppercase ( bytes, shouldEncode: {
13
+ return !allowed. contains ( $0)
14
+ } )
15
+ return encodedBytes. string
16
+ }
17
+ }
18
+
19
+ func percentEncodedUppercase(
20
+ _ input: [ Byte ] ,
21
+ shouldEncode: ( Byte ) throws -> Bool = { _ in true }
22
+ ) throws -> [ Byte ] {
23
+ var group : [ Byte ] = [ ]
24
+ try input. forEach { byte in
25
+ if try shouldEncode ( byte) {
26
+ let hex = String ( byte, radix: 16 ) . uppercased ( ) . utf8
27
+ group. append ( . percent)
28
+ if hex. count == 1 {
29
+ group. append ( . zero)
30
+ }
31
+ group. append ( contentsOf: hex)
32
+ } else {
33
+ group. append ( byte)
34
+ }
35
+ }
36
+ return group
37
+ }
You can’t perform that action at this time.
0 commit comments