-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcacheproxy.cpp
191 lines (161 loc) · 5.11 KB
/
pcacheproxy.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
#include <Poco/URI.h>
#include <Poco/Net/HTMLForm.h>
#include <Poco/Util/ServerApplication.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/StreamCopier.h>
#include <PCacheProxy/ServiceServerParamRequest.h>
#include <PCacheProxy/RequestServer.h>
#include <PCacheProxy/ResponseServer.h>
#include <PCacheProxy/RequestStream.h>
#include <PCacheProxy/ResponseStream.h>
#include <PCacheProxy/MemoryCache.h>
#include <sstream>
class ProxyRequestHandler : public Poco::Net::HTTPRequestHandler
{
public:
ProxyRequestHandler(PCacheProxy::Cache::Ptr cache) : _cache(cache) {};
void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
try {
//std::cout << "METHOD: " << request.getMethod() << std::endl;
//std::cout << "URI: " << request.getURI() << std::endl;
PCacheProxy::ServiceServerParamRequest proxy(_cache, "url");
proxy.serverRequest(request, response);
}
catch (Poco::Exception &e)
{
std::cout << "ERROR: " << e.displayText() << std::endl;
if (!response.sent())
{
std::ostream& ostr = response.send();
ostr << "ERROR: " << e.displayText();
}
}
catch (std::exception &e)
{
std::cout << "ERROR: " << e.what() << std::endl;
if (!response.sent())
{
std::ostream& ostr = response.send();
ostr << "ERROR: " << e.what();
}
}
}
private:
PCacheProxy::Cache::Ptr _cache;
};
class ProxyDumpHandler : public Poco::Net::HTTPRequestHandler
{
public:
ProxyDumpHandler() {}
void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
try {
std::cout << "================== DUMP BEGIN =================" << std::endl;
std::cout << "METHOD: " << request.getMethod() << std::endl;
std::cout << "URI: " << request.getURI() << std::endl;
std::cout << "VERSION: " << request.getVersion() << std::endl;
std::cout << "CONTENT-LENGTH: " << request.getContentLength() << std::endl;
for (Poco::Net::HTTPResponse::ConstIterator ri = request.begin(); ri != request.end(); ++ri)
std::cout << "REQUEST HEADER: " << ri->first << " == " << ri->second << std::endl;
if (request.hasContentLength() && request.getContentLength() > 0)
{
std::cout << "************* CONTENT BEGIN ****************" << std::endl;
Poco::StreamCopier::copyStream(request.stream(), std::cout);
std::cout << std::endl << "************* CONTENT END ****************" << std::endl;
}
std::cout << "================== DUMP END =================" << std::endl;
std::ostream& ostr = response.send();
ostr << "OK";
}
catch (Poco::Exception &e)
{
std::cout << "ERROR: " << e.displayText() << std::endl;
if (!response.sent())
{
std::ostream& ostr = response.send();
ostr << "ERROR: " << e.displayText();
}
}
catch (std::exception &e)
{
std::cout << "ERROR: " << e.what() << std::endl;
if (!response.sent())
{
std::ostream& ostr = response.send();
ostr << "ERROR: " << e.what();
}
}
}
};
class ProxyRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
{
public:
ProxyRequestHandlerFactory(PCacheProxy::Cache::Ptr cache) : _cache(cache) {}
Poco::Net::HTTPRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request)
{
Poco::URI uri(request.getURI());
if (/*uri.getHost() == "127.0.0.1" &&*/ uri.getPath() == "/dump")
return new ProxyDumpHandler;
return new ProxyRequestHandler(_cache);
}
private:
PCacheProxy::Cache::Ptr _cache;
};
class App : public Poco::Util::ServerApplication
{
public:
App() {}
protected:
int main(const std::vector<std::string>& args)
{
try
{
//return manualRequest();
PCacheProxy::Cache::Ptr cache(new PCacheProxy::MemoryCache);
Poco::Net::ServerSocket svs(14500);
Poco::Net::HTTPServer srv(new ProxyRequestHandlerFactory(cache), svs, new Poco::Net::HTTPServerParams);
srv.start();
//Poco::Thread::sleep(1000);
//manualRequest();
waitForTerminationRequest();
srv.stop();
}
catch (Poco::Exception &e)
{
std::cout << "ERROR: " << e.displayText() << std::endl;
}
catch (std::exception &e)
{
std::cout << "ERROR: " << e.what() << std::endl;
}
return EXIT_OK;
}
int manualRequest()
{
PCacheProxy::Service proxy(new PCacheProxy::MemoryCache);
//Poco::Net::HTTPRequest req("GET", "http://www.sibit.com.br", Poco::Net::HTTPRequest::HTTP_1_1);
Poco::Net::HTTPRequest req("GET", "http://127.0.0.1:14500/dump", Poco::Net::HTTPRequest::HTTP_1_1);
std::stringstream reqin;
reqin << "{'u':'joao'}";
req.setContentLength(reqin.tellp());
Poco::Net::HTTPResponse resp;
std::stringstream respout;
proxy.request(PCacheProxy::RequestStream(req, reqin), PCacheProxy::ResponseStream(resp, respout));
std::cout << "====================" << std::endl;
std::cout << respout.str() << std::endl;
std::cout << "====================" << std::endl;
std::cin.ignore();
return EXIT_OK;
}
};
int main(int argc, char *argv[])
{
App app;
return app.run(argc, argv);
}