diff options
| author | 崔一鸣 <[email protected]> | 2019-04-17 14:30:11 +0800 |
|---|---|---|
| committer | 崔一鸣 <[email protected]> | 2019-04-17 14:30:11 +0800 |
| commit | 4333c8d31b65de6d89997d37c752bf5347a026be (patch) | |
| tree | 2c0a6ca7e17d9ce6e9f502d4eb37cabfb572e4c3 /test/test_plugin | |
init commit
Diffstat (limited to 'test/test_plugin')
| -rw-r--r-- | test/test_plugin/http_service.cpp | 157 | ||||
| -rw-r--r-- | test/test_plugin/http_service.h | 51 |
2 files changed, 208 insertions, 0 deletions
diff --git a/test/test_plugin/http_service.cpp b/test/test_plugin/http_service.cpp new file mode 100644 index 0000000..e3d7984 --- /dev/null +++ b/test/test_plugin/http_service.cpp @@ -0,0 +1,157 @@ +/* + * HTTP_Service.c + * + * Created on: 2013-8-19 + * Author: lishu + */ + +#include <math.h> +#include <pthread.h> +#include <time.h> +#include <arpa/inet.h> +#include <stdio.h> +#include <regex.h> +#include <string> +#include "http_service.h" +#include "MESA_handle_logger.h" + +int HTTP_SERVICE_VERSION_1_20160412 = 0; +void http_service_version_1_20160412() +{ + //20160412 create project +} + +void* g_log_handle = NULL; + +int init_pmeinfo(service_pmeinfo **service_pme, int thread_seq) +{ + service_pmeinfo* pme = (service_pmeinfo*)dictator_malloc(thread_seq, sizeof(service_pmeinfo)); + *service_pme = pme; + return 0; +} + +void clear_pmeinfo(service_pmeinfo *service_pme, int thread_seq) +{ + if(service_pme!=NULL) + { + dictator_free(thread_seq, service_pme); + } + service_pme = NULL; +} + +std::string trans_to_binary(unsigned int n){ + std::string res; + while(n){ + res = std::to_string(n % 2) + res; + n /= 2; + } + return res; +} + +uchar HTTP_SERVICE_ENTRY(stSessionInfo* session_info, void **param, int thread_seq, struct streaminfo *a_tcp, void *a_packet) +{ + uchar rec = PROT_STATE_GIVEME; + service_pmeinfo *service_pme = (service_pmeinfo*)*param; + http_infor *a_http = (http_infor *)(session_info->app_info); + char filename[512] = {0}; + const char* region = NULL; + //static int header_len = 0; + //static int content_len = 0; + + uchar http_state = a_http->http_state; + + if(NULL==session_info) + { + return PROT_STATE_DROPME; + } + + if(service_pme == NULL) + { + if(init_pmeinfo(&service_pme, thread_seq) <0) + { + return PROT_STATE_DROPME; + } + *param = service_pme; + } + std::string prot_flag_str = trans_to_binary(session_info->prot_flag); + MESA_handle_runtime_log(g_log_handle, RLOG_LV_INFO, HTTP_SERVICE_PLUGNAME, + "call http_service entry, http_state is %02x\n, prot_flag mask is %s : %d", http_state, prot_flag_str.c_str(), prot_flag_str.length() - 1); + switch(session_info->prot_flag) + { + case HTTP_STATE: + case HTTP_CONTENT: + break; + case HTTP_UNGZIP_CONTENT: + snprintf(filename, sizeof(filename), "%s/HTTP_%s_%u", LOG_PATH, printaddr(&a_tcp->addr, thread_seq), a_http->http_session_seq); + service_pme->fp = fopen(filename, "a+"); + if(NULL==service_pme->fp) + { + MESA_handle_runtime_log(g_log_handle, RLOG_LV_FATAL, HTTP_SERVICE_PLUGNAME, "%s file open error.", filename); + return PROT_STATE_DROPME; + } + MESA_handle_runtime_log(g_log_handle, RLOG_LV_DEBUG, HTTP_SERVICE_PLUGNAME, "%s file open.",filename); + + fwrite(session_info->buf, session_info->buflen, 1, service_pme->fp); + fflush(service_pme->fp); + //content_len += session_info->buflen; + //printf("content_len:%d\n", content_len); + + snprintf(filename, sizeof(filename), "%s/HTTP_%s_%u", LOG_PATH, printaddr(&a_tcp->addr, thread_seq), a_http->http_session_seq); + MESA_handle_runtime_log(g_log_handle, RLOG_LV_DEBUG, HTTP_SERVICE_PLUGNAME, "%s file close.",filename); + fclose(service_pme->fp); + break; + default: + snprintf(filename, sizeof(filename), "%s/HTTP_%s_%u", LOG_PATH, printaddr(&a_tcp->addr, thread_seq), a_http->http_session_seq); + service_pme->fp = fopen(filename, "a+"); + if(NULL==service_pme->fp) + { + MESA_handle_runtime_log(g_log_handle, RLOG_LV_FATAL, HTTP_SERVICE_PLUGNAME, "%s file open error.", filename); + return PROT_STATE_DROPME; + } + MESA_handle_runtime_log(g_log_handle, RLOG_LV_DEBUG, HTTP_SERVICE_PLUGNAME, "%s file open.",filename); + + region = http_proto_flag2region(session_info->prot_flag); + + fwrite(region, strlen(region), 1, service_pme->fp); + fwrite(":", 1, 1, service_pme->fp); + fwrite(session_info->buf, session_info->buflen, 1, service_pme->fp); + fwrite("\r\n", 2, 1, service_pme->fp); + fflush(service_pme->fp); + //header_len += session_info->buflen; + //printf("header_len:%d\n", header_len); + + snprintf(filename, sizeof(filename), "%s/HTTP_%s_%u", LOG_PATH, printaddr(&a_tcp->addr, thread_seq), a_http->http_session_seq); + MESA_handle_runtime_log(g_log_handle, RLOG_LV_DEBUG, HTTP_SERVICE_PLUGNAME, "%s file close.",filename); + fclose(service_pme->fp); + break; + } + + if(session_info->session_state&SESSION_STATE_CLOSE) + { + if(NULL!=service_pme->fp) + { + + service_pme->fp = NULL; + } + clear_pmeinfo((service_pmeinfo*)*param, thread_seq); + *param = NULL; + } + return rec; +} + +int HTTP_SERVICE_INIT(void) +{ + g_log_handle = MESA_create_runtime_log_handle("./log/http/http_service", 10); + if(g_log_handle == NULL) + { + printf("%s init : get log handle error!\n", HTTP_SERVICE_PLUGNAME); + return -1; + } + return 0; +} + +void HTTP_SERVICE_DESTROY(void) +{ + return ; +} + diff --git a/test/test_plugin/http_service.h b/test/test_plugin/http_service.h new file mode 100644 index 0000000..5d0d423 --- /dev/null +++ b/test/test_plugin/http_service.h @@ -0,0 +1,51 @@ +/* + * HTTP_Service.h + * + * Created on: 2013-8-19 + * Author: lishu + */ + +#ifndef HTTP_SERVICE_H_ +#define HTTP_SERVICE_H_ + +#include <stdio.h> +#include <string.h> +#include <pthread.h> +#include <time.h> +#include </usr/include/net/if.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include </usr/include/linux/sockios.h> +#include <arpa/inet.h> +#include <unistd.h> +#include <ctype.h> +#include <stdlib.h> +#include <regex.h> + + +#include "stream.h" +#include "http.h" + +#define HTTP_SERVICE_PLUGNAME "http_service.so" +#define LOG_PATH "./log/http/" + + +typedef struct service_pmeinfo +{ + FILE* fp; +}service_pmeinfo; + + +#ifdef __cplusplus +extern "C" { +#endif + +uchar HTTP_SERVICE_ENTRY(stSessionInfo* session_info, void **param, int thread_seq, struct streaminfo *a_tcp, void *a_packet); +int HTTP_SERVICE_INIT(void); +void HTTP_SERVICE_DESTROY(void); + +#ifdef __cplusplus +} +#endif + +#endif /* HTTP_SERVICE_H_ */ |
