-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.cpp
More file actions
73 lines (60 loc) · 1.62 KB
/
proxy.cpp
File metadata and controls
73 lines (60 loc) · 1.62 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
#include <iostream>
#include "HttpRequest.h"
#include "HttpApplication.h"
#include "RequestHandler.h"
#include "HttpServer.h"
#include "HttpResponse.h"
#include "Logger.h"
#include "HttpClient.h"
#include <map>
#include <thread>
#include <chrono>
size_t request_no = 0;
pthread_mutex_t proxy_lock;
class Proxy : public RequestHandler
{
public:
void get()
{
Logger log("proxy.log");
std::string URI = request.get_uri();
std::string url = URI;
pthread_mutex_lock(&proxy_lock);
request_no++;
log<<request_no<<" "<<request.get_remote_ip()<<":"<<request.get_remote_port()<<" "<<url<<" GET "<<"\n";
log.flush();
pthread_mutex_unlock(&proxy_lock);
HttpClient cli(url);
//Add headers
for(auto& header : request.get_headers())
if(header.first != "Host")
cli.add_header(header.first, header.second);
cli.fetch();
for(auto& header : cli.response.get_headers())
if(header.first != "Content-length")
cli.add_header(header.first, header.second);
write(cli.response.get_content());
}
};
class Ha : public RequestHandler
{
public:
void get()
{
write("haha\n");
}
};
int main()
{
pthread_mutex_init(&proxy_lock, NULL);
std::map<std::string, std::shared_ptr<Utils::iHandler>> H;
H["/*"] = Utils::make_handler<Proxy>();
H["/ha"] = Utils::make_handler<Ha>();
HttpApplication app(H);
HttpServer server(app);
server.bind(8080);
server.listen(1000);
std::cout<<"Starting proxy..."<<std::endl;
server.start();
return 0;
}