forked from apple/swift-http-api-proposal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPServerMiddlewareInput.swift
More file actions
81 lines (76 loc) · 3.26 KB
/
Copy pathHTTPServerMiddlewareInput.swift
File metadata and controls
81 lines (76 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift HTTP API Proposal open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift HTTP API Proposal project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
public import HTTPAPIs
/// A struct that encapsulates all parameters passed to HTTP server request handlers.
///
/// ``HTTPServerMiddlewareInput`` serves as a container for the request, request context,
/// request body reader, and response sender. This boxing is necessary because some of these
/// parameters are `~Copyable` types that cannot be stored in tuples, and it provides a
/// convenient way to pass all request-handling components through the middleware chain.
@available(anyAppleOS 26.0, *)
public struct HTTPServerMiddlewareInput<
RequestReader: ConcludingAsyncReader & ~Copyable,
ResponseWriter: ConcludingAsyncWriter & ~Copyable
>: ~Copyable where RequestReader.Underlying: ~Copyable, ResponseWriter.Underlying: ~Copyable {
private let request: HTTPRequest
private let requestContext: HTTPRequestContext
private let requestReader: RequestReader
private let responseSender: HTTPResponseSender<ResponseWriter>
/// Creates a new HTTP server middleware input container.
///
/// - Parameters:
/// - request: The HTTP request headers and metadata.
/// - requestContext: Additional context information for the request.
/// - requestReader: A reader for accessing the request body data and trailing headers.
/// - responseSender: A sender for transmitting the HTTP response and response body.
public init(
request: HTTPRequest,
requestContext: HTTPRequestContext,
requestReader: consuming RequestReader,
responseSender: consuming HTTPResponseSender<ResponseWriter>
) {
self.request = request
self.requestContext = requestContext
self.requestReader = requestReader
self.responseSender = responseSender
}
/// Provides scoped access to the contents of this input container.
///
/// This method exposes all the encapsulated request components to a closure, allowing
/// middleware to access and process them. The closure receives the request, request context,
/// request reader, and response sender as separate parameters.
///
/// - Parameter handler: A closure that processes the request components.
///
/// - Returns: The value returned by the handler closure.
///
/// - Throws: Any error thrown by the handler closure.
public consuming func withContents<Return: ~Copyable>(
_ handler:
(
HTTPRequest,
HTTPRequestContext,
consuming RequestReader,
consuming HTTPResponseSender<ResponseWriter>
) async throws -> Return
) async throws -> Return {
try await handler(
self.request,
self.requestContext,
self.requestReader,
self.responseSender
)
}
}
@available(*, unavailable)
extension HTTPServerMiddlewareInput: Sendable {}