yhirose 4 years ago
parent
commit
9d3365df54
2 changed files with 61 additions and 0 deletions
  1. 3 0
      httplib.h
  2. 58 0
      test/test.cc

+ 3 - 0
httplib.h

@@ -5820,6 +5820,9 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
         req.content_receiver
             ? static_cast<ContentReceiverWithProgress>(
                   [&](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;

+ 58 - 0
test/test.cc

@@ -889,6 +889,64 @@ TEST(UrlWithSpace, Redirect) {
   EXPECT_EQ(200, res->status);
   EXPECT_EQ(18527, res->get_header_value<uint64_t>("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) {