Fix #431
This commit is contained in:
parent
8674555b88
commit
c2b6e4ac04
16
httplib.h
16
httplib.h
@ -312,7 +312,7 @@ struct Response {
|
|||||||
void set_header(const char *key, const char *val);
|
void set_header(const char *key, const char *val);
|
||||||
void set_header(const char *key, const std::string &val);
|
void set_header(const char *key, const std::string &val);
|
||||||
|
|
||||||
void set_redirect(const char *url);
|
void set_redirect(const char *url, int status = 302);
|
||||||
void set_content(const char *s, size_t n, const char *content_type);
|
void set_content(const char *s, size_t n, const char *content_type);
|
||||||
void set_content(std::string s, const char *content_type);
|
void set_content(std::string s, const char *content_type);
|
||||||
|
|
||||||
@ -2048,6 +2048,12 @@ inline bool redirect(T &cli, const Request &req, Response &res,
|
|||||||
new_req.path = path;
|
new_req.path = path;
|
||||||
new_req.redirect_count -= 1;
|
new_req.redirect_count -= 1;
|
||||||
|
|
||||||
|
if (res.status == 303 && (req.method != "GET" && req.method != "HEAD")) {
|
||||||
|
new_req.method = "GET";
|
||||||
|
new_req.body.clear();
|
||||||
|
new_req.headers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
Response new_res;
|
Response new_res;
|
||||||
|
|
||||||
auto ret = cli.send(new_req, new_res);
|
auto ret = cli.send(new_req, new_res);
|
||||||
@ -2790,10 +2796,14 @@ inline void Response::set_header(const char *key, const std::string &val) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Response::set_redirect(const char *url) {
|
inline void Response::set_redirect(const char *url, int status) {
|
||||||
if (!detail::has_crlf(url)) {
|
if (!detail::has_crlf(url)) {
|
||||||
set_header("Location", url);
|
set_header("Location", url);
|
||||||
status = 302;
|
if (300 <= status && status < 400) {
|
||||||
|
this->status = status;
|
||||||
|
} else {
|
||||||
|
this->status = 302;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
32
test/test.cc
32
test/test.cc
@ -749,6 +749,13 @@ protected:
|
|||||||
})
|
})
|
||||||
.Get("/", [&](const Request & /*req*/,
|
.Get("/", [&](const Request & /*req*/,
|
||||||
Response &res) { res.set_redirect("/hi"); })
|
Response &res) { res.set_redirect("/hi"); })
|
||||||
|
.Post("/1", [](const Request & /*req*/,
|
||||||
|
Response &res) { res.set_redirect("/2", 303); })
|
||||||
|
.Get("/2",
|
||||||
|
[](const Request & /*req*/, Response &res) {
|
||||||
|
res.set_content("redirected.", "text/plain");
|
||||||
|
res.status = 200;
|
||||||
|
})
|
||||||
.Post("/person",
|
.Post("/person",
|
||||||
[&](const Request &req, Response &res) {
|
[&](const Request &req, Response &res) {
|
||||||
if (req.has_param("name") && req.has_param("note")) {
|
if (req.has_param("name") && req.has_param("note")) {
|
||||||
@ -1116,6 +1123,14 @@ TEST_F(ServerTest, GetMethod302) {
|
|||||||
EXPECT_EQ("/hi", res->get_header_value("Location"));
|
EXPECT_EQ("/hi", res->get_header_value("Location"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, GetMethod302Redirect) {
|
||||||
|
cli_.set_follow_location(true);
|
||||||
|
auto res = cli_.Get("/");
|
||||||
|
ASSERT_TRUE(res != nullptr);
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
EXPECT_EQ("Hello World!", res->body);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetMethod404) {
|
TEST_F(ServerTest, GetMethod404) {
|
||||||
auto res = cli_.Get("/invalid");
|
auto res = cli_.Get("/invalid");
|
||||||
ASSERT_TRUE(res != nullptr);
|
ASSERT_TRUE(res != nullptr);
|
||||||
@ -1315,6 +1330,21 @@ TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) {
|
|||||||
EXPECT_EQ(404, res->status);
|
EXPECT_EQ(404, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, PostMethod303) {
|
||||||
|
auto res = cli_.Post("/1", "body", "text/plain");
|
||||||
|
ASSERT_TRUE(res != nullptr);
|
||||||
|
EXPECT_EQ(303, res->status);
|
||||||
|
EXPECT_EQ("/2", res->get_header_value("Location"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, PostMethod303Redirect) {
|
||||||
|
cli_.set_follow_location(true);
|
||||||
|
auto res = cli_.Post("/1", "body", "text/plain");
|
||||||
|
ASSERT_TRUE(res != nullptr);
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
EXPECT_EQ("redirected.", res->body);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
|
TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
|
||||||
auto res = cli_.Get("/dir/test.abcde");
|
auto res = cli_.Get("/dir/test.abcde");
|
||||||
ASSERT_TRUE(res != nullptr);
|
ASSERT_TRUE(res != nullptr);
|
||||||
@ -2143,7 +2173,7 @@ TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
|
|||||||
"GET /hi HTTP/1.1\r\n"
|
"GET /hi HTTP/1.1\r\n"
|
||||||
" : "
|
" : "
|
||||||
" "
|
" "
|
||||||
);
|
" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity2) {
|
TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity2) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user