diff --git a/Sources/SotoCore/Middleware/Middleware.swift b/Sources/SotoCore/Middleware/Middleware.swift index 8bc1af283..7ce04209e 100644 --- a/Sources/SotoCore/Middleware/Middleware.swift +++ b/Sources/SotoCore/Middleware/Middleware.swift @@ -21,12 +21,14 @@ public struct AWSMiddlewareContext { public var logger: Logger } +/// Function to call next middleware in the chain +public typealias AWSMiddlewareNextHandler = (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse /// Middleware handler, function that takes a request, context and the next function to call -public typealias AWSMiddlewareHandler = @Sendable (AWSHTTPRequest, AWSMiddlewareContext, _ next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse +public typealias AWSMiddlewareHandler = @Sendable (AWSHTTPRequest, AWSMiddlewareContext, _ next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse /// Middleware protocol, with function that takes a request, context and the next function to call public protocol AWSMiddlewareProtocol: Sendable { - func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse + func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse } /// Middleware initialized with a middleware handle @@ -39,7 +41,7 @@ public struct AWSMiddleware: AWSMiddlewareProtocol { } @inlinable - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { try await self.middleware(request, context, next) } } @@ -56,7 +58,7 @@ public struct AWSMiddleware2 AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { try await self.m0.handle(request, context: context) { request, context in try await self.m1.handle(request, context: context, next: next) } @@ -80,7 +82,7 @@ public struct AWSDynamicMiddlewareStack: AWSMiddlewareProtocol { } @inlinable - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { let iterator = self.stack.makeIterator() return try await self.run(request, context: context, iterator: iterator, finally: next) } @@ -90,7 +92,7 @@ public struct AWSDynamicMiddlewareStack: AWSMiddlewareProtocol { _ request: AWSHTTPRequest, context: AWSMiddlewareContext, iterator: Stack.Iterator, - finally: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse + finally: AWSMiddlewareNextHandler ) async throws -> AWSHTTPResponse { var iterator = iterator switch iterator.next() { @@ -107,7 +109,7 @@ public struct AWSDynamicMiddlewareStack: AWSMiddlewareProtocol { public struct PassThruMiddleware: AWSMiddlewareProtocol { public init() {} - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { try await next(request, context) } } diff --git a/Sources/SotoCore/Middleware/Middleware/EditHeadersMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/EditHeadersMiddleware.swift index 32d4b9b17..e1a2b95ef 100644 --- a/Sources/SotoCore/Middleware/Middleware/EditHeadersMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/EditHeadersMiddleware.swift @@ -34,7 +34,7 @@ public struct AWSEditHeadersMiddleware: AWSMiddlewareProtocol { } @inlinable - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { var request = request for edit in self.edits { switch edit { diff --git a/Sources/SotoCore/Middleware/Middleware/EndpointDiscoveryMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/EndpointDiscoveryMiddleware.swift index bfa765b04..59299a26a 100644 --- a/Sources/SotoCore/Middleware/Middleware/EndpointDiscoveryMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/EndpointDiscoveryMiddleware.swift @@ -37,7 +37,7 @@ public struct EndpointDiscoveryMiddleware: AWSMiddlewareProtocol { self.isRequired = required } - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { let isEnabled = context.serviceConfig.options.contains(.enableEndpointDiscovery) guard isEnabled || self.isRequired else { return try await next(request, context) diff --git a/Sources/SotoCore/Middleware/Middleware/ErrorHandlingMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/ErrorHandlingMiddleware.swift index d8e306cac..dfaf6461e 100644 --- a/Sources/SotoCore/Middleware/Middleware/ErrorHandlingMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/ErrorHandlingMiddleware.swift @@ -20,7 +20,7 @@ import SotoSignerV4 struct ErrorHandlingMiddleware: AWSMiddlewareProtocol { let options: AWSClient.Options - func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { let response = try await next(request, context) // if response has an HTTP status code outside 2xx then throw an error diff --git a/Sources/SotoCore/Middleware/Middleware/LoggingMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/LoggingMiddleware.swift index ae37a3eac..93c67ac74 100644 --- a/Sources/SotoCore/Middleware/Middleware/LoggingMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/LoggingMiddleware.swift @@ -38,7 +38,7 @@ public struct AWSLoggingMiddleware: AWSMiddlewareProtocol { } @inlinable - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { self.log( "Request:\n" + " \(context.operation)\n" + diff --git a/Sources/SotoCore/Middleware/Middleware/RetryMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/RetryMiddleware.swift index 618983378..9d8203bf0 100644 --- a/Sources/SotoCore/Middleware/Middleware/RetryMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/RetryMiddleware.swift @@ -22,7 +22,7 @@ struct RetryMiddleware: AWSMiddlewareProtocol { let retryPolicy: RetryPolicy @inlinable - func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { var attempt = 0 while true { do { diff --git a/Sources/SotoCore/Middleware/Middleware/S3Middleware.swift b/Sources/SotoCore/Middleware/Middleware/S3Middleware.swift index ee908ce18..e070f3f2c 100644 --- a/Sources/SotoCore/Middleware/Middleware/S3Middleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/S3Middleware.swift @@ -30,7 +30,7 @@ import Foundation /// - Fixes up the GetBucketLocation response, so it can be decoded correctly /// - Creates error body for notFound responses to HEAD requests public struct S3Middleware: AWSMiddlewareProtocol { - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { var request = request self.virtualAddressFixup(request: &request, context: context) diff --git a/Sources/SotoCore/Middleware/Middleware/SigningMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/SigningMiddleware.swift index 930d20a88..aafefa81e 100644 --- a/Sources/SotoCore/Middleware/Middleware/SigningMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/SigningMiddleware.swift @@ -22,7 +22,7 @@ struct SigningMiddleware: AWSMiddlewareProtocol { let credentialProvider: any CredentialProvider @inlinable - func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { var request = request // get credentials let credential = try await self.credentialProvider.getCredential(logger: context.logger) diff --git a/Sources/SotoCore/Middleware/Middleware/TracingMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/TracingMiddleware.swift index ff738d40a..82923e255 100644 --- a/Sources/SotoCore/Middleware/Middleware/TracingMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/TracingMiddleware.swift @@ -24,7 +24,7 @@ public struct AWSTracingMiddleware: AWSMiddlewareProtocol { public func handle( _ request: AWSHTTPRequest, context: AWSMiddlewareContext, - next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse + next: AWSMiddlewareNextHandler ) async throws -> AWSHTTPResponse { try await InstrumentationSystem.tracer.withSpan( "\(context.serviceConfig.serviceName).\(context.operation)", diff --git a/Sources/SotoCore/Middleware/Middleware/TreeHashMiddleware.swift b/Sources/SotoCore/Middleware/Middleware/TreeHashMiddleware.swift index be0e31d5d..857a35a08 100644 --- a/Sources/SotoCore/Middleware/Middleware/TreeHashMiddleware.swift +++ b/Sources/SotoCore/Middleware/Middleware/TreeHashMiddleware.swift @@ -21,7 +21,7 @@ let MEGA_BYTE = 1024 * 1024 /// Calculates a tree hash calculated from the SHA256 of each 1MB section of the request body /// and adds it to the request as a header value public struct TreeHashMiddleware: AWSMiddlewareProtocol { - public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: (AWSHTTPRequest, AWSMiddlewareContext) async throws -> AWSHTTPResponse) async throws -> AWSHTTPResponse { + public func handle(_ request: AWSHTTPRequest, context: AWSMiddlewareContext, next: AWSMiddlewareNextHandler) async throws -> AWSHTTPResponse { var request = request if request.headers[self.treeHashHeader].first == nil { if case .byteBuffer(let buffer) = request.body.storage {