[18] | 1 | /**
|
---|
| 2 | * CRXProxy.cpp
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #include "CRXProxy.h"
|
---|
| 6 |
|
---|
[19] | 7 | #include <string.h>
|
---|
| 8 |
|
---|
[38] | 9 | CRXFilter CRXProxy::mFilter;
|
---|
[28] | 10 |
|
---|
[24] | 11 | CRXProxy::CRXProxy (void)
|
---|
[36] | 12 | : mClient (0),
|
---|
| 13 | mServer (0),
|
---|
| 14 | mServerTimeout (0)
|
---|
[24] | 15 | {
|
---|
| 16 | /*----------------------------------------------------------------*/
|
---|
| 17 | /*----------------------------------------------------------------*/
|
---|
| 18 | }
|
---|
[18] | 19 |
|
---|
[24] | 20 | CRXProxy::~CRXProxy (void)
|
---|
| 21 | {
|
---|
| 22 | /*----------------------------------------------------------------*/
|
---|
| 23 | Close ();
|
---|
| 24 | /*----------------------------------------------------------------*/
|
---|
| 25 | }
|
---|
[18] | 26 |
|
---|
[24] | 27 | void
|
---|
| 28 | CRXProxy::SetClientSocket (const int aSocket)
|
---|
| 29 | {
|
---|
| 30 | /*----------------------------------------------------------------*/
|
---|
| 31 | if (aSocket <= 0)
|
---|
| 32 | return ;
|
---|
| 33 |
|
---|
[18] | 34 | mClient = aSocket;
|
---|
| 35 | /*----------------------------------------------------------------*/
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[24] | 38 | void
|
---|
[36] | 39 | CRXProxy::SetServerTimeout (const int aTimeout)
|
---|
| 40 | {
|
---|
| 41 | /*----------------------------------------------------------------*/
|
---|
| 42 | mServerTimeout = aTimeout;
|
---|
| 43 | /*----------------------------------------------------------------*/
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | void
|
---|
[24] | 47 | CRXProxy::Close (void)
|
---|
[20] | 48 | {
|
---|
[24] | 49 | /*----------------------------------------------------------------*/
|
---|
| 50 | mServer.Close ();
|
---|
| 51 | mClient.Close ();
|
---|
| 52 | /*----------------------------------------------------------------*/
|
---|
[20] | 53 | }
|
---|
| 54 |
|
---|
[28] | 55 | void
|
---|
| 56 | CRXProxy::SetRequestFilter (const E_CRX_FILTER_REQUEST aType,
|
---|
| 57 | const bool aIsMatched,
|
---|
| 58 | const std::string aValue)
|
---|
[18] | 59 | {
|
---|
| 60 | /*----------------------------------------------------------------*/
|
---|
[28] | 61 | mFilter.SetRequestFilter (aType, aIsMatched, aValue);
|
---|
[24] | 62 | /*----------------------------------------------------------------*/
|
---|
[28] | 63 | }
|
---|
[24] | 64 |
|
---|
[28] | 65 | void
|
---|
| 66 | CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
| 67 | {
|
---|
| 68 | /*----------------------------------------------------------------*/
|
---|
| 69 | mFilter.RemoveRequestFilter (aType);
|
---|
| 70 | /*----------------------------------------------------------------*/
|
---|
[24] | 71 | }
|
---|
| 72 |
|
---|
[28] | 73 | bool
|
---|
| 74 | CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
| 75 | {
|
---|
| 76 | /*----------------------------------------------------------------*/
|
---|
| 77 | /*----------------------------------------------------------------*/
|
---|
| 78 |
|
---|
| 79 | return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[24] | 82 | int
|
---|
| 83 | CRXProxy::Forward (void)
|
---|
| 84 | {
|
---|
| 85 | int aResult = 0;
|
---|
| 86 |
|
---|
| 87 | /*----------------------------------------------------------------*/
|
---|
| 88 | aResult = ReceiveRequest ();
|
---|
| 89 | if (aResult < 0)
|
---|
[18] | 90 | {
|
---|
[24] | 91 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 92 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
[18] | 93 | }
|
---|
| 94 |
|
---|
[24] | 95 | aResult = SendRequest ();
|
---|
| 96 | if (aResult < 0)
|
---|
| 97 | {
|
---|
| 98 | aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
|
---|
[35] | 99 | CRX_ERROR_SET (aResult, "Failed to send to server.");
|
---|
[24] | 100 | return aResult;
|
---|
| 101 | }
|
---|
[23] | 102 |
|
---|
[24] | 103 | aResult = ReceiveResponse ();
|
---|
| 104 | if (aResult < 0)
|
---|
| 105 | {
|
---|
| 106 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
|
---|
[35] | 107 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
[24] | 108 | }
|
---|
| 109 |
|
---|
| 110 | aResult = SendResponse ();
|
---|
| 111 | if (aResult < 0)
|
---|
| 112 | {
|
---|
| 113 | aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
|
---|
[35] | 114 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 115 | return aResult;
|
---|
| 116 | }
|
---|
[18] | 117 | /*----------------------------------------------------------------*/
|
---|
[24] | 118 |
|
---|
| 119 | return aResult;
|
---|
[18] | 120 | }
|
---|
| 121 |
|
---|
[24] | 122 | void
|
---|
| 123 | CRXProxy::SetHttpRequest (const char * aHttpRequest)
|
---|
| 124 | {
|
---|
| 125 | /*----------------------------------------------------------------*/
|
---|
[44] | 126 | mHttpRequest.SetHeader (aHttpRequest);
|
---|
[24] | 127 | /*----------------------------------------------------------------*/
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | char *
|
---|
| 131 | CRXProxy::GetHttpRequest (char * aBuffer,
|
---|
| 132 | const int aBufferSize) const
|
---|
| 133 | {
|
---|
[34] | 134 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
[24] | 135 |
|
---|
[34] | 136 | int aCopyLength = 0;
|
---|
| 137 | int aRemainLength = aBufferSize;
|
---|
| 138 | const int aHeaderLength = mHttpRequest.GetHeader ().length ();
|
---|
| 139 |
|
---|
[24] | 140 | /*----------------------------------------------------------------*/
|
---|
| 141 | if (aPtr == NULL)
|
---|
| 142 | return NULL;
|
---|
[44] | 143 |
|
---|
[34] | 144 | aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
|
---|
| 145 | if (aCopyLength > 0)
|
---|
| 146 | memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aCopyLength);
|
---|
[24] | 147 | /*----------------------------------------------------------------*/
|
---|
| 148 |
|
---|
| 149 | return aBuffer;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[18] | 152 | int
|
---|
[24] | 153 | CRXProxy::GetHttpRequestLength (void) const
|
---|
[18] | 154 | {
|
---|
[24] | 155 | /*----------------------------------------------------------------*/
|
---|
| 156 | /*----------------------------------------------------------------*/
|
---|
[18] | 157 |
|
---|
[24] | 158 | return mHttpRequest.GetHeader().length ();
|
---|
| 159 | }
|
---|
[18] | 160 |
|
---|
[24] | 161 | void
|
---|
| 162 | CRXProxy::SetHttpResponse (const char * aHttpResponse)
|
---|
| 163 | {
|
---|
| 164 | /*----------------------------------------------------------------*/
|
---|
[44] | 165 | mHttpResponse.SetHeader (aHttpResponse);
|
---|
[24] | 166 | /*----------------------------------------------------------------*/
|
---|
| 167 | }
|
---|
[18] | 168 |
|
---|
[24] | 169 | char *
|
---|
[35] | 170 | CRXProxy::GetHttpResponseHeader (char * aBuffer,
|
---|
| 171 | const int aBufferSize) const
|
---|
[24] | 172 | {
|
---|
[34] | 173 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
[18] | 174 |
|
---|
[34] | 175 | int aCopyLength = 0;
|
---|
| 176 | int aRemainLength = aBufferSize;
|
---|
| 177 | const int aHeaderLength = mHttpResponse.GetHeader ().length ();
|
---|
[18] | 178 |
|
---|
| 179 | /*----------------------------------------------------------------*/
|
---|
[24] | 180 | if (aPtr == NULL)
|
---|
| 181 | return NULL;
|
---|
| 182 |
|
---|
[34] | 183 | aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
|
---|
| 184 | if (aCopyLength > 0)
|
---|
| 185 | memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aCopyLength);
|
---|
[35] | 186 | /*----------------------------------------------------------------*/
|
---|
[24] | 187 |
|
---|
[35] | 188 | return aBuffer;
|
---|
| 189 | }
|
---|
[24] | 190 |
|
---|
[35] | 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 |
|
---|
[34] | 214 | aCopyLength = aRemainLength < aContentLength ? aRemainLength : aContentLength;
|
---|
| 215 | if (aCopyLength > 0)
|
---|
| 216 | memcpy (aPtr, mHttpResponse.GetContentBody (), aCopyLength);
|
---|
[24] | 217 | /*----------------------------------------------------------------*/
|
---|
| 218 |
|
---|
| 219 | return aBuffer;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | int
|
---|
[35] | 223 | CRXProxy::GetHttpResponseBodyLength (void) const
|
---|
[24] | 224 | {
|
---|
| 225 | /*----------------------------------------------------------------*/
|
---|
| 226 | /*----------------------------------------------------------------*/
|
---|
| 227 |
|
---|
[35] | 228 | return mHttpResponse.GetContentLength ();
|
---|
[24] | 229 | }
|
---|
| 230 |
|
---|
| 231 | int
|
---|
| 232 | CRXProxy::ReceiveRequest (void)
|
---|
| 233 | {
|
---|
[44] | 234 | int aResult = 0;
|
---|
| 235 | char aBuffer;
|
---|
[24] | 236 |
|
---|
[44] | 237 | std::string aHttpHeader;
|
---|
[24] | 238 | /*----------------------------------------------------------------*/
|
---|
[44] | 239 | for (;;)
|
---|
[18] | 240 | {
|
---|
[44] | 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;
|
---|
[24] | 252 | }
|
---|
| 253 |
|
---|
[44] | 254 | mHttpRequest.SetHeader (aHttpHeader.c_str ());
|
---|
[24] | 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 |
|
---|
[38] | 266 | struct sockaddr_in aSockName;
|
---|
| 267 | socklen_t aSockLen = sizeof (aSockName);
|
---|
| 268 |
|
---|
[24] | 269 | /*----------------------------------------------------------------*/
|
---|
| 270 | if (!mServer)
|
---|
| 271 | {
|
---|
| 272 | /* connect */
|
---|
[36] | 273 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
|
---|
[18] | 274 | if (aResult < 0)
|
---|
| 275 | {
|
---|
[24] | 276 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
[35] | 277 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 278 | CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
[24] | 279 | return aResult;
|
---|
[18] | 280 | }
|
---|
[24] | 281 | }
|
---|
[18] | 282 |
|
---|
[24] | 283 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
| 284 | if (aResult != aSize)
|
---|
| 285 | {
|
---|
[35] | 286 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 287 | CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
[24] | 288 | }
|
---|
| 289 | /*----------------------------------------------------------------*/
|
---|
[18] | 290 |
|
---|
[24] | 291 | return aResult < 0 ? aResult : 0;
|
---|
| 292 | }
|
---|
[18] | 293 |
|
---|
[24] | 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)
|
---|
[18] | 309 | {
|
---|
[24] | 310 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 311 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 312 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
[18] | 313 | return aResult;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[44] | 316 | aResult = mHttpResponse.SetResponseAll (aBuffer, aResult);
|
---|
[24] | 317 | if (aResult)
|
---|
[18] | 318 | {
|
---|
[24] | 319 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
[35] | 320 | CRX_ERROR_SET (aResult, "Failed to set response.");
|
---|
[24] | 321 | return aResult;
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 | /*----------------------------------------------------------------*/
|
---|
[18] | 325 |
|
---|
[24] | 326 | return aResult < 0 ? aResult : 0;
|
---|
| 327 | }
|
---|
[18] | 328 |
|
---|
[24] | 329 | int
|
---|
| 330 | CRXProxy::SendResponse (void)
|
---|
| 331 | {
|
---|
| 332 | int aResult = 0;
|
---|
| 333 | int aSize = 0;
|
---|
[18] | 334 |
|
---|
| 335 | /*----------------------------------------------------------------*/
|
---|
[24] | 336 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
| 337 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
| 338 | if (aResult != aSize)
|
---|
| 339 | {
|
---|
[35] | 340 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 341 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 342 | return aResult;
|
---|
| 343 | }
|
---|
[18] | 344 |
|
---|
[24] | 345 | aSize = mHttpResponse.GetContentLength ();
|
---|
| 346 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
| 347 | if (aResult != aSize)
|
---|
| 348 | {
|
---|
[35] | 349 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 350 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 351 | }
|
---|
| 352 | /*----------------------------------------------------------------*/
|
---|
| 353 |
|
---|
| 354 | return aResult < 0 ? aResult : 0;
|
---|
| 355 | } |
---|