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

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

#1 a interface to set timeout to server socket is added to CRXProxy

File size: 10.0 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 /*----------------------------------------------------------------*/
280 if (!mServer)
281 {
282 /* connect */
283 aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
284 if (aResult < 0)
285 {
286 aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
287 CRX_ADD_SUBCLASS_ERROR (mServer);
288 CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
289 return aResult;
290 }
291 }
292
293 aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
294 if (aResult != aSize)
295 {
296 CRX_ADD_SUBCLASS_ERROR (mServer);
297 CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
298 }
299 /*----------------------------------------------------------------*/
300
301 return aResult < 0 ? aResult : 0;
302}
303
304int
305CRXProxy::ReceiveResponse (void)
306{
307 int aResult = 0;
308
309 const unsigned int aBufferSize = 1024 * 64;
310 char aBuffer[aBufferSize + 1] = {0x00, };
311
312 std::string aHttpResponse;
313
314 /*----------------------------------------------------------------*/
315 for (;;)
316 {
317 memset (aBuffer, 0x00, sizeof (aBuffer));
318
319 aResult = mServer.Receive (aBuffer, aBufferSize);
320 if (aResult < 0)
321 {
322 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
323 CRX_ADD_SUBCLASS_ERROR (mServer);
324 CRX_ERROR_SET (aResult, "Failed to receive from server.");
325 return aResult;
326 }
327 else if (aResult == 0)
328 {
329 break;
330 }
331
332 aResult = mHttpResponse.SetResponse (aBuffer, aResult);
333 if (aResult)
334 {
335 aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
336 CRX_ERROR_SET (aResult, "Failed to set response.");
337 return aResult;
338 }
339
340 if (!CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION) &&
341 mHttpResponse.GetContentLength () > 0)
342 {
343 CRX_DEBUG_TRACE_BIN (mHttpResponse.GetContentBody (),
344 mHttpResponse.GetContentLength (),
345 "");
346 }
347 }
348 /*----------------------------------------------------------------*/
349
350 return aResult < 0 ? aResult : 0;
351}
352
353int
354CRXProxy::SendResponse (void)
355{
356 int aResult = 0;
357 int aSize = 0;
358
359 /*----------------------------------------------------------------*/
360 aSize = mHttpResponse.GetHeader ().length ();;
361 aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
362 if (aResult != aSize)
363 {
364 CRX_ADD_SUBCLASS_ERROR (mClient);
365 CRX_ERROR_SET (aResult, "Failed to send to client.");
366 return aResult;
367 }
368
369 aSize = mHttpResponse.GetContentLength ();
370 aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
371 if (aResult != aSize)
372 {
373 CRX_ADD_SUBCLASS_ERROR (mClient);
374 CRX_ERROR_SET (aResult, "Failed to send to client.");
375 }
376 /*----------------------------------------------------------------*/
377
378 return aResult < 0 ? aResult : 0;
379}
Note: See TracBrowser for help on using the repository browser.