source: cheroxy/trunk/src/CRXProxy.cpp@ 35

Last change on this file since 35 was 35, checked in by cheese, 11 years ago

#1 add debugging message

File size: 9.8 KB
Line 
1/**
2 * CRXProxy.cpp
3 */
4
5#include "CRXProxy.h"
6
7#include <string.h>
8
9CRXFilter CRXProxy::mFilter;
10
11CRXProxy::CRXProxy (void)
12 : mIsIntercepted (false),
13 mClient (0),
14 mServer (0)
15{
16 /*----------------------------------------------------------------*/
17 /*----------------------------------------------------------------*/
18}
19
20CRXProxy::~CRXProxy (void)
21{
22 /*----------------------------------------------------------------*/
23 Close ();
24 /*----------------------------------------------------------------*/
25}
26
27CRXProxy *
28CRXProxy::GetNewInstance(void)
29{
30 /*----------------------------------------------------------------*/
31 /*----------------------------------------------------------------*/
32
33 return new(std::nothrow) CRXProxy ();
34}
35
36void
37CRXProxy::ReleaseInstance (void)
38{
39 /*----------------------------------------------------------------*/
40 delete this;
41 /*----------------------------------------------------------------*/
42}
43
44void
45CRXProxy::SetClientSocket (const int aSocket)
46{
47 /*----------------------------------------------------------------*/
48 if (aSocket <= 0)
49 return ;
50
51 mClient = aSocket;
52 /*----------------------------------------------------------------*/
53}
54
55void
56CRXProxy::Close (void)
57{
58 /*----------------------------------------------------------------*/
59 mServer.Close ();
60 mClient.Close ();
61 /*----------------------------------------------------------------*/
62}
63
64void
65CRXProxy::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
74void
75CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
76{
77 /*----------------------------------------------------------------*/
78 mFilter.RemoveRequestFilter (aType);
79 /*----------------------------------------------------------------*/
80}
81
82bool
83CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
84{
85 /*----------------------------------------------------------------*/
86 /*----------------------------------------------------------------*/
87
88 return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
89}
90
91int
92CRXProxy::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_SET (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_SET (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_SET (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_SET (aResult, "Failed to send to client.");
124 return aResult;
125 }
126 /*----------------------------------------------------------------*/
127
128 return aResult;
129}
130
131void
132CRXProxy::SetHttpRequest (const char * aHttpRequest)
133{
134 /*----------------------------------------------------------------*/
135 mHttpRequest = aHttpRequest;
136 /*----------------------------------------------------------------*/
137}
138
139char *
140CRXProxy::GetHttpRequest (char * aBuffer,
141 const int aBufferSize) const
142{
143 void * aPtr = static_cast<void *> (aBuffer);
144
145 int aCopyLength = 0;
146 int aRemainLength = aBufferSize;
147 const int aHeaderLength = mHttpRequest.GetHeader ().length ();
148
149 /*----------------------------------------------------------------*/
150 if (aPtr == NULL)
151 return NULL;
152
153 aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
154 if (aCopyLength > 0)
155 memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aCopyLength);
156 /*----------------------------------------------------------------*/
157
158 return aBuffer;
159}
160
161int
162CRXProxy::GetHttpRequestLength (void) const
163{
164 /*----------------------------------------------------------------*/
165 /*----------------------------------------------------------------*/
166
167 return mHttpRequest.GetHeader().length ();
168}
169
170void
171CRXProxy::SetHttpResponse (const char * aHttpResponse)
172{
173 /*----------------------------------------------------------------*/
174 mHttpResponse = aHttpResponse;
175 /*----------------------------------------------------------------*/
176}
177
178char *
179CRXProxy::GetHttpResponseHeader (char * aBuffer,
180 const int aBufferSize) const
181{
182 void * aPtr = static_cast<void *> (aBuffer);
183
184 int aCopyLength = 0;
185 int aRemainLength = aBufferSize;
186 const int aHeaderLength = mHttpResponse.GetHeader ().length ();
187
188 /*----------------------------------------------------------------*/
189 if (aPtr == NULL)
190 return NULL;
191
192 aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
193 if (aCopyLength > 0)
194 memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aCopyLength);
195 /*----------------------------------------------------------------*/
196
197 return aBuffer;
198}
199
200int
201CRXProxy::GetHttpResponseHeaderLength (void) const
202{
203 /*----------------------------------------------------------------*/
204 /*----------------------------------------------------------------*/
205
206 return mHttpResponse.GetHeader ().length ();
207}
208
209char *
210CRXProxy::GetHttpResponseBody (char * aBuffer,
211 const int aBufferSize) const
212{
213 void * aPtr = static_cast<void *> (aBuffer);
214
215 int aCopyLength = 0;
216 int aRemainLength = aBufferSize;
217 const int aContentLength = mHttpResponse.GetContentLength ();
218
219 /*----------------------------------------------------------------*/
220 if (aPtr == NULL)
221 return NULL;
222
223 aCopyLength = aRemainLength < aContentLength ? aRemainLength : aContentLength;
224 if (aCopyLength > 0)
225 memcpy (aPtr, mHttpResponse.GetContentBody (), aCopyLength);
226 /*----------------------------------------------------------------*/
227
228 return aBuffer;
229}
230
231int
232CRXProxy::GetHttpResponseBodyLength (void) const
233{
234 /*----------------------------------------------------------------*/
235 /*----------------------------------------------------------------*/
236
237 return mHttpResponse.GetContentLength ();
238}
239
240int
241CRXProxy::ReceiveRequest (void)
242{
243 int aResult = 0;
244
245 const unsigned int aBufferSize = 1024 * 56;
246 char aBuffer[aBufferSize + 1] = {0x00, };
247
248 /*----------------------------------------------------------------*/
249 aResult = mClient.Receive (aBuffer, aBufferSize);
250 if (aResult < 0)
251 {
252 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
253 CRX_ADD_SUBCLASS_ERROR (mClient);
254 CRX_ERROR_SET (aResult, "Failed to receive from client.");
255 return aResult;
256 }
257
258 /* parse http request */
259 mHttpRequest = aBuffer;
260 /*----------------------------------------------------------------*/
261
262 return aResult < 0 ? aResult : 0;
263}
264
265int
266CRXProxy::SendRequest (void)
267{
268 int aResult = 0;
269 int aSize = mHttpRequest.GetHeader ().length ();
270
271 /*----------------------------------------------------------------*/
272 if (!mServer)
273 {
274 /* connect */
275 aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), 1);
276 if (aResult < 0)
277 {
278 aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
279 CRX_ADD_SUBCLASS_ERROR (mServer);
280 CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
281 return aResult;
282 }
283 }
284
285 aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
286 if (aResult != aSize)
287 {
288 CRX_ADD_SUBCLASS_ERROR (mServer);
289 CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
290 }
291 /*----------------------------------------------------------------*/
292
293 return aResult < 0 ? aResult : 0;
294}
295
296int
297CRXProxy::ReceiveResponse (void)
298{
299 int aResult = 0;
300
301 const unsigned int aBufferSize = 1024 * 64;
302 char aBuffer[aBufferSize + 1] = {0x00, };
303
304 std::string aHttpResponse;
305
306 /*----------------------------------------------------------------*/
307 for (;;)
308 {
309 memset (aBuffer, 0x00, sizeof (aBuffer));
310
311 aResult = mServer.Receive (aBuffer, aBufferSize);
312 if (aResult < 0)
313 {
314 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
315 CRX_ADD_SUBCLASS_ERROR (mServer);
316 CRX_ERROR_SET (aResult, "Failed to receive from server.");
317 return aResult;
318 }
319 else if (aResult == 0)
320 {
321 break;
322 }
323
324 aResult = mHttpResponse.SetResponse (aBuffer, aResult);
325 if (aResult)
326 {
327 aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
328 CRX_ERROR_SET (aResult, "Failed to set response.");
329 return aResult;
330 }
331
332 if (!CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION) &&
333 mHttpResponse.GetContentLength () > 0)
334 {
335 CRX_DEBUG_TRACE_BIN (mHttpResponse.GetContentBody (),
336 mHttpResponse.GetContentLength (),
337 "");
338 }
339 }
340 /*----------------------------------------------------------------*/
341
342 return aResult < 0 ? aResult : 0;
343}
344
345int
346CRXProxy::SendResponse (void)
347{
348 int aResult = 0;
349 int aSize = 0;
350
351 /*----------------------------------------------------------------*/
352 aSize = mHttpResponse.GetHeader ().length ();;
353 aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
354 if (aResult != aSize)
355 {
356 CRX_ADD_SUBCLASS_ERROR (mClient);
357 CRX_ERROR_SET (aResult, "Failed to send to client.");
358 return aResult;
359 }
360
361 aSize = mHttpResponse.GetContentLength ();
362 aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
363 if (aResult != aSize)
364 {
365 CRX_ADD_SUBCLASS_ERROR (mClient);
366 CRX_ERROR_SET (aResult, "Failed to send to client.");
367 }
368 /*----------------------------------------------------------------*/
369
370 return aResult < 0 ? aResult : 0;
371}
Note: See TracBrowser for help on using the repository browser.