Backup:Servers module.

This commit is contained in:
Fancy code 2024-02-28 03:49:11 -08:00
parent c679ced96c
commit 64ad852f91
13 changed files with 126 additions and 122 deletions

6
.gitignore vendored
View File

@ -2,4 +2,8 @@
cmake-shell/ cmake-shell/
external/gtest/googletest-release-1.11.0/ external/gtest/googletest-release-1.11.0/
external/libconfig/libconfig-1.7.3/ external/libconfig/libconfig-1.7.3/
output_files/ output_files/
external/openssl/openssl-1.1.1s/
external/openssl/openssl-3.1.0/
external/openssl/build/
external/curl/curl-8.1.2/

View File

@ -44,19 +44,19 @@ TEST(ServersTest, FtpsUpload)
const char *url = "ftp://150.109.112.64/ServersTest";test/bin/ServersTest"; const char *url = "ftp://150.109.112.64/ServersTest";test/bin/ServersTest";
const char *uploadFile = "./ServersTest"; const char *uploadFile = "./ServersTest";
const char *user_password = "ftp_user:Sifar%123456"; const char *user_password = "ftp_user:Sifar%123456";
SERVERS_INIT init = { ServerInit init = {
.logFlag = LOG_FLAG_ENABLE, // 开启curl日志 .logFlag = LOG_FLAG_ENABLE, // 开启curl日志
.sslVerifyFlag = SSL_VERIFY_DISABLE, //关闭ssl的证书校验功能 .sslVerifyFlag = SSL_VERIFY_DISABLE, //关闭ssl的证书校验功能
}; };
InitLog(LOG_EASYLOGGING, nullptr); // 初始化自研log库 InitLog(LOG_EASYLOGGING, nullptr); // 初始化自研log库
servers_init(init); // 初始化Servers模块 ServersInit(init); // 初始化Servers模块
LogInfo("servers test start.\n"); LogInfo("servers test start.\n");
SERVERS_FTP *ftp = new_servers_ftp(url, FTPS_FLAG_ENABLE); // 创建ftp的参数“句柄” ServerFtp *ftp = NewServersFtp(url, FTPS_FLAG_ENABLE); // 创建ftp的参数“句柄”
if (ftp) if (ftp)
{ {
ftp->user_password = (char *)user_password; ftp->user_password = (char *)user_password;
ftp->filePath = (char *)uploadFile; ftp->filePath = (char *)uploadFile;
ftp_upload(ftp); // 使用FTP上传文件 FtpUpload(ftp); // 使用FTP上传文件
if (SERVERS_CODE_OK == ftp->code) if (SERVERS_CODE_OK == ftp->code)
{ {
LogInfo("ftp succeed.\n"); LogInfo("ftp succeed.\n");
@ -65,7 +65,7 @@ TEST(ServersTest, FtpsUpload)
{ {
LogError("ftp failed, code = %d.\n", static_cast<int>(ftp->code)); LogError("ftp failed, code = %d.\n", static_cast<int>(ftp->code));
} }
delete_servers_ftp(ftp); // 释放ftp“句柄” DeleteServersFtp(ftp); // 释放ftp“句柄”
} }
UnInitLog(); UnInitLog();
} }

View File

