mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 06:26:02 -07:00
Fix #765
This commit is contained in:
parent
c2afc5ca44
commit
a5c239c174
2 changed files with 16 additions and 5 deletions
15
httplib.h
15
httplib.h
|
@ -428,6 +428,7 @@ struct Response {
|
|||
std::string reason;
|
||||
Headers headers;
|
||||
std::string body;
|
||||
std::string location; // Redirect location
|
||||
|
||||
bool has_header(const char *key) 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>
|
||||
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;
|
||||
new_req.path = path;
|
||||
new_req.redirect_count_ -= 1;
|
||||
|
@ -2968,7 +2970,10 @@ inline bool redirect(T &cli, const Request &req, Response &res,
|
|||
Response 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;
|
||||
}
|
||||
|
||||
|
@ -5027,13 +5032,13 @@ inline bool ClientImpl::redirect(const Request &req, Response &res) {
|
|||
if (next_path.empty()) { next_path = "/"; }
|
||||
|
||||
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 {
|
||||
if (next_scheme == "https") {
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
SSLClient cli(next_host.c_str(), next_port);
|
||||
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(); }
|
||||
return ret;
|
||||
#else
|
||||
|
@ -5042,7 +5047,7 @@ inline bool ClientImpl::redirect(const Request &req, Response &res) {
|
|||
} else {
|
||||
ClientImpl cli(next_host.c_str(), next_port);
|
||||
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(); }
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -778,6 +778,7 @@ TEST(YahooRedirectTest, Redirect) {
|
|||
res = cli.Get("/");
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("https://yahoo.com/", res->location);
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
@ -1434,6 +1435,7 @@ TEST_F(ServerTest, GetMethod302Redirect) {
|
|||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("Hello World!", res->body);
|
||||
EXPECT_EQ("/hi", res->location);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetMethod404) {
|
||||
|
@ -1662,6 +1664,7 @@ TEST_F(ServerTest, PostMethod303Redirect) {
|
|||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("redirected.", res->body);
|
||||
EXPECT_EQ("/2", res->location);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
|
||||
|
@ -3638,6 +3641,7 @@ TEST(YahooRedirectTest2, SimpleInterface) {
|
|||
res = cli.Get("/");
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("https://yahoo.com/", res->location);
|
||||
}
|
||||
|
||||
TEST(YahooRedirectTest3, SimpleInterface) {
|
||||
|
@ -3651,6 +3655,7 @@ TEST(YahooRedirectTest3, SimpleInterface) {
|
|||
res = cli.Get("/");
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("https://www.yahoo.com/", res->location);
|
||||
}
|
||||
|
||||
TEST(YahooRedirectTest3, NewResultInterface) {
|
||||
|
@ -3674,6 +3679,7 @@ TEST(YahooRedirectTest3, NewResultInterface) {
|
|||
EXPECT_EQ(200, res.value().status);
|
||||
EXPECT_EQ(200, (*res).status);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("https://www.yahoo.com/", res->location);
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
|
||||
|
|
Loading…
Reference in a new issue