From 67c79c2c6e9eb1fbe68b9a6396880b7dd1d6f320 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Fri, 1 Mar 2024 02:25:24 -0800 Subject: [PATCH] Backup:AppManager test code. --- .../src/Protocol/SixFrame/SixFrameHandle.cpp | 48 ++++++------ .../src/Protocol/SixFrame/SixFrameHandle.h | 3 +- test/GtestUsing.h | 48 ++++++++++++ test/middleware/AppManager/CMakeLists.txt | 13 +--- .../AppManager/src/AppManager_Test.cpp | 6 +- .../middleware/AppManager/tool/CMakeLists.txt | 54 +++++++++++++ .../tool/include/AppManagerTestTool.h | 29 +++++++ .../tool/src/AppManagerTestTool.cpp | 29 +++++++ .../AppManager/tool/src/ServersMock.cpp | 75 +++++++++++++++++++ .../AppManager/tool/src/ServersMock.h | 26 +++++++ .../middleware/McuManager/tool/CMakeLists.txt | 2 - utils/Servers/CMakeLists.txt | 8 +- utils/Servers/README.md | 2 +- utils/Servers/include/servers.h | 4 +- utils/Servers/src/curl_serve.c | 2 +- utils/Servers/src/servers.c | 2 +- utils/WebServer/src/WebServer.cpp | 29 ++++--- 17 files changed, 318 insertions(+), 62 deletions(-) create mode 100644 test/GtestUsing.h create mode 100644 test/middleware/AppManager/tool/CMakeLists.txt create mode 100644 test/middleware/AppManager/tool/include/AppManagerTestTool.h create mode 100644 test/middleware/AppManager/tool/src/AppManagerTestTool.cpp create mode 100644 test/middleware/AppManager/tool/src/ServersMock.cpp create mode 100644 test/middleware/AppManager/tool/src/ServersMock.h diff --git a/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp index f871a4c..45ec7d1 100644 --- a/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp +++ b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp @@ -22,31 +22,26 @@ using std::placeholders::_3; // using std::placeholders::_4; SixFrameHandle::SixFrameHandle() { - // mResquesHandleFunc["set"] = std::bind(&SixFrameHandle::test, this, _1, _2, _3); - mResquesHandleFunc["app/set"] = std::bind(&SixFrameHandle::test, this, _1, _2, _3); - mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _3); + mResquesHandleFunc["/app/getproductinfo"] = 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 urlStr = url; - size_t queryStartPos = urlStr.find("HTTP"); - if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr.length()) { - const std::string urlStr2 = urlStr.substr(0, queryStartPos - 1); - LogInfo("URL = %s\n", urlStr2.c_str()); - queryStartPos = urlStr2.find('?'); - std::string command = ""; - if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr2.length()) { - command = urlStr2.substr(1, queryStartPos - 1); - } - else { - command = urlStr2.substr(1, urlStr2.length()); - } - LogInfo("command = %s\n", command.c_str()); - ExtractParamsFromUrl(urlStr2, paramsMap); - RequestHandle2(command, paramsMap, responseHandle, context); + 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) { @@ -67,12 +62,6 @@ void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, std::multimap< } } } -void SixFrameHandle::test(std::multimap ¶msMap, ResponseHandle responseHandle, - void *context) -{ - // - LogInfo("sssssssssssssssssssssssssssssssss\n"); -} void SixFrameHandle::RequestHandle2(const std::string command, std::multimap ¶msMap, ResponseHandle responseHandle, void *context) { @@ -82,10 +71,19 @@ void SixFrameHandle::RequestHandle2(const std::string command, std::multimap ¶msMap, ResponseHandle responseHandle, void *context) { // + responseHandle("Unknown command.", context); +} +void SixFrameHandle::RequestGetProductInfo(std::multimap ¶msMap, + ResponseHandle responseHandle, void *context) +{ + // + LogInfo("RequestGetProductInfo.\n"); + responseHandle("hello world.", context); } \ No newline at end of file diff --git a/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h index 2f2de6d..0b65a58 100644 --- a/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h +++ b/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.h @@ -34,10 +34,11 @@ public: private: void ExtractParamsFromUrl(const std::string &url, std::multimap ¶msMap); - void test(std::multimap ¶msMap, ResponseHandle responseHandle, void *context); void RequestHandle2(const std::string command, std::multimap ¶msMap, ResponseHandle responseHandle, void *context); void DoNothing(std::multimap ¶msMap, ResponseHandle responseHandle, void *context); + void RequestGetProductInfo(std::multimap ¶msMap, ResponseHandle responseHandle, + void *context); private: std::map mResquesHandleFunc; diff --git a/test/GtestUsing.h b/test/GtestUsing.h new file mode 100644 index 0000000..7cfd887 --- /dev/null +++ b/test/GtestUsing.h @@ -0,0 +1,48 @@ +/* + * 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. + */ +#ifndef GTESTUSING_H +#define GTESTUSING_H +#include +#include +using ::testing::_; +using ::testing::Action; +using ::testing::ActionInterface; +using ::testing::AnyNumber; +using ::testing::Assign; +using ::testing::AtLeast; +using ::testing::ByMove; +using ::testing::ByRef; +using ::testing::DefaultValue; +using ::testing::DoAll; +using ::testing::DoDefault; +using ::testing::IgnoreResult; +using ::testing::Invoke; +using ::testing::InvokeWithoutArgs; +using ::testing::MakePolymorphicAction; +using ::testing::PolymorphicAction; +using ::testing::Return; +using ::testing::ReturnNew; +using ::testing::ReturnNull; +using ::testing::ReturnPointee; +using ::testing::ReturnRef; +using ::testing::ReturnRefOfCopy; +using ::testing::ReturnRoundRobin; +using ::testing::SaveArg; +using ::testing::SetArgPointee; +using ::testing::SetArgumentPointee; +using ::testing::Unused; +using ::testing::WithArgs; +using ::testing::internal::BuiltInDefaultValue; +#endif \ No newline at end of file diff --git a/test/middleware/AppManager/CMakeLists.txt b/test/middleware/AppManager/CMakeLists.txt index 8dc716b..dafeb26 100644 --- a/test/middleware/AppManager/CMakeLists.txt +++ b/test/middleware/AppManager/CMakeLists.txt @@ -8,20 +8,13 @@ include_directories( ./tool/include ${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/StatusCode/include - # ${UTILS_SOURCE_PATH}/WebServer/include - # ${UTILS_SOURCE_PATH}/McuProtocol/include ${UTILS_SOURCE_PATH}/KeyControl/include ${UTILS_SOURCE_PATH}/LedControl/include ${HAL_SOURCE_PATH}/include - # ${HAL_SOURCE_PATH}/src ${MIDDLEWARE_SOURCE_PATH}/AppManager/include ${MIDDLEWARE_SOURCE_PATH}/AppManager/src - # ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include - # ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include + ${TEST_SOURCE_PATH} ${TEST_SOURCE_PATH}/hal/tool/include - # ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include - # ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include - # ${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include ) @@ -39,7 +32,7 @@ endif() set(TARGET_NAME AppManagerTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) -target_link_libraries(${TARGET_NAME} AppManager gtest gmock pthread) +target_link_libraries(${TARGET_NAME} AppManager AppManagerTestTool gtest gmock pthread) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() @@ -84,4 +77,4 @@ endif() define_file_name(${TARGET_NAME}) -# add_subdirectory(tool) \ No newline at end of file +add_subdirectory(tool) \ No newline at end of file diff --git a/test/middleware/AppManager/src/AppManager_Test.cpp b/test/middleware/AppManager/src/AppManager_Test.cpp index a284519..eb1a0c9 100644 --- a/test/middleware/AppManager/src/AppManager_Test.cpp +++ b/test/middleware/AppManager/src/AppManager_Test.cpp @@ -12,15 +12,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "AppManagerTestTool.h" #include "IAppManager.h" #include "ILog.h" -// #include "WebServer.h" #include #include #include namespace AppManagerTest { -class AppManagerTest : public testing::Test +class AppManagerTest : public testing::Test, public AppManagerTestTool { public: AppManagerTest() {} @@ -51,6 +51,8 @@ public: TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo) { IAppManager::GetInstance()->Init(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + MockGetProductInfo(); std::this_thread::sleep_for(std::chrono::milliseconds(10000)); IAppManager::GetInstance()->UnInit(); } diff --git a/test/middleware/AppManager/tool/CMakeLists.txt b/test/middleware/AppManager/tool/CMakeLists.txt new file mode 100644 index 0000000..0f65d9c --- /dev/null +++ b/test/middleware/AppManager/tool/CMakeLists.txt @@ -0,0 +1,54 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/Servers/include + ${MIDDLEWARE_SOURCE_PATH}/AppManager/src + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include + ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include +) +# link_directories( +# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs +# ) + +aux_source_directory(./src TEST_TOOL_SRC_FILES) +set(TEST_TOOL_TARGET AppManagerTestTool) +add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) +target_link_libraries(${TEST_TOOL_TARGET} Servers Log) + +if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") +add_custom_target( + AppManagerTestTool_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${TEST_TOOL_SRC_FILES} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager/tool +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + AppManagerTestTool_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${TEST_TOOL_SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager/tool +) +add_custom_command( + TARGET ${TEST_TOOL_TARGET} + PRE_BUILD + COMMAND make AppManagerTestTool_code_check + COMMAND make AppManagerTestTool_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TEST_TOOL_TARGET}) \ No newline at end of file diff --git a/test/middleware/AppManager/tool/include/AppManagerTestTool.h b/test/middleware/AppManager/tool/include/AppManagerTestTool.h new file mode 100644 index 0000000..380e96a --- /dev/null +++ b/test/middleware/AppManager/tool/include/AppManagerTestTool.h @@ -0,0 +1,29 @@ +/* + * 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. + */ +#ifndef APP_MANAGER_TEST_TOOL_H +#define APP_MANAGER_TEST_TOOL_H +#include "GtestUsing.h" +class AppManagerTestTool +{ +public: + AppManagerTestTool() = default; + virtual ~AppManagerTestTool() = default; + void Init(void); + void UnInit(void); + +protected: + void MockGetProductInfo(void); +}; +#endif \ No newline at end of file diff --git a/test/middleware/AppManager/tool/src/AppManagerTestTool.cpp b/test/middleware/AppManager/tool/src/AppManagerTestTool.cpp new file mode 100644 index 0000000..4e4b1fe --- /dev/null +++ b/test/middleware/AppManager/tool/src/AppManagerTestTool.cpp @@ -0,0 +1,29 @@ +/* + * 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 "AppManagerTestTool.h" +#include "ServersMock.h" +void AppManagerTestTool::Init(void) +{ + // +} +void AppManagerTestTool::UnInit(void) +{ + // +} +void AppManagerTestTool::MockGetProductInfo(void) +{ + // + ServersMock::GetInstance()->MockGetProductInfo(); +} \ No newline at end of file diff --git a/test/middleware/AppManager/tool/src/ServersMock.cpp b/test/middleware/AppManager/tool/src/ServersMock.cpp new file mode 100644 index 0000000..8edc794 --- /dev/null +++ b/test/middleware/AppManager/tool/src/ServersMock.cpp @@ -0,0 +1,75 @@ +/* + * 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 "ServersMock.h" +#include "ILog.h" +#include "servers.h" +std::shared_ptr &ServersMock::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + if (instance.use_count() == 1) { + LogInfo("Instance changed succeed.\n"); + instance = *impl; + } + else { + LogError("Can't changing the instance becase of using by some one.\n"); + } + } + return instance; +} +#include +#include +#include +#include +#include +#include +static int GetIp(void) +{ + char hostname[256]; + if (gethostname(hostname, sizeof(hostname)) == -1) { + std::cout << "Failed to get hostname." << std::endl; + return 1; + } + + struct hostent *hostent = gethostbyname(hostname); + if (hostent == nullptr || hostent->h_addr_list[0] == nullptr) { + std::cout << "Failed to get IP address." << std::endl; + return 1; + } + + struct in_addr **addr_list = reinterpret_cast(hostent->h_addr_list); + for (int i = 0; addr_list[i] != nullptr; ++i) { + std::cout << inet_ntoa(*addr_list[i]) << std::endl; + } + return 1; +} +void ServersMock::MockGetProductInfo(void) +{ + GetIp(); + ServerParam init = { + .logFlag = LOG_FLAG_ENABLE, + .sslVerifyFlag = SSL_VERIFY_DISABLE, + }; + ServersInit(init); + LogInfo("servers test start.\n"); + ServerHttp *http = NewServersHttp("http:192.168.1.29:80/app/test"); + if (http) { + HttpGet(http); + if (http->reply) { + LogInfo("HttpGet ========\n %s\n", http->reply); + } + DeleteServersHttp(http); + } +} \ No newline at end of file diff --git a/test/middleware/AppManager/tool/src/ServersMock.h b/test/middleware/AppManager/tool/src/ServersMock.h new file mode 100644 index 0000000..02974ce --- /dev/null +++ b/test/middleware/AppManager/tool/src/ServersMock.h @@ -0,0 +1,26 @@ +/* + * 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. + */ +#ifndef SERVERS_MOCK_H +#define SERVERS_MOCK_H +#include +class ServersMock +{ +public: + ServersMock() = default; + virtual ~ServersMock() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual void MockGetProductInfo(void); +}; +#endif \ No newline at end of file diff --git a/test/middleware/McuManager/tool/CMakeLists.txt b/test/middleware/McuManager/tool/CMakeLists.txt index f710903..09d1c2c 100644 --- a/test/middleware/McuManager/tool/CMakeLists.txt +++ b/test/middleware/McuManager/tool/CMakeLists.txt @@ -16,8 +16,6 @@ include_directories( # ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs # ) - - aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET McuManagerTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) diff --git a/utils/Servers/CMakeLists.txt b/utils/Servers/CMakeLists.txt index da1a1a2..64500a8 100755 --- a/utils/Servers/CMakeLists.txt +++ b/utils/Servers/CMakeLists.txt @@ -21,9 +21,13 @@ set(TARGET_NAME Servers) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) target_link_libraries(${TARGET_NAME} Log) if(${CURL_OPENSSL_LIB_SHARED_ENABLE} MATCHES "false") - target_link_libraries(${TARGET_NAME} ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/lib/.libs/libcurl.a) + target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcurl.a) + target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libssl.a) + target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcrypto.a dl pthread) else() - target_link_libraries(${TARGET_NAME} ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/lib/.libs/libcurl.so) + target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcurl.so) + target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libssl.so) + target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcrypto.so dl pthread) endif() # ------------------ openssl ------------------ start diff --git a/utils/Servers/README.md b/utils/Servers/README.md index 84c70a3..763d0ff 100644 --- a/utils/Servers/README.md +++ b/utils/Servers/README.md @@ -44,7 +44,7 @@ TEST(ServersTest, FtpsUpload) const char *url = "ftp://150.109.112.64/ServersTest";test/bin/ServersTest"; const char *uploadFile = "./ServersTest"; const char *user_password = "ftp_user:Sifar%123456"; - ServerInit init = { + ServerParam init = { .logFlag = LOG_FLAG_ENABLE, // 开启curl日志 .sslVerifyFlag = SSL_VERIFY_DISABLE, //关闭ssl的证书校验功能 }; diff --git a/utils/Servers/include/servers.h b/utils/Servers/include/servers.h index ef89c87..9c48c14 100644 --- a/utils/Servers/include/servers.h +++ b/utils/Servers/include/servers.h @@ -47,7 +47,7 @@ typedef struct ServersInit { LogFlag logFlag; SslFlag sslVerifyFlag; -} ServerInit; +} ServerParam; typedef struct servers_http { const char *url; @@ -84,7 +84,7 @@ typedef struct servers_smtp char **attachment; int code; } ServerSmtp; -void ServersInit(ServerInit init); +void ServersInit(ServerParam init); void ServersUnInit(void); // HTTP API ServerHttp *NewServersHttp(const char *url); diff --git a/utils/Servers/src/curl_serve.c b/utils/Servers/src/curl_serve.c index 95502ca..8d2e27b 100644 --- a/utils/Servers/src/curl_serve.c +++ b/utils/Servers/src/curl_serve.c @@ -13,7 +13,7 @@ * limitations under the License. */ #include "curl_serve.h" -static ServerInit gCurlServe; +static ServerParam gCurlServe; void SetVerboseLog(LogFlag flag) { gCurlServe.logFlag = flag; } void SetSslVerify(SslFlag flag) { gCurlServe.sslVerifyFlag = flag; } CURL *CurlEasyMake(void) diff --git a/utils/Servers/src/servers.c b/utils/Servers/src/servers.c index 194ca1d..62a3411 100644 --- a/utils/Servers/src/servers.c +++ b/utils/Servers/src/servers.c @@ -21,7 +21,7 @@ #include #include #include -void ServersInit(ServerInit init) +void ServersInit(ServerParam init) { SetVerboseLog(init.logFlag); SetSslVerify(init.sslVerifyFlag); diff --git a/utils/WebServer/src/WebServer.cpp b/utils/WebServer/src/WebServer.cpp index 529cf40..0ddc30a 100644 --- a/utils/WebServer/src/WebServer.cpp +++ b/utils/WebServer/src/WebServer.cpp @@ -18,6 +18,7 @@ #include "js.h" #include static int finished = 0; +static HttpHandleCallback gHttpHandle = NULL; static void sigHandler(int signo) { LogInfo("Stop goahead web server.\n"); @@ -50,25 +51,20 @@ void initPlatform(void) signal(SIGKILL, sigHandler); signal(SIGPIPE, SIG_IGN); } -static bool testHandler(Webs *wp) +static void response_handle(const char *responseStr, void *context) { - if (smatch(wp->path, "/")) { - // websRewriteRequest(wp, "/home.html"); - /* Fall through */ + struct Webs *wp = (struct Webs *)context; + if (NULL != responseStr) { + websWrite(wp, "%s", responseStr); } - LogInfo("sssssssssssssssssssssssssssss url = %s\n", wp->url); - // websSetStatus(wp, HTTP_CODE_OK); - // websWriteHeaders(wp, 0, 0); - // websWriteEndHeaders(wp); - // websWrite(wp, "

"); - // websWrite(wp, "sssssssssssssssssssssss"); - // websWrite(wp, "

\n"); - // websDone(wp); +} +static bool AppRequestHandle(Webs *wp) +{ websSetStatus(wp, 200); websWriteHeaders(wp, -1, 0); websWriteHeader(wp, "Content-Type", "text/plain"); websWriteEndHeaders(wp); - websWrite(wp, "Hello Legacy World\n"); + gHttpHandle(wp->url, 0, response_handle, wp); websDone(wp); return 1; return 1; @@ -98,8 +94,11 @@ StatusCode WebServerInit(const WebServerParam webParam) if (websListen(listen) < 0) { return CreateStatusCode(STATUS_CODE_NOT_OK); } - websDefineHandler("test", 0, testHandler, 0, 0); - websAddRoute("/test", "test", 0); + if (nullptr != webParam.mHttpRequestHandle) { + gHttpHandle = webParam.mHttpRequestHandle; + websDefineHandler("appRequestHandle", 0, AppRequestHandle, 0, 0); + websAddRoute("/app", "appRequestHandle", 0); + } websServiceEvents(&finished); logmsg(1, "Instructed to exit\n"); websClose();