Skip to content

Fix parsing binary including CRLF#103

Merged
417-72KI merged 7 commits intomainfrom
binary-contains-crlf
Nov 14, 2025
Merged

Fix parsing binary including CRLF#103
417-72KI merged 7 commits intomainfrom
binary-contains-crlf

Conversation

@417-72KI
Copy link
Copy Markdown
Owner

Resolves: #102

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes parsing of binary data (such as PDFs) that contains CRLF (Carriage Return Line Feed) sequences within multipart form data. The fix addresses issue #102 by modifying the parsing logic to properly reconstruct binary content that spans multiple lines when split by CRLF delimiters.

Key Changes:

  • Modified MultipartFormData.Element.from() to return an optional type and handle binary data with embedded CRLF sequences
  • Added comprehensive test coverage with a PDF file resource that likely contains CRLF in its binary content
  • Updated test helper functions across URLSession and APIKit to include PDF upload testing

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
Sources/MultipartFormDataParser/MultipartFormData.swift Refactored parsing logic to properly handle binary data with CRLF by appending line separators when reconstructing data from split chunks; extracted regex matching to private helper methods
Tests/MultipartFormDataParserTests/_Util/TestResource.swift Added base64-encoded PDF resource and accessor property for testing binary file parsing
Tests/MultipartFormDataParserTests/_TestFunctions/TestFunction_URLSession.swift Added pdf parameter to all URLSession-based upload test functions
Tests/MultipartFormDataParserTests/_TestFunctions/TestFunction_APIKit.swift Added pdf parameter to all APIKit-based upload test functions
Tests/MultipartFormDataParserTests/MultipartFormDataParserTests.swift Updated test cases to include PDF in multipart uploads and verify correct parsing; expanded assertions to check PDF data in sequence test
Comments suppressed due to low confidence (2)

Tests/MultipartFormDataParserTests/MultipartFormDataParserTests.swift:36

  • The PDF data is now included in the test request (line 30), but there's no assertion to verify it was parsed correctly in this test case. While testSequence() does verify the PDF data, it would be more thorough to add an explicit assertion here as well:
XCTAssertEqual(data.element(forName: "pdf")?.data, pdf)

This would ensure that PDF files (which may contain CRLF sequences in their binary data) are correctly parsed and retrieved.

        let data = try MultipartFormData.parse(from: request)
        XCTAssertEqual(data.element(forName: "genbaNeko")?.data, genbaNeko)
        XCTAssertEqual(data.element(forName: "denwaNeko")?.data, denwaNeko)
        XCTAssertEqual(data.element(forName: "message")?.string, "Hello world!")

Tests/MultipartFormDataParserTests/MultipartFormDataParserTests.swift:131

  • The Alamofire test functions (uploadWithAlamoFire and uploadWithAlamoFireConcurrency) were not updated to include the pdf parameter, unlike the APIKit and URLSession test functions which were all updated to test PDF uploads. Since this PR fixes parsing of binary data containing CRLF sequences (like PDFs), the Alamofire tests should also be updated to include PDF in the test data to ensure comprehensive test coverage across all upload methods.
    func testAlamofire() throws {
        let genbaNeko = try XCTUnwrap(genbaNeko)
        let denwaNeko = try XCTUnwrap(denwaNeko)
        let message = Data("Hello world!".utf8)

        let result = try XCTUnwrap(
            uploadWithAlamoFire(
                genbaNeko: genbaNeko,
                denwaNeko: denwaNeko,
                message: message
            )
        )
        XCTAssertEqual(result.status, 200)
        XCTAssertNil(result.error)
    }

    func testAlamofireWithConcurrency() async throws {
        let genbaNeko = try XCTUnwrap(genbaNeko)
        let denwaNeko = try XCTUnwrap(denwaNeko)
        let message = Data("Hello world!".utf8)
        let result = try await uploadWithAlamoFireConcurrency(genbaNeko: genbaNeko, denwaNeko: denwaNeko, message: message)
        XCTAssertEqual(result.status, 200)
        XCTAssertNil(result.error)
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@417-72KI 417-72KI merged commit bff653c into main Nov 14, 2025
21 checks passed
@417-72KI 417-72KI deleted the binary-contains-crlf branch November 14, 2025 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected data includes in element when PDF file is set

2 participants