@ -43,11 +43,11 @@ typedef enum
SSL_VERIFY_DISABLE, SSL_VERIFY_DISABLE,
SSL_VERIFY_END SSL_VERIFY_END
} SslFlag; } SslFlag;
typedef struct servers_init typedef struct ServersInit
{ {
LogFlag logFlag; LogFlag logFlag;
SslFlag sslVerifyFlag; SslFlag sslVerifyFlag;
} SERVERS_INIT; } ServerInit;
typedef struct servers_http typedef struct servers_http
{ {
const char *url; const char *url;
@ -57,7 +57,7 @@ typedef struct servers_http
char *reply; char *reply;
unsigned int replyLength; unsigned int replyLength;
int code; int code;
} SERVERS_HTTP; } ServerHttp;
typedef struct servers_ftp typedef struct servers_ftp
{ {
const char *url; const char *url;
@ -66,7 +66,7 @@ typedef struct servers_ftp
char *filePath; char *filePath;
unsigned int timeOutMs; unsigned int timeOutMs;
int code; int code;
} SERVERS_FTP; } ServerFtp;
typedef struct servers_smtp typedef struct servers_smtp
{ {
const char *url; const char *url;
@ -83,26 +83,26 @@ typedef struct servers_smtp
// unsigned int textLength; // unsigned int textLength;
char **attachment; char **attachment;
int code; int code;
} SERVERS_SMTP; } ServerSmtp;
void servers_init(SERVERS_INIT init); void ServersInit(ServerInit init);
void servers_unit(void); void ServersUnInit(void);
// HTTP API // HTTP API
SERVERS_HTTP *new_servers_http(const char *url); ServerHttp *NewServersHttp(const char *url);
void delete_servers_http(SERVERS_HTTP *ptr); void DeleteServersHttp(ServerHttp *ptr);
void http_get(SERVERS_HTTP *param); void HttpGet(ServerHttp *param);
void http_post(SERVERS_HTTP *param); void HttpPost(ServerHttp *param);
void http_put(SERVERS_HTTP *param); void HttpPut(ServerHttp *param);
// FTP API // FTP API
SERVERS_FTP *new_servers_ftp(const char *url, const FtpsFlag ftpsFlag); ServerFtp *NewServersFtp(const char *url, const FtpsFlag ftpsFlag);
void delete_servers_ftp(SERVERS_FTP *ptr); void DeleteServersFtp(ServerFtp *ptr);
void ftp_servers_check(SERVERS_FTP *param); void FtpServersCheck(ServerFtp *param);
void ftp_download(SERVERS_FTP *param); void FtpDownload(ServerFtp *param);
void ftp_upload(SERVERS_FTP *param); void FtpUpload(ServerFtp *param);
// SMTP API // SMTP API
SERVERS_SMTP *new_servers_smtp(const char *url, const char *subject, const char *from, const char *to, ServerSmtp *new_servers_smtp(const char *url, const char *subject, const char *from, const char *to,
const char *userName, const char *password, const char *date); const char *userName, const char *password, const char *date);
void delete_servers_smtp(SERVERS_SMTP *ptr); void delete_servers_smtp(ServerSmtp *ptr);
void smtp_send_email(SERVERS_SMTP *param); void smtp_send_email(ServerSmtp *param);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -13,10 +13,10 @@
* limitations under the License. * limitations under the License.
*/ */
#include "curl_serve.h" #include "curl_serve.h"
static SERVERS_INIT gCurlServe; static ServerInit gCurlServe;
void set_verbose_log(LogFlag flag) { gCurlServe.logFlag = flag; } void SetVerboseLog(LogFlag flag) { gCurlServe.logFlag = flag; }
void set_ssl_verify(SslFlag flag) { gCurlServe.sslVerifyFlag = flag; } void SetSslVerify(SslFlag flag) { gCurlServe.sslVerifyFlag = flag; }
CURL *curl_easy_make(void) CURL *CurlEasyMake(void)
{ {
CURL *curl; CURL *curl;
curl = curl_easy_init(); curl = curl_easy_init();

View File

@ -19,9 +19,9 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void set_verbose_log(LogFlag flag); void SetVerboseLog(LogFlag flag);
void set_ssl_verify(SslFlag flag); void SetSslVerify(SslFlag flag);
CURL *curl_easy_make(void); CURL *CurlEasyMake(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -36,10 +36,10 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
} }
return fwrite(buffer, size, nmemb, out->stream); return fwrite(buffer, size, nmemb, out->stream);
} }
static CURL *ftp_curl_easy_make(SERVERS_FTP *param) static CURL *ftp_curl_easy_make(ServerFtp *param)
{ {
CURL *curl = NULL; CURL *curl = NULL;
curl = curl_easy_make(); curl = CurlEasyMake();
if (curl) { if (curl) {
/* /*
* You better replace the URL with one that works! * You better replace the URL with one that works!
@ -59,7 +59,7 @@ static CURL *ftp_curl_easy_make(SERVERS_FTP *param)
} }
return curl; return curl;
} }
void ftp_servers_check_connect(SERVERS_FTP *param) void FtpServersCheckConnect(ServerFtp *param)
{ {
if (!param) { if (!param) {
LogError("null pointer.\n"); LogError("null pointer.\n");
@ -87,7 +87,7 @@ void ftp_servers_check_connect(SERVERS_FTP *param)
curl_global_cleanup(); curl_global_cleanup();
} }
void ftp_servers_download(SERVERS_FTP *param) void FtpServersDownload(ServerFtp *param)
{ {
if (!param) { if (!param) {
LogError("null pointer.\n"); LogError("null pointer.\n");
@ -143,7 +143,7 @@ static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
return retcode; return retcode;
} }
void ftp_servers_upload(SERVERS_FTP *param) void FtpServersUpload(ServerFtp *param)
{ {
if (!param) { if (!param) {
LogError("null pointer.\n"); LogError("null pointer.\n");

View File

@ -18,9 +18,9 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void ftp_servers_check_connect(SERVERS_FTP *param); void FtpServersCheckConnect(ServerFtp *param);
void ftp_servers_download(SERVERS_FTP *param); void FtpServersDownload(ServerFtp *param);
void ftp_servers_upload(SERVERS_FTP *param); void FtpServersUpload(ServerFtp *param);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -27,7 +27,7 @@ static size_t write_cb(char *data, size_t n, size_t l, void *userp)
/* take care of the data here, ignored in this example */ /* take care of the data here, ignored in this example */
(void)data; (void)data;
(void)userp; (void)userp;
SERVERS_HTTP *p = (SERVERS_HTTP *)userp; ServerHttp *p = (ServerHttp *)userp;
const int LENGTH = n * l; const int LENGTH = n * l;
// p->reply = (char *)realloc(p->reply, LENGTH); // p->reply = (char *)realloc(p->reply, LENGTH);
char *newData = (char *)malloc(p->replyLength + LENGTH); char *newData = (char *)malloc(p->replyLength + LENGTH);
@ -43,7 +43,7 @@ static size_t write_cb(char *data, size_t n, size_t l, void *userp)
} }
return n * l; return n * l;
} }
void http_servers_get(SERVERS_HTTP *param) void HttpServersGet(ServerHttp *param)
{ {
if (!param) { if (!param) {
LogError("null pointer.\n"); LogError("null pointer.\n");
@ -52,7 +52,7 @@ void http_servers_get(SERVERS_HTTP *param)
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
curl = curl_easy_make(); curl = CurlEasyMake();
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, param->url); curl_easy_setopt(curl, CURLOPT_URL, param->url);
/* example.com is redirected, so we tell libcurl to follow redirection */ /* example.com is redirected, so we tell libcurl to follow redirection */
@ -72,7 +72,7 @@ void http_servers_get(SERVERS_HTTP *param)
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
} }
void http_servers_post(SERVERS_HTTP *param) void HttpServersPost(ServerHttp *param)
{ {
if (!param) { if (!param) {
LogError("null pointer.\n"); LogError("null pointer.\n");
@ -85,7 +85,7 @@ void http_servers_post(SERVERS_HTTP *param)
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */ /* get a curl handle */
curl = curl_easy_make(); curl = CurlEasyMake();
if (curl) { if (curl) {
/* First set the URL that is about to receive our POST. This URL can /* First set the URL that is about to receive our POST. This URL can
just as well be an https:// URL if that is what should receive the just as well be an https:// URL if that is what should receive the
@ -140,7 +140,7 @@ static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
return retcode; return retcode;
} }
void http_servers_put(SERVERS_HTTP *param) void HttpServersPut(ServerHttp *param)
{ {
if (!param) { if (!param) {
LogError("null pointer.\n"); LogError("null pointer.\n");
@ -176,7 +176,7 @@ void http_servers_put(SERVERS_HTTP *param)
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */ /* get a curl handle */
curl = curl_easy_make(); curl = CurlEasyMake();
if (curl) { if (curl) {
/* we want to use our own read function */ /* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

View File

@ -18,9 +18,9 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void http_servers_get(SERVERS_HTTP *param); void HttpServersGet(ServerHttp *param);
void http_servers_post(SERVERS_HTTP *param); void HttpServersPost(ServerHttp *param);
void http_servers_put(SERVERS_HTTP *param); void HttpServersPut(ServerHttp *param);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -21,50 +21,50 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
void servers_init(SERVERS_INIT init) void ServersInit(ServerInit init)
{ {
set_verbose_log(init.logFlag); SetVerboseLog(init.logFlag);
set_ssl_verify(init.sslVerifyFlag); SetSslVerify(init.sslVerifyFlag);
} }
void servers_unit(void) {} void ServersUnInit(void) {}
void http_get(SERVERS_HTTP *param) void HttpGet(ServerHttp *param)
{ {
LogInfo("http_get\n"); LogInfo("HttpGet\n");
http_servers_get(param); HttpServersGet(param);
} }
void http_post(SERVERS_HTTP *param) void HttpPost(ServerHttp *param)
{ {
LogInfo("http_post\n"); LogInfo("HttpPost\n");
http_servers_post(param); HttpServersPost(param);
} }
void http_put(SERVERS_HTTP *param) void HttpPut(ServerHttp *param)
{ {
LogInfo("http_put\n"); LogInfo("HttpPut\n");
http_servers_put(param); HttpServersPut(param);
} }
void smtp_send_email(SERVERS_SMTP *param) void smtp_send_email(ServerSmtp *param)
{ {
LogInfo("smtp_send_email\n"); LogInfo("smtp_send_email\n");
smtp_servers_send_email(param); SmtpServersSendEmail(param);
// smtp_servers_send_email_only_text(param); // SmtpServersSendEmailOnlyText(param);
} }
SERVERS_HTTP *new_servers_http(const char *url) ServerHttp *NewServersHttp(const char *url)
{ {
if (!url) { if (!url) {
LogError("new_servers_http failed.\n"); LogError("NewServersHttp failed.\n");
return NULL; return NULL;
} }
SERVERS_HTTP result = { ServerHttp result = {
.url = url, .postData = NULL, .filePath = NULL, .header = NULL, .reply = NULL, .replyLength = 0, .code = 0}; .url = url, .postData = NULL, .filePath = NULL, .header = NULL, .reply = NULL, .replyLength = 0, .code = 0};
const int LENGTH = sizeof(SERVERS_HTTP); const int LENGTH = sizeof(ServerHttp);
SERVERS_HTTP *p = (SERVERS_HTTP *)malloc(LENGTH); ServerHttp *p = (ServerHttp *)malloc(LENGTH);
if (p) { if (p) {
LogInfo("malloc succeed.\n"); LogInfo("malloc succeed.\n");
memcpy(p, &result, LENGTH); memcpy(p, &result, LENGTH);
} }
return p; return p;
} }
void delete_servers_http(SERVERS_HTTP *ptr) void DeleteServersHttp(ServerHttp *ptr)
{ {
if (ptr) { if (ptr) {
if (ptr->reply) { if (ptr->reply) {
@ -74,71 +74,71 @@ void delete_servers_http(SERVERS_HTTP *ptr)
free(ptr); free(ptr);
} }
} }
SERVERS_FTP *new_servers_ftp(const char *url, const FtpsFlag ftpsFlag) ServerFtp *NewServersFtp(const char *url, const FtpsFlag ftpsFlag)
{ {
if (!url) { if (!url) {
LogError("new_servers_ftp failed.\n"); LogError("NewServersFtp failed.\n");
return NULL; return NULL;
} }
SERVERS_FTP result = {.url = url, ServerFtp result = {.url = url,
.ftpsFlag = ftpsFlag, .ftpsFlag = ftpsFlag,
.user_password = NULL, .user_password = NULL,
.filePath = NULL, .filePath = NULL,
.timeOutMs = SERVERS_NEVER_TIMEOUT, .timeOutMs = SERVERS_NEVER_TIMEOUT,
.code = -1}; .code = -1};
const int LENGTH = sizeof(SERVERS_FTP); const int LENGTH = sizeof(ServerFtp);
SERVERS_FTP *p = (SERVERS_FTP *)malloc(LENGTH); ServerFtp *p = (ServerFtp *)malloc(LENGTH);
if (p) { if (p) {
LogInfo("malloc succeed.\n"); LogInfo("malloc succeed.\n");
memcpy(p, &result, LENGTH); memcpy(p, &result, LENGTH);
} }
return p; return p;
} }
void delete_servers_ftp(SERVERS_FTP *ptr) void DeleteServersFtp(ServerFtp *ptr)
{ {
if (ptr) { if (ptr) {
free(ptr); free(ptr);
} }
} }
void ftp_servers_check(SERVERS_FTP *param) { ftp_servers_check_connect(param); } void FtpServersCheck(ServerFtp *param) { FtpServersCheckConnect(param); }
void ftp_download(SERVERS_FTP *param) void FtpDownload(ServerFtp *param)
{ {
LogInfo("ftp_download\n"); LogInfo("FtpDownload\n");
ftp_servers_download(param); FtpServersDownload(param);
} }
void ftp_upload(SERVERS_FTP *param) void FtpUpload(ServerFtp *param)
{ {
LogInfo("ftp_upload\n"); LogInfo("FtpUpload\n");
ftp_servers_upload(param); FtpServersUpload(param);
} }
SERVERS_SMTP *new_servers_smtp(const char *url, const char *subject, const char *from, const char *to, ServerSmtp *new_servers_smtp(const char *url, const char *subject, const char *from, const char *to,
const char *userName, const char *password, const char *date) const char *userName, const char *password, const char *date)
{ {
if (!url || !from || !to || !userName || !password || !subject || !date) { if (!url || !from || !to || !userName || !password || !subject || !date) {
LogError("new_servers_smtp failed.\n"); LogError("new_servers_smtp failed.\n");
return NULL; return NULL;
} }
SERVERS_SMTP result = {.url = url, ServerSmtp result = {.url = url,
.subject = subject, .subject = subject,
.from = from, .from = from,
.to = to, .to = to,
.userName = userName, .userName = userName,
.password = password, .password = password,
.date = date, .date = date,
.inlineText = NULL, .inlineText = NULL,
// .text = NULL, // .text = NULL,
// .textLength = 0, // .textLength = 0,
.attachment = NULL, .attachment = NULL,
.code = -1}; .code = -1};
const int LENGTH = sizeof(SERVERS_SMTP); const int LENGTH = sizeof(ServerSmtp);
SERVERS_SMTP *p = (SERVERS_SMTP *)malloc(LENGTH); ServerSmtp *p = (ServerSmtp *)malloc(LENGTH);
if (p) { if (p) {
LogInfo("malloc succeed.\n"); LogInfo("malloc succeed.\n");
memcpy(p, &result, LENGTH); memcpy(p, &result, LENGTH);
} }
return p; return p;
} }
void delete_servers_smtp(SERVERS_SMTP *ptr) void delete_servers_smtp(ServerSmtp *ptr)
{ {
if (ptr) { if (ptr) {
free(ptr); free(ptr);

View File

@ -72,7 +72,7 @@ enum HEADERS_NAME
HEADERS_NAME_SUBJECT, HEADERS_NAME_SUBJECT,
HEADERS_NAME_END HEADERS_NAME_END
}; };
static void headers_make(char **headers, SERVERS_SMTP *param) static void headers_make(char **headers, ServerSmtp *param)
{ {
int textLength = strlen(DATE) + strlen(param->date) + 1; int textLength = strlen(DATE) + strlen(param->date) + 1;
char *text = (char *)malloc(textLength); char *text = (char *)malloc(textLength);
@ -118,7 +118,7 @@ static void headers_free(char **headers)
cpp++; cpp++;
} }
} }
void smtp_servers_send_email(SERVERS_SMTP *param) void SmtpServersSendEmail(ServerSmtp *param)
{ {
if (!param) { if (!param) {
LogError("null pointer.\n"); LogError("null pointer.\n");
@ -129,7 +129,7 @@ void smtp_servers_send_email(SERVERS_SMTP *param)
CURL *curl; CURL *curl;
CURLcode res = CURLE_OK; CURLcode res = CURLE_OK;
curl = curl_easy_make(); curl = CurlEasyMake();
if (curl) { if (curl) {
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
struct curl_slist *recipients = NULL; struct curl_slist *recipients = NULL;
@ -231,7 +231,7 @@ void smtp_servers_send_email(SERVERS_SMTP *param)
headers_free(headers_text); headers_free(headers_text);
} }
} }
void smtp_servers_send_email_only_text(SERVERS_SMTP *param) void SmtpServersSendEmailOnlyText(ServerSmtp *param)
{ {
#if 0 // only for test. #if 0 // only for test.
if (!param) if (!param)
@ -269,7 +269,7 @@ void smtp_servers_send_email_only_text(SERVERS_SMTP *param)
struct upload_status upload_ctx = {0}; struct upload_status upload_ctx = {0};
upload_ctx.payload_text = payload_text; upload_ctx.payload_text = payload_text;
curl = curl_easy_make(); curl = CurlEasyMake();
if (curl) if (curl)
{ {
curl_mime *mime; curl_mime *mime;

View File

@ -18,8 +18,8 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void smtp_servers_send_email(SERVERS_SMTP *param); void SmtpServersSendEmail(ServerSmtp *param);
void smtp_servers_send_email_only_text(SERVERS_SMTP *param); void SmtpServersSendEmailOnlyText(ServerSmtp *param);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -4,15 +4,15 @@ void test0()
{ {
// InitLog(LOG_EASYLOGGING, NULL); // InitLog(LOG_EASYLOGGING, NULL);
// LogInfo("servers test start.\n"); // LogInfo("servers test start.\n");
SERVERS_HTTP *http = new_servers_http("http://example.com"); ServerHttp *http = NewServersHttp("http://example.com");
if (http) if (http)
{ {
http_get(http); HttpGet(http);
if (http->reply) if (http->reply)
{ {
// LogInfo("HttpGet ========\n %s\n", http->reply); // LogInfo("HttpGet ========\n %s\n", http->reply);
} }
delete_servers_http(http); DeleteServersHttp(http);
} }
// UnInitLog(); // UnInitLog();
} }
@ -20,15 +20,15 @@ void test1()
{ {
// InitLog(LOG_EASYLOGGING, NULL); // InitLog(LOG_EASYLOGGING, NULL);
// LogInfo("servers test start.\n"); // LogInfo("servers test start.\n");
// SERVERS_HTTP *http = new_servers_http("https://example.com"); // ServerHttp *http = NewServersHttp("https://example.com");
// if (http) // if (http)
// { // {
// http_get(http); // HttpGet(http);
// if (http->reply) // if (http->reply)
// { // {
// LogInfo("HttpGet ========\n %s\n", http->reply); // LogInfo("HttpGet ========\n %s\n", http->reply);
// } // }
// delete_servers_http(http); // DeleteServersHttp(http);
// } // }
// UnInitLog(); // UnInitLog();
} }