|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | + |
| 17 | +#if canImport(FoundationEssentials) |
| 18 | +import FoundationEssentials |
| 19 | +#else |
| 20 | +import Foundation |
| 21 | +#endif |
| 22 | + |
| 23 | +/// A response structure specifically designed for streaming Lambda responses that contains |
| 24 | +/// HTTP status code and headers without body content. |
| 25 | +/// |
| 26 | +/// This structure is used with `LambdaResponseStreamWriter.writeStatusAndHeaders(_:)` to send |
| 27 | +/// HTTP response metadata before streaming the response body. |
| 28 | +public struct StreamingLambdaStatusAndHeadersResponse: Codable, Sendable { |
| 29 | + /// The HTTP status code for the response (e.g., 200, 404, 500) |
| 30 | + public let statusCode: Int |
| 31 | + |
| 32 | + /// Dictionary of single-value HTTP headers |
| 33 | + public let headers: [String: String]? |
| 34 | + |
| 35 | + /// Dictionary of multi-value HTTP headers (e.g., Set-Cookie headers) |
| 36 | + public let multiValueHeaders: [String: [String]]? |
| 37 | + |
| 38 | + /// Creates a new streaming Lambda response with status code and optional headers |
| 39 | + /// |
| 40 | + /// - Parameters: |
| 41 | + /// - statusCode: The HTTP status code for the response |
| 42 | + /// - headers: Optional dictionary of single-value HTTP headers |
| 43 | + /// - multiValueHeaders: Optional dictionary of multi-value HTTP headers |
| 44 | + public init( |
| 45 | + statusCode: Int, |
| 46 | + headers: [String: String]? = nil, |
| 47 | + multiValueHeaders: [String: [String]]? = nil |
| 48 | + ) { |
| 49 | + self.statusCode = statusCode |
| 50 | + self.headers = headers |
| 51 | + self.multiValueHeaders = multiValueHeaders |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +extension LambdaResponseStreamWriter { |
| 56 | + /// Writes the HTTP status code and headers to the response stream. |
| 57 | + /// |
| 58 | + /// This method serializes the status and headers as JSON and writes them to the stream, |
| 59 | + /// followed by eight null bytes as a separator before the response body. |
| 60 | + /// |
| 61 | + /// - Parameters: |
| 62 | + /// - response: The status and headers response to write |
| 63 | + /// - encoder: The encoder to use for serializing the response, |
| 64 | + /// - Throws: An error if JSON serialization or writing fails |
| 65 | + public func writeStatusAndHeaders<Encoder: LambdaOutputEncoder>( |
| 66 | + _ response: StreamingLambdaStatusAndHeadersResponse, |
| 67 | + encoder: Encoder |
| 68 | + ) async throws where Encoder.Output == StreamingLambdaStatusAndHeadersResponse { |
| 69 | + |
| 70 | + // Convert Data to ByteBuffer |
| 71 | + var buffer = ByteBuffer() |
| 72 | + try encoder.encode(response, into: &buffer) |
| 73 | + |
| 74 | + // Write the JSON data |
| 75 | + try await write(buffer) |
| 76 | + |
| 77 | + // Write eight null bytes as separator |
| 78 | + var separatorBuffer = ByteBuffer() |
| 79 | + separatorBuffer.writeBytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) |
| 80 | + try await write(separatorBuffer) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +extension LambdaResponseStreamWriter { |
| 85 | + /// Writes the HTTP status code and headers to the response stream. |
| 86 | + /// |
| 87 | + /// This method serializes the status and headers as JSON and writes them to the stream, |
| 88 | + /// followed by eight null bytes as a separator before the response body. |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - response: The status and headers response to write |
| 92 | + /// - encoder: The encoder to use for serializing the response, use JSONEncoder by default |
| 93 | + /// - Throws: An error if JSON serialization or writing fails |
| 94 | + public func writeStatusAndHeaders( |
| 95 | + _ response: StreamingLambdaStatusAndHeadersResponse, |
| 96 | + encoder: JSONEncoder = JSONEncoder() |
| 97 | + ) async throws { |
| 98 | + try await self.writeStatusAndHeaders(response, encoder: LambdaJSONOutputEncoder(encoder)) |
| 99 | + } |
| 100 | +} |
0 commit comments