diff --git a/API/WebAPI.cpp b/API/WebAPI.cpp index 579c753..45f9a34 100644 --- a/API/WebAPI.cpp +++ b/API/WebAPI.cpp @@ -27,14 +27,25 @@ WebAPI::~WebAPI() { } -std::string WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData) +Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData) { - std::string output; + if (!StringAlgorithm::iequals(uri, "/api") && !StringAlgorithm::istarts_with(uri, "/api/")) + { + Http::HttpServer::HttpReply reply; + reply.status = Http::HttpServer::HttpReply::Status::Unauthorized; + return reply; + } - std::vector uriTokens = StringAlgorithm::split(uri, '/'); + Http::HttpServer::HttpReply reply; + reply.status = Http::HttpServer::HttpReply::Status::InternalServerError; + reply.content = "{\"result\": \"error\"}\r\n"; + + std::string apiURI = uri.substr(4); + + std::vector uriTokens = StringAlgorithm::split(apiURI, '/'); if (uriTokens.size() < 7) - return std::string(); + return reply; std::string action = uriTokens[1]; std::string brand = uriTokens[2]; @@ -50,58 +61,59 @@ std::string WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClien if (uriTokens.size() > 7) numberOfImages = stoi(uriTokens[7]); + reply.status = Http::HttpServer::HttpReply::Status::Ok; if (StringAlgorithm::iequals("Digoo", brand)) { auto recorder = Recorder::DigooRecorder(pHttpClient, settings); if (StringAlgorithm::iequals("Snapshot", action)) - output = recorder.Snapshot(pThreadPool); + reply.content = recorder.Snapshot(pThreadPool); else if (StringAlgorithm::iequals("MultiSnapshot", action)) - output = recorder.MultiSnapshot(pThreadPool, numberOfImages); + reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages); else if (StringAlgorithm::iequals("Video", action)) - output = recorder.Video(pThreadPool, ffmpeg); + reply.content = recorder.Video(pThreadPool, ffmpeg); } else if (StringAlgorithm::iequals("Foscam", brand)) { auto recorder = Recorder::FoscamRecorder(pHttpClient, settings); if (StringAlgorithm::iequals("Snapshot", action)) - output = recorder.Snapshot(pThreadPool); + reply.content = recorder.Snapshot(pThreadPool); else if (StringAlgorithm::iequals("MultiSnapshot", action)) - output = recorder.MultiSnapshot(pThreadPool, numberOfImages); + reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages); else if (StringAlgorithm::iequals("Video", action)) - output = recorder.Video(pThreadPool, ffmpeg); + reply.content = recorder.Video(pThreadPool, ffmpeg); } else if (StringAlgorithm::iequals("VStarCam", brand)) { auto recorder = Recorder::VStarCamRecorder(pHttpClient, settings); if (StringAlgorithm::iequals("Snapshot", action)) - output = recorder.Snapshot(pThreadPool); + reply.content = recorder.Snapshot(pThreadPool); else if (StringAlgorithm::iequals("MultiSnapshot", action)) - output = recorder.MultiSnapshot(pThreadPool, numberOfImages); + reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages); else if (StringAlgorithm::iequals("Video", action)) - output = recorder.Video(pThreadPool, ffmpeg); + reply.content = recorder.Video(pThreadPool, ffmpeg); } else if (StringAlgorithm::iequals("WatchBot", brand)) { auto recorder = Recorder::WatchBotRecorder(pHttpClient, settings); if (StringAlgorithm::iequals("Snapshot", action)) - output = recorder.Snapshot(pThreadPool); + reply.content = recorder.Snapshot(pThreadPool); else if (StringAlgorithm::iequals("MultiSnapshot", action)) - output = recorder.MultiSnapshot(pThreadPool, numberOfImages); + reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages); else if (StringAlgorithm::iequals("Video", action)) - output = recorder.Video(pThreadPool, ffmpeg); + reply.content = recorder.Video(pThreadPool, ffmpeg); } else if (StringAlgorithm::iequals("ZModo", brand)) { auto recorder = Recorder::ZModoRecorder(pHttpClient, settings); if (StringAlgorithm::iequals("Snapshot", action)) - output = recorder.Snapshot(pThreadPool); + reply.content = recorder.Snapshot(pThreadPool); else if (StringAlgorithm::iequals("MultiSnapshot", action)) - output = recorder.MultiSnapshot(pThreadPool, numberOfImages); + reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages); else if (StringAlgorithm::iequals("Video", action)) - output = recorder.Video(pThreadPool, ffmpeg); + reply.content = recorder.Video(pThreadPool, ffmpeg); } - return output; + return reply; } } // namespace API diff --git a/API/WebAPI.h b/API/WebAPI.h index 443f11a..48005c5 100644 --- a/API/WebAPI.h +++ b/API/WebAPI.h @@ -2,6 +2,7 @@ #define API_WEBAPI_H #include +#include #include #include #include @@ -22,7 +23,7 @@ public: WebAPI(); ~WebAPI(); - static std::string ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData); + static Http::HttpServer::HttpReply ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData); }; } // namespace API diff --git a/Application/CameraRecorder.cc b/Application/CameraRecorder.cc index a86c641..4fe1881 100644 --- a/Application/CameraRecorder.cc +++ b/Application/CameraRecorder.cc @@ -41,7 +41,7 @@ int main(int argc, char** argv) ctpl::thread_pool threadPool(6); Http::HttpClient httpClient; - auto callback = std::bind(&CameraRecorder::API::WebAPI::ProcessQuery, &threadPool, &httpClient, path.str(), ffmpeg, std::placeholders::_1, std::placeholders::_2); + Http::HttpServer::CallbackMethod callback = std::bind(&CameraRecorder::API::WebAPI::ProcessQuery, &threadPool, &httpClient, path.str(), ffmpeg, std::placeholders::_1, std::placeholders::_2); Http::HttpServer server(port, callback); Logging::Log(Logging::Severity::Info, "Startup Complete");