forked from KTZ-Goel/PacketSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.cpp
More file actions
executable file
·121 lines (110 loc) · 3.13 KB
/
http.cpp
File metadata and controls
executable file
·121 lines (110 loc) · 3.13 KB
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
#include "http.h"
#include <stdint.h>
#include <stdio.h>
#include "colors.h"
#include "shared.h"
#include "tags.h"
void handle_http(QList<QStandardItem*>* row, const char* data, uint16_t size)
{
int n; // 读取的字符数
int i = 0; // 数据索引
printf(CYAN " HTTP:\n" RESET);
row->append(new QStandardItem("HTTP"));
if (size == 0) {
printf(" This packet contains no more data\n");
row->append(new QStandardItem("Acknowledgement, No more data"));
return;
}
if (strncmp(data, "GET", 3) == 0 || strncmp(data, "HTTP", 4) == 0) {
bool firstLineRead = false;
do {
n = 0;
printf("\t\t");
if (firstLineRead == false) {
QString infoString;
while (data[i] != '\r') {
putchar(data[i]);
infoString.append(data[i]);
i++;
n++;
}
row->append(new QStandardItem(infoString));
firstLineRead = true;
} else {
while (data[i] != '\r') {
putchar(data[i]);
i++;
n++;
}
}
printf("\n");
i += 2;
} while (n > 0);
} else {
row->append(new QStandardItem("HTTP Data"));
i = 0;
n = 1;
printf("\t\t");
while (i < size) {
if (data[i] >= 32 && data[i] <= 126) {
putchar(data[i]);
} else {
putchar('.');
}
if ((n & 0x3F) == 0) {
printf("\n\t\t");
}
n++;
i++;
}
putchar('\n');
}
}
void handle_http_fill(QString* infoStr, const char* data, uint16_t size)
{
int n; // 读取的字符数
int i = 0; // 数据索引
infoStr->append(HEADER_TAG_START "HTTP:" HEADER_TAG_END NEWLINE);
if (size == 0) {
infoStr->append(TAB "This packet contains no more data" NEWLINE);
return;
}
if (strncmp(data, "GET", 3) == 0 || strncmp(data, "HTTP", 4) == 0) {
do {
n = 0;
infoStr->append(TAB);
while (data[i] != '\r') {
infoStr->append(IS_PRINTABLE(data[i]) ? data[i] : '.');
i++;
n++;
}
infoStr->append(NEWLINE);
i += 2;
} while (n > 0);
} else {
QHash<char, QString> htmlEntities;
htmlEntities.insert('<', "<");
htmlEntities.insert('>', ">");
i = 0;
n = 1;
infoStr->append(TAB);
while (i < size) {
char c = data[i];
if (IS_PRINTABLE(c)) {
if (htmlEntities.contains(c)) {
infoStr->append(htmlEntities.value(c));
} else {
infoStr->append(c);
}
} else {
infoStr->append('.');
}
if (n == 32) {
infoStr->append(NEWLINE TAB);
n = 0;
}
n++;
i++;
}
}
}