Przeglądaj źródła

#IQRV #comment [Other] 新增 libRestfulFunc 库用于 http 探测

gifur 3 lat temu
rodzic
commit
527793ebdd

+ 1 - 0
Other/CMakeLists.txt

@@ -31,6 +31,7 @@ else(MSVC)
    add_subdirectory(libvideocapture)
    add_subdirectory(libvideorender)
    add_subdirectory(libaudiomgr)
+   add_subdirectory(libRestfulFunc)
 endif(MSVC)
 
 # rvc_add_all_cmake_subdirectory()

+ 47 - 0
Other/libRestfulFunc/CMakeLists.txt

@@ -0,0 +1,47 @@
+set(MODULE_NAME "RestfulFunc")
+set(MODULE_PREFIX "LIB_RESTFUL_FUNC")
+
+set(${MODULE_PREFIX}_SRCS
+    RestfulFunc.h
+    RestfulFuncImpl.cpp
+    HttpProbeImpl.cpp
+)
+
+add_library(${MODULE_NAME} SHARED ${${MODULE_PREFIX}_SRCS})
+
+
+# 依赖libscreencodec、acmstrdec、acmstrenc
+target_include_directories(${MODULE_NAME} PRIVATE
+	${RVC_COMMON_INCLUDE_DIR}
+    ${CONAN_INCLUDE_DIRS_OPENSSL}
+    ${CONAN_INCLUDE_DIRS_CPPRESTSDK}
+    ${CONAN_BOOST_ROOT}
+    )
+
+target_link_directories(${MODULE_NAME} PRIVATE 
+    ${CONAN_LIB_DIRS_OPENSSL} 
+    ${CONAN_LIB_DIRS_CPPRESTSDK})
+
+target_compile_definitions(${MODULE_NAME} PUBLIC "RESTFULPUBLIC_EXPORTS")
+
+# 添加需要依赖的其他共享库(包括系统库)
+target_link_libraries(${MODULE_NAME} PRIVATE 
+    ${CONAN_PKG_LIBS_OPENSSL} ${CONAN_LIBS_CPPRESTSDK})
+
+if(MSVC)
+	install(TARGETS ${MODULE_NAME} 
+    RUNTIME DESTINATION "${RVC_RUNTIME_PATH}" COMPONENT libraries
+    ARCHIVE DESTINATION "${RVC_LIBRARY_PATH}" COMPONENT develops EXCLUDE_FROM_ALL
+    LIBRARY DESTINATION "${RVC_LIBRARY_PATH}" COMPONENT libraries
+    )
+else(MSVC)
+install(TARGETS ${MODULE_NAME} 
+    RUNTIME DESTINATION "${RVC_RUNTIME_PATH}"
+    ARCHIVE DESTINATION "${RVC_LIBRARY_PATH}"
+    LIBRARY DESTINATION "${RVC_RUNTIME_PATH}"
+    COMPONENT libraries)
+endif(MSVC)
+
+if(BUILD_TESTING)
+    add_subdirectory(test)
+endif(BUILD_TESTING)

+ 63 - 0
Other/libRestfulFunc/HttpProbeImpl.cpp

@@ -0,0 +1,63 @@
+#include "RestfulFunc.h"
+#include <iostream>
+#include "cpprest/uri.h"
+#include "cpprest/http_client.h"
+
+using namespace utility;                    // Common utilities like string conversions
+using namespace web;                        // Common features like URIs.
+using namespace web::http;                  // Common HTTP functionality
+using namespace web::http::client;          // HTTP client features
+using namespace concurrency::streams;       // Asynchronous streams
+
+
+int HttpProbe(const std::string& url, std::string& out_msg, uint32_t wait_max_secs)
+{
+    int result(-1);
+
+    out_msg.clear();
+
+    if (url.empty()) {
+        out_msg = "invalid param";
+        result = -3;
+    } else {
+
+        utility::string_t response_content;
+        utf8string utf8_response_content;
+
+        const pplx::task<void> request_task = pplx::task<http_response>([&]() {
+
+            http_client_config config;
+
+            if (wait_max_secs != (uint32_t)(-1)) {
+                config.set_timeout(utility::seconds(wait_max_secs));
+            }
+
+            config.set_validate_certificates(false);
+
+            http_client client(url, config);
+            return client.request(methods::GET);
+                                                                        }).then([&](http_response response) {
+                                                                            ucout << U(">>>>>> response status: ") << response.status_code() << std::endl;
+                                                                            result = response.status_code();
+                                                                                });
+                                                                        try {
+
+                                                                            request_task.wait();
+
+                                                                        } catch (const std::exception& e) {
+
+                                                                            out_msg = e.what();
+                                                                            std::cout << "exception occurs: " << e.what() << "\n\n";
+
+                                                                            if (strstr(e.what(), "12002") != nullptr || strstr(e.what(), "Request canceled by user") != nullptr) {
+                                                                                result = -2;
+                                                                            } else if (strstr(e.what(), "URI must contain a hostname") != nullptr) {
+                                                                                result = -3;
+                                                                            } else {
+                                                                                result = -1;
+                                                                            }
+                                                                        }
+    }
+
+    return result;
+}

