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

request with multipart/form-data is parsed only when its size is very small #607

Closed
shk755 opened this issue Mar 23, 2023 · 4 comments
Closed

Comments

@shk755
Copy link

shk755 commented Mar 23, 2023

Hello,

I am making a file uploading server. the simplified code is like this.

CROW_ROUTE(app, "/upload").methods(crow::HTTPMethod::POST)([&](const crow::request &req){
    crow::multipart::message msg(req);
    std::string filecont = msg.get_part_by_name("file").body;
    std::string userid = msg.get_part_by_name("user_id").body;
    CROW_LOG_DEBUG << filecont;
    CROW_LOG_DEBUG << userid;
    crow::json::wvalue x({{"message", "success"}});
    return x; 
});

When the Content-length exceeds 1024, http parser raises an error "invalid HTTP method". I tested with varying sizes and file types but it always stops to work when the content-length is 1025 or over

  • server log on error
(2023-03-23 07:25:50) [DEBUG   ] 0x564305ed41b0 {1} queue length: 1
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 timer cancelled: 0x7f62837fd690 0
(2023-03-23 07:25:50) [DEBUG   ] task_timer cancelled: 0x7f62837fd690 0
(2023-03-23 07:25:50) [DEBUG   ] task_timer scheduled: 0x7f62837fd690 1
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 timer added: 0x7f62837fd690 1
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 timer cancelled: 0x7f62837fd690 1
(2023-03-23 07:25:50) [DEBUG   ] task_timer cancelled: 0x7f62837fd690 1
(2023-03-23 07:25:50) [DEBUG   ] task_timer scheduled: 0x7f62837fd690 2
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 timer added: 0x7f62837fd690 2
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 timer cancelled: 0x7f62837fd690 2
(2023-03-23 07:25:50) [DEBUG   ] task_timer cancelled: 0x7f62837fd690 2
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 from read(1) with description: "invalid HTTP method"
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 is_reading 0 is_writing 0
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 delete (idle) (queue length: 0)
(2023-03-23 07:25:50) [DEBUG   ] 0x7f61b4002550 timer cancelled: 0x7f62837fd690 2
(2023-03-23 07:25:50) [DEBUG   ] task_timer cancelled: 0x7f62837fd690 2

What should I do to make it work with bigger files?
Thanks

@kiner-shah
Copy link
Contributor

Very strange, I don't get this error when I try to upload a file with Content-Length 2593701.
This is my code:

CROW_ROUTE(app, "/")
   .methods(crow::HTTPMethod::Post)
([](const crow::request& req)
{
    for (auto [headerKey, headerValue] : req.headers)
        std::cout << headerKey << ' ' << headerValue << '\n';
    // std::cout << req.body << '\n';

    crow::multipart::message file_message(req);
    std::ofstream out_file("input_file.txt");
    for (auto& part : file_message.parts)
        out_file << part.body;
        out_file.close();
    return crow::response(200);
});

@shk755
Copy link
Author

shk755 commented Apr 5, 2023

Strange indeed... it must be a silly mistake but I cannot find it. I copied your code and it does not work for me either. maybe I included libraries in a bad way since i'm using standalone ASIO and manually including Crow, but the code works just fine other than this specific case. Thanks for checking it out @kiner-shah

@kiner-shah
Copy link
Contributor

Similar to #558 #626

@lkoeller
Copy link

Probably curl inserts a HTTP header "Expect: 100-continue" when uploaded file is greater than some threshold.

I have a similar issue when POSTING json data with curl :

curl -k -v --header "Content-Type: application/json" --header "Transfer-Encoding: chunked" --header "Accept: application/json" --header "Expect: 100-continue" -X POST https://myserver/api/xxx -d @data.json

By forcing Expect:100-continue, I have the same issue : from read(1) with description: "invalid HTTP method"

When I remove the header, and send a small file, it works !

curl -k -v --header "Content-Type: application/json" --header "Transfer-Encoding: chunked" --header "Accept: application/json" -X POST https://myserver/api/xxx -d "{}"

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

3 participants