1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 ;
}
|