Giter Club home page Giter Club logo

Comments (5)

tanner0101 avatar tanner0101 commented on May 17, 2024

Hi @dalu93, unfortunately this library's 4.0.0 version is no longer being maintained. It was merged into vapor/vapor a while ago: https://github.com/vapor/vapor/tree/master/Sources/Vapor/Multipart.

Eventually my goal is to split the package back out again and formally pitch this to the swift server work group's package index. Until then, you can access the MultipartParser and MultipartSerializer as part of the Vapor module.

That said, I'm not sure why the length is about 2x longer via MultipartKit here. Seeing the full output (attached as a .txt or something) could help. If this bug exists in the vapor/vapor version of this code I'm happy to fix it.

from multipart-kit.

dalu93 avatar dalu93 commented on May 17, 2024

Hi @tanner0101,

Vapor is not an option for us since we are planning to create a very small CLI tool and importing the whole Vapor product is going to be heavy and useless.

I created, however, an example repository where you can see the error: https://github.com/dalu93/multipartkit-issue
When running through Xcode, set the environment variable FILE_PATH to your own file path (the file is included in the project as well)

from multipart-kit.

dalu93 avatar dalu93 commented on May 17, 2024

Hi @tanner0101 have you had chance to have a look at it? Unfortunately I cannot run Vapor locally because I'm on MacOS Mojave (10.14) and Vapor only supports 10.15+.

I was anyway able to verify few things:

There is a difference in terms of bytes between the two methods, using MultipartKit the data is almost 2x the data I get from using my small helper.
So I tried to replicate as much as possible what MultipartKit is doing

  1. I created my multipartData using my small helper
let multipartData = try MultipartHelper().multipart(
    from: fileUrl.path,
    partName: "file",
    boundary: boundary
)
  1. I created a new ByteBuffer and I wrote the data there
var buffer = ByteBufferAllocator().buffer(capacity: 0)
buffer.writeBytes(multipartData)
  1. I converted the buffer to a String
let bufferString = String(decoding: buffer.readableBytesView, as: UTF8.self)
  1. I expected to have multipartData.count equal to Data(bufferString.utf8).count
print(Data(bufferString.utf8).count == multipartData.count) // got `false`

When I compare the string result of this operation (bufferString) to the string I get directly using MultipartSerializer, they are identical

let body = try MultipartSerializer().serialize(
    parts: [multipart],
    boundary: boundary
)

print(body == bufferString) // got `true`

from multipart-kit.

t-ae avatar t-ae commented on May 17, 2024

There are two serialization methods:

public func serialize(parts: [MultipartPart], boundary: String) throws -> String {
var buffer = ByteBufferAllocator().buffer(capacity: 0)
try self.serialize(parts: parts, boundary: boundary, into: &buffer)
return String(decoding: buffer.readableBytesView, as: UTF8.self)
}

public func serialize(parts: [MultipartPart], boundary: String, into buffer: inout ByteBuffer) throws {
for part in parts {
buffer.writeString("--")
buffer.writeString(boundary)
buffer.writeString("\r\n")
for (key, val) in part.headers {
buffer.writeString(key)
buffer.writeString(": ")
buffer.writeString(val)
buffer.writeString("\r\n")
}
buffer.writeString("\r\n")
var body = part.body
buffer.writeBuffer(&body)
buffer.writeString("\r\n")
}
buffer.writeString("--")
buffer.writeString(boundary)
buffer.writeString("--\r\n")
}

The former method you are using uses String internally so it can corrupt input data.
You have to use the latter.

from multipart-kit.

0xTim avatar 0xTim commented on May 17, 2024

@dalu93 are you seeing this in the latest release? We've rewritten the parser in Swift so hopefully this issue has been resolved

from multipart-kit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.