|
13 | 13 | #include <cstdlib> |
14 | 14 | #include <stdexcept> |
15 | 15 |
|
| 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 | + |
16 | 71 | static void config_error(const std::string& msg, int line = -1) |
17 | 72 | { |
18 | 73 | if (line != -1) { |
@@ -256,42 +311,6 @@ static void parse_client_max_body_size(const AstNode& node, SharedConfig& config |
256 | 311 | config.max_body_size = size; |
257 | 312 | } |
258 | 313 |
|
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 | | - |
295 | 314 | static void parse_error_pages(const AstNode& node, SharedConfig& config) |
296 | 315 | { |
297 | 316 | expect_min_argc(node, 2); |
@@ -426,23 +445,9 @@ RouteConfig ConfigBuilder::build_route_config(const AstNode& node, const SharedC |
426 | 445 |
|
427 | 446 | SharedConfig shared = build_shared_config(shared_nodes, &parent); |
428 | 447 |
|
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 |
445 | 449 |
|
446 | 450 | route.shared = shared; |
| 451 | + |
447 | 452 | return route; |
448 | 453 | } |
0 commit comments