From 9d3365df54fabd84d25c032eb9e64d3585700b59 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 5 Apr 2021 11:09:08 -0400 Subject: [PATCH] Fix #889 --- httplib.h | 3 +++ test/test.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/httplib.h b/httplib.h index 9209f6c..06169ac 100644 --- a/httplib.h +++ b/httplib.h @@ -5820,6 +5820,9 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, req.content_receiver ? static_cast( [&](const char *buf, size_t n, uint64_t off, uint64_t len) { + if (300 < res.status && res.status < 400 && follow_location_) { + return true; + } auto ret = req.content_receiver(buf, n, off, len); if (!ret) { error = Error::Canceled; } return ret; diff --git a/test/test.cc b/test/test.cc index d7e5021..804f555 100644 --- a/test/test.cc +++ b/test/test.cc @@ -889,6 +889,64 @@ TEST(UrlWithSpace, Redirect) { EXPECT_EQ(200, res->status); EXPECT_EQ(18527, res->get_header_value("Content-Length")); } + +TEST(RedirectFromPageWithContent, Redirect) { + Server svr; + + svr.Get("/1", [&](const Request & /*req*/, Response &res) { + res.set_content("___", "text/plain"); + res.set_redirect("/2"); + }); + + svr.Get("/2", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + auto th = std::thread([&]() { svr.listen("localhost", PORT); }); + + while (!svr.is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + // Give GET time to get a few messages. + std::this_thread::sleep_for(std::chrono::seconds(1)); + + { + Client cli("localhost", PORT); + cli.set_follow_location(true); + + std::string body; + auto res = cli.Get("/1", + [&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_EQ("Hello World!", body); + } + + { + Client cli("localhost", PORT); + + std::string body; + auto res = cli.Get("/1", + [&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(302, res->status); + EXPECT_EQ("___", body); + } + + svr.stop(); + th.join(); + ASSERT_FALSE(svr.is_running()); +} + #endif TEST(BindServerTest, BindDualStack) {