+ 33 - 0
Other/libRestfulFunc/RestfulFunc.h

@@ -0,0 +1,33 @@
+#ifndef _OTHER_RESFULFUNCEXPORT_HEADER_
+#define _OTHER_RESFULFUNCEXPORT_HEADER_
+#pragma once
+
+#ifdef _WIN32
+#ifdef RESTFULPUBLIC_EXPORTS
+#ifndef RESTFULFUNC_API
+#define RESTFULFUNC_API __declspec(dllexport)
+#endif
+#else
+#ifndef RESTFULFUNC_API
+#define RESTFULFUNC_API __declspec(dllimport)
+#endif
+#endif
+#else
+#if ( defined(__GNUC__) &&  __GNUC__ >= 4 )
+#define RESTFULFUNC_API __attribute__((visibility("default")))
+#else
+#define RESTFULFUNC_API
+#endif
+#endif
+
+#include <string>
+
+/*
+* > 0 : 网站返回的错误码,比如 200,404 等
+* -1:未归类,具体错误信息见 out_msg 返回的内容
+* -2:超时
+* -3:地址不符合规范
+*/
+RESTFULFUNC_API int HttpProbe(const std::string& url, std::string& out_msg, uint32_t wait_max_secs = 30);
+
+#endif //_OTHER_RESFULFUNCEXPORT_HEADER_

+ 1 - 0
Other/libRestfulFunc/RestfulFuncImpl.cpp

@@ -0,0 +1 @@
+#include "RestfulFunc.h"

+ 16 - 0
Other/libRestfulFunc/test/CMakeLists.txt

@@ -0,0 +1,16 @@
+set(MODULE_PREFIX "TEST_RESTFULFUNC")
+
+file(GLOB ${MODULE_PREFIX}_TESTS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
+
+
+foreach(test ${${MODULE_PREFIX}_TESTS})
+	get_filename_component(test_name ${test} NAME_WE)
+	add_executable(${test_name} ${test})
+	target_include_directories(${test_name} PRIVATE ${CONAN_INCLUDE_DIRS_CATCH} ${OTHER_LIB_BASE_DIR}/libRestfulFunc)
+	target_link_libraries(${test_name} PRIVATE RestfulFunc)
+	set_property(TARGET ${test_name} PROPERTY FOLDER "test/other")
+	set_target_properties(${test_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
+	message(STATUS "add test case: ${test_name}...")
+	add_test(NAME ${test_name} COMMAND ${TESTING_OUTPUT_DIRECTORY}/${test_name})
+	set_tests_properties(${teset_name} PROPERTIES FAIL_REGULAR_EXPRESSION "FAILED;ERROR;Failed;error")
+endforeach()

+ 97 - 0
Other/libRestfulFunc/test/testHttpProbe.cpp

@@ -0,0 +1,97 @@
+#define CATCH_CONFIG_MAIN
+#include <catch2.hpp>
+#include "RestfulFunc.h"
+
+TEST_CASE("test http connected probe", "[probe1]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("http://mayun.itc.cmbchina.cn", msg) > 0);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http baidu probe", "[probe2]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://www.baidu.com/", msg) > 0);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http illgal format probe", "[probe3]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("", msg) == -3);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http illgal format probe 2", "[probe4]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("http", msg) == -3);
+    std::cout << msg << std::endl;
+}
+
+
+TEST_CASE("test http illgal format probe 3", "[probe5]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("99.12.23.78", msg) == -3);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http illgal format probe 4", "[probe6]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("55.4.33.22", msg) == -3);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http cmb format probe json", "[probe7]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://rvcgateway.paas.cmbchina.cn/health", msg) > 0);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http cmb format probe 2", "[probe8]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://tsaccess.paas.cmbchina.cn/tsas/detect", msg, -1) == -2);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http cmb format probe 3", "[probe9]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://uploadservice.paas.cmbchina.cn/rvc/detect", msg) > 0);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http cmb format probe 4", "[probe10]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://rvcterminal.paasst.cmbchina.cn/health", msg) > 0);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http github probe", "[probe11]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://github.com/gifur", msg) == -1);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("test http github with timeout probe", "[probe]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://github.com/gifur", msg) == -1);
+    std::cout << msg << std::endl;
+}
+
+TEST_CASE("Test http probe protocols implement", "[normal]")
+{
+    std::string msg;
+    REQUIRE(HttpProbe("https://rvcgateway.paas.cmbchina.cn/health", msg) == 200);
+    std::cout << msg << std::endl;
+}
+
+

+ 1 - 1
addin/cmake/DependencyConanFiles.cmake

@@ -77,7 +77,7 @@ else()
 			#mod_resourceWatcher
 			sogoulib/2021.1008.01@LR04.02_VendorLib/testing
 
-			# cpprestsdk/2.10.15@LR04.02_ThirdParty/testing
+			cpprestsdk/2.10.15@LR04.02_ThirdParty/testing
 			)
 
 	if(PACK_AS_DEB_PKG)