-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
190 additions
and
174 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import HTTPTypes | ||
|
||
extension MultipartParser { | ||
public static func parse(_ data: some Collection<UInt8>, boundary: some Collection<UInt8>) throws -> [MultipartPart<ArraySlice<UInt8>>] { | ||
var output: [MultipartPart<ArraySlice<UInt8>>] = [] | ||
var parser = MultipartParser(boundary: boundary) | ||
|
||
var currentHeaders: HTTPFields? | ||
var currentBody = ArraySlice<UInt8>() | ||
|
||
// Append data to the parser and process the sections | ||
parser.append(buffer: data) | ||
|
||
while true { | ||
switch parser.read() { | ||
case .success(let optionalPart): | ||
switch optionalPart { | ||
case .none: | ||
continue | ||
case .some(let part): | ||
switch part { | ||
case .headerFields(let newFields): | ||
if let headers = currentHeaders { | ||
// Merge multiple header fields into the current headers | ||
currentHeaders = HTTPFields(headers + newFields) | ||
} else { | ||
currentHeaders = newFields | ||
} | ||
case .bodyChunk(let bodyChunk): | ||
// Accumulate body chunks | ||
currentBody.append(contentsOf: bodyChunk) | ||
case .boundary: | ||
// Create a MultipartPart when reaching a boundary | ||
if let headers = currentHeaders { | ||
output.append(MultipartPart(headerFields: headers, body: currentBody)) | ||
} | ||
// Reset for the next part | ||
currentHeaders = nil | ||
currentBody = [] | ||
} | ||
} | ||
case .needMoreData: | ||
// No more data is available in synchronous parsing, this should never happen | ||
preconditionFailure("More data is needed") | ||
case .error(let error): | ||
throw error | ||
case .finished: | ||
// If finished, add any remaining part | ||
if let headers = currentHeaders { | ||
output.append(MultipartPart(headerFields: headers, body: currentBody)) | ||
} | ||
return output | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import Foundation | ||
import HTTPTypes | ||
|
||
public enum MultipartPart: Equatable, Sendable { | ||
case headerField(HTTPField) | ||
case bodyChunk(ArraySlice<UInt8>) | ||
case boundary | ||
public struct MultipartPart<Body: Collection<UInt8>>: Equatable, Sendable where Body: Sendable & Equatable { | ||
public let headerFields: HTTPFields | ||
public var body: Body | ||
|
||
public init(headerFields: HTTPFields, body: Body) { | ||
self.headerFields = headerFields | ||
self.body = body | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
import HTTPTypes | ||
|
||
public enum MultipartSection: Equatable, Sendable { | ||
case headerFields(HTTPFields) | ||
case bodyChunk(ArraySlice<UInt8>) | ||
case boundary(end: Bool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.