-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.cpp
194 lines (163 loc) · 5.41 KB
/
proxy.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
#include <iostream>
#include <string>
#include "httprequest.h"
#include "httpresponse.h"
#include "proxy.h"
#include "log.h"
// make the server up and listen.
void Proxy::compose_up(){
try{
im_server_sk.listen_to(10000);
}
catch(...){
throw;
}
}
// accept client, and connect.
// HttpSocket Proxy::accept_client(){
int Proxy::accept_client(){
int client_fd;
try{
client_fd = im_server_sk.accept_connect();
}
catch(...){
throw;
}
return client_fd;
}
// receive a request from ...
HttpRequest Proxy::recv_request_from(HttpSocket sk){
HttpRequest this_request;
try{
this_request.receive(sk);
}
catch(...){
// re-throw.
throw;
}
return this_request;
}
// receive a response from ...
HttpResponse Proxy::recv_response_from(HttpSocket sk){
HttpResponse this_response;
try{
this_response.receive(sk);
}
catch(...){
// re-throw.
throw;
}
return this_response;
}
//handle all request
void Proxy::handle_request(HttpRequest &request, HttpSocket &server, HttpSocket &client, cache &mycache){
HttpResponse response;
Log log;
if(request.get_method()=="GET"){
std::cout<<" -------- GET -------- "<<std::endl;
if(request.can_store()){
try{
response = mycache.returndata(server, request);
log.output(request.get_id(), "GET " + request.get_host() + " from " + std::to_string(server.get_fd()) + "\n" );
log.timestamp(request.get_id());
response.send(client);
}
catch(...){
log.output(request.get_id(), "[GET] EXCEPTION with " + request.get_identifier() + "\n");
log.timestamp(request.get_id());
throw std::exception();
}
}
else{
try{
log.output(request.get_id(), "not cacheable.\n");
log.timestamp(request.get_id());
// send to server
request.send(server);
// recv from server
response.receive(server);
}
catch(...){
return;
}
// send to client
response.send(client);
}
}
else if(request.get_method()=="CONNECT"){
std::cout<<" -------- CONNECT -------- "<<std::endl;
try{
request.connect(server, client);
}
catch(...){
log.output(request.get_id(),"CONNECT: Error when communicating with server/client. (Request: " + request.get_identifier() + ")\n");
log.timestamp(request.get_id());
throw std::exception();
}
}
else if(request.get_method()=="POST"){
std::cout<<" -------- POST -------- "<<std::endl;
try{
// send to server
request.send(server);
// recv from server
response.receive(server);
// send to client
response.send(client);
}
catch(...){
// return;
log.output(request.get_id(), "POST: Error when communicating with server/client. (Request: " + request.get_identifier() + ")\n");
log.timestamp(request.get_id());
throw std::exception();
}
}
else{
//throw
request.send_400_bad_request(client);
throw std::invalid_argument("invalid method.");
}
}
void Proxy::handle(int client_fd, cache& cache){
std::cout<<" ----- Handling Client_FD = "<<client_fd<<" -----"<<std::endl;
std::cout<<" ---- In Thread "<<std::this_thread::get_id()<<" ---- "<<std::endl;
HttpSocket client_sk;
HttpSocket server_sk;
HttpRequest this_request;
Log log;
try{
client_sk = HttpSocket(client_fd);
this_request = recv_request_from(client_sk);
std::stringstream trystr;
trystr << "request " << this_request.get_method()+" "<<this_request.get_identifier()<<" "<<this_request.get_version() << " from " << this_request.get_host() << " ===\n";
log.output(this_request.get_id(), trystr.str());
log.timestamp(this_request.get_id());
log.log_request(this_request.get_id(), this_request);
}
catch(...){
// LOG err.
close(client_fd);
std::stringstream errstr;
errstr << "=== FAILED Recv Request From Client_FD: " << std::to_string(client_fd) << " in Thread: " << std::this_thread::get_id() << " ===\n";
log.output(this_request.get_id(), errstr.str());
// log.log_request(this_request);
log.timestamp(this_request.get_id());
return;
}
try{
std::cout<<" ---- Connect to Port: "<<this_request.get_port()<<", Host: "<<this_request.get_host()<<" ---- "<<std::endl;
server_sk = HttpSocket(this_request.get_port().c_str(), this_request.get_host().c_str());
handle_request(this_request, server_sk, client_sk, cache);
close(server_sk.get_fd());
close(client_sk.get_fd());
}
catch(...){
this_request.send_502_bad_gateway(client_sk);
std::stringstream errstr;
errstr << "=== FAILED Connect Server/Handle Request: " << " in Thread: " << std::this_thread::get_id() << " ===\n";
log.output(this_request.get_id(), errstr.str());
//log.log_request(this_request);
log.timestamp(this_request.get_id());
return;
}
}