Repository navigation
Expand file tree
/
Copy pathlocal_proxy.cc
More file actions
281 lines (243 loc) · 12.6 KB
/
Copy pathlocal_proxy.cc
File metadata and controls
281 lines (243 loc) · 12.6 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <iostream>
#include <thread>
#include "address.hh"
#include "system_runner.hh"
#include "poller.hh"
#include "http_request_parser.hh"
#include "event_loop.hh"
#include "local_proxy.hh"
#include "length_value_parser.hh"
#include "http_request.hh"
#include "http_header.hh"
#include "http_response.hh"
#include "timestamp.hh"
#include "google/protobuf/io/gzip_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
using namespace std;
using namespace PollerShortNames;
LocalProxy::LocalProxy( const Address & listener_addr, const Address & remote_proxy )
: listener_socket_( TCP ),
remote_proxy_addr_( remote_proxy ),
archive(),
sent_requests()
{
listener_socket_.bind( listener_addr );
listener_socket_.listen();
}
string make_bulk_request( const HTTPRequest & request, const string & scheme )
{
MahimahiProtobufs::BulkRequest bulk_request;
bulk_request.set_scheme( scheme == "https" ? MahimahiProtobufs::BulkRequest_Scheme_HTTPS :
MahimahiProtobufs::BulkRequest_Scheme_HTTP );
bulk_request.mutable_request()->CopyFrom( request.toprotobuf() );
string request_proto;
bulk_request.SerializeToString( &request_proto );
/* Return request string to send (prepended with size) */
return( static_cast<string>( Integer32( request_proto.size() ) ) + request_proto );
}
int LocalProxy::add_bulk_requests( const string & bulk_requests, vector< pair< int, int > > & request_positions )
{
MahimahiProtobufs::BulkMessage requests;
{
google::protobuf::io::ArrayInputStream compressedStream( bulk_requests.c_str(), bulk_requests.size() );
google::protobuf::io::GzipInputStream compressingStream( &compressedStream );
requests.ParseFromZeroCopyStream( &compressingStream );
}
/* add requests to archive */
for ( int i = 0; i < requests.msg_size(); i++ ) {
/* Don't check freshness since these are newer than whatever is in archive */
auto find_result = archive.find_request( requests.msg( i ), false, false );
if ( find_result.first == false ) { /* request not already in archive */
auto pos = archive.add_request( requests.msg( i ) );
request_positions.emplace_back( make_pair( i, pos ) );
}
}
return requests.msg_size();
}
void LocalProxy::handle_response( const string & res, const vector< pair< int, int > > & request_positions, int & response_counter )
{
MahimahiProtobufs::HTTPMessage single_response;
single_response.ParseFromString( res );
for ( unsigned int j = 0; j < request_positions.size(); j++ ) {
if ( response_counter == request_positions.at( j ).first ) { /* we want to add this response */
archive.add_response( single_response, request_positions.at( j ).second );
}
}
response_counter++;
}
template <class SocketType>
void LocalProxy::handle_new_requests( Poller & client_poller, HTTPRequestParser & request_parser, SocketType && client )
{
bool cannot_handle = false;
8000
client_poller.poll( 0 );
while ( ( not request_parser.empty() ) and ( not cannot_handle ) ) {
auto new_req_status = archive.find_request( request_parser.front().toprotobuf(), false );
if ( new_req_status.first and ( new_req_status.second != "" ) ) { /* we have new request on same thread */
//cout << "WROTE RESPONSE FOR: " << request_parser.front().first_line() << " AT: " << timestamp() << endl;
client.write( new_req_status.second );
request_parser.pop();
} else {
cannot_handle = true;
}
}
}
/* function blocks until it can send a response back to the client for the given request */
template <class SocketType1, class SocketType2>
bool LocalProxy::get_response( const HTTPRequest & new_request, const string & scheme, SocketType1 && server, bool & already_connected, SocketType2 && client, HTTPRequestParser & request_parser )
{
//cout << "INCOMING REQUEST: " << new_request.first_line() << "AT: " << timestamp() << endl;
/* check if request and response are in the archive */
auto to_send = archive.find_request( new_request.toprotobuf(), false );
if ( to_send.first ) {
while ( to_send.second == "" ) { /* response is pending */
archive.wait();
to_send = archive.find_request( new_request.toprotobuf(), false );
}
//cout << "WROTE RESPONSE FOR: " << new_request.first_line() << " AT: " << timestamp() << endl;
client.write( to_send.second );
request_parser.pop();
return false;
}
if ( not already_connected ) {
server.connect( remote_proxy_addr_ );
already_connected = true;
}
/* not in archive, so send to remote proxy and wait for response */
Poller server_poller;
LengthValueParser bulk_parser;
vector< pair< int, int > > request_positions;
bool parsed_requests = false;
bool sent_request = false;
bool first_response = true;
bool finished_bulk = false;
int response_counter = 0;
int total_requests = 0;
Poller client_poller;
client_poller.add_action( Poller::Action( client, Direction::In,
[&] () {
string buffer = client.read();
request_parser.parse( buffer );
return ResultType::Continue;
},
[&] () { return not server.eof(); } ) );
server_poller.add_action( Poller::Action( server, Direction::Out,
[&] () {
server.write( make_bulk_request( new_request , scheme ) );
sent_request = true;
auto path_start = new_request.first_line().find( "/" );
auto path_end = new_request.first_line().find( " ", path_start );
string req_sent = new_request.get_header_value( "Host" ) + new_request.first_line().substr( path_start, path_end - path_start );
sent_requests.emplace_back( req_sent );
return ResultType::Continue;
},
[&] () { return not sent_request; } ) );
server_poller.add_action( Poller::Action( server, Direction::In,
[&] () {
string buffer = server.read();
auto res = bulk_parser.parse( buffer );
while ( res.first ) {
if ( not parsed_requests ) { /* it is the requests */
parsed_requests = true;
total_requests = add_bulk_requests( res.second, request_positions );
} else { /* it is a response */
//cout << "RECEIVED A RESPONSE AT: " << timestamp() << endl;
MahimahiProtobufs::HTTPMessage current_response;
{
google::protobuf::io::ArrayInputStream compressedStream( res.second.c_str(), res.second.size() );
google::protobuf::io::GzipInputStream compressingStream( &compressedStream );
current_response.ParseFromZeroCopyStream( &compressingStream );
}
if ( first_response ) { /* this is the first response, send back to client */
HTTPResponse first_res( current_response );
client.write( first_res.str() );
request_parser.pop();
first_response = false;
}
string curr_response;
current_response.SerializeToString( &curr_response );
handle_response( curr_response, request_positions, response_counter );
handle_new_requests( client_poller, request_parser, move( client ) );
}
res = bulk_parser.parse( "" );
}
if ( total_requests == response_counter && total_requests != 0 ) {
//cout << "FINISHED ADDING BULK FOR: " << new_request.first_line() << " WITH # RES: " << total_requests << " AT: " << timestamp() << endl;
finished_bulk = true;
}
return ResultType::Continue;
},
[&] () { return sent_request; } ) );
while ( true ) {
if ( server_poller.poll( -1 ).result == Poller::Result::Type::Exit ) {
return true;
}
if ( finished_bulk ) {
return false;
}
}
}
template <class SocketType>
void LocalProxy::handle_client( SocketType && client, const string & scheme )
{
HTTPRequestParser request_parser;
Poller poller;
Socket server( TCP );
bool already_connected = false;
poller.add_action( Poller::Action( client, Direction::In,
[&] () {
string buffer = client.read();
request_parser.parse( buffer );
while ( not request_parser.empty() ) { /* we have a complete request */
HTTPRequest new_req( request_parser.front() );
bool status = get_response( new_req, scheme, move( server ), already_connected, move( client ), request_parser );
if ( status ) { /* we should handle response */
return ResultType::Exit;
}
}
return ResultType::Continue;
},
[&] () { return not server.eof(); } ) );
while ( true ) {
if ( poller.poll( -1 ).result == Poller::Result::Type::Exit ) {
return;
}
}
}
void LocalProxy::listen( void )
{
thread newthread( [&] ( Socket client ) {
try {
string scheme = "http";
if ( client.original_dest().port() == 443 ) {
scheme = "https";
SSLContext server_context( SERVER );
SecureSocket tls_client( server_context.new_secure_socket( move( client ) ) );
tls_client.accept();
handle_client( move( tls_client ), scheme );
return;
}
handle_client( move( client ), scheme );
return;
} catch ( const Exception & e ) {
e.perror();
}
}, listener_socket_.accept() );
/* don't wait around for the reply */
newthread.detach();
}
void LocalProxy::register_handlers( EventLoop & event_loop )
{
event_loop.add_simple_input_handler( tcp_listener(),
[&] () {
listen();
return ResultType::Continue;
} );
}
void LocalProxy::print_sent_requests( void )
{
for ( uint i = 0; i < sent_requests.size(); i++ ) {
//cout << "Sent: " << sent_requests.at( i ) << endl;
}
//cout << "Total requests sent: " << sent_requests.size() << endl;
}