-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPParse.cpp
189 lines (167 loc) · 5.8 KB
/
HTTPParse.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
//
// Created by user on 19-4-24.
//
#include "HTTPParse.h"
const char *buf_404 = "HTTP/1.1 404 Not Found\r\nContemt-Type:text/plain\r\nContent-Length:13\r\n\r\n404 Not Found";
const char *buf_302 = "HTTP/1.1 302 Found\r\nContemt-Type:text/plain\r\nContent-Length:9\r\n\r\n302 Found";
const char *buf_500 = "HTTP/1.1 500 Internal Server Error\r\nContemt-Type:text/plain\r\nContent-Length:20\r\n\r\n500 Internal Server Error";
char *find_s(char *buf, const char s[]) {
char *c;
c = buf;
while ((c = strpbrk(c, ":"))) {
c--;
int l = strlen(s) - 1;
int i = 0;
while (i < l + 1) {
if ((*c == s[l - i]) || (*c == (s[l - i] - 32))) {
c--;
i++;
} else {
break;
}
}
if (i == l + 1) {
c += (l + 4);
return c;
}
c += l;
}
return nullptr;
}
//#include <iostream>
bool CHttpRequestHeader(HTTPRequest *array, int *error) {
*((char *) array->buf->addr + array->buf->used) = '\0';
if (array->jump) {
return true;
}
if (!array->urn_begin) {
if ((array->urn_begin = strpbrk((char *) array->buf->addr, " /"))) {
array->urn_begin++;
} else if (array->buf->used < 10) {
return false;
} else {
*(error) = 1;
array->firstLine = true;
array->jump = true;
return true;
}
if (!(array->urn_end = strpbrk(array->urn_begin, " ?;"))) {
if (array->buf->used > 500) {
*error = 1;
array->firstLine = true;
array->jump = true;
return true;
}
}
if (*((char *) array->buf->addr) == 'G') {
array->method = METHOD::GET;
} else if (*((char *) array->buf->addr) == 'P') {
array->method = METHOD::POST;
} else {
*(error)++;
array->firstLine = true;
array->jump = true;
return true;
}
}
if (!array->firstLine && !array->query_end) {
if (*(array->urn_end) == '?' || *(array->urn_end) == ';') {
array->query_begin = array->urn_end + 1;
array->query_end = strpbrk(array->urn_end, " ");
//*array->query_end='\0';
array->firstLine = true;
} else {
array->query_begin = nullptr;
array->query_end = nullptr;
}
}
if (!array->headerEnd && !(array->connection)) {
//array->connection = find_s(array->urn_end, "connection");
}
if (!array->headerEnd && !(array->referer_end)) {
if ((array->referer_begin = find_s(array->urn_end, "referer")) &&
(array->referer_end = strpbrk(array->referer_begin, "\r\n"))) {
//*array->referer_end='\0';
array->firstLine = true;
}
}
if (!array->headerEnd && !(array->accept_end)) {
if ((array->accept_begin = find_s(array->urn_end, "accept")) &&
(array->accept_end = strpbrk(array->accept_begin, "\r\n"))) {
//*array->accept_end = '\0';
array->firstLine = true;
}
}
if (!array->headerEnd && !(array->cookie_end)) {
if ((array->cookie_begin = find_s(array->urn_end, "cookie")) &&
(array->cookie_end = strpbrk(array->cookie_begin, "\r\n"))) {
//*array->cookie_end='\0';
array->firstLine = true;
}
}
if (!array->headerEnd && !(array->content_type_end)) {
if ((array->content_type_begin = find_s(array->urn_end, "Content-Type")) &&
(array->content_type_end = strpbrk(array->content_type_begin, "\r\n"))) {
//*array->content_type_end='\0';
array->firstLine = true;
}
}
//std::cout<<int(*((char*)array->buf->addr+445))<<std::endl;
//std::cout<<*(array->urn_end-1)<<std::endl;
if (!array->headerEnd && std::strstr((char *) array->urn_end, "\n\r\n")) {
array->headerEnd = true;
array->firstLine = true;
if (array->method == METHOD::GET) {
array->jump = true;
return true;
} else {
//return false;
}
}
//std::cout<<array->headerEnd<<std::endl;
if (array->headerEnd && array->method == METHOD::POST) {
if ((array->data_begin = find_s(array->urn_end, "content-length"))) {
array->content_length = atoi(array->data_begin);
if ((array->data_begin = strstr(array->urn_end, "\n\r\n"))) {
array->data_begin += 3;
int now_used = array->buf->used - (array->data_begin - (char *) array->begin);
if (array->content_length == now_used) {
array->data_end = array->data_begin + now_used;
return true;
} else {
return false;
}
}
} else if (find_s(array->urn_end, "transfer-encoding")) {
if ((array->data_begin = strstr(array->urn_end, "\n\r\n"))) {
array->data_begin += 3;
if ((array->data_end = strstr(array->urn_end, "\r\n0\r\n"))) {
*array->data_end = '\0';
return true;
} else {
return false;
}
}
return true;
} else {
array->jump = true;
return true;
}
}
return false;
}
void sendstatus(int socket, int statuscode) {
switch (statuscode) {
case 404:
send(socket, buf_404, strlen(buf_404), 0);
break;
case 302:
send(socket, buf_302, strlen(buf_302), 0);
break;
case 500:
send(socket, buf_500, strlen(buf_500), 0);
break;
default:
break;
}
}