Skip to content

Commit

Permalink
\r delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Nov 20, 2023
1 parent 5c1dd51 commit 69896c8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct DelimitedDataIterator<I: AsyncIteratorProtocol> where I.Element == Data {
let slice = Data(buffer[..<range.startIndex])
buffer = Data(buffer[range.endIndex...])

return slice.isEmpty ? nil : slice
return slice
}

mutating func next() async throws -> Data? {
Expand Down
8 changes: 4 additions & 4 deletions FlyingFox/Sources/MultipartForm/FormDataSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public struct FormDataSequence<S: AsyncSequence & Sendable>: Sendable, AsyncSequ
public static func make(body: S, boundary: String) async throws -> Self {
var delimiter = DelimitedDataIterator(
iterator: body.makeAsyncIterator(),
delimiter: "\n--\(boundary)".data(using: .utf8)!
delimiter: "\r\n--\(boundary)".data(using: .utf8)!
)
// consume first delimiter
_ = try await delimiter.nextUntil("--\(boundary)".data(using: .utf8)!)
try await delimiter.consumeNext(of: "\n", "--")
try await delimiter.consumeNext(of: "\r\n", "--")
return FormDataSequence(delimiter: delimiter)
}

Expand All @@ -83,7 +83,7 @@ public struct FormDataSequence<S: AsyncSequence & Sendable>: Sendable, AsyncSequ
}

// todo `--` end sequence
try await iterator.consumeNext(of: "\n", "--")
try await iterator.consumeNext(of: "\r\n", "--")

return FormData(
headers: headers,
Expand All @@ -105,7 +105,7 @@ extension DelimitedDataIterator {
}

mutating func nextLine() async throws -> String? {
guard let data = try await nextUntil("\n".data(using: .utf8)!) else {
guard let data = try await nextUntil("\r\n".data(using: .utf8)!) else {
return nil
}
return String(data: data, encoding: .utf8)!
Expand Down
28 changes: 14 additions & 14 deletions FlyingFox/Tests/MultipartForm/FormDataSequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ final class FormDataSequenceTests: XCTestCase {
let request = HTTPRequest.make(
headers: [.contentType: "multipart/form-data; boundary=**ZZ"],
body: #"""
--**ZZ
Content-Disposition: form-data; name="text1"
Content-Type: text/plain
Fish & Chips
--**ZZ
Content-Disposition: form-data; name="text2"
--**ZZ\#r
Content-Disposition: form-data; name="text1"\#r
Content-Type: text/plain\#r
\#r
Fish & Chips\#r
--**ZZ\#r
Content-Disposition: form-data; name="text2"\#r
\#r
Shrimp
Scampi
--**ZZ
Content-Disposition: form-data; name="text3"
Ocean
Scampi\#r
--**ZZ\#r
Content-Disposition: form-data; name="text3"\#r
\#r
\#r
--**ZZ--
"""#
)
Expand All @@ -81,7 +81,7 @@ final class FormDataSequenceTests: XCTestCase {
headers: [
.contentDisposition: #"form-data; name="text3""#,
],
body: "Ocean"
body: ""
)
]
)
Expand Down

0 comments on commit 69896c8

Please sign in to comment.