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

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

#1 add debugging message

File size: 9.8 KB
RevLine 
[18]1/**
2 * CRXProxy.cpp
3 */
4
5#include "CRXProxy.h"
6
[19]7#include <string.h>
8
[28]9CRXFilter CRXProxy::mFilter;
10
[24]11CRXProxy::CRXProxy (void)
[28]12 : mIsIntercepted (false),
13 mClient (0),
[24]14 mServer (0)
15{
16 /*----------------------------------------------------------------*/
17 /*----------------------------------------------------------------*/
18}
[18]19
[24]20CRXProxy::~CRXProxy (void)
21{
22 /*----------------------------------------------------------------*/
23 Close ();
24 /*----------------------------------------------------------------*/
25}
[18]26
[24]27CRXProxy *
28CRXProxy::GetNewInstance(void)
[18]29{
[24]30 /*----------------------------------------------------------------*/
31 /*----------------------------------------------------------------*/
[19]32
[24]33 return new(std::nothrow) CRXProxy ();
[18]34}
35
[24]36void
37CRXProxy::ReleaseInstance (void)
[18]38{
39 /*----------------------------------------------------------------*/
[24]40 delete this;
41 /*----------------------------------------------------------------*/
42}
43
44void
45CRXProxy::SetClientSocket (const int aSocket)
46{
47 /*----------------------------------------------------------------*/
48 if (aSocket <= 0)
49 return ;
50
[18]51 mClient = aSocket;
52 /*----------------------------------------------------------------*/
53}
54
[24]55void
56CRXProxy::Close (void)
[20]57{
[24]58 /*----------------------------------------------------------------*/
59 mServer.Close ();
60 mClient.Close ();
61 /*----------------------------------------------------------------*/
[20]62}
63
[28]64void
65CRXProxy::SetRequestFilter (const E_CRX_FILTER_REQUEST aType,
66 const bool aIsMatched,
67 const std::string aValue)
[18]68{
69 /*----------------------------------------------------------------*/
[28]70 mFilter.SetRequestFilter (aType, aIsMatched, aValue);
[24]71 /*----------------------------------------------------------------*/
[28]72}
[24]73
[28]74void
75CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
76{
77 /*----------------------------------------------------------------*/
78 mFilter.RemoveRequestFilter (aType);
79 /*----------------------------------------------------------------*/
[24]80}
81
[28]82bool
83CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
84{
85 /*----------------------------------------------------------------*/
86 /*----------------------------------------------------------------*/
87
88 return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
89}
90
[24]91int
92CRXProxy::Forward (void)
93{
94 int aResult = 0;
95
96 /*----------------------------------------------------------------*/
97 aResult = ReceiveRequest ();
98 if (aResult < 0)
[18]99 {
[24]100 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
[35]101 CRX_ERROR_SET (aResult, "Failed to receive from client.");
[18]102 }
103
[24]104 aResult = SendRequest ();
105 if (aResult < 0)
106 {
107 aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
[35]108 CRX_ERROR_SET (aResult, "Failed to send to server.");
[24]109 return aResult;
110 }
[23]111
[24]112 aResult = ReceiveResponse ();
113 if (aResult < 0)
114 {
115 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
[35]116 CRX_ERROR_SET (aResult, "Failed to receive from server.");
[24]117 }
118
119 aResult = SendResponse ();
120 if (aResult < 0)
121 {
122 aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
[35]123 CRX_ERROR_SET (aResult, "Failed to send to client.");
[24]124 return aResult;
125 }
[18]126 /*----------------------------------------------------------------*/
[24]127
128 return aResult;
[18]129}
130
[24]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{
[34]143 void * aPtr = static_cast<void *> (aBuffer);
[24]144
[34]145 int aCopyLength = 0;
146 int aRemainLength = aBufferSize;
147 const int aHeaderLength = mHttpRequest.GetHeader ().length ();
148
[24]149 /*----------------------------------------------------------------*/
150 if (aPtr == NULL)
151 return NULL;
152
[34]153 aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
154 if (aCopyLength > 0)
155 memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aCopyLength);
[24]156 /*----------------------------------------------------------------*/
157
158 return aBuffer;
159}
160
[18]161int
[24]162CRXProxy::GetHttpRequestLength (void) const
[18]163{
[24]164 /*----------------------------------------------------------------*/
165 /*----------------------------------------------------------------*/
[18]166
[24]167 return mHttpRequest.GetHeader().length ();
168}
[18]169
[24]170void
171CRXProxy::SetHttpResponse (const char * aHttpResponse)
172{
173 /*----------------------------------------------------------------*/
174 mHttpResponse = aHttpResponse;
175 /*----------------------------------------------------------------*/
176}
[18]177
[24]178char *
[35]179CRXProxy::GetHttpResponseHeader (char * aBuffer,
180 const int aBufferSize) const
[24]181{
[34]182 void * aPtr = static_cast<void *> (aBuffer);
[18]183
[34]184 int aCopyLength = 0;
185 int aRemainLength = aBufferSize;
186 const int aHeaderLength = mHttpResponse.GetHeader ().length ();
[18]187
188 /*----------------------------------------------------------------*/
[24]189 if (aPtr == NULL)
190 return NULL;
191
[34]192 aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
193 if (aCopyLength > 0)
194 memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aCopyLength);
[35]195 /*----------------------------------------------------------------*/
[24]196
[35]197 return aBuffer;
198}
[24]199
[35]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
[34]223 aCopyLength = aRemainLength < aContentLength ? aRemainLength : aContentLength;
224 if (aCopyLength > 0)
225 memcpy (aPtr, mHttpResponse.GetContentBody (), aCopyLength);
[24]226 /*----------------------------------------------------------------*/
227
228 return aBuffer;
229}
230
231int
[35]232CRXProxy::GetHttpResponseBodyLength (void) const
[24]233{
234 /*----------------------------------------------------------------*/
235 /*----------------------------------------------------------------*/
236
[35]237 return mHttpResponse.GetContentLength ();
[24]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)
[18]251 {
[24]252 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
[35]253 CRX_ADD_SUBCLASS_ERROR (mClient);
254 CRX_ERROR_SET (aResult, "Failed to receive from client.");
[24]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);
[18]276 if (aResult < 0)
277 {
[24]278 aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
[35]279 CRX_ADD_SUBCLASS_ERROR (mServer);
280 CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
[24]281 return aResult;
[18]282 }
[24]283 }
[18]284
[24]285 aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
286 if (aResult != aSize)
287 {
[35]288 CRX_ADD_SUBCLASS_ERROR (mServer);
289 CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
[24]290 }
291 /*----------------------------------------------------------------*/
[18]292
[24]293 return aResult < 0 ? aResult : 0;
294}
[18]295
[24]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)
[18]313 {
[24]314 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
[35]315 CRX_ADD_SUBCLASS_ERROR (mServer);
316 CRX_ERROR_SET (aResult, "Failed to receive from server.");
[18]317 return aResult;
318 }
[24]319 else if (aResult == 0)
320 {
321 break;
322 }
[18]323
[24]324 aResult = mHttpResponse.SetResponse (aBuffer, aResult);
325 if (aResult)
[18]326 {
[24]327 aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
[35]328 CRX_ERROR_SET (aResult, "Failed to set response.");
[24]329 return aResult;
330 }
[35]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 }
[24]339 }
340 /*----------------------------------------------------------------*/
[18]341
[24]342 return aResult < 0 ? aResult : 0;
343}
[18]344
[24]345int
346CRXProxy::SendResponse (void)
347{
348 int aResult = 0;
349 int aSize = 0;
[18]350
351 /*----------------------------------------------------------------*/
[24]352 aSize = mHttpResponse.GetHeader ().length ();;
353 aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
354 if (aResult != aSize)
355 {
[35]356 CRX_ADD_SUBCLASS_ERROR (mClient);
357 CRX_ERROR_SET (aResult, "Failed to send to client.");
[24]358 return aResult;
359 }
[18]360
[24]361 aSize = mHttpResponse.GetContentLength ();
362 aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
363 if (aResult != aSize)
364 {
[35]365 CRX_ADD_SUBCLASS_ERROR (mClient);
366 CRX_ERROR_SET (aResult, "Failed to send to client.");
[24]367 }
368 /*----------------------------------------------------------------*/
369
370 return aResult < 0 ? aResult : 0;
371}
Note: See TracBrowser for help on using the repository browser.