Skip to content

Commit 8ac0d92

Browse files
committed
Use AsyncBufferedPrefixSequence for large requests
1 parent 8529fe3 commit 8ac0d92

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

FlyingFox/Sources/HTTPDecoder.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,10 @@ struct HTTPDecoder {
122122
if length <= sharedRequestReplaySize {
123123
return HTTPBodySequence(shared: bytes, count: length, suggestedBufferSize: 4096)
124124
} else {
125-
return HTTPBodySequence(from: bytes, count: length, suggestedBufferSize: 4096)
125+
let prefix = AsyncBufferedPrefixSequence(base: bytes, count: length)
126+
return HTTPBodySequence(from: prefix, count: length, suggestedBufferSize: 4096)
126127
}
127128
}
128-
129-
func makeBodyData(from bytes: some AsyncBufferedSequence<UInt8>, length: Int) async throws -> Data {
130-
var iterator = bytes.makeAsyncIterator()
131-
guard let buffer = try await iterator.nextBuffer(count: length) else {
132-
throw Error("AsyncBufferedSequence prematurely ended")
133-
}
134-
return Data(buffer)
135-
}
136129
}
137130

138131
extension HTTPDecoder {

FlyingFox/Tests/HTTPServerTests.swift

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,29 @@ actor HTTPServerTests {
184184
)
185185
}
186186

187+
@Test
188+
func requests_larger_than_shared_buffer() async throws {
189+
// given
190+
let server = HTTPServer.make(sharedRequestReplaySize: 100)
191+
let port = try await startServerWithPort(server, preferConnectionsDiscarding: true)
192+
193+
await server.appendRoute("/fish") { req in
194+
let count = try await req.bodyData.count
195+
return HTTPResponse(statusCode: .ok, body: "\(count) bytes".data(using: .utf8)!)
196+
}
197+
198+
// when
199+
var request = URLRequest(url: URL(string: "http://localhost:\(port)/fish")!)
200+
request.httpMethod = "POST"
201+
request.httpBody = Data(repeating: 0x0, count: 200)
202+
let (body, _) = try await URLSession.shared.data(for: request)
203+
204+
// then
205+
#expect(
206+
String(data: body, encoding: .utf8) == "200 bytes"
207+
)
208+
}
209+
187210
@Test
188211
func connections_AreHandled_FallbackTaskGroup() async throws {
189212
let server = HTTPServer.make()
@@ -539,12 +562,18 @@ extension HTTPServer {
539562

540563
static func make(port: UInt16 = 0,
541564
timeout: TimeInterval = 15,
565+
sharedRequestReplaySize: Int? = nil,
542566
logger: some Logging = .disabled,
543567
handler: (any HTTPHandler)? = nil) -> HTTPServer {
544-
HTTPServer(port: port,
545-
timeout: timeout,
546-
logger: logger,
547-
handler: handler)
568+
var config = Configuration(
569+
port: port,
570+
timeout: timeout,
571+
logger: logger
572+
)
573+
if let sharedRequestReplaySize {
574+
config.sharedRequestReplaySize = sharedRequestReplaySize
575+
}
576+
return HTTPServer(config: config, handler: handler)
548577
}
549578

550579
static func make(port: UInt16 = 0,

0 commit comments

Comments
 (0)