Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiPartParser not detecting uploaded files #1974

Open
GladBarsan opened this issue Mar 18, 2024 · 5 comments
Open

MultiPartParser not detecting uploaded files #1974

GladBarsan opened this issue Mar 18, 2024 · 5 comments

Comments

@GladBarsan
Copy link

GladBarsan commented Mar 18, 2024

Description:
I am experiencing an issue with the Drogon web framework where the MultiPartParser is not getting any uploaded files in my POST request handler. I have installed Drogon from the git repository on February 16, 2024, with the last pulled commit being 88d0668. I am running the code on Linux Ubuntu 22.04.2.

Here is a simplified version of my code:

void POSHttpController::set_picture(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
    MultiPartParser fileUpload;
    int parseResult = fileUpload.parse(req);
    if (parseResult != 0) {
        LOG_ERROR << "Failed to parse multipart request. Error code: " << parseResult;
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody("Failed to parse multipart request");
        resp->setStatusCode(k400BadRequest);
        callback(resp);
        return;
    }

    auto file_map = fileUpload.getFilesMap();
    LOG_DEBUG << "File map size: " << file_map.size(); // Always 0

    auto files = fileUpload.getFiles();
    if (files.size() != 1) {
        LOG_ERROR << "Invalid number of files. Expected 1, got " << files.size(); // Got 0
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody("Must only be one file");
        resp->setStatusCode(k403Forbidden);
        callback(resp);
        return;
    }

    // ...
}

The parse() function returns 0, indicating a successful parse, but both getFilesMap() and getFiles() return empty collections. I have verified that the request contains a file, but it seems that the MultiPartParser is not detecting it.
I should also specify that this same code does work with an older version of drogon that I have on another machine (even with the same file/request).
I would greatly appreciate any help or suggestions on how to resolve this issue. Please let me know if you need any additional information.

Thank you in advance!

@an-tao
Copy link
Member

an-tao commented Mar 19, 2024

Could you please share your client code so I can reproduce this issue locally?

@GladBarsan
Copy link
Author

I was just using Postman for testing.
image
And these are the headers
image
Most are auto calculated.

And yes, this exact request works fine on the older version, but the new one responds with "Must be only one file"

@an-tao
Copy link
Member

an-tao commented Mar 21, 2024

I did some tests with your controller code and postman, but I can't reproduce this issue, it works fine in my local environment.

@GladBarsan
Copy link
Author

After multiple tries (even reinstalling drogon) I managed to narrow it down.
It only happens for files larger than 64kb (when they are over the default client_max_memory_body_size). So it appears it only goes wrong when the request body is put in the temp file.
I tested this by removing the limit, and the files were detected. Hope this helps!

@an-tao
Copy link
Member

an-tao commented Mar 22, 2024

I works fine when I tried to upload a file of 300k bytes. Please check if your application has write permission to the temporary files folder.

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

No branches or pull requests

2 participants