Skip to content

Commit 9d3365d

Browse files
committed
Fix #889
1 parent 6ff84d3 commit 9d3365d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

httplib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5820,6 +5820,9 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
58205820
req.content_receiver
58215821
? static_cast<ContentReceiverWithProgress>(
58225822
[&](const char *buf, size_t n, uint64_t off, uint64_t len) {
5823+
if (300 < res.status && res.status < 400 && follow_location_) {
5824+
return true;
5825+
}
58235826
auto ret = req.content_receiver(buf, n, off, len);
58245827
if (!ret) { error = Error::Canceled; }
58255828
return ret;

test/test.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,64 @@ TEST(UrlWithSpace, Redirect) {
889889
EXPECT_EQ(200, res->status);
890890
EXPECT_EQ(18527, res->get_header_value<uint64_t>("Content-Length"));
891891
}
892+
893+
TEST(RedirectFromPageWithContent, Redirect) {
894+
Server svr;
895+
896+
svr.Get("/1", [&](const Request & /*req*/, Response &res) {
897+
res.set_content("___", "text/plain");
898+
res.set_redirect("/2");
899+
});
900+
901+
svr.Get("/2", [&](const Request & /*req*/, Response &res) {
902+
res.set_content("Hello World!", "text/plain");
903+
});
904+
905+
auto th = std::thread([&]() { svr.listen("localhost", PORT); });
906+
907+
while (!svr.is_running()) {
908+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
909+
}
910+
911+
// Give GET time to get a few messages.
912+
std::this_thread::sleep_for(std::chrono::seconds(1));
913+
914+
{
915+
Client cli("localhost", PORT);
916+
cli.set_follow_location(true);
917+
918+
std::string body;
919+
auto res = cli.Get("/1",
920+
[&](const char *data, size_t data_length) {
921+
body.append(data, data_length);
922+
return true;
923+
});
924+
925+
ASSERT_TRUE(res);
926+
EXPECT_EQ(200, res->status);
927+
EXPECT_EQ("Hello World!", body);
928+
}
929+
930+
{
931+
Client cli("localhost", PORT);
932+
933+
std::string body;
934+
auto res = cli.Get("/1",
935+
[&](const char *data, size_t data_length) {
936+
body.append(data, data_length);
937+
return true;
938+
});
939+
940+
ASSERT_TRUE(res);
941+
EXPECT_EQ(302, res->status);
942+
EXPECT_EQ("___", body);
943+
}
944+
945+
svr.stop();
946+
th.join();
947+
ASSERT_FALSE(svr.is_running());
948+
}
949+
892950
#endif
893951

894952
TEST(BindServerTest, BindDualStack) {

0 commit comments

Comments
 (0)