ソースを参照

Update Http Library

JDierkse 5 年 前
コミット
b01fdb8be8
3 ファイル変更35 行追加22 行削除
  1. 32 20
      API/WebAPI.cpp
  2. 2 1
      API/WebAPI.h
  3. 1 1
      Application/CameraRecorder.cc

+ 32 - 20
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<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;
+	}
+
+	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(uri, '/');
+	std::vector<std::string> 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

+ 2 - 1
API/WebAPI.h

@@ -2,6 +2,7 @@
 #define API_WEBAPI_H
 
 #include <HttpPostData.h>
+#include <HttpServer.h>
 #include <ctpl_stl.h>
 #include <string>
 #include <vector>
@@ -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<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

+ 1 - 1
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");