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

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

#1 more mutex interface and add debugging code to check connection port number

File size: 10.2 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 : mClient (0),
13 mServer (0),
14 mServerTimeout (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::SetServerTimeout (const int aTimeout)
57{
58 /*----------------------------------------------------------------*/
59 mServerTimeout = aTimeout;
60 /*----------------------------------------------------------------*/
61}
62
63void
64CRXProxy::Close (void)
65{
66 /*----------------------------------------------------------------*/
67 mServer.Close ();
68 mClient.Close ();
69 /*----------------------------------------------------------------*/
70}
71
72void
73CRXProxy::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
82void
83CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
84{
85 /*----------------------------------------------------------------*/
86 mFilter.RemoveRequestFilter (aType);
87 /*----------------------------------------------------------------*/
88}
89
90bool
91CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
92{
93 /*----------------------------------------------------------------*/
94 /*----------------------------------------------------------------*/
95
96 return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
97}
98
99int
100CRXProxy::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
139void
140CRXProxy::SetHttpRequest (const char * aHttpRequest)
141{
142 /*----------------------------------------------------------------*/
143 mHttpRequest = aHttpRequest;
144 /*----------------------------------------------------------------*/
145}
146
147char *
148CRXProxy::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
169int
170CRXProxy::GetHttpRequestLength (void) const
171{
172 /*----------------------------------------------------------------*/
173 /*----------------------------------------------------------------*/
174
175 return mHttpRequest.GetHeader().length ();
176}
177
178void
179CRXProxy::SetHttpResponse (const char * aHttpResponse)
180{
181 /*----------------------------------------------------------------*/
182 mHttpResponse = aHttpResponse;
183 /*----------------------------------------------------------------*/
184}
185
186char *
187CRXProxy::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
208int
209CRXProxy::GetHttpResponseHeaderLength (void) const
210{
211 /*----------------------------------------------------------------*/
212 /*----------------------------------------------------------------*/
213
214 return mHttpResponse.GetHeader ().length ();
215}
216
217char *
218CRXProxy::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
239int
240CRXProxy::GetHttpResponseBodyLength (void) const
241{
242 /*----------------------------------------------------------------*/
243 /*----------------------------------------------------------------*/
244
245 return mHttpResponse.GetContentLength ();
246}
247
248int
249CRXProxy::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
273int
274CRXProxy::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
296 getsockname (mServer, (struct sockaddr *) &aSockName, &aSockLen);
297 CRX_DEBUG_TRACE ("Connected on port number (%d)\n", ntohs (aSockName.sin_port));
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
310int
311CRXProxy::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
359int
360CRXProxy::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}
Note: See TracBrowser for help on using the repository browser.