1 | /**
|
---|
2 | * CRXProxy.cpp
|
---|
3 | */
|
---|
4 |
|
---|
5 | #include "CRXProxy.h"
|
---|
6 |
|
---|
7 | #include <string.h>
|
---|
8 |
|
---|
9 | CRXFilter CRXProxy::mFilter;
|
---|
10 |
|
---|
11 | CRXProxy::CRXProxy (void)
|
---|
12 | : mIsIntercepted (false),
|
---|
13 | mClient (0),
|
---|
14 | mServer (0)
|
---|
15 | {
|
---|
16 | /*----------------------------------------------------------------*/
|
---|
17 | /*----------------------------------------------------------------*/
|
---|
18 | }
|
---|
19 |
|
---|
20 | CRXProxy::~CRXProxy (void)
|
---|
21 | {
|
---|
22 | /*----------------------------------------------------------------*/
|
---|
23 | Close ();
|
---|
24 | /*----------------------------------------------------------------*/
|
---|
25 | }
|
---|
26 |
|
---|
27 | CRXProxy *
|
---|
28 | CRXProxy::GetNewInstance(void)
|
---|
29 | {
|
---|
30 | /*----------------------------------------------------------------*/
|
---|
31 | /*----------------------------------------------------------------*/
|
---|
32 |
|
---|
33 | return new(std::nothrow) CRXProxy ();
|
---|
34 | }
|
---|
35 |
|
---|
36 | void
|
---|
37 | CRXProxy::ReleaseInstance (void)
|
---|
38 | {
|
---|
39 | /*----------------------------------------------------------------*/
|
---|
40 | delete this;
|
---|
41 | /*----------------------------------------------------------------*/
|
---|
42 | }
|
---|
43 |
|
---|
44 | void
|
---|
45 | CRXProxy::SetClientSocket (const int aSocket)
|
---|
46 | {
|
---|
47 | /*----------------------------------------------------------------*/
|
---|
48 | if (aSocket <= 0)
|
---|
49 | return ;
|
---|
50 |
|
---|
51 | mClient = aSocket;
|
---|
52 | /*----------------------------------------------------------------*/
|
---|
53 | }
|
---|
54 |
|
---|
55 | void
|
---|
56 | CRXProxy::Close (void)
|
---|
57 | {
|
---|
58 | /*----------------------------------------------------------------*/
|
---|
59 | mServer.Close ();
|
---|
60 | mClient.Close ();
|
---|
61 | /*----------------------------------------------------------------*/
|
---|
62 | }
|
---|
63 |
|
---|
64 | void
|
---|
65 | CRXProxy::SetRequestFilter (const E_CRX_FILTER_REQUEST aType,
|
---|
66 | const bool aIsMatched,
|
---|
67 | const std::string aValue)
|
---|
68 | {
|
---|
69 | /*----------------------------------------------------------------*/
|
---|
70 | mFilter.SetRequestFilter (aType, aIsMatched, aValue);
|
---|
71 | /*----------------------------------------------------------------*/
|
---|
72 | }
|
---|
73 |
|
---|
74 | void
|
---|
75 | CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
76 | {
|
---|
77 | /*----------------------------------------------------------------*/
|
---|
78 | mFilter.RemoveRequestFilter (aType);
|
---|
79 | /*----------------------------------------------------------------*/
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool
|
---|
83 | CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
84 | {
|
---|
85 | /*----------------------------------------------------------------*/
|
---|
86 | /*----------------------------------------------------------------*/
|
---|
87 |
|
---|
88 | return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
|
---|
89 | }
|
---|
90 |
|
---|
91 | int
|
---|
92 | CRXProxy::Forward (void)
|
---|
93 | {
|
---|
94 | int aResult = 0;
|
---|
95 |
|
---|
96 | /*----------------------------------------------------------------*/
|
---|
97 | aResult = ReceiveRequest ();
|
---|
98 | if (aResult < 0)
|
---|
99 | {
|
---|
100 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
101 | CRX_ERROR (aResult, "Failed to receive from client");
|
---|
102 | }
|
---|
103 |
|
---|
104 | aResult = SendRequest ();
|
---|
105 | if (aResult < 0)
|
---|
106 | {
|
---|
107 | aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
|
---|
108 | CRX_ERROR (aResult, "Failed to send to server");
|
---|
109 | return aResult;
|
---|
110 | }
|
---|
111 |
|
---|
112 | aResult = ReceiveResponse ();
|
---|
113 | if (aResult < 0)
|
---|
114 | {
|
---|
115 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
|
---|
116 | CRX_ERROR (aResult, "Failed to receive from server");
|
---|
117 | }
|
---|
118 |
|
---|
119 | aResult = SendResponse ();
|
---|
120 | if (aResult < 0)
|
---|
121 | {
|
---|
122 | aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
|
---|
123 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
124 | return aResult;
|
---|
125 | }
|
---|
126 | /*----------------------------------------------------------------*/
|
---|
127 |
|
---|
128 | return aResult;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void
|
---|
132 | CRXProxy::SetHttpRequest (const char * aHttpRequest)
|
---|
133 | {
|
---|
134 | /*----------------------------------------------------------------*/
|
---|
135 | mHttpRequest = aHttpRequest;
|
---|
136 | /*----------------------------------------------------------------*/
|
---|
137 | }
|
---|
138 |
|
---|
139 | char *
|
---|
140 | CRXProxy::GetHttpRequest (char * aBuffer,
|
---|
141 | const int aBufferSize) const
|
---|
142 | {
|
---|
143 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
144 |
|
---|
145 | /*----------------------------------------------------------------*/
|
---|
146 | if (aPtr == NULL)
|
---|
147 | return NULL;
|
---|
148 |
|
---|
149 | if (aBufferSize > 0)
|
---|
150 | memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aBufferSize);
|
---|
151 | /*----------------------------------------------------------------*/
|
---|
152 |
|
---|
153 | return aBuffer;
|
---|
154 | }
|
---|
155 |
|
---|
156 | int
|
---|
157 | CRXProxy::GetHttpRequestLength (void) const
|
---|
158 | {
|
---|
159 | /*----------------------------------------------------------------*/
|
---|
160 | /*----------------------------------------------------------------*/
|
---|
161 |
|
---|
162 | return mHttpRequest.GetHeader().length ();
|
---|
163 | }
|
---|
164 |
|
---|
165 | void
|
---|
166 | CRXProxy::SetHttpResponse (const char * aHttpResponse)
|
---|
167 | {
|
---|
168 | /*----------------------------------------------------------------*/
|
---|
169 | mHttpResponse = aHttpResponse;
|
---|
170 | /*----------------------------------------------------------------*/
|
---|
171 | }
|
---|
172 |
|
---|
173 | char *
|
---|
174 | CRXProxy::GetHttpResponse (char * aBuffer,
|
---|
175 | const int aBufferSize) const
|
---|
176 | {
|
---|
177 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
178 |
|
---|
179 | int aRemainLength = aBufferSize;
|
---|
180 |
|
---|
181 | /*----------------------------------------------------------------*/
|
---|
182 | if (aPtr == NULL)
|
---|
183 | return NULL;
|
---|
184 |
|
---|
185 | if (aRemainLength > 0)
|
---|
186 | memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aRemainLength);
|
---|
187 |
|
---|
188 | aRemainLength -= mHttpResponse.GetHeader ().length ();
|
---|
189 |
|
---|
190 | if (aRemainLength > 0)
|
---|
191 | memcpy (aPtr, mHttpResponse.GetContentBody (), aRemainLength);
|
---|
192 | /*----------------------------------------------------------------*/
|
---|
193 |
|
---|
194 | return aBuffer;
|
---|
195 | }
|
---|
196 |
|
---|
197 | int
|
---|
198 | CRXProxy::GetHttpResponseLength (void) const
|
---|
199 | {
|
---|
200 | /*----------------------------------------------------------------*/
|
---|
201 | /*----------------------------------------------------------------*/
|
---|
202 |
|
---|
203 | return mHttpResponse.GetHeader ().length ()
|
---|
204 | + mHttpResponse.GetContentLength ();
|
---|
205 | }
|
---|
206 |
|
---|
207 | int
|
---|
208 | CRXProxy::ReceiveRequest (void)
|
---|
209 | {
|
---|
210 | int aResult = 0;
|
---|
211 |
|
---|
212 | const unsigned int aBufferSize = 1024 * 56;
|
---|
213 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
214 |
|
---|
215 | /*----------------------------------------------------------------*/
|
---|
216 | aResult = mClient.Receive (aBuffer, aBufferSize);
|
---|
217 | if (aResult < 0)
|
---|
218 | {
|
---|
219 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
220 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
221 | CRX_ERROR (aResult, "Failed to receive from client");
|
---|
222 | return aResult;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /* parse http request */
|
---|
226 | mHttpRequest = aBuffer;
|
---|
227 | /*----------------------------------------------------------------*/
|
---|
228 |
|
---|
229 | return aResult < 0 ? aResult : 0;
|
---|
230 | }
|
---|
231 |
|
---|
232 | int
|
---|
233 | CRXProxy::SendRequest (void)
|
---|
234 | {
|
---|
235 | int aResult = 0;
|
---|
236 | int aSize = mHttpRequest.GetHeader ().length ();
|
---|
237 |
|
---|
238 | /*----------------------------------------------------------------*/
|
---|
239 | if (!mServer)
|
---|
240 | {
|
---|
241 | /* connect */
|
---|
242 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), 1);
|
---|
243 | if (aResult < 0)
|
---|
244 | {
|
---|
245 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
246 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
247 | CRX_ERROR (aResult, "Failed to connect to server <%s>", mHttpRequest.GetURL ().c_str ());
|
---|
248 | return aResult;
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
253 | if (aResult != aSize)
|
---|
254 | {
|
---|
255 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
256 | CRX_ERROR (aResult, "Failed to send to server <%s>", mHttpRequest.GetURL ().c_str ());
|
---|
257 | }
|
---|
258 | /*----------------------------------------------------------------*/
|
---|
259 |
|
---|
260 | return aResult < 0 ? aResult : 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | int
|
---|
264 | CRXProxy::ReceiveResponse (void)
|
---|
265 | {
|
---|
266 | int aResult = 0;
|
---|
267 |
|
---|
268 | const unsigned int aBufferSize = 1024 * 64;
|
---|
269 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
270 |
|
---|
271 | std::string aHttpResponse;
|
---|
272 |
|
---|
273 | /*----------------------------------------------------------------*/
|
---|
274 | for (;;)
|
---|
275 | {
|
---|
276 | memset (aBuffer, 0x00, sizeof (aBuffer));
|
---|
277 |
|
---|
278 | aResult = mServer.Receive (aBuffer, aBufferSize);
|
---|
279 | if (aResult < 0)
|
---|
280 | {
|
---|
281 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
282 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
283 | CRX_ERROR (aResult, "Failed to receive from server");
|
---|
284 | return aResult;
|
---|
285 | }
|
---|
286 | else if (aResult == 0)
|
---|
287 | {
|
---|
288 | break;
|
---|
289 | }
|
---|
290 |
|
---|
291 | aResult = mHttpResponse.SetResponse (aBuffer, aResult);
|
---|
292 | if (aResult)
|
---|
293 | {
|
---|
294 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
295 | CRX_ERROR (aResult, "Failed to set response");
|
---|
296 | return aResult;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | /*----------------------------------------------------------------*/
|
---|
300 |
|
---|
301 | return aResult < 0 ? aResult : 0;
|
---|
302 | }
|
---|
303 |
|
---|
304 | int
|
---|
305 | CRXProxy::SendResponse (void)
|
---|
306 | {
|
---|
307 | int aResult = 0;
|
---|
308 | int aSize = 0;
|
---|
309 |
|
---|
310 | /*----------------------------------------------------------------*/
|
---|
311 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
312 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
313 | if (aResult != aSize)
|
---|
314 | {
|
---|
315 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
316 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
317 | return aResult;
|
---|
318 | }
|
---|
319 |
|
---|
320 | aSize = mHttpResponse.GetContentLength ();
|
---|
321 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
322 | if (aResult != aSize)
|
---|
323 | {
|
---|
324 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
325 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
326 | }
|
---|
327 | /*----------------------------------------------------------------*/
|
---|
328 |
|
---|
329 | return aResult < 0 ? aResult : 0;
|
---|
330 | } |
---|