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 | : mClient (0),
|
---|
13 | mServer (0),
|
---|
14 | mServerTimeout (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::SetServerTimeout (const int aTimeout)
|
---|
57 | {
|
---|
58 | /*----------------------------------------------------------------*/
|
---|
59 | mServerTimeout = aTimeout;
|
---|
60 | /*----------------------------------------------------------------*/
|
---|
61 | }
|
---|
62 |
|
---|
63 | void
|
---|
64 | CRXProxy::Close (void)
|
---|
65 | {
|
---|
66 | /*----------------------------------------------------------------*/
|
---|
67 | mServer.Close ();
|
---|
68 | mClient.Close ();
|
---|
69 | /*----------------------------------------------------------------*/
|
---|
70 | }
|
---|
71 |
|
---|
72 | void
|
---|
73 | CRXProxy::SetRequestFilter (const E_CRX_FILTER_REQUEST aType,
|
---|
74 | const bool aIsMatched,
|
---|
75 | const std::string aValue)
|
---|
76 | {
|
---|
77 | /*----------------------------------------------------------------*/
|
---|
78 | mFilter.SetRequestFilter (aType, aIsMatched, aValue);
|
---|
79 | /*----------------------------------------------------------------*/
|
---|
80 | }
|
---|
81 |
|
---|
82 | void
|
---|
83 | CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
84 | {
|
---|
85 | /*----------------------------------------------------------------*/
|
---|
86 | mFilter.RemoveRequestFilter (aType);
|
---|
87 | /*----------------------------------------------------------------*/
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool
|
---|
91 | CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
92 | {
|
---|
93 | /*----------------------------------------------------------------*/
|
---|
94 | /*----------------------------------------------------------------*/
|
---|
95 |
|
---|
96 | return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
|
---|
97 | }
|
---|
98 |
|
---|
99 | int
|
---|
100 | CRXProxy::Forward (void)
|
---|
101 | {
|
---|
102 | int aResult = 0;
|
---|
103 |
|
---|
104 | /*----------------------------------------------------------------*/
|
---|
105 | aResult = ReceiveRequest ();
|
---|
106 | if (aResult < 0)
|
---|
107 | {
|
---|
108 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
109 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
110 | }
|
---|
111 |
|
---|
112 | aResult = SendRequest ();
|
---|
113 | if (aResult < 0)
|
---|
114 | {
|
---|
115 | aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
|
---|
116 | CRX_ERROR_SET (aResult, "Failed to send to server.");
|
---|
117 | return aResult;
|
---|
118 | }
|
---|
119 |
|
---|
120 | aResult = ReceiveResponse ();
|
---|
121 | if (aResult < 0)
|
---|
122 | {
|
---|
123 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
|
---|
124 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
125 | }
|
---|
126 |
|
---|
127 | aResult = SendResponse ();
|
---|
128 | if (aResult < 0)
|
---|
129 | {
|
---|
130 | aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
|
---|
131 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
132 | return aResult;
|
---|
133 | }
|
---|
134 | /*----------------------------------------------------------------*/
|
---|
135 |
|
---|
136 | return aResult;
|
---|
137 | }
|
---|
138 |
|
---|
139 | void
|
---|
140 | CRXProxy::SetHttpRequest (const char * aHttpRequest)
|
---|
141 | {
|
---|
142 | /*----------------------------------------------------------------*/
|
---|
143 | mHttpRequest = aHttpRequest;
|
---|
144 | /*----------------------------------------------------------------*/
|
---|
145 | }
|
---|
146 |
|
---|
147 | char *
|
---|
148 | CRXProxy::GetHttpRequest (char * aBuffer,
|
---|
149 | const int aBufferSize) const
|
---|
150 | {
|
---|
151 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
152 |
|
---|
153 | int aCopyLength = 0;
|
---|
154 | int aRemainLength = aBufferSize;
|
---|
155 | const int aHeaderLength = mHttpRequest.GetHeader ().length ();
|
---|
156 |
|
---|
157 | /*----------------------------------------------------------------*/
|
---|
158 | if (aPtr == NULL)
|
---|
159 | return NULL;
|
---|
160 |
|
---|
161 | aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
|
---|
162 | if (aCopyLength > 0)
|
---|
163 | memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aCopyLength);
|
---|
164 | /*----------------------------------------------------------------*/
|
---|
165 |
|
---|
166 | return aBuffer;
|
---|
167 | }
|
---|
168 |
|
---|
169 | int
|
---|
170 | CRXProxy::GetHttpRequestLength (void) const
|
---|
171 | {
|
---|
172 | /*----------------------------------------------------------------*/
|
---|
173 | /*----------------------------------------------------------------*/
|
---|
174 |
|
---|
175 | return mHttpRequest.GetHeader().length ();
|
---|
176 | }
|
---|
177 |
|
---|
178 | void
|
---|
179 | CRXProxy::SetHttpResponse (const char * aHttpResponse)
|
---|
180 | {
|
---|
181 | /*----------------------------------------------------------------*/
|
---|
182 | mHttpResponse = aHttpResponse;
|
---|
183 | /*----------------------------------------------------------------*/
|
---|
184 | }
|
---|
185 |
|
---|
186 | char *
|
---|
187 | CRXProxy::GetHttpResponseHeader (char * aBuffer,
|
---|
188 | const int aBufferSize) const
|
---|
189 | {
|
---|
190 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
191 |
|
---|
192 | int aCopyLength = 0;
|
---|
193 | int aRemainLength = aBufferSize;
|
---|
194 | const int aHeaderLength = mHttpResponse.GetHeader ().length ();
|
---|
195 |
|
---|
196 | /*----------------------------------------------------------------*/
|
---|
197 | if (aPtr == NULL)
|
---|
198 | return NULL;
|
---|
199 |
|
---|
200 | aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
|
---|
201 | if (aCopyLength > 0)
|
---|
202 | memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aCopyLength);
|
---|
203 | /*----------------------------------------------------------------*/
|
---|
204 |
|
---|
205 | return aBuffer;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int
|
---|
209 | CRXProxy::GetHttpResponseHeaderLength (void) const
|
---|
210 | {
|
---|
211 | /*----------------------------------------------------------------*/
|
---|
212 | /*----------------------------------------------------------------*/
|
---|
213 |
|
---|
214 | return mHttpResponse.GetHeader ().length ();
|
---|
215 | }
|
---|
216 |
|
---|
217 | char *
|
---|
218 | CRXProxy::GetHttpResponseBody (char * aBuffer,
|
---|
219 | const int aBufferSize) const
|
---|
220 | {
|
---|
221 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
222 |
|
---|
223 | int aCopyLength = 0;
|
---|
224 | int aRemainLength = aBufferSize;
|
---|
225 | const int aContentLength = mHttpResponse.GetContentLength ();
|
---|
226 |
|
---|
227 | /*----------------------------------------------------------------*/
|
---|
228 | if (aPtr == NULL)
|
---|
229 | return NULL;
|
---|
230 |
|
---|
231 | aCopyLength = aRemainLength < aContentLength ? aRemainLength : aContentLength;
|
---|
232 | if (aCopyLength > 0)
|
---|
233 | memcpy (aPtr, mHttpResponse.GetContentBody (), aCopyLength);
|
---|
234 | /*----------------------------------------------------------------*/
|
---|
235 |
|
---|
236 | return aBuffer;
|
---|
237 | }
|
---|
238 |
|
---|
239 | int
|
---|
240 | CRXProxy::GetHttpResponseBodyLength (void) const
|
---|
241 | {
|
---|
242 | /*----------------------------------------------------------------*/
|
---|
243 | /*----------------------------------------------------------------*/
|
---|
244 |
|
---|
245 | return mHttpResponse.GetContentLength ();
|
---|
246 | }
|
---|
247 |
|
---|
248 | int
|
---|
249 | CRXProxy::ReceiveRequest (void)
|
---|
250 | {
|
---|
251 | int aResult = 0;
|
---|
252 |
|
---|
253 | const unsigned int aBufferSize = 1024 * 56;
|
---|
254 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
255 |
|
---|
256 | /*----------------------------------------------------------------*/
|
---|
257 | aResult = mClient.Receive (aBuffer, aBufferSize);
|
---|
258 | if (aResult < 0)
|
---|
259 | {
|
---|
260 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
261 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
262 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
263 | return aResult;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /* parse http request */
|
---|
267 | mHttpRequest = aBuffer;
|
---|
268 | /*----------------------------------------------------------------*/
|
---|
269 |
|
---|
270 | return aResult < 0 ? aResult : 0;
|
---|
271 | }
|
---|
272 |
|
---|
273 | int
|
---|
274 | CRXProxy::SendRequest (void)
|
---|
275 | {
|
---|
276 | int aResult = 0;
|
---|
277 | int aSize = mHttpRequest.GetHeader ().length ();
|
---|
278 |
|
---|
279 | struct sockaddr_in aSockName;
|
---|
280 | socklen_t aSockLen = sizeof (aSockName);
|
---|
281 |
|
---|
282 | /*----------------------------------------------------------------*/
|
---|
283 | if (!mServer)
|
---|
284 | {
|
---|
285 | /* connect */
|
---|
286 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
|
---|
287 | if (aResult < 0)
|
---|
288 | {
|
---|
289 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
290 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
291 | CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
292 | return aResult;
|
---|
293 | }
|
---|
294 |
|
---|
295 | if (getsockname (mServer, (struct sockaddr *) &aSockName, &aSockLen) == 0)
|
---|
296 | CRX_DEBUG_TRACE ("Connected on port number (%d)\n", ntohs (aSockName.sin_port));
|
---|
297 | }
|
---|
298 |
|
---|
299 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
300 | if (aResult != aSize)
|
---|
301 | {
|
---|
302 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
303 | CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
304 | }
|
---|
305 | /*----------------------------------------------------------------*/
|
---|
306 |
|
---|
307 | return aResult < 0 ? aResult : 0;
|
---|
308 | }
|
---|
309 |
|
---|
310 | int
|
---|
311 | CRXProxy::ReceiveResponse (void)
|
---|
312 | {
|
---|
313 | int aResult = 0;
|
---|
314 |
|
---|
315 | const unsigned int aBufferSize = 1024 * 64;
|
---|
316 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
317 |
|
---|
318 | std::string aHttpResponse;
|
---|
319 |
|
---|
320 | /*----------------------------------------------------------------*/
|
---|
321 | for (;;)
|
---|
322 | {
|
---|
323 | memset (aBuffer, 0x00, sizeof (aBuffer));
|
---|
324 |
|
---|
325 | aResult = mServer.Receive (aBuffer, aBufferSize);
|
---|
326 | if (aResult < 0)
|
---|
327 | {
|
---|
328 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
329 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
330 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
331 | return aResult;
|
---|
332 | }
|
---|
333 | else if (aResult == 0)
|
---|
334 | {
|
---|
335 | break;
|
---|
336 | }
|
---|
337 |
|
---|
338 | aResult = mHttpResponse.SetResponse (aBuffer, aResult);
|
---|
339 | if (aResult)
|
---|
340 | {
|
---|
341 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
342 | CRX_ERROR_SET (aResult, "Failed to set response.");
|
---|
343 | return aResult;
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (!CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION) &&
|
---|
347 | mHttpResponse.GetContentLength () > 0)
|
---|
348 | {
|
---|
349 | CRX_DEBUG_TRACE_BIN (mHttpResponse.GetContentBody (),
|
---|
350 | mHttpResponse.GetContentLength (),
|
---|
351 | "");
|
---|
352 | }
|
---|
353 | }
|
---|
354 | /*----------------------------------------------------------------*/
|
---|
355 |
|
---|
356 | return aResult < 0 ? aResult : 0;
|
---|
357 | }
|
---|
358 |
|
---|
359 | int
|
---|
360 | CRXProxy::SendResponse (void)
|
---|
361 | {
|
---|
362 | int aResult = 0;
|
---|
363 | int aSize = 0;
|
---|
364 |
|
---|
365 | /*----------------------------------------------------------------*/
|
---|
366 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
367 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
368 | if (aResult != aSize)
|
---|
369 | {
|
---|
370 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
371 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
372 | return aResult;
|
---|
373 | }
|
---|
374 |
|
---|
375 | aSize = mHttpResponse.GetContentLength ();
|
---|
376 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
377 | if (aResult != aSize)
|
---|
378 | {
|
---|
379 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
380 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
381 | }
|
---|
382 | /*----------------------------------------------------------------*/
|
---|
383 |
|
---|
384 | return aResult < 0 ? aResult : 0;
|
---|
385 | } |
---|