Skip to content
Draft
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
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"),
.package(url: "https://github.com/vapor/multipart-kit.git", branch: "stream-multipart-part"),
],
targets: [
.target(
name: "OpenAPIRuntime",
dependencies: [
.product(name: "HTTPTypes", package: "swift-http-types")
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "MultipartKit", package: "multipart-kit"),
]
),
.testTarget(
Expand Down
29 changes: 24 additions & 5 deletions Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import FoundationEssentials
import Foundation
#endif
import HTTPTypes
import MultipartKit

extension ParameterStyle {

Expand Down Expand Up @@ -233,9 +234,14 @@ extension Converter {
return untypedPart
}
let validated = MultipartValidationSequence(upstream: untyped, requirements: requirements)
let frames = MultipartRawPartsToFramesSequence(upstream: validated)
let bytes = MultipartFramesToBytesSequence(upstream: frames, boundary: boundary)
return HTTPBody(bytes, length: .unknown, iterationBehavior: multipart.iterationBehavior)
let parts = validated.map { StreamingMultipartPart(headerFields: $0.headerFields, body: $0.body) }
let sections = StreamingMultipartSectionAsyncSequence(parts: parts)
let writer = StreamingMultipartWriterAsyncSequence(
backingSequence: sections,
boundary: boundary,
outboundBody: ArraySlice<UInt8>.self
)
return HTTPBody(writer, length: .unknown, iterationBehavior: multipart.iterationBehavior)
}

/// Returns a parsed multipart body.
Expand All @@ -251,8 +257,21 @@ extension Converter {
requirements: MultipartBodyRequirements,
transform: @escaping @Sendable (MultipartRawPart) async throws -> Part
) -> MultipartBody<Part> {
let frames = MultipartBytesToFramesSequence(upstream: bytes, boundary: boundary)
let raw = MultipartFramesToRawPartsSequence(upstream: frames)
let sections = StreamingMultipartParserAsyncSequence(boundary: boundary, buffer: bytes)
let parts = StreamingMultipartPartAsyncSequence(backingSequence: sections)
let raw = parts.map { part in
let length: HTTPBody.Length
if let contentLength = part.headerFields[.contentLength], let byteCount = Int64(contentLength) {
length = .known(byteCount)
} else {
length = .unknown
}
// The body is a view over the shared parser iterator, so it can only be iterated once.
return MultipartRawPart(
headerFields: part.headerFields,
body: HTTPBody(part.body, length: length, iterationBehavior: .single)
)
}
let validated = MultipartValidationSequence(upstream: raw, requirements: requirements)
let typed = validated.map(transform)
return .init(typed, iterationBehavior: bytes.iterationBehavior)
Expand Down
Loading