Skip to content

Conversation

@zpalmtree
Copy link

If we have an empty body, the content length doesn't get set. This can cause hangs in some clients, for example, curl. A simple program to illustrate:

#include "httplib.h"

using namespace httplib;

int main()
{
    Server svr;

    svr.Get("/", [](const Request &req, Response &res)
    {
        res.status = 200;
    });

    svr.listen("127.0.0.1", 1234);
}

If we then perform curl http://127.0.0.1:1234 -v - We can see:

< HTTP/1.1 200 OK
* no chunk, no close, no size. Assume close to signal end
< 
* Closing connection 0

On my machine, this hangs for approximately 3 or so seconds.
If you then apply the patch in my PR, and try again, it should complete instantly:

< HTTP/1.1 200 OK
< Content-Length: 0
< 
* Connection #0 to host 127.0.0.1 left intact

Environment: Arch Linux 4.19.1, Curl 7.62.0

@yhirose yhirose closed this in 5ad4311 Nov 23, 2018
@yhirose
Copy link
Owner

yhirose commented Nov 23, 2018

@zpalmtree, thanks for your contribution! But it breaks the following unit test...

[ RUN      ] ServerTest.GetStreamedChunked
test.cc:836: Failure
Value of: res->body == "aaabbb"
  Actual: false
Expected: true

Could you try this new version? 5ad4311
It should fix your problem and pass all the unit test cases.

Thank you!

@zpalmtree
Copy link
Author

zpalmtree commented Nov 23, 2018

My bad, I should have run the tests. Your fix works perfectly, thank you!

Oh - and thanks for great library - It's exactly what I was looking for - I took a while looking through all the libs, and they all seemed to heavy on dependencies, or very verbose - this is perfect ❤️

ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this pull request May 2, 2023
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.

2 participants