File tree Expand file tree Collapse file tree 4 files changed +34
-9
lines changed
Tests/AsyncHTTPClientTests Expand file tree Collapse file tree 4 files changed +34
-9
lines changed Original file line number Diff line number Diff line change @@ -82,7 +82,25 @@ extension HTTPClientRequest.Body {
82
82
) -> Self where Bytes. Element == UInt8 {
83
83
self . init ( . sequence(
84
84
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
86
104
) { allocator in
87
105
if let buffer = bytes. withContiguousStorageIfAvailable ( { allocator. buffer ( bytes: $0) } ) {
88
106
// fastpath
Original file line number Diff line number Diff line change 12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
- public enum RequestBodyLength : Hashable {
15
+ public struct RequestBodyLength {
16
16
/// size of the request body is not known before starting the request
17
- case dynamic
17
+ public static let dynamic : Self = . init ( storage : . dynamic )
18
18
/// 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
20
27
}
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ extension HTTPHeaders {
23
23
try self . validateFieldNames ( )
24
24
25
25
if case . TRACE = method {
26
- switch bodyLength {
26
+ switch bodyLength. storage {
27
27
case . fixed( 0 ) :
28
28
break
29
29
case . dynamic, . fixed:
@@ -36,7 +36,7 @@ extension HTTPHeaders {
36
36
self . setTransportFraming ( method: method, bodyLength: bodyLength)
37
37
38
38
let connectionClose = self [ canonicalForm: " connection " ] . lazy. map { $0. lowercased ( ) } . contains ( " close " )
39
- switch bodyLength {
39
+ switch bodyLength. storage {
40
40
case . dynamic:
41
41
return . init( connectionClose: connectionClose, body: . stream)
42
42
case . fixed( let length) :
@@ -87,7 +87,7 @@ extension HTTPHeaders {
87
87
self . remove ( name: " Content-Length " )
88
88
self . remove ( name: " Transfer-Encoding " )
89
89
90
- switch bodyLength {
90
+ switch bodyLength. storage {
91
91
case . fixed( 0 ) :
92
92
// if we don't have a body we might not need to send the Content-Length field
93
93
// https://tools.ietf.org/html/rfc7230#section-3.3.2
Original file line number Diff line number Diff line change @@ -500,7 +500,7 @@ extension Optional where Wrapped == HTTPClientRequest.Body {
500
500
return buffer
501
501
case . sequence( let announcedLength, _, let generate) :
502
502
let buffer = generate ( ByteBufferAllocator ( ) )
503
- if case let . fixed( announcedLength) = announcedLength,
503
+ if case let . fixed( announcedLength) = announcedLength. storage ,
504
504
announcedLength != buffer. readableBytes {
505
505
throw LengthMismatch ( announcedLength: announcedLength, actualLength: buffer. readableBytes)
506
506
}
@@ -510,7 +510,7 @@ extension Optional where Wrapped == HTTPClientRequest.Body {
510
510
while var buffer = try await generate ( ByteBufferAllocator ( ) ) {
511
511
accumulatedBuffer. writeBuffer ( & buffer)
512
512
}
513
- if case let . fixed ( announcedLength) = announcedLength,
513
+ if case let . fixed ( announcedLength) = announcedLength. storage ,
514
514
announcedLength != accumulatedBuffer. readableBytes {
515
515
throw LengthMismatch ( announcedLength: announcedLength, actualLength: accumulatedBuffer. readableBytes)
516
516
}
You can’t perform that action at this time.
0 commit comments