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