MCPcopy Create free account
hub / github.com/IppClub/Dora-SSR / start

Method start

Source/Http/HttpServer.cpp:700–941  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

698}
699
700bool 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);

Callers

nothing calls this directly

Calls 15

is_valid_pathFunction · 0.85
find_content_typeFunction · 0.85
moveFunction · 0.85
set_unauthorizedFunction · 0.85
SDL_RWFromFileFunction · 0.85
SDL_RWcloseFunction · 0.85
SDL_RWwriteFunction · 0.85
LogErrorFunction · 0.85
set_default_headersMethod · 0.80
set_headerMethod · 0.80
bind_to_portMethod · 0.80
invokeInLogicMethod · 0.80

Tested by

no test coverage detected