Skip to content

Commit

Permalink
Add AWSShape option .md5ChecksumHeader, config option .calculateMD5 (#…
Browse files Browse the repository at this point in the history
…484)

If you set `.calculateMD5` in the config then any request which has an input shape with option `.md5ChecksumHeader` will have the its md5 checksum calculated.
  • Loading branch information
adam-fowler authored Nov 23, 2021
1 parent 97bdcfd commit a26b4bd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Sources/SotoCore/AWSServiceConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public final class AWSServiceConfig {

/// Disable S3 signed chunked uploads
public static let s3DisableChunkedUploads = Options(rawValue: 1 << 4)

/// calculate MD5 for requests with content-md5 header
public static let calculateMD5 = Options(rawValue: 1 << 5)
}

private init(
Expand Down
2 changes: 2 additions & 0 deletions Sources/SotoCore/Doc/AWSShape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ public struct AWSShapeOptions: OptionSet {
public static let rawPayload = AWSShapeOptions(rawValue: 1 << 2)
/// Calculate MD5 of body is required
public static let md5ChecksumRequired = AWSShapeOptions(rawValue: 1 << 3)
/// Request includes a MD5 checksum
public static let md5ChecksumHeader = AWSShapeOptions(rawValue: 1 << 4)
}

/// Root AWSShape which include a payload
Expand Down
4 changes: 3 additions & 1 deletion Sources/SotoCore/Message/AWSRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ extension AWSRequest {
}

/// MD5 checksum
if Input._options.contains(.md5ChecksumRequired),
let checksumRequired = Input._options.contains(.md5ChecksumRequired) ||
(Input._options.contains(.md5ChecksumHeader) && configuration.options.contains(.calculateMD5))
if checksumRequired,
let buffer = body.asByteBuffer(byteBufferAllocator: configuration.byteBufferAllocator),
headers["content-md5"].first == nil,
let md5 = Self.calculateMD5(buffer)
Expand Down
19 changes: 18 additions & 1 deletion Tests/SotoCoreTests/AWSRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class AWSRequestTests: XCTestCase {
XCTAssertFalse(body.isEmpty)
}

func testMD5Checksum() {
func testRequiredMD5Checksum() {
struct Input: AWSEncodableShape {
static let _options: AWSShapeOptions = .md5ChecksumRequired
let q: [String]
Expand All @@ -471,6 +471,23 @@ class AWSRequestTests: XCTestCase {
XCTAssertEqual(request?.httpHeaders["Content-MD5"].first, "3W1MVcXgkODdv+m6VeZqdQ==")
}

func testMD5ChecksumHeader() {
struct Input: AWSEncodableShape {
static let _options: AWSShapeOptions = .md5ChecksumHeader
let q: [String]
}
let input = Input(q: ["one", "two", "three", "four"])
let config = createServiceConfig(region: .useast2, service: "myservice", options: .calculateMD5)
var request: AWSRequest?
XCTAssertNoThrow(request = try AWSRequest(operation: "Test", path: "/", httpMethod: .GET, input: input, configuration: config))
XCTAssertEqual(request?.httpHeaders["Content-MD5"].first, "3W1MVcXgkODdv+m6VeZqdQ==")

let config2 = createServiceConfig(region: .useast2, service: "myservice")
var request2: AWSRequest?
XCTAssertNoThrow(request2 = try AWSRequest(operation: "Test", path: "/", httpMethod: .GET, input: input, configuration: config2))
XCTAssertNil(request2?.httpHeaders["Content-MD5"].first)
}

func testMD5ChecksumSetAlready() {
struct Input: AWSEncodableShape {
static let _options: AWSShapeOptions = .md5ChecksumRequired
Expand Down

0 comments on commit a26b4bd

Please sign in to comment.