[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 | CRXProxy *
|
---|
[38] | 28 | CRXProxy::GetNewInstance (void)
|
---|
[18] | 29 | {
|
---|
[24] | 30 | /*----------------------------------------------------------------*/
|
---|
| 31 | /*----------------------------------------------------------------*/
|
---|
[19] | 32 |
|
---|
[24] | 33 | return new(std::nothrow) CRXProxy ();
|
---|
[18] | 34 | }
|
---|
| 35 |
|
---|
[24] | 36 | void
|
---|
| 37 | CRXProxy::ReleaseInstance (void)
|
---|
[18] | 38 | {
|
---|
| 39 | /*----------------------------------------------------------------*/
|
---|
[24] | 40 | delete this;
|
---|
| 41 | /*----------------------------------------------------------------*/
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | void
|
---|
| 45 | CRXProxy::SetClientSocket (const int aSocket)
|
---|
| 46 | {
|
---|
| 47 | /*----------------------------------------------------------------*/
|
---|
| 48 | if (aSocket <= 0)
|
---|
| 49 | return ;
|
---|
| 50 |
|
---|
[18] | 51 | mClient = aSocket;
|
---|
| 52 | /*----------------------------------------------------------------*/
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[24] | 55 | void
|
---|
[36] | 56 | CRXProxy::SetServerTimeout (const int aTimeout)
|
---|
| 57 | {
|
---|
| 58 | /*----------------------------------------------------------------*/
|
---|
| 59 | mServerTimeout = aTimeout;
|
---|
| 60 | /*----------------------------------------------------------------*/
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | void
|
---|
[24] | 64 | CRXProxy::Close (void)
|
---|
[20] | 65 | {
|
---|
[24] | 66 | /*----------------------------------------------------------------*/
|
---|
| 67 | mServer.Close ();
|
---|
| 68 | mClient.Close ();
|
---|
| 69 | /*----------------------------------------------------------------*/
|
---|
[20] | 70 | }
|
---|
| 71 |
|
---|
[28] | 72 | void
|
---|
| 73 | CRXProxy::SetRequestFilter (const E_CRX_FILTER_REQUEST aType,
|
---|
| 74 | const bool aIsMatched,
|
---|
| 75 | const std::string aValue)
|
---|
[18] | 76 | {
|
---|
| 77 | /*----------------------------------------------------------------*/
|
---|
[28] | 78 | mFilter.SetRequestFilter (aType, aIsMatched, aValue);
|
---|
[24] | 79 | /*----------------------------------------------------------------*/
|
---|
[28] | 80 | }
|
---|
[24] | 81 |
|
---|
[28] | 82 | void
|
---|
| 83 | CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
| 84 | {
|
---|
| 85 | /*----------------------------------------------------------------*/
|
---|
| 86 | mFilter.RemoveRequestFilter (aType);
|
---|
| 87 | /*----------------------------------------------------------------*/
|
---|
[24] | 88 | }
|
---|
| 89 |
|
---|
[28] | 90 | bool
|
---|
| 91 | CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
| 92 | {
|
---|
| 93 | /*----------------------------------------------------------------*/
|
---|
| 94 | /*----------------------------------------------------------------*/
|
---|
| 95 |
|
---|
| 96 | return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[24] | 99 | int
|
---|
| 100 | CRXProxy::Forward (void)
|
---|
| 101 | {
|
---|
| 102 | int aResult = 0;
|
---|
| 103 |
|
---|
| 104 | /*----------------------------------------------------------------*/
|
---|
| 105 | aResult = ReceiveRequest ();
|
---|
| 106 | if (aResult < 0)
|
---|
[18] | 107 | {
|
---|
[24] | 108 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 109 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
[18] | 110 | }
|
---|
| 111 |
|
---|
[24] | 112 | aResult = SendRequest ();
|
---|
| 113 | if (aResult < 0)
|
---|
| 114 | {
|
---|
| 115 | aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
|
---|
[35] | 116 | CRX_ERROR_SET (aResult, "Failed to send to server.");
|
---|
[24] | 117 | return aResult;
|
---|
| 118 | }
|
---|
[23] | 119 |
|
---|
[24] | 120 | aResult = ReceiveResponse ();
|
---|
| 121 | if (aResult < 0)
|
---|
| 122 | {
|
---|
| 123 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
|
---|
[35] | 124 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
[24] | 125 | }
|
---|
| 126 |
|
---|
| 127 | aResult = SendResponse ();
|
---|
| 128 | if (aResult < 0)
|
---|
| 129 | {
|
---|
| 130 | aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
|
---|
[35] | 131 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 132 | return aResult;
|
---|
| 133 | }
|
---|
[18] | 134 | /*----------------------------------------------------------------*/
|
---|
[24] | 135 |
|
---|
| 136 | return aResult;
|
---|
[18] | 137 | }
|
---|
| 138 |
|
---|
[24] | 139 | void
|
---|
| 140 | CRXProxy::SetHttpRequest (const char * aHttpRequest)
|
---|
| 141 | {
|
---|
| 142 | /*----------------------------------------------------------------*/
|
---|
| 143 | mHttpRequest = aHttpRequest;
|
---|
| 144 | /*----------------------------------------------------------------*/
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | char *
|
---|
| 148 | CRXProxy::GetHttpRequest (char * aBuffer,
|
---|
| 149 | const int aBufferSize) const
|
---|
| 150 | {
|
---|
[34] | 151 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
[24] | 152 |
|
---|
[34] | 153 | int aCopyLength = 0;
|
---|
| 154 | int aRemainLength = aBufferSize;
|
---|
| 155 | const int aHeaderLength = mHttpRequest.GetHeader ().length ();
|
---|
| 156 |
|
---|
[24] | 157 | /*----------------------------------------------------------------*/
|
---|
| 158 | if (aPtr == NULL)
|
---|
| 159 | return NULL;
|
---|
| 160 |
|
---|
[34] | 161 | aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
|
---|
| 162 | if (aCopyLength > 0)
|
---|
| 163 | memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aCopyLength);
|
---|
[24] | 164 | /*----------------------------------------------------------------*/
|
---|
| 165 |
|
---|
| 166 | return aBuffer;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[18] | 169 | int
|
---|
[24] | 170 | CRXProxy::GetHttpRequestLength (void) const
|
---|
[18] | 171 | {
|
---|
[24] | 172 | /*----------------------------------------------------------------*/
|
---|
| 173 | /*----------------------------------------------------------------*/
|
---|
[18] | 174 |
|
---|
[24] | 175 | return mHttpRequest.GetHeader().length ();
|
---|
| 176 | }
|
---|
[18] | 177 |
|
---|
[24] | 178 | void
|
---|
| 179 | CRXProxy::SetHttpResponse (const char * aHttpResponse)
|
---|
| 180 | {
|
---|
| 181 | /*----------------------------------------------------------------*/
|
---|
| 182 | mHttpResponse = aHttpResponse;
|
---|
| 183 | /*----------------------------------------------------------------*/
|
---|
| 184 | }
|
---|
[18] | 185 |
|
---|
[24] | 186 | char *
|
---|
[35] | 187 | CRXProxy::GetHttpResponseHeader (char * aBuffer,
|
---|
| 188 | const int aBufferSize) const
|
---|
[24] | 189 | {
|
---|
[34] | 190 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
[18] | 191 |
|
---|
[34] | 192 | int aCopyLength = 0;
|
---|
| 193 | int aRemainLength = aBufferSize;
|
---|
| 194 | const int aHeaderLength = mHttpResponse.GetHeader ().length ();
|
---|
[18] | 195 |
|
---|
| 196 | /*----------------------------------------------------------------*/
|
---|
[24] | 197 | if (aPtr == NULL)
|
---|
| 198 | return NULL;
|
---|
| 199 |
|
---|
[34] | 200 | aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
|
---|
| 201 | if (aCopyLength > 0)
|
---|
| 202 | memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aCopyLength);
|
---|
[35] | 203 | /*----------------------------------------------------------------*/
|
---|
[24] | 204 |
|
---|
[35] | 205 | return aBuffer;
|
---|
| 206 | }
|
---|
[24] | 207 |
|
---|
[35] | 208 | int
|
---|
| 209 | CRXProxy::GetHttpResponseHeaderLength (void) const
|
---|
| 210 | {
|
---|
| 211 | /*----------------------------------------------------------------*/
|
---|
| 212 | /*----------------------------------------------------------------*/
|
---|
| 213 |
|
---|
| 214 | return mHttpResponse.GetHeader ().length ();
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | char *
|
---|
| 218 | CRXProxy::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 |
|
---|
[34] | 231 | aCopyLength = aRemainLength < aContentLength ? aRemainLength : aContentLength;
|
---|
| 232 | if (aCopyLength > 0)
|
---|
| 233 | memcpy (aPtr, mHttpResponse.GetContentBody (), aCopyLength);
|
---|
[24] | 234 | /*----------------------------------------------------------------*/
|
---|
| 235 |
|
---|
| 236 | return aBuffer;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | int
|
---|
[35] | 240 | CRXProxy::GetHttpResponseBodyLength (void) const
|
---|
[24] | 241 | {
|
---|
| 242 | /*----------------------------------------------------------------*/
|
---|
| 243 | /*----------------------------------------------------------------*/
|
---|
| 244 |
|
---|
[35] | 245 | return mHttpResponse.GetContentLength ();
|
---|
[24] | 246 | }
|
---|
| 247 |
|
---|
| 248 | int
|
---|
| 249 | CRXProxy::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)
|
---|
[18] | 259 | {
|
---|
[24] | 260 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 261 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 262 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
[24] | 263 | return aResult;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /* parse http request */
|
---|
| 267 | mHttpRequest = aBuffer;
|
---|
| 268 | /*----------------------------------------------------------------*/
|
---|
| 269 |
|
---|
| 270 | return aResult < 0 ? aResult : 0;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | int
|
---|
| 274 | CRXProxy::SendRequest (void)
|
---|
| 275 | {
|
---|
| 276 | int aResult = 0;
|
---|
| 277 | int aSize = mHttpRequest.GetHeader ().length ();
|
---|
| 278 |
|
---|
[38] | 279 | struct sockaddr_in aSockName;
|
---|
| 280 | socklen_t aSockLen = sizeof (aSockName);
|
---|
| 281 |
|
---|
[24] | 282 | /*----------------------------------------------------------------*/
|
---|
| 283 | if (!mServer)
|
---|
| 284 | {
|
---|
| 285 | /* connect */
|
---|
[36] | 286 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
|
---|
[18] | 287 | if (aResult < 0)
|
---|
| 288 | {
|
---|
[24] | 289 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
[35] | 290 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 291 | CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
[24] | 292 | return aResult;
|
---|
[18] | 293 | }
|
---|
[39] | 294 |
|
---|
[40] | 295 | if (getsockname (mServer, (struct sockaddr *) &aSockName, &aSockLen) == 0)
|
---|
[39] | 296 | CRX_DEBUG_TRACE ("Connected on port number (%d)\n", ntohs (aSockName.sin_port));
|
---|
[24] | 297 | }
|
---|
[18] | 298 |
|
---|
[24] | 299 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
| 300 | if (aResult != aSize)
|
---|
| 301 | {
|
---|
[35] | 302 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 303 | CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
[24] | 304 | }
|
---|
| 305 | /*----------------------------------------------------------------*/
|
---|
[18] | 306 |
|
---|
[24] | 307 | return aResult < 0 ? aResult : 0;
|
---|
| 308 | }
|
---|
[18] | 309 |
|
---|
[24] | 310 | int
|
---|
| 311 | CRXProxy::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)
|
---|
[18] | 327 | {
|
---|
[24] | 328 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 329 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 330 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
[18] | 331 | return aResult;
|
---|
| 332 | }
|
---|
[24] | 333 | else if (aResult == 0)
|
---|
| 334 | {
|
---|
| 335 | break;
|
---|
| 336 | }
|
---|
[18] | 337 |
|
---|
[24] | 338 | aResult = mHttpResponse.SetResponse (aBuffer, aResult);
|
---|
| 339 | if (aResult)
|
---|
[18] | 340 | {
|
---|
[24] | 341 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
[35] | 342 | CRX_ERROR_SET (aResult, "Failed to set response.");
|
---|
[24] | 343 | return aResult;
|
---|
| 344 | }
|
---|
[35] | 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 | }
|
---|
[24] | 353 | }
|
---|
| 354 | /*----------------------------------------------------------------*/
|
---|
[18] | 355 |
|
---|
[24] | 356 | return aResult < 0 ? aResult : 0;
|
---|
| 357 | }
|
---|
[18] | 358 |
|
---|
[24] | 359 | int
|
---|
| 360 | CRXProxy::SendResponse (void)
|
---|
| 361 | {
|
---|
| 362 | int aResult = 0;
|
---|
| 363 | int aSize = 0;
|
---|
[18] | 364 |
|
---|
| 365 | /*----------------------------------------------------------------*/
|
---|
[24] | 366 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
| 367 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
| 368 | if (aResult != aSize)
|
---|
| 369 | {
|
---|
[35] | 370 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 371 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 372 | return aResult;
|
---|
| 373 | }
|
---|
[18] | 374 |
|
---|
[24] | 375 | aSize = mHttpResponse.GetContentLength ();
|
---|
| 376 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
| 377 | if (aResult != aSize)
|
---|
| 378 | {
|
---|
[35] | 379 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 380 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 381 | }
|
---|
| 382 | /*----------------------------------------------------------------*/
|
---|
| 383 |
|
---|
| 384 | return aResult < 0 ? aResult : 0;
|
---|
| 385 | } |
---|