* @brief Get an asset from the node_modules directory. * @param response The HTTP response object. * @param request The HTTP request object. */
| 526 | * @param request The HTTP request object. |
| 527 | */ |
| 528 | void getNodeModules(resp_https_t response, req_https_t request) { |
| 529 | print_req(request); |
| 530 | |
| 531 | fs::path webDirPath(WEB_DIR); |
| 532 | fs::path nodeModulesPath(webDirPath / "assets"); |
| 533 | |
| 534 | // .relative_path is needed to shed any leading slash that might exist in the request path |
| 535 | auto filePath = fs::weakly_canonical(webDirPath / fs::path(request->path).relative_path()); |
| 536 | |
| 537 | // Don't do anything if file does not exist or is outside the assets directory |
| 538 | if (!isChildPath(filePath, nodeModulesPath)) { |
| 539 | BOOST_LOG(warning) << "Someone requested a path " << filePath << " that is outside the assets folder"; |
| 540 | bad_request(response, request); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | if (!fs::exists(filePath)) { |
| 545 | not_found(response, request); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | auto relPath = fs::relative(filePath, webDirPath); |
| 550 | // get the mime type from the file extension mime_types map |
| 551 | // remove the leading period from the extension |
| 552 | auto mimeType = mime_types.find(relPath.extension().string().substr(1)); |
| 553 | if (mimeType == mime_types.end()) { |
| 554 | bad_request(response, request); |
| 555 | return; |
| 556 | } |
| 557 | SimpleWeb::CaseInsensitiveMultimap headers; |
| 558 | headers.emplace("Content-Type", mimeType->second); |
| 559 | headers.emplace("X-Frame-Options", "DENY"); |
| 560 | headers.emplace("Content-Security-Policy", "frame-ancestors 'none';"); |
| 561 | std::ifstream in(filePath.string(), std::ios::binary); |
| 562 | response->write(SimpleWeb::StatusCode::success_ok, in, headers); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * @brief Get the list of available applications. |
nothing calls this directly
no test coverage detected