-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathhttp.cpp
122 lines (114 loc) · 3.48 KB
/
http.cpp
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
/*
*
* Copyright (C) Max <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "http.h"
#include "worker.h"
namespace http
{
int on_url_ext (http_parser *p, const char* at, size_t length, dpi_pkt_infos_t* pkt_informations, void** flow_specific_user_data, void* user_data)
{
WorkerThread *obj = (WorkerThread *) user_data;
// if(length > 0 && p->type==HTTP_REQUEST && (p->method == DPI_HTTP_POST || p->method == DPI_HTTP_GET || p->method == DPI_HTTP_HEAD))
if(length > 0 && p->type==HTTP_REQUEST && p->method == DPI_HTTP_GET)
{
struct http_req_buf *d = (struct http_req_buf *) *flow_specific_user_data;
if(d == nullptr)
{
d = obj->allocateHTTPBuf();
if(unlikely(d == nullptr))
{
obj->getStats().dpi_no_mempool_http++;
return 0;
}
*flow_specific_user_data = d;
}
if(d->uri.length + length > d->uri.buf_size)
length = d->uri.buf_size - d->uri.length;
if(likely(length > 0))
{
for(size_t i = 0; i < length; i++)
d->uri.buf[d->uri.length++] = at[i];
// rte_memcpy(d->uri.buf + d->uri.length, at, length);
// d->uri.length += length;
}
}
return 0;
}
int on_header_field_ext(http_parser *p, const char *at, size_t length, dpi_pkt_infos_t* pkt_informations, void** flow_specific_user_data, void* user_data)
{
if(*flow_specific_user_data != NULL && length > 0)
{
struct http_req_buf *d = (struct http_req_buf *) *flow_specific_user_data;
for(size_t z = 0; z < length; z++)
{
switch (d->h_state)
{
case hstate_nothing:
switch (at[z])
{
case 'h':
case 'H':
d->h_state = http::hstate_host;
d->h_prev_char = at[z];
break;
}
break;
case hstate_host:
if(((d->h_prev_char == 'h' || d->h_prev_char == 'H') && (at[z] == 'o' || at[z] == 'O')) || ((d->h_prev_char == 'O' || d->h_prev_char == 'o') && (at[z] == 's' || at[z] == 'S')))
{
d->h_prev_char = at[z];
break;
}
if((d->h_prev_char == 's' || d->h_prev_char == 'S') && (at[z] == 't' || at[z] == 'T'))
{
d->host_r.length = 0;
break;
}
d->h_state = hstate_nothing;
break;
default:
d->h_prev_char = 0;
d->h_state = hstate_nothing;
break;
}
}
}
return 0;
}
int on_header_value_ext(http_parser *p, const char *at, size_t length, dpi_pkt_infos_t* pkt_informations, void** flow_specific_user_data, void* user_data)
{
if(*flow_specific_user_data != NULL && length > 0)
{
struct http::http_req_buf *d = (struct http::http_req_buf *) *flow_specific_user_data;
switch (d->h_state)
{
case http::hstate_host:
if(d->host_r.length + length > d->host_r.buf_size)
length = d->host_r.buf_size - d->host_r.length;
for(size_t i = 0; i < length; i++)
d->host_r.buf[d->host_r.length++] = at[i];
// rte_memcpy(d->host_r.buf + d->host_r.length, at, length);
// d->host_r.length += length;
break;
default:
break;
}
}
return 0;
}
}