Skip to content

Commit 042776f

Browse files
paulharrisgittiver
authored andcommitted
Redirect to url with trailing slash, WITHOUT the protocol and host.
Crow does not know the correct protocol when behind a reverse proxy. So instead, simply redirect to the same url and add a trailing slash. Note, it is now valid for the "Location" header field to be relative. From wikipedia: An obsolete version of the HTTP 1.1 specifications (IETF RFC 2616) required a complete absolute URI for redirection. The IETF HTTP working group found that the most popular web browsers tolerate the passing of a relative URL[3] and, consequently, the updated HTTP 1.1 specifications (IETF RFC 7231) relaxed the original constraint, allowing the use of relative URLs in Location headers.
1 parent 6df4f4c commit 042776f

File tree

2 files changed

+2
-32
lines changed

2 files changed

+2
-32
lines changed

include/crow/http_connection.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,6 @@ namespace crow
258258
}
259259
}
260260
#endif
261-
//if there is a redirection with a partial URL, treat the URL as a route.
262-
std::string location = res.get_header_value("Location");
263-
if (!location.empty() && location.find("://", 0) == std::string::npos)
264-
{
265-
#ifdef CROW_ENABLE_SSL
266-
if (handler_->ssl_used())
267-
location.insert(0, "https://" + req_.get_header_value("Host"));
268-
else
269-
#endif
270-
location.insert(0, "http://" + req_.get_header_value("Host"));
271-
res.set_header("location", location);
272-
}
273261

274262
prepare_buffers();
275263

include/crow/routing.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,16 +1462,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
14621462
{
14631463
CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url;
14641464
res = response(301);
1465-
1466-
// TODO(ipkn) absolute url building
1467-
if (req.get_header_value("Host").empty())
1468-
{
1469-
res.add_header("Location", req.url + "/");
1470-
}
1471-
else
1472-
{
1473-
res.add_header("Location", (using_ssl ? "https://" : "http://") + req.get_header_value("Host") + req.url + "/");
1474-
}
1465+
res.add_header("Location", req.url + "/");
14751466
res.end();
14761467
return;
14771468
}
@@ -1710,16 +1701,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
17101701
{
17111702
CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url;
17121703
res = response(301);
1713-
1714-
// TODO(ipkn) absolute url building
1715-
if (req.get_header_value("Host").empty())
1716-
{
1717-
res.add_header("Location", req.url + "/");
1718-
}
1719-
else
1720-
{
1721-
res.add_header("Location", (using_ssl ? "https://" : "http://") + req.get_header_value("Host") + req.url + "/");
1722-
}
1704+
res.add_header("Location", req.url + "/");
17231705
res.end();
17241706
return;
17251707
}

0 commit comments

Comments
 (0)