| 698 | } |
| 699 | |
| 700 | bool HttpServer::start(int port) { |
| 701 | auto& server = getServer(); |
| 702 | if (server.is_running()) return false; |
| 703 | server.set_default_headers({{"Access-Control-Allow-Origin"s, "*"s}, |
| 704 | {"Access-Control-Allow-Headers"s, "*"s}}); |
| 705 | server.set_file_request_handler([this](const httplib::Request& req, httplib::Response& res) { |
| 706 | std::string path = req.path; |
| 707 | if (!httplib::detail::is_valid_path(path)) { |
| 708 | return false; |
| 709 | } |
| 710 | if (path.back() == '/') { |
| 711 | path += "index.html"; |
| 712 | } |
| 713 | if (path.size() > 0 && path[0] == '/') { |
| 714 | path.erase(path.begin()); |
| 715 | } |
| 716 | if (!SharedContent.exist(path)) { |
| 717 | bool found = false; |
| 718 | if (!_wwwPath.empty()) { |
| 719 | auto checkPath = Path::concat({_wwwPath, path}); |
| 720 | if (SharedContent.exist(checkPath)) { |
| 721 | path = checkPath; |
| 722 | found = true; |
| 723 | } |
| 724 | } |
| 725 | if (!found) { |
| 726 | auto checkPath = Path::concat({SharedContent.getWritablePath(), path}); |
| 727 | if (SharedContent.exist(checkPath)) { |
| 728 | path = checkPath; |
| 729 | found = true; |
| 730 | } |
| 731 | } |
| 732 | if (!found) { |
| 733 | return false; |
| 734 | } |
| 735 | } |
| 736 | auto content_type = httplib::detail::find_content_type(path, {}, "application/octet-stream"s); |
| 737 | bx::Semaphore waitForLoaded; |
| 738 | std::string result; |
| 739 | SharedContent.getThread()->run([&]() { |
| 740 | result = SharedContent.loadUnsafe(path); |
| 741 | waitForLoaded.post(); |
| 742 | }); |
| 743 | waitForLoaded.wait(); |
| 744 | if (!result.empty()) { |
| 745 | res.set_header("Content-Type", content_type); |
| 746 | res.body = std::move(result); |
| 747 | return true; |
| 748 | } |
| 749 | return false; |
| 750 | }); |
| 751 | server.Options(".*", [](const httplib::Request& req, httplib::Response& res) { }); |
| 752 | bool success = server.bind_to_port("0.0.0.0", port); |
| 753 | if (success) { |
| 754 | for (const auto& get : _gets) { |
| 755 | server.Get(get.pattern, [&get](const httplib::Request& req, httplib::Response& res) { |
| 756 | HttpServer::Request request; |
| 757 | request.headers.reserve(req.headers.size() * 2); |
nothing calls this directly
no test coverage detected