websocketpp::http::status_code::value FileServer::handleRequest(const websocketpp::http::parser::request& aRequest, string& output_, StringPairList& headers_, const SessionPtr& aSession) noexcept { if (aRequest.get_method() == "GET") { return handleGetRequest(aRequest, output_, headers_, aSession); } else if (aRequest.get_method() == "POST") { return handlePostRequest(aRequest, output_, headers_, aSession); } output_ = "Requested resource was not found"; return websocketpp::http::status_code::not_found; }
websocketpp::http::status_code::value ApiRouter::handleHttpRequest(const string& aRequestPath, const websocketpp::http::parser::request& aRequest, json& output_, json& error_, bool aIsSecure, const string& aIp) noexcept { SessionPtr session = nullptr; auto token = aRequest.get_header("Authorization"); if (token != websocketpp::http::empty_header) { session = WebServerManager::getInstance()->getUserManager().getSession(token); } auto& requestBody = aRequest.get_body(); dcdebug("Received HTTP request: %s\n", aRequest.get_body().c_str()); try { ApiRequest apiRequest(aRequestPath, aRequest.get_method(), output_, error_); apiRequest.validate(); apiRequest.parseHttpRequestJson(requestBody); apiRequest.setSession(session); return handleRequest(apiRequest, aIsSecure, nullptr, aIp); } catch (const std::exception& e) { error_ = { "message", "Parsing failed: " + string(e.what()) }; return websocketpp::http::status_code::bad_request; } return websocketpp::http::status_code::ok; }