This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpDetector.cpp
191 lines (152 loc) · 4.76 KB
/
httpDetector.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// Created by brendan on 23/10/18.
//
#include "httpDetector.h"
#include <iostream>
#include <cstring>
bool httpDetector::parseRequestData(unsigned char *buffer, uint16_t max_len) {
unsigned char *start = buffer;
unsigned char *cursor = buffer;
if (max_len < MIN_METHOD_LENGTH) {
return false;
}
// print_bytes(buffer, max_len);
bool isMethodValid = false;
for (std::string method : this->m_vMethodLists) {
if (strncmp(reinterpret_cast<const char *>(cursor), method.c_str(), method.length()) == 0) {
cursor += method.length();
this->method = method;
isMethodValid = true;
}
}
if (!isMethodValid)
return false;
int space_count = 0;
//Sauter les espaces
while ((cursor - start < max_len) && *cursor != 0 && (*cursor == ' ' || *cursor == '\t')) {
space_count ++;
cursor ++;
}
if (*cursor == 0)
return false;
if (!space_count)
return false;
if (cursor - start >= max_len)
return false;
//Verifier l'url
const std::string reserved_char = "!*'();:@&=+$,/?#[]";
const std::string standard_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-~";
int url_count = 0;
while ((cursor - start < max_len) && *cursor != 0 && (reserved_char.find(*cursor) != std::string::npos || standard_chars.find(*cursor) != std::string::npos)) {
url_count ++;
this->url += *cursor;
cursor ++;
}
if (*cursor == 0)
return false;
if (!url_count)
return false;
if (cursor - start >= max_len)
return false;
//Sauter les espaces
space_count = 0;
while ((cursor - start < max_len) && *cursor != 0 && (*cursor == ' ' || *cursor == '\t')) {
space_count ++;
cursor ++;
}
if (*cursor == 0)
return false;
if (!space_count)
return false;
if (cursor - start >= max_len)
return false;
//Vérifier la version du protocol http
bool isProtoVersionValid = false;
for (std::string proto : this->m_vProtoVersionLists) {
if (strncmp(reinterpret_cast<const char *>(cursor), proto.c_str(), proto.length()) == 0) {
cursor += proto.length();
this->protoVersion = proto;
isProtoVersionValid = true;
}
}
if (!isProtoVersionValid)
return false;
this->isPacketValid = true;
this->isRequest = true;
return true;
}
bool httpDetector::parseResponseData(unsigned char *buffer, uint16_t max_len) {
unsigned char *start = buffer;
unsigned char *cursor = buffer;
if (max_len < MIN_METHOD_LENGTH) {
return false;
}
//Vérifier la version du protocol http
bool isProtoVersionValid = false;
for (std::string proto : this->m_vProtoVersionLists) {
if (strncmp(reinterpret_cast<const char *>(cursor), proto.c_str(), proto.length()) == 0) {
cursor += proto.length();
this->protoVersion = proto;
isProtoVersionValid = true;
}
}
if (!isProtoVersionValid)
return false;
int space_count = 0;
//Sauter les espaces
while ((cursor - start < max_len) && *cursor != 0 && (*cursor == ' ' || *cursor == '\t')) {
space_count ++;
cursor ++;
}
if (*cursor == 0)
return false;
if (!space_count)
return false;
if (cursor - start >= max_len)
return false;
//Verifier que le code de retour fasse 3 chiffres
const std::string standard_chars = "0123456789";
int ret_count = 0;
while ((cursor - start < max_len) && *cursor != 0 && standard_chars.find(*cursor) != std::string::npos) {
ret_count ++;
cursor ++;
}
if (*cursor == 0)
return false;
if (ret_count != 3)
return false;
if (cursor - start >= max_len)
return false;
this->isResponse = true;
this->isPacketValid = true;
return true;
}
void httpDetector::parseData(unsigned char *buffer, uint16_t max_len) {
this->url = "";
this->returnCode = "";
this->method = "";
this->protoVersion = "";
if (!this->parseRequestData(buffer, max_len))
this->parseResponseData(buffer, max_len);
}
bool httpDetector::isValiddHTTPPacket() const {
return this->isPacketValid;
}
bool httpDetector::isHTTPRequest() const {
return this->isRequest;
}
bool httpDetector::isHTTPResponse() const {
return this->isResponse;
}
const std::string &httpDetector::getUrl() const {
return this->url;
}
const std::string &httpDetector::getMethod() const {
return this->method;
}
const std::string &httpDetector::getProtocolVersion() const {
return this->protoVersion;
}
const std::string &httpDetector::getReturnCode() const {
return this->returnCode;
}