[18] | 1 | /**
|
---|
| 2 | * CRXProxy.cpp
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #include "CRXProxy.h"
|
---|
| 6 |
|
---|
[19] | 7 | #include <string.h>
|
---|
| 8 |
|
---|
[28] | 9 | CRXFilter CRXProxy::mFilter;
|
---|
| 10 |
|
---|
[24] | 11 | CRXProxy::CRXProxy (void)
|
---|
[28] | 12 | : mIsIntercepted (false),
|
---|
| 13 | mClient (0),
|
---|
[24] | 14 | mServer (0)
|
---|
| 15 | {
|
---|
| 16 | /*----------------------------------------------------------------*/
|
---|
| 17 | /*----------------------------------------------------------------*/
|
---|
| 18 | }
|
---|
[18] | 19 |
|
---|
[24] | 20 | CRXProxy::~CRXProxy (void)
|
---|
| 21 | {
|
---|
| 22 | /*----------------------------------------------------------------*/
|
---|
| 23 | Close ();
|
---|
| 24 | /*----------------------------------------------------------------*/
|
---|
| 25 | }
|
---|
[18] | 26 |
|
---|
[24] | 27 | CRXProxy *
|
---|
| 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
|
---|
| 56 | CRXProxy::Close (void)
|
---|
[20] | 57 | {
|
---|
[24] | 58 | /*----------------------------------------------------------------*/
|
---|
| 59 | mServer.Close ();
|
---|
| 60 | mClient.Close ();
|
---|
| 61 | /*----------------------------------------------------------------*/
|
---|
[20] | 62 | }
|
---|
| 63 |
|
---|
[28] | 64 | void
|
---|
| 65 | CRXProxy::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] | 74 | void
|
---|
| 75 | CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
| 76 | {
|
---|
| 77 | /*----------------------------------------------------------------*/
|
---|
| 78 | mFilter.RemoveRequestFilter (aType);
|
---|
| 79 | /*----------------------------------------------------------------*/
|
---|
[24] | 80 | }
|
---|
| 81 |
|
---|
[28] | 82 | bool
|
---|
| 83 | CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
|
---|
| 84 | {
|
---|
| 85 | /*----------------------------------------------------------------*/
|
---|
| 86 | /*----------------------------------------------------------------*/
|
---|
| 87 |
|
---|
| 88 | return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[24] | 91 | int
|
---|
| 92 | CRXProxy::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;
|
---|
| 101 | CRX_ERROR (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;
|
---|
| 108 | CRX_ERROR (aResult, "Failed to send to server");
|
---|
| 109 | return aResult;
|
---|
| 110 | }
|
---|
[23] | 111 |
|
---|
[24] | 112 | aResult = ReceiveResponse ();
|
---|
| 113 | if (aResult < 0)
|
---|
| 114 | {
|
---|
| 115 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
|
---|
| 116 | CRX_ERROR (aResult, "Failed to receive from server");
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | aResult = SendResponse ();
|
---|
| 120 | if (aResult < 0)
|
---|
| 121 | {
|
---|
| 122 | aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
|
---|
| 123 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
| 124 | return aResult;
|
---|
| 125 | }
|
---|
[18] | 126 | /*----------------------------------------------------------------*/
|
---|
[24] | 127 |
|
---|
| 128 | return aResult;
|
---|
[18] | 129 | }
|
---|
| 130 |
|
---|
[24] | 131 | void
|
---|
| 132 | CRXProxy::SetHttpRequest (const char * aHttpRequest)
|
---|
| 133 | {
|
---|
| 134 | /*----------------------------------------------------------------*/
|
---|
| 135 | mHttpRequest = aHttpRequest;
|
---|
| 136 | /*----------------------------------------------------------------*/
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | char *
|
---|
| 140 | CRXProxy::GetHttpRequest (char * aBuffer,
|
---|
| 141 | const int aBufferSize) const
|
---|
| 142 | {
|
---|
| 143 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
| 144 |
|
---|
| 145 | /*----------------------------------------------------------------*/
|
---|
| 146 | if (aPtr == NULL)
|
---|
| 147 | return NULL;
|
---|
| 148 |
|
---|
| 149 | if (aBufferSize > 0)
|
---|
| 150 | memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aBufferSize);
|
---|
| 151 | /*----------------------------------------------------------------*/
|
---|
| 152 |
|
---|
| 153 | return aBuffer;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[18] | 156 | int
|
---|
[24] | 157 | CRXProxy::GetHttpRequestLength (void) const
|
---|
[18] | 158 | {
|
---|
[24] | 159 | /*----------------------------------------------------------------*/
|
---|
| 160 | /*----------------------------------------------------------------*/
|
---|
[18] | 161 |
|
---|
[24] | 162 | return mHttpRequest.GetHeader().length ();
|
---|
| 163 | }
|
---|
[18] | 164 |
|
---|
[24] | 165 | void
|
---|
| 166 | CRXProxy::SetHttpResponse (const char * aHttpResponse)
|
---|
| 167 | {
|
---|
| 168 | /*----------------------------------------------------------------*/
|
---|
| 169 | mHttpResponse = aHttpResponse;
|
---|
| 170 | /*----------------------------------------------------------------*/
|
---|
| 171 | }
|
---|
[18] | 172 |
|
---|
[24] | 173 | char *
|
---|
| 174 | CRXProxy::GetHttpResponse (char * aBuffer,
|
---|
| 175 | const int aBufferSize) const
|
---|
| 176 | {
|
---|
| 177 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
[18] | 178 |
|
---|
[24] | 179 | int aRemainLength = aBufferSize;
|
---|
[18] | 180 |
|
---|
| 181 | /*----------------------------------------------------------------*/
|
---|
[24] | 182 | if (aPtr == NULL)
|
---|
| 183 | return NULL;
|
---|
| 184 |
|
---|
| 185 | if (aRemainLength > 0)
|
---|
| 186 | memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aRemainLength);
|
---|
| 187 |
|
---|
| 188 | aRemainLength -= mHttpResponse.GetHeader ().length ();
|
---|
| 189 |
|
---|
| 190 | if (aRemainLength > 0)
|
---|
| 191 | memcpy (aPtr, mHttpResponse.GetContentBody (), aRemainLength);
|
---|
| 192 | /*----------------------------------------------------------------*/
|
---|
| 193 |
|
---|
| 194 | return aBuffer;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | int
|
---|
| 198 | CRXProxy::GetHttpResponseLength (void) const
|
---|
| 199 | {
|
---|
| 200 | /*----------------------------------------------------------------*/
|
---|
| 201 | /*----------------------------------------------------------------*/
|
---|
| 202 |
|
---|
| 203 | return mHttpResponse.GetHeader ().length ()
|
---|
| 204 | + mHttpResponse.GetContentLength ();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | int
|
---|
| 208 | CRXProxy::ReceiveRequest (void)
|
---|
| 209 | {
|
---|
| 210 | int aResult = 0;
|
---|
| 211 |
|
---|
| 212 | const unsigned int aBufferSize = 1024 * 56;
|
---|
| 213 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
| 214 |
|
---|
| 215 | /*----------------------------------------------------------------*/
|
---|
| 216 | aResult = mClient.Receive (aBuffer, aBufferSize);
|
---|
| 217 | if (aResult < 0)
|
---|
[18] | 218 | {
|
---|
[24] | 219 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
| 220 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
| 221 | CRX_ERROR (aResult, "Failed to receive from client");
|
---|
| 222 | return aResult;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /* parse http request */
|
---|
| 226 | mHttpRequest = aBuffer;
|
---|
| 227 | /*----------------------------------------------------------------*/
|
---|
| 228 |
|
---|
| 229 | return aResult < 0 ? aResult : 0;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | int
|
---|
| 233 | CRXProxy::SendRequest (void)
|
---|
| 234 | {
|
---|
| 235 | int aResult = 0;
|
---|
| 236 | int aSize = mHttpRequest.GetHeader ().length ();
|
---|
| 237 |
|
---|
| 238 | /*----------------------------------------------------------------*/
|
---|
| 239 | if (!mServer)
|
---|
| 240 | {
|
---|
| 241 | /* connect */
|
---|
| 242 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), 1);
|
---|
[18] | 243 | if (aResult < 0)
|
---|
| 244 | {
|
---|
[24] | 245 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
| 246 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
| 247 | CRX_ERROR (aResult, "Failed to connect to server <%s>", mHttpRequest.GetURL ().c_str ());
|
---|
| 248 | return aResult;
|
---|
[18] | 249 | }
|
---|
[24] | 250 | }
|
---|
[18] | 251 |
|
---|
[24] | 252 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
| 253 | if (aResult != aSize)
|
---|
| 254 | {
|
---|
| 255 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
| 256 | CRX_ERROR (aResult, "Failed to send to server <%s>", mHttpRequest.GetURL ().c_str ());
|
---|
| 257 | }
|
---|
| 258 | /*----------------------------------------------------------------*/
|
---|
[18] | 259 |
|
---|
[24] | 260 | return aResult < 0 ? aResult : 0;
|
---|
| 261 | }
|
---|
[18] | 262 |
|
---|
[24] | 263 | int
|
---|
| 264 | CRXProxy::ReceiveResponse (void)
|
---|
| 265 | {
|
---|
| 266 | int aResult = 0;
|
---|
| 267 |
|
---|
| 268 | const unsigned int aBufferSize = 1024 * 64;
|
---|
| 269 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
| 270 |
|
---|
| 271 | std::string aHttpResponse;
|
---|
| 272 |
|
---|
| 273 | /*----------------------------------------------------------------*/
|
---|
| 274 | for (;;)
|
---|
| 275 | {
|
---|
| 276 | memset (aBuffer, 0x00, sizeof (aBuffer));
|
---|
| 277 |
|
---|
| 278 | aResult = mServer.Receive (aBuffer, aBufferSize);
|
---|
| 279 | if (aResult < 0)
|
---|
[18] | 280 | {
|
---|
[24] | 281 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
| 282 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
| 283 | CRX_ERROR (aResult, "Failed to receive from server");
|
---|
[18] | 284 | return aResult;
|
---|
| 285 | }
|
---|
[24] | 286 | else if (aResult == 0)
|
---|
| 287 | {
|
---|
| 288 | break;
|
---|
| 289 | }
|
---|
[18] | 290 |
|
---|
[24] | 291 | aResult = mHttpResponse.SetResponse (aBuffer, aResult);
|
---|
| 292 | if (aResult)
|
---|
[18] | 293 | {
|
---|
[24] | 294 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
| 295 | CRX_ERROR (aResult, "Failed to set response");
|
---|
| 296 | return aResult;
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | /*----------------------------------------------------------------*/
|
---|
[18] | 300 |
|
---|
[24] | 301 | return aResult < 0 ? aResult : 0;
|
---|
| 302 | }
|
---|
[18] | 303 |
|
---|
[24] | 304 | int
|
---|
| 305 | CRXProxy::SendResponse (void)
|
---|
| 306 | {
|
---|
| 307 | int aResult = 0;
|
---|
| 308 | int aSize = 0;
|
---|
[18] | 309 |
|
---|
| 310 | /*----------------------------------------------------------------*/
|
---|
[24] | 311 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
| 312 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
| 313 | if (aResult != aSize)
|
---|
| 314 | {
|
---|
| 315 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
| 316 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
| 317 | return aResult;
|
---|
| 318 | }
|
---|
[18] | 319 |
|
---|
[24] | 320 | aSize = mHttpResponse.GetContentLength ();
|
---|
| 321 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
| 322 | if (aResult != aSize)
|
---|
| 323 | {
|
---|
| 324 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
| 325 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
| 326 | }
|
---|
| 327 | /*----------------------------------------------------------------*/
|
---|
| 328 |
|
---|
| 329 | return aResult < 0 ? aResult : 0;
|
---|
| 330 | } |
---|