/* * Copyright (c) 2023 Fancy Code. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "SixFrameHandle.h" #include "ILog.h" #include #include using std::placeholders::_1; using std::placeholders::_2; using std::placeholders::_3; // using std::placeholders::_4; const char *APP_GET_PRODUCT_INFO = "/app/getproductinfo"; SixFrameHandle::SixFrameHandle() { mResquesHandleFunc[APP_GET_PRODUCT_INFO] = std::bind(&SixFrameHandle::RequestGetProductInfo, this, _1, _2, _3); // mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _3); } void SixFrameHandle::RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context) { std::multimap paramsMap; const std::string urlStr2 = url; LogInfo("URL = %s\n", urlStr2.c_str()); size_t queryStartPos = urlStr2.find('?'); std::string command = ""; if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr2.length()) { command = urlStr2.substr(0, queryStartPos); } else { command = urlStr2.substr(0, urlStr2.length()); } LogInfo("command = %s\n", command.c_str()); ExtractParamsFromUrl(urlStr2, paramsMap); RequestHandle2(command, paramsMap, responseHandle, context); } void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, std::multimap ¶msMap) { size_t queryStartPos = url.find('?'); if (queryStartPos != std::string::npos && queryStartPos + 1 < url.length()) { std::string paramsStr = url.substr(queryStartPos + 1); std::istringstream iss(paramsStr); std::string token; while (getline(iss, token, '&')) { size_t equalSignPos = token.find('='); if (equalSignPos != std::string::npos) { std::string key = token.substr(0, equalSignPos); std::string value = token.substr(equalSignPos + 1); LogInfo("url get [%s] = %s\n", key.c_str(), value.c_str()); paramsMap.insert({key, value}); } } } } void SixFrameHandle::RequestHandle2(const std::string command, std::multimap ¶msMap, ResponseHandle responseHandle, void *context) { auto result = mResquesHandleFunc.find(command); if (result != mResquesHandleFunc.end()) { (*result).second(paramsMap, responseHandle, context); } else { LogError("Unknown command.\n"); DoNothing(paramsMap, responseHandle, context); } } void SixFrameHandle::DoNothing(std::multimap ¶msMap, ResponseHandle responseHandle, void *context) { // responseHandle("Unknown command.", context); } void SixFrameHandle::RequestGetProductInfo(std::multimap ¶msMap, ResponseHandle responseHandle, void *context) { LogInfo("RequestGetProductInfo.\n"); char *resultStr = nullptr; cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL); resultStr = cJSON_Print(result); responseHandle(resultStr, context); free(resultStr); cJSON_Delete(result); } cJSON *SixFrameHandle::MakeResponseResult(const ResposeResult result) { const char *RESPONSE_RESULT = "result"; cJSON *resultCJSON = cJSON_CreateObject(); cJSON_AddNumberToObject(resultCJSON, RESPONSE_RESULT, static_cast(result)); return resultCJSON; }