Update Http Library

This commit is contained in:
2020-06-10 22:17:13 +02:00
parent a161173dec
commit b01fdb8be8
3 changed files with 35 additions and 22 deletions
+32 -20
View File
@@ -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<Http::HttpPostData>& 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<Http::HttpPostData>& 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<std::string> 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<std::string> uriTokens = StringAlgorithm::split(apiURI, '/');
if (uriTokens.size() < 7) if (uriTokens.size() < 7)
return std::string(); return reply;
std::string action = uriTokens[1]; std::string action = uriTokens[1];
std::string brand = uriTokens[2]; std::string brand = uriTokens[2];
@@ -50,58 +61,59 @@ std::string WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClien
if (uriTokens.size() > 7) if (uriTokens.size() > 7)
numberOfImages = stoi(uriTokens[7]); numberOfImages = stoi(uriTokens[7]);
reply.status = Http::HttpServer::HttpReply::Status::Ok;
if (StringAlgorithm::iequals("Digoo", brand)) if (StringAlgorithm::iequals("Digoo", brand))
{ {
auto recorder = Recorder::DigooRecorder(pHttpClient, settings); auto recorder = Recorder::DigooRecorder(pHttpClient, settings);
if (StringAlgorithm::iequals("Snapshot", action)) if (StringAlgorithm::iequals("Snapshot", action))
output = recorder.Snapshot(pThreadPool); reply.content = recorder.Snapshot(pThreadPool);
else if (StringAlgorithm::iequals("MultiSnapshot", action)) else if (StringAlgorithm::iequals("MultiSnapshot", action))
output = recorder.MultiSnapshot(pThreadPool, numberOfImages); reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages);
else if (StringAlgorithm::iequals("Video", action)) else if (StringAlgorithm::iequals("Video", action))
output = recorder.Video(pThreadPool, ffmpeg); reply.content = recorder.Video(pThreadPool, ffmpeg);
} }
else if (StringAlgorithm::iequals("Foscam", brand)) else if (StringAlgorithm::iequals("Foscam", brand))
{ {
auto recorder = Recorder::FoscamRecorder(pHttpClient, settings); auto recorder = Recorder::FoscamRecorder(pHttpClient, settings);
if (StringAlgorithm::iequals("Snapshot", action)) if (StringAlgorithm::iequals("Snapshot", action))
output = recorder.Snapshot(pThreadPool); reply.content = recorder.Snapshot(pThreadPool);
else if (StringAlgorithm::iequals("MultiSnapshot", action)) else if (StringAlgorithm::iequals("MultiSnapshot", action))
output = recorder.MultiSnapshot(pThreadPool, numberOfImages); reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages);
else if (StringAlgorithm::iequals("Video", action)) else if (StringAlgorithm::iequals("Video", action))
output = recorder.Video(pThreadPool, ffmpeg); reply.content = recorder.Video(pThreadPool, ffmpeg);
} }
else if (StringAlgorithm::iequals("VStarCam", brand)) else if (StringAlgorithm::iequals("VStarCam", brand))
{ {
auto recorder = Recorder::VStarCamRecorder(pHttpClient, settings); auto recorder = Recorder::VStarCamRecorder(pHttpClient, settings);
if (StringAlgorithm::iequals("Snapshot", action)) if (StringAlgorithm::iequals("Snapshot", action))
output = recorder.Snapshot(pThreadPool); reply.content = recorder.Snapshot(pThreadPool);
else if (StringAlgorithm::iequals("MultiSnapshot", action)) else if (StringAlgorithm::iequals("MultiSnapshot", action))
output = recorder.MultiSnapshot(pThreadPool, numberOfImages); reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages);
else if (StringAlgorithm::iequals("Video", action)) else if (StringAlgorithm::iequals("Video", action))
output = recorder.Video(pThreadPool, ffmpeg); reply.content = recorder.Video(pThreadPool, ffmpeg);
} }
else if (StringAlgorithm::iequals("WatchBot", brand)) else if (StringAlgorithm::iequals("WatchBot", brand))
{ {
auto recorder = Recorder::WatchBotRecorder(pHttpClient, settings); auto recorder = Recorder::WatchBotRecorder(pHttpClient, settings);
if (StringAlgorithm::iequals("Snapshot", action)) if (StringAlgorithm::iequals("Snapshot", action))
output = recorder.Snapshot(pThreadPool); reply.content = recorder.Snapshot(pThreadPool);
else if (StringAlgorithm::iequals("MultiSnapshot", action)) else if (StringAlgorithm::iequals("MultiSnapshot", action))
output = recorder.MultiSnapshot(pThreadPool, numberOfImages); reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages);
else if (StringAlgorithm::iequals("Video", action)) else if (StringAlgorithm::iequals("Video", action))
output = recorder.Video(pThreadPool, ffmpeg); reply.content = recorder.Video(pThreadPool, ffmpeg);
} }
else if (StringAlgorithm::iequals("ZModo", brand)) else if (StringAlgorithm::iequals("ZModo", brand))
{ {
auto recorder = Recorder::ZModoRecorder(pHttpClient, settings); auto recorder = Recorder::ZModoRecorder(pHttpClient, settings);
if (StringAlgorithm::iequals("Snapshot", action)) if (StringAlgorithm::iequals("Snapshot", action))
output = recorder.Snapshot(pThreadPool); reply.content = recorder.Snapshot(pThreadPool);
else if (StringAlgorithm::iequals("MultiSnapshot", action)) else if (StringAlgorithm::iequals("MultiSnapshot", action))
output = recorder.MultiSnapshot(pThreadPool, numberOfImages); reply.content = recorder.MultiSnapshot(pThreadPool, numberOfImages);
else if (StringAlgorithm::iequals("Video", action)) else if (StringAlgorithm::iequals("Video", action))
output = recorder.Video(pThreadPool, ffmpeg); reply.content = recorder.Video(pThreadPool, ffmpeg);
} }
return output; return reply;
} }
} // namespace API } // namespace API
+2 -1
View File
@@ -2,6 +2,7 @@
#define API_WEBAPI_H #define API_WEBAPI_H
#include <HttpPostData.h> #include <HttpPostData.h>
#include <HttpServer.h>
#include <ctpl_stl.h> #include <ctpl_stl.h>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -22,7 +23,7 @@ public:
WebAPI(); WebAPI();
~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<Http::HttpPostData>& 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<Http::HttpPostData>& postData);
}; };
} // namespace API } // namespace API
+1 -1
View File
@@ -41,7 +41,7 @@ int main(int argc, char** argv)
ctpl::thread_pool threadPool(6); ctpl::thread_pool threadPool(6);
Http::HttpClient httpClient; 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); Http::HttpServer server(port, callback);
Logging::Log(Logging::Severity::Info, "Startup Complete"); Logging::Log(Logging::Severity::Info, "Startup Complete");