yhirose 5 years ago
parent
commit
a5c239c174
2 changed files with 16 additions and 5 deletions
  1. 10 5
      httplib.h
  2. 6 0
      test/test.cc

+ 10 - 5
httplib.h

@@ -428,6 +428,7 @@ struct Response {
   std::string reason;
   std::string reason;
   Headers headers;
   Headers headers;
   std::string body;
   std::string body;
+  std::string location; // Redirect location
 
 
   bool has_header(const char *key) const;
   bool has_header(const char *key) const;
   std::string get_header_value(const char *key, size_t id = 0) const;
   std::string get_header_value(const char *key, size_t id = 0) const;
@@ -2954,7 +2955,8 @@ inline bool write_content_chunked(Stream &strm,
 
 
 template <typename T>
 template <typename T>
 inline bool redirect(T &cli, const Request &req, Response &res,
 inline bool redirect(T &cli, const Request &req, Response &res,
-                     const std::string &path) {
+                     const std::string &path,
+                     const std::string &location) {
   Request new_req = req;
   Request new_req = req;
   new_req.path = path;
   new_req.path = path;
   new_req.redirect_count_ -= 1;
   new_req.redirect_count_ -= 1;
@@ -2968,7 +2970,10 @@ inline bool redirect(T &cli, const Request &req, Response &res,
   Response new_res;
   Response new_res;
 
 
   auto ret = cli.send(new_req, new_res);
   auto ret = cli.send(new_req, new_res);
-  if (ret) { res = new_res; }
+  if (ret) {
+    new_res.location = location;
+    res = new_res;
+  }
   return ret;
   return ret;
 }
 }
 
 
@@ -5027,13 +5032,13 @@ inline bool ClientImpl::redirect(const Request &req, Response &res) {
   if (next_path.empty()) { next_path = "/"; }
   if (next_path.empty()) { next_path = "/"; }
 
 
   if (next_scheme == scheme && next_host == host_ && next_port == port_) {
   if (next_scheme == scheme && next_host == host_ && next_port == port_) {
-    return detail::redirect(*this, req, res, next_path);
+    return detail::redirect(*this, req, res, next_path, location);
   } else {
   } else {
     if (next_scheme == "https") {
     if (next_scheme == "https") {
 #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
 #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
       SSLClient cli(next_host.c_str(), next_port);
       SSLClient cli(next_host.c_str(), next_port);
       cli.copy_settings(*this);
       cli.copy_settings(*this);
-      auto ret = detail::redirect(cli, req, res, next_path);
+      auto ret = detail::redirect(cli, req, res, next_path, location);
       if (!ret) { error_ = cli.get_last_error(); }
       if (!ret) { error_ = cli.get_last_error(); }
       return ret;
       return ret;
 #else
 #else
@@ -5042,7 +5047,7 @@ inline bool ClientImpl::redirect(const Request &req, Response &res) {
     } else {
     } else {
       ClientImpl cli(next_host.c_str(), next_port);
       ClientImpl cli(next_host.c_str(), next_port);
       cli.copy_settings(*this);
       cli.copy_settings(*this);
-      auto ret = detail::redirect(cli, req, res, next_path);
+      auto ret = detail::redirect(cli, req, res, next_path, location);
       if (!ret) { error_ = cli.get_last_error(); }
       if (!ret) { error_ = cli.get_last_error(); }
       return ret;
       return ret;
     }
     }

+ 6 - 0
test/test.cc

@@ -778,6 +778,7 @@ TEST(YahooRedirectTest, Redirect) {
   res = cli.Get("/");
   res = cli.Get("/");
   ASSERT_TRUE(res);
   ASSERT_TRUE(res);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ(200, res->status);
+  EXPECT_EQ("https://yahoo.com/", res->location);
 }
 }
 
 
 #if 0
 #if 0
@@ -1434,6 +1435,7 @@ TEST_F(ServerTest, GetMethod302Redirect) {
   ASSERT_TRUE(res);
   ASSERT_TRUE(res);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ("Hello World!", res->body);
   EXPECT_EQ("Hello World!", res->body);
+  EXPECT_EQ("/hi", res->location);
 }
 }
 
 
 TEST_F(ServerTest, GetMethod404) {
 TEST_F(ServerTest, GetMethod404) {
@@ -1662,6 +1664,7 @@ TEST_F(ServerTest, PostMethod303Redirect) {
   ASSERT_TRUE(res);
   ASSERT_TRUE(res);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ("redirected.", res->body);
   EXPECT_EQ("redirected.", res->body);
+  EXPECT_EQ("/2", res->location);
 }
 }
 
 
 TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
 TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
@@ -3638,6 +3641,7 @@ TEST(YahooRedirectTest2, SimpleInterface) {
   res = cli.Get("/");
   res = cli.Get("/");
   ASSERT_TRUE(res);
   ASSERT_TRUE(res);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ(200, res->status);
+  EXPECT_EQ("https://yahoo.com/", res->location);
 }
 }
 
 
 TEST(YahooRedirectTest3, SimpleInterface) {
 TEST(YahooRedirectTest3, SimpleInterface) {
@@ -3651,6 +3655,7 @@ TEST(YahooRedirectTest3, SimpleInterface) {
   res = cli.Get("/");
   res = cli.Get("/");
   ASSERT_TRUE(res);
   ASSERT_TRUE(res);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ(200, res->status);
+  EXPECT_EQ("https://www.yahoo.com/", res->location);
 }
 }
 
 
 TEST(YahooRedirectTest3, NewResultInterface) {
 TEST(YahooRedirectTest3, NewResultInterface) {
@@ -3674,6 +3679,7 @@ TEST(YahooRedirectTest3, NewResultInterface) {
   EXPECT_EQ(200, res.value().status);
   EXPECT_EQ(200, res.value().status);
   EXPECT_EQ(200, (*res).status);
   EXPECT_EQ(200, (*res).status);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ(200, res->status);
+  EXPECT_EQ("https://www.yahoo.com/", res->location);
 }
 }
 
 
 #ifdef CPPHTTPLIB_BROTLI_SUPPORT
 #ifdef CPPHTTPLIB_BROTLI_SUPPORT