forked from apple/swift-http-api-proposal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPClientRequestBody.swift
More file actions
172 lines (162 loc) · 6.47 KB
/
Copy pathHTTPClientRequestBody.swift
File metadata and controls
172 lines (162 loc) · 6.47 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
import AsyncStreaming
/// A type that represents the body of an HTTP client request.
///
/// ``HTTPClientRequestBody`` wraps a closure that encapsulates the logic
/// to write a request body. It also contains extra hints and inputs to inform
/// the custom request body writing.
///
/// ## Usage
///
/// ### Seekable bodies
///
/// If the source of the request body bytes can be not only restarted from the beginning,
/// but even restarted from an arbitrary offset, prefer to create a seekable body.
///
/// A seekable body allows the HTTP client to support resumable uploads.
///
/// ```swift
/// try await HTTP.perform(request: request, body: .seekable { byteOffset, writer in
/// // Inspect byteOffset and start writing contents into writer
/// }) { response, body in
/// // Handle the response
/// }
/// ```
///
/// ### Restartable bodies
///
/// If the source of the request body bytes cannot be restarted from an arbitrary offset, but
/// can be restarted from the beginning, use a restartable body.
///
/// A restartable body allows the HTTP client to handle redirects and retries.
///
/// ```swift
/// try await HTTP.perform(request: request, body: .restartable { writer in
/// // Start writing contents into writer from the beginning
/// }) { response, body in
/// // Handle the response
/// }
/// ```
@available(anyAppleOS 26.0, *)
public struct HTTPClientRequestBody<Writer: AsyncWriter & ~Copyable>: Sendable
where Writer.WriteElement == UInt8, Writer: SendableMetatype {
/// The body can be asked to restart writing from an arbitrary offset.
public var isSeekable: Bool {
switch self.writeBody {
case .restartable:
false
case .seekable:
true
}
}
/// The length of the body is known upfront and can be specified in
/// the `Content-Length` header field.
public let knownLength: Int64?
private enum WriteBody {
case restartable(@Sendable (consuming Writer) async throws -> HTTPFields?)
case seekable(@Sendable (Int64, consuming Writer) async throws -> HTTPFields?)
}
private let writeBody: WriteBody
/// Requests the body to be written into the writer.
/// - Parameters:
/// - writer: The destination into which to write the body.
/// - Throws: An error thrown from the body closure.
public func produce(into writer: consuming Writer) async throws -> HTTPFields? {
switch self.writeBody {
case .restartable(let writeBody):
try await writeBody(writer)
case .seekable(let writeBody):
try await writeBody(0, writer)
}
}
/// Requests the partial body at the specified offset to be written into the writer.
/// - Precondition: The body must be seekable.
/// - Parameters:
/// - offset: The offset from which to start writing the body.
/// - writer: The destination into which to write the body.
/// - Throws: An error thrown from the body closure.
public func produce(offset: Int64, into writer: consuming Writer) async throws -> HTTPFields? {
switch self.writeBody {
case .restartable:
fatalError("Request body is not seekable")
case .seekable(let writeBody):
try await writeBody(offset, writer)
}
}
/// A restartable request body that can be replayed from the beginning.
///
/// Use this case when the client may need to retry or follow redirects with
/// the same request body. The closure receives a writer and streams the entire
/// body content. The closure may be called multiple times if the request needs
/// to be retried.
///
/// - Parameters:
/// - knownLength: The length of the body is known upfront and can be specified in
/// the `content-length` header field.
/// - body: The closure that writes the request body using the provided writer and
/// returns an optional trailer.
/// - writer: The writer that receives the request body bytes.
public static func restartable(
knownLength: Int64? = nil,
_ body: @escaping @Sendable (consuming Writer) async throws -> HTTPFields?
) -> Self {
Self.init(
knownLength: knownLength,
writeBody: .restartable(body)
)
}
/// A seekable request body that supports resuming from a specific byte offset.
///
/// Use this case for resumable uploads where the client can start streaming
/// from a specific position in the body. The closure receives an offset indicating
/// where to begin writing and a writer for streaming the body content.
///
/// - Parameters:
/// - knownLength: The length of the body is known upfront and can be specified in
/// the `content-length` header field.
/// - body: The closure that writes the request body using the provided writer and
/// returns an optional trailer.
/// - offset: The byte offset from which to start writing the body.
/// - writer: The writer that receives the request body bytes.
public static func seekable(
knownLength: Int64? = nil,
_ body: @escaping @Sendable (Int64, consuming Writer) async throws -> HTTPFields?
) -> Self {
Self.init(
knownLength: knownLength,
writeBody: .seekable(body)
)
}
private init(knownLength: Int64?, writeBody: WriteBody) {
self.knownLength = knownLength
self.writeBody = writeBody
}
package init<OtherWriter: ~Copyable>(
other: HTTPClientRequestBody<OtherWriter>,
transform: @escaping @Sendable (consuming Writer) -> OtherWriter
) {
self.knownLength = other.knownLength
self.writeBody =
switch other.writeBody {
case .restartable(let writeBody):
.restartable { writer in
try await writeBody(transform(writer))
}
case .seekable(let writeBody):
.seekable { offset, writer in
try await writeBody(offset, transform(writer))
}
}
}
}