Skip to content

Commit b2d1f25

Browse files
files for testing route specific error_pages
1 parent 0746a06 commit b2d1f25

File tree

5 files changed

+68
-53
lines changed

5 files changed

+68
-53
lines changed

config/vitepress.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ server {
88
error_page 403 /404.html;
99

1010
location / {
11+
error_page 404 /404_route_specific1.html;
12+
}
1113

14+
location /override/ {
15+
error_page 404 /404_route_specific2.html;
1216
}
17+
18+
location /nooverride/ {
19+
20+
}
1321
}

src/config/config_builder.cpp

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,61 @@
1313
#include <cstdlib>
1414
#include <stdexcept>
1515

16+
static bool file_readable(const std::string& path)
17+
{
18+
struct stat sb;
19+
return (stat(path.c_str(), &sb) == 0 && S_ISREG(sb.st_mode));
20+
}
21+
22+
static bool read_error_page(const std::string& path, std::string& error_page_content)
23+
{
24+
if (!file_readable(path)) {
25+
LOG(DEBUG) << "error page not readable : " << path;
26+
return false;
27+
}
28+
29+
int fd = 0;
30+
char buf[4096];
31+
ssize_t bytes = 1;
32+
fd = open(path.c_str(), O_RDONLY);
33+
if (fd == -1) {
34+
return false;
35+
}
36+
37+
while (bytes) {
38+
bytes = read(fd, buf, sizeof(buf));
39+
if (bytes == 0) {
40+
break;
41+
}
42+
if (bytes < 0) {
43+
close(fd);
44+
return false;
45+
}
46+
error_page_content.append(buf, bytes);
47+
}
48+
close(fd);
49+
return true;
50+
}
51+
52+
static void update_error_pages(SharedConfig& shared)
53+
{
54+
for (std::map<HttpResponse::Status, std::string>::iterator it = shared.error_pages.begin();
55+
it != shared.error_pages.end();
56+
++it) {
57+
HttpResponse::Status status = it->first;
58+
const std::string& url = it->second;
59+
std::string path = shared.document_root + url;
60+
std::string error_page = "";
61+
if (read_error_page(path, error_page)) {
62+
LOG(DEBUG) << "ERROR page loaded : " << static_cast<int>(status) << " " << url;
63+
it->second = error_page;
64+
}
65+
else {
66+
it->second = "";
67+
}
68+
}
69+
}
70+
1671
static void config_error(const std::string& msg, int line = -1)
1772
{
1873
if (line != -1) {
@@ -256,42 +311,6 @@ static void parse_client_max_body_size(const AstNode& node, SharedConfig& config
256311
config.max_body_size = size;
257312
}
258313

259-
static bool file_readable(const std::string& path)
260-
{
261-
struct stat sb;
262-
return (stat(path.c_str(), &sb) == 0 && S_ISREG(sb.st_mode));
263-
}
264-
265-
static bool read_error_page(const std::string& path, std::string& error_page_content)
266-
{
267-
if (!file_readable(path)) {
268-
LOG(DEBUG) << "error page not readable : " << path;
269-
return false;
270-
}
271-
272-
int fd = 0;
273-
char buf[4096];
274-
ssize_t bytes = 1;
275-
fd = open(path.c_str(), O_RDONLY);
276-
if (fd == -1) {
277-
return false;
278-
}
279-
280-
while (bytes) {
281-
bytes = read(fd, buf, sizeof(buf));
282-
if (bytes == 0) {
283-
break;
284-
}
285-
if (bytes < 0) {
286-
close(fd);
287-
return false;
288-
}
289-
error_page_content.append(buf, bytes);
290-
}
291-
close(fd);
292-
return true;
293-
}
294-
295314
static void parse_error_pages(const AstNode& node, SharedConfig& config)
296315
{
297316
expect_min_argc(node, 2);
@@ -426,23 +445,9 @@ RouteConfig ConfigBuilder::build_route_config(const AstNode& node, const SharedC
426445

427446
SharedConfig shared = build_shared_config(shared_nodes, &parent);
428447

429-
// loading the error pages
430-
for (std::map<HttpResponse::Status, std::string>::iterator it = shared.error_pages.begin();
431-
it != shared.error_pages.end();
432-
++it) {
433-
HttpResponse::Status status = it->first;
434-
const std::string& url = it->second;
435-
std::string path = shared.document_root + url;
436-
std::string error_page = "";
437-
if (read_error_page(path, error_page)) {
438-
LOG(DEBUG) << "ERROR page loaded : " << static_cast<int>(status) << " " << url;
439-
it->second = error_page;
440-
}
441-
else {
442-
it->second = "";
443-
}
444-
}
448+
update_error_pages(shared); // loading the error pages
445449

446450
route.shared = shared;
451+
447452
return route;
448453
}

src/router/router.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Handler* Router::handle_request(const HttpRequest& request)
123123
assert(!request.path.empty() && "Request URI can never be empty in the router");
124124

125125
const RouteConfig& best_route = find_best_route(request.path);
126-
126+
LOG(DEBUG) << "best_route.path = " << best_route.path;
127127
if (!best_route.shared.redirect.url.empty()) {
128128
return new RedirectHandler(best_route, request);
129129
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>ROUTE1 404</h1>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>ROUTE2 404</h1>

0 commit comments

Comments
 (0)