-
Notifications
You must be signed in to change notification settings - Fork 0
/
httprequest.cpp
197 lines (171 loc) · 5.24 KB
/
httprequest.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
192
193
194
195
196
#include "httprequest.h"
#include <exception>
#include <algorithm>
// get hostname from request.
std::string HttpRequest::get_host() {
std::string host;
if(headerpair.count("Host") == 0) { // host not exist.
return hostname = host;
}
host = headerpair["Host"];
std::size_t pos = headerpair["Host"].find_first_of(':');
if(pos == std::string::npos)
hostname = host;
else {
hostname = host.substr(0, pos);
port = host.substr(pos+1);
}
hostname.erase(std::remove(hostname.begin(), hostname.end(), ' '), hostname.end());
return hostname;
}
// get port from request.
std::string HttpRequest::get_port() {
std::size_t pos;
if(headerpair.find("Host") != headerpair.end()){
if( (pos = headerpair["Host"].find_first_of(':')) != std::string::npos ){
return port = headerpair["Host"].substr(pos+1);
}
}
// using meta to determine port.
if(meta.size() == 3){
if(meta[2].find("HTTPS") != std::string::npos){
port = "443";
}
else if(meta[2].find("HTTP") != std::string::npos){
port = "80";
}
}
return port;
// other situation will make the port empty string, we can check empty for exception.
}
// return request's method.
std::string HttpRequest::get_method(){
return meta[0];
}
// return identifier for request to id the request in cache.
std::string HttpRequest::get_identifier(){
return meta[1];
}
// return request's version.
std::string HttpRequest::get_version(){
return meta[2];
}
// deal with CONNECT method of the request.
void HttpRequest::connect(HttpSocket& server, HttpSocket& client){
std::string response(get_version() + " 200 OK\r\n\r\n");
std::cout<<"CONNECT Response: "<<response<<std::endl;
try{
client.send_msg(const_cast<char *>(response.c_str()), response.size());
}
catch(...){
send_502_bad_gateway(server);
return;
}
int client_fd = client.get_fd();
int server_fd = server.get_fd();
// int active;
//connect server and client
while(1){
fd_set read_fd;
FD_ZERO(&read_fd);
FD_SET (client_fd, &read_fd);
FD_SET (server_fd, &read_fd);
int max_fd = client_fd > server_fd ? client_fd : server_fd;
// struct timeval timeout;
// timeout.tv_sec = 1;
// timeout.tv_usec = 500000;//500ms
// active = select(max_fd+1, &read_fd, NULL, NULL, &timeout);
int active = select(max_fd+1, &read_fd, NULL, NULL,NULL);
if(active==0){
break;
}
else{
// std::cout<<"select active\n"<<std::endl;
if(FD_ISSET(client_fd, &read_fd)){
// std::cout<<"=== CLIENT Active ==="<<std::endl;
std::vector<char> buffer(10000,0);
int actual_byte;
try{
actual_byte = client.recv_msg(&buffer.data()[0], 10000, 0);
buffer.resize(actual_byte);
}
catch(...){
close(client_fd);close(server_fd); break;
}
//std::cout<<"actual byte"<<actual_byte<<std::endl;
//if connect closed
if(actual_byte == 0){
break;
}
try{
server.send_msg(&buffer.data()[0], actual_byte);
}
catch(...){
send_502_bad_gateway(client);
close(client_fd);close(server_fd); break;
}
}
else if(FD_ISSET(server_fd, &read_fd)){
//std::cout<<"=== SERVER Active ==="<<std::endl;
std::vector<char> buffer(10000,0);
int actual_byte;
try{
actual_byte = server.recv_msg(&buffer.data()[0], 10000, 0);
buffer.resize(actual_byte);
}
catch(...){
close(client_fd);close(server_fd); break;
}
//std::cout<<"actual byte"<<actual_byte<<std::endl;
//if connect close
if(actual_byte == 0){
break;
}
try{
client.send_msg(&buffer.data()[0], actual_byte);
}
catch(...){
send_502_bad_gateway(server);
close(client_fd);close(server_fd); break;
}
}
}
}
// if(active == 0){
// std::cout<<"Tunnel closed"<<std::endl;
// close(server_fd);
// }
}
// receive request.
void HttpRequest::receive(HttpSocket& sk) {
int id = 0;
try {
id = recv_header(sk);
}
catch (...) {
send_400_bad_request(sk);
throw;
}
if(meta[0] == "POST"){
if(meta[2] == "HTTP/1.0"){
try{
recv_http_1_0(sk);
}
catch(...){
throw;
}
}
if(meta[2] == "HTTP/1.1"){
try{
recv_http_1_1(sk, id);
}
catch(...){
throw;
}
}
else{
send_400_bad_request(sk);
throw std::invalid_argument("invlalid protocol.");
}
}
}