Skip to content

Commit 54cfc84

Browse files
committed
only expose HTTPClientRequest.Body.Length publicy
1 parent 526e707 commit 54cfc84

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ extension HTTPClientRequest.Body {
7777

7878
@inlinable
7979
public static func bytes<Bytes: Sequence>(
80-
length: RequestBodyLength,
80+
length: Length,
8181
_ bytes: Bytes
8282
) -> Self where Bytes.Element == UInt8 {
8383
self.init(.sequence(
84-
length: length,
84+
length: length.storage,
8585
canBeConsumedMultipleTimes: false
8686
) { allocator in
8787
if let buffer = bytes.withContiguousStorageIfAvailable({ allocator.buffer(bytes: $0) }) {
@@ -95,11 +95,11 @@ extension HTTPClientRequest.Body {
9595

9696
@inlinable
9797
public static func bytes<Bytes: Collection>(
98-
length: RequestBodyLength,
98+
length: Length,
9999
_ bytes: Bytes
100100
) -> Self where Bytes.Element == UInt8 {
101101
self.init(.sequence(
102-
length: length,
102+
length: length.storage,
103103
canBeConsumedMultipleTimes: true
104104
) { allocator in
105105
if let buffer = bytes.withContiguousStorageIfAvailable({ allocator.buffer(bytes: $0) }) {
@@ -113,23 +113,23 @@ extension HTTPClientRequest.Body {
113113

114114
@inlinable
115115
public static func stream<SequenceOfBytes: AsyncSequence>(
116-
length: RequestBodyLength,
116+
length: Length,
117117
_ sequenceOfBytes: SequenceOfBytes
118118
) -> Self where SequenceOfBytes.Element == ByteBuffer {
119119
var iterator = sequenceOfBytes.makeAsyncIterator()
120-
let body = self.init(.asyncSequence(length: length) { _ -> ByteBuffer? in
120+
let body = self.init(.asyncSequence(length: length.storage) { _ -> ByteBuffer? in
121121
try await iterator.next()
122122
})
123123
return body
124124
}
125125

126126
@inlinable
127127
public static func stream<Bytes: AsyncSequence>(
128-
length: RequestBodyLength,
128+
length: Length,
129129
_ bytes: Bytes
130130
) -> Self where Bytes.Element == UInt8 {
131131
var iterator = bytes.makeAsyncIterator()
132-
let body = self.init(.asyncSequence(length: length) { allocator -> ByteBuffer? in
132+
let body = self.init(.asyncSequence(length: length.storage) { allocator -> ByteBuffer? in
133133
var buffer = allocator.buffer(capacity: 1024) // TODO: Magic number
134134
while buffer.writableBytes > 0, let byte = try await iterator.next() {
135135
buffer.writeInteger(byte)
@@ -155,4 +155,19 @@ extension Optional where Wrapped == HTTPClientRequest.Body {
155155
}
156156
}
157157

158+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
159+
extension HTTPClientRequest.Body {
160+
public struct Length {
161+
/// size of the request body is not known before starting the request
162+
public static let dynamic: Self = .init(storage: .dynamic)
163+
/// size of the request body is fixed and exactly `count` bytes
164+
public static func fixed(_ count: Int) -> Self {
165+
.init(storage: .fixed(count))
166+
}
167+
168+
@usableFromInline
169+
internal var storage: RequestBodyLength
170+
}
171+
}
172+
158173
#endif

Sources/AsyncHTTPClient/ConnectionPool/RequestBodyLength.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
public struct RequestBodyLength {
15+
16+
/// - Note: use `HTTPClientRequest.Body.Length` if you want to expose `RequestBodyLength` publicly
17+
@usableFromInline
18+
internal enum RequestBodyLength: Hashable {
1619
/// size of the request body is not known before starting the request
17-
public static let dynamic: Self = .init(storage: .dynamic)
20+
case dynamic
1821
/// size of the request body is fixed and exactly `count` bytes
19-
public static func fixed(_ count: Int) -> Self {
20-
.init(storage: .fixed(count))
21-
}
22-
internal enum Storage: Hashable {
23-
case dynamic
24-
case fixed(_ count: Int)
25-
}
26-
internal var storage: Storage
22+
case fixed(_ count: Int)
2723
}

Sources/AsyncHTTPClient/RequestValidation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension HTTPHeaders {
2323
try self.validateFieldNames()
2424

2525
if case .TRACE = method {
26-
switch bodyLength.storage {
26+
switch bodyLength {
2727
case .fixed(0):
2828
break
2929
case .dynamic, .fixed:
@@ -36,7 +36,7 @@ extension HTTPHeaders {
3636
self.setTransportFraming(method: method, bodyLength: bodyLength)
3737

3838
let connectionClose = self[canonicalForm: "connection"].lazy.map { $0.lowercased() }.contains("close")
39-
switch bodyLength.storage {
39+
switch bodyLength {
4040
case .dynamic:
4141
return .init(connectionClose: connectionClose, body: .stream)
4242
case .fixed(let length):
@@ -87,7 +87,7 @@ extension HTTPHeaders {
8787
self.remove(name: "Content-Length")
8888
self.remove(name: "Transfer-Encoding")
8989

90-
switch bodyLength.storage {
90+
switch bodyLength {
9191
case .fixed(0):
9292
// if we don't have a body we might not need to send the Content-Length field
9393
// https://tools.ietf.org/html/rfc7230#section-3.3.2

Tests/AsyncHTTPClientTests/HTTPClientRequestTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ extension Optional where Wrapped == HTTPClientRequest.Body {
500500
return buffer
501501
case .sequence(let announcedLength, _, let generate):
502502
let buffer = generate(ByteBufferAllocator())
503-
if case let .fixed(announcedLength) = announcedLength.storage,
503+
if case let .fixed(announcedLength) = announcedLength,
504504
announcedLength != buffer.readableBytes {
505505
throw LengthMismatch(announcedLength: announcedLength, actualLength: buffer.readableBytes)
506506
}
@@ -510,7 +510,7 @@ extension Optional where Wrapped == HTTPClientRequest.Body {
510510
while var buffer = try await generate(ByteBufferAllocator()) {
511511
accumulatedBuffer.writeBuffer(&buffer)
512512
}
513-
if case let .fixed(announcedLength) = announcedLength.storage,
513+
if case let .fixed(announcedLength) = announcedLength,
514514
announcedLength != accumulatedBuffer.readableBytes {
515515
throw LengthMismatch(announcedLength: announcedLength, actualLength: accumulatedBuffer.readableBytes)
516516
}

0 commit comments

Comments
 (0)