diff options
| author | git commit -m first <[email protected]> | 2019-08-29 10:14:11 +0800 |
|---|---|---|
| committer | git commit -m first <[email protected]> | 2019-08-29 10:14:11 +0800 |
| commit | d962394a02426072737d50318e71e2286023fab6 (patch) | |
| tree | b29dfda5f90f237b356b6cd65fcf1557b3096daf /src/http_stat_header.cpp | |
Diffstat (limited to 'src/http_stat_header.cpp')
| -rw-r--r-- | src/http_stat_header.cpp | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/src/http_stat_header.cpp b/src/http_stat_header.cpp new file mode 100644 index 0000000..d724199 --- /dev/null +++ b/src/http_stat_header.cpp @@ -0,0 +1,255 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <syslog.h> +#include <signal.h> +#include <error.h> +#include <getopt.h> +#include <unistd.h> +#include <assert.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/syslog.h> +#include<algorithm> + +#include <MESA/MESA_htable.h> +#include <MESA/MESA_handle_logger.h> +#include <MESA/MESA_prof_load.h> +#include <MESA/field_stat2.h> +#include <MESA/MESA_list_queue.h> +#include <MESA/stream.h> +#include <MESA/http.h> + +#include "http_stat_header.h" +#include "http_header_v1.hpp" +#define HTTP_LOG_DEBUG 0 +g_env_conf_t g_env_conf_info; +long long bflag = 0; +int init_pmeinfo(content_header_flag **flag_pme, int thread_seq) +{ + content_header_flag* pme = (content_header_flag*)dictator_malloc(thread_seq, sizeof(content_header_flag)); + memset(pme, 0, sizeof(content_header_flag)); + *flag_pme = pme; + return 0; +} + +void clear_pmeinfo(content_header_flag *service_pme, int thread_seq) +{ + if(service_pme!=NULL) + { + dictator_free(thread_seq, service_pme); + } + service_pme = NULL; +} + +int checkout_head_val(std::string & line_name, int flag) +{ + int i = 0; + string tmp = line_name; + string::size_type idx; + char special_ch[14] = {'|',':','.',' ','<','>','[',']','#','!','@','\n','\r','\t'}; + for(i = 0; i < 14; i++) + { + idx = line_name.find(special_ch[i]); + if(idx != string::npos) + { + replace(line_name.begin(), line_name.end(), special_ch[i], '*'); + } + } + transform(line_name.begin(), line_name.end(), line_name.begin(), (int (*)(int))tolower); + if(flag == 1) + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_INFO, "HTTP_CONT_TYPE", "src header value[%s], dest header value[%s]", tmp.c_str(), line_name.c_str()); + else if(2 == flag) + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_INFO, "HTTP_CONT_ENCODING", "src header value[%s], dest header value[%s]", tmp.c_str(), line_name.c_str()); + else + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_INFO, "HTTP_OTHER", "src header value[%s], dest header value[%s]", tmp.c_str(), line_name.c_str()); + + return 1; +} + + +void deal_with_data(stSessionInfo * session_info, int thread_seq, struct streaminfo *a_stream, content_header_flag * flag_pme) +{ + /*if(a_stream->curdir == DIR_C2S) + { + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "FLAG", "dir error"); + return; + }*/ + int ret = 0; + long long len = 0; + string line_name; + char content_len[64] = {'0'}; + switch(session_info->prot_flag) + { + case HTTP_CONT_TYPE: + line_name = string((char *)session_info->buf, session_info->buflen); + if(0 == checkout_head_val(line_name, 1)) + { + break; + } + g_env_conf_info.cont_type_fs->operate(FS_OP_ADD, line_name, 1); + flag_pme->type_flag = 1; + break; + case HTTP_CONT_ENCODING: + line_name = string((char *)session_info->buf, session_info->buflen); + if(0 == checkout_head_val(line_name, 2)) + { + break; + } + g_env_conf_info.cont_encoding_fs->operate(FS_OP_ADD, line_name,1); + flag_pme->encoding_flag = 1; + break; + case HTTP_CONT_LENGTH: + memcpy(content_len,(char *)session_info->buf, session_info->buflen); + len = atoll(content_len); + ret = g_env_conf_info.cont_lenght_fs->content_lenght_operate(len); + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "HTTP_CONT_LENGTH", "content_length[%s], buflen:%d, addr[%s], ret = [%d]", content_len, session_info->buflen, printaddr(&(a_stream->addr), thread_seq), ret); + break; + case HTTP_RES_LINE: + flag_pme->uri_flag = 1; + break; + default: + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "ABNORMAL_HTTP", "buf:%s", (char *)session_info->buf); + break; + } +} + +void print_addr(stSessionInfo * session_info, int thread_seq, struct streaminfo *a_stream, content_header_flag * flag_pme) +{ + char p[1024] = {'\0'}; + if(session_info && session_info->buf) + { + memcpy(p, (char *)session_info->buf, session_info->buflen); +#ifdef HTTP_LOG_DEBUG + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "TEST", "[%p],[%lld],flag[%d]", flag_pme, flag_pme->bflag, session_info->session_state); +#endif + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "SESSION_INFO", "buf:[%s],buflen:[%d],addr:[%s]", p,session_info->buflen, printaddr(&(a_stream->addr), thread_seq)); + } + else + { +#ifdef HTTP_LOG_DEBUG + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "TEST", "[%p],[%lld],flag[%d]", flag_pme, flag_pme->bflag, session_info->session_state); +#endif + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "SESSION_INFO", "addr:[%s]", printaddr(&(a_stream->addr), thread_seq)); + } + return; +} + +char HTTP_STAT_HEADER_ENTRY(stSessionInfo * session_info,void **pme, int thread_seq,struct streaminfo *a_stream,void *a_packet) +{ + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "HTTP_STAT_HEADER_ENTRY","HTTP_STAT_HEADER_ENTRY:[%d]", session_info->session_state); + content_header_flag * flag_pme = (content_header_flag *)*pme; + if(NULL==session_info) + { + return PROT_STATE_DROPME; + } + + if(NULL == flag_pme) + { + if(init_pmeinfo(&flag_pme, thread_seq)<0) + { + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "INIT", "error"); + return PROT_STATE_DROPME; + } + flag_pme->bflag = (++bflag); + *pme = flag_pme; + } +#ifdef HTTP_LOG_DEBUG + if(session_info->session_state&SESSION_STATE_PENDING) + { + __sync_add_and_fetch(&(g_env_conf_info.pend_sum), 1); + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "s-sum1", "[%p],[%lld],pend_sum:%lld, addr:[%s],state[%d]",flag_pme,flag_pme->bflag,g_env_conf_info.pend_sum, printaddr(&(a_stream->addr), thread_seq), session_info->session_state); + } +#endif + if(session_info->buflen < 200) + { + print_addr(session_info, thread_seq, a_stream, flag_pme); + deal_with_data(session_info, thread_seq, a_stream, flag_pme); + } + else + { + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_FATAL, "HTTP_HEADER_ERROR", "The length of header:[%s]>200,[%d] bytes. Not do statistics! [%s]", session_info->buf, session_info->buflen, printaddr(&(a_stream->addr), thread_seq)); + } + if(session_info->session_state&SESSION_STATE_CLOSE) + { +#ifdef HTTP_LOG_DEBUG + __sync_add_and_fetch(&(g_env_conf_info.close_sum), 1); + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "s-sum2", "[%p],[%lld],close_sum:%lld, addr:[%s]",flag_pme,flag_pme->bflag,g_env_conf_info.close_sum, printaddr(&(a_stream->addr), thread_seq)); +#endif + if(1 == flag_pme->uri_flag && 1 != flag_pme->type_flag) + { + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "NO_TYPE", "[%p],[%lld]", flag_pme, flag_pme->bflag); + g_env_conf_info.cont_type_fs->set_nocontent_num(); + } + if(1 == flag_pme->uri_flag && 1 != flag_pme->encoding_flag) + { + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "NO_ENCODING", "[%p], [%lld]", flag_pme, flag_pme->bflag); + g_env_conf_info.cont_encoding_fs->set_nocontent_num(); + } + clear_pmeinfo((content_header_flag*)*pme, thread_seq); +#ifdef HTTP_LOG_DEBUG + MESA_handle_runtime_log(g_env_conf_info.logger, RLOG_LV_DEBUG, "sum", "pend_sum:%lld, close_sum:%lld", g_env_conf_info.pend_sum, g_env_conf_info.close_sum); +#endif + //return PROT_STATE_DROPME; + } + return PROT_STATE_GIVEME; +} + + +int http_service_readconf(const char* filename) +{ + MESA_load_profile_short_def(filename, "LOG", "log_level", (short*)&(g_env_conf_info.log_level), 30); + MESA_load_profile_string_def(filename, "LOG", "log_path", g_env_conf_info.log_path, sizeof(g_env_conf_info.log_path), DEFAULT_LOG_PATH); + MESA_load_profile_string_def(filename, "LOG", "fs_path", g_env_conf_info.fs_path, sizeof(g_env_conf_info.fs_path),DEFAULT_FS_PATH); + g_env_conf_info.logger = MESA_create_runtime_log_handle(g_env_conf_info.log_path, g_env_conf_info.log_level); + if(NULL == g_env_conf_info.logger) + { + printf("http_stat_header.so get log handle error!\n"); + return -1; + } + return 0; +} + +void * update_ratio(void * arg) +{ + while(1) + { + sleep(10); + g_env_conf_info.cont_type_fs->operate_ratio(); + g_env_conf_info.cont_encoding_fs->operate_ratio(); + g_env_conf_info.cont_lenght_fs->passive_output(); + } + return NULL; +} + +int HTTP_STAT_HEADER_INIT() +{ + pthread_t thread_tid; + pthread_attr_t attr; + memset(&g_env_conf_info, 0, sizeof(g_env_conf_t)); + if(0 != http_service_readconf(CONF_PATH)) + { + printf("http_stat_header.so http_service.conf read error!\n"); + return -1; + } + g_env_conf_info.cont_type_fs = new ContentTable(CONT_TYPE, g_env_conf_info.fs_path); + g_env_conf_info.cont_type_fs->start(); + g_env_conf_info.cont_encoding_fs = new ContentTable(CONT_ENCODING, g_env_conf_info.fs_path); + g_env_conf_info.cont_encoding_fs->start(); + g_env_conf_info.cont_lenght_fs = new ContentHistogram(CONT_LENGHT, g_env_conf_info.fs_path); + g_env_conf_info.cont_lenght_fs->start(); + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + if(0 != pthread_create(&thread_tid, &attr, update_ratio, NULL)) + { + printf("create thread update_ratio failed!\n"); + return -1; + } + return 0; +} + +void HTTP_STAT_HEADER_DESTROY() +{ + return; +} + |
