Skip to content

Commit 526e707

Browse files
committed
add Collection overload and make RequestBodyLength a struct
1 parent f0c09bd commit 526e707

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,25 @@ extension HTTPClientRequest.Body {
8282
) -> Self where Bytes.Element == UInt8 {
8383
self.init(.sequence(
8484
length: length,
85-
canBeConsumedMultipleTimes: bytes is Collection
85+
canBeConsumedMultipleTimes: false
86+
) { allocator in
87+
if let buffer = bytes.withContiguousStorageIfAvailable({ allocator.buffer(bytes: $0) }) {
88+
// fastpath
89+
return buffer
90+
}
91+
// potentially really slow path
92+
return allocator.buffer(bytes: bytes)
93+
})
94+
}
95+
96+
@inlinable
97+
public static func bytes<Bytes: Collection>(
98+
length: RequestBodyLength,
99+
_ bytes: Bytes
100+
) -> Self where Bytes.Element == UInt8 {
101+
self.init(.sequence(
102+
length: length,
103+
canBeConsumedMultipleTimes: true
86104
) { allocator in
87105
if let buffer = bytes.withContiguousStorageIfAvailable({ allocator.buffer(bytes: $0) }) {
88106
// fastpath

Sources/AsyncHTTPClient/ConnectionPool/RequestBodyLength.swift

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

15-
public enum RequestBodyLength: Hashable {
15+
public struct RequestBodyLength {
1616
/// size of the request body is not known before starting the request
17-
case dynamic
17+
public static let dynamic: Self = .init(storage: .dynamic)
1818
/// size of the request body is fixed and exactly `count` bytes
19-
case fixed(_ count: Int)
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
2027
}

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 {
26+
switch bodyLength.storage {
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 {
39+
switch bodyLength.storage {
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 {
90+
switch bodyLength.storage {
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,
503+
if case let .fixed(announcedLength) = announcedLength.storage,
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,
513+
if case let .fixed(announcedLength) = announcedLength.storage,
514514
announcedLength != accumulatedBuffer.readableBytes {
515515
throw LengthMismatch(announcedLength: announcedLength, actualLength: accumulatedBuffer.readableBytes)
516516
}

0 commit comments

Comments
 (0)