Skip to content

Fix HTTPBodySequence for large requests (adds AsyncBufferedPrefixSequence) #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions FlyingFox/Sources/HTTPDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,10 @@ struct HTTPDecoder {
if length <= sharedRequestReplaySize {
return HTTPBodySequence(shared: bytes, count: length, suggestedBufferSize: 4096)
} else {
return HTTPBodySequence(from: bytes, count: length, suggestedBufferSize: 4096)
let prefix = AsyncBufferedPrefixSequence(base: bytes, count: length)
return HTTPBodySequence(from: prefix, count: length, suggestedBufferSize: 4096)
}
}

func makeBodyData(from bytes: some AsyncBufferedSequence<UInt8>, length: Int) async throws -> Data {
var iterator = bytes.makeAsyncIterator()
guard let buffer = try await iterator.nextBuffer(count: length) else {
throw Error("AsyncBufferedSequence prematurely ended")
}
return Data(buffer)
}
}

extension HTTPDecoder {
Expand Down
37 changes: 33 additions & 4 deletions FlyingFox/Tests/HTTPServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,29 @@ actor HTTPServerTests {
)
}

@Test
func requests_larger_than_shared_buffer() async throws {
// given
let server = HTTPServer.make(sharedRequestReplaySize: 100)
let port = try await startServerWithPort(server, preferConnectionsDiscarding: true)

await server.appendRoute("/fish") { req in
let count = try await req.bodyData.count
return HTTPResponse(statusCode: .ok, body: "\(count) bytes".data(using: .utf8)!)
}

// when
var request = URLRequest(url: URL(string: "http://localhost:\(port)/fish")!)
request.httpMethod = "POST"
request.httpBody = Data(repeating: 0x0, count: 200)
let (body, _) = try await URLSession.shared.data(for: request)

// then
#expect(
String(data: body, encoding: .utf8) == "200 bytes"
)
}

@Test
func connections_AreHandled_FallbackTaskGroup() async throws {
let server = HTTPServer.make()
Expand Down Expand Up @@ -539,12 +562,18 @@ extension HTTPServer {

static func make(port: UInt16 = 0,
timeout: TimeInterval = 15,
sharedRequestReplaySize: Int? = nil,
logger: some Logging = .disabled,
handler: (any HTTPHandler)? = nil) -> HTTPServer {
HTTPServer(port: port,
timeout: timeout,
logger: logger,
handler: handler)
var config = Configuration(
port: port,
timeout: timeout,
logger: logger
)
if let sharedRequestReplaySize {
config.sharedRequestReplaySize = sharedRequestReplaySize
}
return HTTPServer(config: config, handler: handler)
}

static func make(port: UInt16 = 0,
Expand Down
81 changes: 81 additions & 0 deletions FlyingSocks/Sources/AsyncBufferedPrefixSequence.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// AsyncBufferedPrefixSequence.swift
// FlyingFox
//
// Created by Simon Whitty on 04/02/2025.
// Copyright © 2025 Simon Whitty. All rights reserved.
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/swhitty/FlyingFox
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

package struct AsyncBufferedPrefixSequence<Base: AsyncBufferedSequence>: AsyncBufferedSequence {
package typealias Element = Base.Element

private let base: Base
private let count: Int

package init(base: Base, count: Int) {
self.base = base
self.count = count
}

package func makeAsyncIterator() -> Iterator {
Iterator(iterator: base.makeAsyncIterator(), remaining: count)
}

package struct Iterator: AsyncBufferedIteratorProtocol {
private var iterator: Base.AsyncIterator
private var remaining: Int

init (iterator: Base.AsyncIterator, remaining: Int) {
self.iterator = iterator
self.remaining = remaining
}

package mutating func next() async throws -> Base.Element? {
guard remaining > 0 else { return nil }

if let element = try await iterator.next() {
remaining -= 1
return element
} else {
remaining = 0
return nil
}
}

package mutating func nextBuffer(suggested count: Int) async throws -> Base.AsyncIterator.Buffer? {
guard remaining > 0 else { return nil }

let count = Swift.min(remaining, count)
if let buffer = try await iterator.nextBuffer(suggested: count) {
remaining -= buffer.count
return buffer
} else {
remaining = 0
return nil
}
}
}
}
101 changes: 101 additions & 0 deletions FlyingSocks/Tests/AsyncBufferedPrefixSequenceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// AsyncBufferedPrefixSequenceTests.swift
// FlyingFox
//
// Created by Simon Whitty on 04/02/2025.
// Copyright © 2025 Simon Whitty. All rights reserved.
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/swhitty/FlyingFox
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

@testable import FlyingSocks
import Foundation
import Testing

struct AsyncBufferedPrefixSequenceTests {

@Test
func next_terminates_after_count() async throws {
let buffer = AsyncBufferedCollection(["a", "b", "c", "d", "e", "f"])
var prefix = AsyncBufferedPrefixSequence(base: buffer, count: 4).makeAsyncIterator()

#expect(
try await prefix.next() == "a"
)
#expect(
try await prefix.next() == "b"
)
#expect(
try await prefix.next() == "c"
)
#expect(
try await prefix.next() == "d"
)
#expect(
try await prefix.next() == nil
)
}

@Test
func nextBuffer_terminates_after_count() async throws {
let buffer = AsyncBufferedCollection(["a", "b", "c", "d", "e", "f"])
var prefix = AsyncBufferedPrefixSequence(base: buffer, count: 4).makeAsyncIterator()

#expect(
try await prefix.nextBuffer(suggested: 3) == ["a", "b", "c"]
)
#expect(
try await prefix.nextBuffer(suggested: 3) == ["d"]
)
#expect(
try await prefix.nextBuffer(suggested: 3) == nil
)
}

@Test
func next_terminates_when_base_terminates() async throws {
let buffer = AsyncBufferedCollection(["a"])
var prefix = AsyncBufferedPrefixSequence(base: buffer, count: 2).makeAsyncIterator()

#expect(
try await prefix.next() == "a"
)
#expect(
try await prefix.next() == nil
)
}

@Test
func nextBuffer_terminates_when_base_terminates() async throws {
let buffer = AsyncBufferedCollection(["a"])
var prefix = AsyncBufferedPrefixSequence(base: buffer, count: 10).makeAsyncIterator()

#expect(
try await prefix.nextBuffer(suggested: 3) == ["a"]
)
#expect(
try await prefix.nextBuffer(suggested: 3) == nil
)
}
}