[18] | 1 | /**
|
---|
| 2 | * CRXProxy.cpp
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #include "CRXProxy.h"
|
---|
| 6 |
|
---|
[19] | 7 | #include <string.h>
|
---|
| 8 |
|
---|
[24] | 9 | CRXProxy::CRXProxy (void)
|
---|
| 10 | : mClient (0),
|
---|
| 11 | mServer (0)
|
---|
| 12 | {
|
---|
| 13 | /*----------------------------------------------------------------*/
|
---|
| 14 | /*----------------------------------------------------------------*/
|
---|
| 15 | }
|
---|
[18] | 16 |
|
---|
[24] | 17 | CRXProxy::~CRXProxy (void)
|
---|
| 18 | {
|
---|
| 19 | /*----------------------------------------------------------------*/
|
---|
| 20 | Close ();
|
---|
| 21 | /*----------------------------------------------------------------*/
|
---|
| 22 | }
|
---|
[18] | 23 |
|
---|
[24] | 24 | CRXProxy *
|
---|
| 25 | CRXProxy::GetNewInstance(void)
|
---|
[18] | 26 | {
|
---|
[24] | 27 | /*----------------------------------------------------------------*/
|
---|
| 28 | /*----------------------------------------------------------------*/
|
---|
[19] | 29 |
|
---|
[24] | 30 | return new(std::nothrow) CRXProxy ();
|
---|
[18] | 31 | }
|
---|
| 32 |
|
---|
[24] | 33 | void
|
---|
| 34 | CRXProxy::ReleaseInstance (void)
|
---|
[18] | 35 | {
|
---|
| 36 | /*----------------------------------------------------------------*/
|
---|
[24] | 37 | delete this;
|
---|
| 38 | /*----------------------------------------------------------------*/
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void
|
---|
| 42 | CRXProxy::SetClientSocket (const int aSocket)
|
---|
| 43 | {
|
---|
| 44 | /*----------------------------------------------------------------*/
|
---|
| 45 | if (aSocket <= 0)
|
---|
| 46 | return ;
|
---|
| 47 |
|
---|
[18] | 48 | mClient = aSocket;
|
---|
| 49 | /*----------------------------------------------------------------*/
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[24] | 52 | void
|
---|
| 53 | CRXProxy::Close (void)
|
---|
[20] | 54 | {
|
---|
[24] | 55 | /*----------------------------------------------------------------*/
|
---|
| 56 | mServer.Close ();
|
---|
| 57 | mClient.Close ();
|
---|
| 58 | /*----------------------------------------------------------------*/
|
---|
[20] | 59 | }
|
---|
| 60 |
|
---|
[18] | 61 | int
|
---|
[24] | 62 | CRXProxy::GetStatus (void)
|
---|
[18] | 63 | {
|
---|
| 64 | /*----------------------------------------------------------------*/
|
---|
[24] | 65 | /*----------------------------------------------------------------*/
|
---|
| 66 |
|
---|
| 67 | return mProxyStatus;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | int
|
---|
| 71 | CRXProxy::Forward (void)
|
---|
| 72 | {
|
---|
| 73 | int aResult = 0;
|
---|
| 74 |
|
---|
| 75 | /*----------------------------------------------------------------*/
|
---|
| 76 | aResult = ReceiveRequest ();
|
---|
| 77 | if (aResult < 0)
|
---|
[18] | 78 | {
|
---|
[24] | 79 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
| 80 | CRX_ERROR (aResult, "Failed to receive from client");
|
---|
[18] | 81 | }
|
---|
| 82 |
|
---|
[24] | 83 | aResult = SendRequest ();
|
---|
| 84 | if (aResult < 0)
|
---|
| 85 | {
|
---|
| 86 | aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
|
---|
| 87 | CRX_ERROR (aResult, "Failed to send to server");
|
---|
| 88 | return aResult;
|
---|
| 89 | }
|
---|
[23] | 90 |
|
---|
[24] | 91 | aResult = ReceiveResponse ();
|
---|
| 92 | if (aResult < 0)
|
---|
| 93 | {
|
---|
| 94 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
|
---|
| 95 | CRX_ERROR (aResult, "Failed to receive from server");
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | aResult = SendResponse ();
|
---|
| 99 | if (aResult < 0)
|
---|
| 100 | {
|
---|
| 101 | aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
|
---|
| 102 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
| 103 | return aResult;
|
---|
| 104 | }
|
---|
[18] | 105 | /*----------------------------------------------------------------*/
|
---|
[24] | 106 |
|
---|
| 107 | return aResult;
|
---|
[18] | 108 | }
|
---|
| 109 |
|
---|
[24] | 110 | void
|
---|
| 111 | CRXProxy::SetHttpRequest (const char * aHttpRequest)
|
---|
| 112 | {
|
---|
| 113 | /*----------------------------------------------------------------*/
|
---|
| 114 | mHttpRequest = aHttpRequest;
|
---|
| 115 | /*----------------------------------------------------------------*/
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | char *
|
---|
| 119 | CRXProxy::GetHttpRequest (char * aBuffer,
|
---|
| 120 | const int aBufferSize) const
|
---|
| 121 | {
|
---|
| 122 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
| 123 |
|
---|
| 124 | /*----------------------------------------------------------------*/
|
---|
| 125 | if (aPtr == NULL)
|
---|
| 126 | return NULL;
|
---|
| 127 |
|
---|
| 128 | if (aBufferSize > 0)
|
---|
| 129 | memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aBufferSize);
|
---|
| 130 | /*----------------------------------------------------------------*/
|
---|
| 131 |
|
---|
| 132 | return aBuffer;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[18] | 135 | int
|
---|
[24] | 136 | CRXProxy::GetHttpRequestLength (void) const
|
---|
[18] | 137 | {
|
---|
[24] | 138 | /*----------------------------------------------------------------*/
|
---|
| 139 | /*----------------------------------------------------------------*/
|
---|
[18] | 140 |
|
---|
[24] | 141 | return mHttpRequest.GetHeader().length ();
|
---|
| 142 | }
|
---|
[18] | 143 |
|
---|
[24] | 144 | void
|
---|
| 145 | CRXProxy::SetHttpResponse (const char * aHttpResponse)
|
---|
| 146 | {
|
---|
| 147 | /*----------------------------------------------------------------*/
|
---|
| 148 | mHttpResponse = aHttpResponse;
|
---|
| 149 | /*----------------------------------------------------------------*/
|
---|
| 150 | }
|
---|
[18] | 151 |
|
---|
[24] | 152 | char *
|
---|
| 153 | CRXProxy::GetHttpResponse (char * aBuffer,
|
---|
| 154 | const int aBufferSize) const
|
---|
| 155 | {
|
---|
| 156 | void * aPtr = static_cast<void *> (aBuffer);
|
---|
[18] | 157 |
|
---|
[24] | 158 | int aRemainLength = aBufferSize;
|
---|
[18] | 159 |
|
---|
| 160 | /*----------------------------------------------------------------*/
|
---|
[24] | 161 | if (aPtr == NULL)
|
---|
| 162 | return NULL;
|
---|
| 163 |
|
---|
| 164 | if (aRemainLength > 0)
|
---|
| 165 | memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aRemainLength);
|
---|
| 166 |
|
---|
| 167 | aRemainLength -= mHttpResponse.GetHeader ().length ();
|
---|
| 168 |
|
---|
| 169 | if (aRemainLength > 0)
|
---|
| 170 | memcpy (aPtr, mHttpResponse.GetContentBody (), aRemainLength);
|
---|
| 171 | /*----------------------------------------------------------------*/
|
---|
| 172 |
|
---|
| 173 | return aBuffer;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | int
|
---|
| 177 | CRXProxy::GetHttpResponseLength (void) const
|
---|
| 178 | {
|
---|
| 179 | /*----------------------------------------------------------------*/
|
---|
| 180 | /*----------------------------------------------------------------*/
|
---|
| 181 |
|
---|
| 182 | return mHttpResponse.GetHeader ().length ()
|
---|
| 183 | + mHttpResponse.GetContentLength ();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | int
|
---|
| 187 | CRXProxy::ReceiveRequest (void)
|
---|
| 188 | {
|
---|
| 189 | int aResult = 0;
|
---|
| 190 |
|
---|
| 191 | const unsigned int aBufferSize = 1024 * 56;
|
---|
| 192 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
| 193 |
|
---|
| 194 | /*----------------------------------------------------------------*/
|
---|
| 195 | aResult = mClient.Receive (aBuffer, aBufferSize);
|
---|
| 196 | if (aResult < 0)
|
---|
[18] | 197 | {
|
---|
[24] | 198 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
| 199 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
| 200 | CRX_ERROR (aResult, "Failed to receive from client");
|
---|
| 201 | return aResult;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /* parse http request */
|
---|
| 205 | mHttpRequest = aBuffer;
|
---|
| 206 | /*----------------------------------------------------------------*/
|
---|
| 207 |
|
---|
| 208 | return aResult < 0 ? aResult : 0;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | int
|
---|
| 212 | CRXProxy::SendRequest (void)
|
---|
| 213 | {
|
---|
| 214 | int aResult = 0;
|
---|
| 215 | int aSize = mHttpRequest.GetHeader ().length ();
|
---|
| 216 |
|
---|
| 217 | /*----------------------------------------------------------------*/
|
---|
| 218 | if (!mServer)
|
---|
| 219 | {
|
---|
| 220 | /* connect */
|
---|
| 221 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), 1);
|
---|
[18] | 222 | if (aResult < 0)
|
---|
| 223 | {
|
---|
[24] | 224 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
| 225 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
| 226 | CRX_ERROR (aResult, "Failed to connect to server <%s>", mHttpRequest.GetURL ().c_str ());
|
---|
| 227 | return aResult;
|
---|
[18] | 228 | }
|
---|
[24] | 229 | }
|
---|
[18] | 230 |
|
---|
[24] | 231 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
| 232 | if (aResult != aSize)
|
---|
| 233 | {
|
---|
| 234 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
| 235 | CRX_ERROR (aResult, "Failed to send to server <%s>", mHttpRequest.GetURL ().c_str ());
|
---|
| 236 | }
|
---|
| 237 | /*----------------------------------------------------------------*/
|
---|
[18] | 238 |
|
---|
[24] | 239 | return aResult < 0 ? aResult : 0;
|
---|
| 240 | }
|
---|
[18] | 241 |
|
---|
[24] | 242 | int
|
---|
| 243 | CRXProxy::ReceiveResponse (void)
|
---|
| 244 | {
|
---|
| 245 | int aResult = 0;
|
---|
| 246 |
|
---|
| 247 | const unsigned int aBufferSize = 1024 * 64;
|
---|
| 248 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
| 249 |
|
---|
| 250 | std::string aHttpResponse;
|
---|
| 251 |
|
---|
| 252 | /*----------------------------------------------------------------*/
|
---|
| 253 | for (;;)
|
---|
| 254 | {
|
---|
| 255 | memset (aBuffer, 0x00, sizeof (aBuffer));
|
---|
| 256 |
|
---|
| 257 | aResult = mServer.Receive (aBuffer, aBufferSize);
|
---|
| 258 | if (aResult < 0)
|
---|
[18] | 259 | {
|
---|
[24] | 260 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
| 261 | CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ());
|
---|
| 262 | CRX_ERROR (aResult, "Failed to receive from server");
|
---|
[18] | 263 | return aResult;
|
---|
| 264 | }
|
---|
[24] | 265 | else if (aResult == 0)
|
---|
| 266 | {
|
---|
| 267 | break;
|
---|
| 268 | }
|
---|
[18] | 269 |
|
---|
[24] | 270 | aResult = mHttpResponse.SetResponse (aBuffer, aResult);
|
---|
| 271 | if (aResult)
|
---|
[18] | 272 | {
|
---|
[24] | 273 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
| 274 | CRX_ERROR (aResult, "Failed to set response");
|
---|
| 275 | return aResult;
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | /*----------------------------------------------------------------*/
|
---|
[18] | 279 |
|
---|
[24] | 280 | return aResult < 0 ? aResult : 0;
|
---|
| 281 | }
|
---|
[18] | 282 |
|
---|
[24] | 283 | int
|
---|
| 284 | CRXProxy::SendResponse (void)
|
---|
| 285 | {
|
---|
| 286 | int aResult = 0;
|
---|
| 287 | int aSize = 0;
|
---|
[18] | 288 |
|
---|
| 289 | /*----------------------------------------------------------------*/
|
---|
[24] | 290 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
| 291 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
| 292 | if (aResult != aSize)
|
---|
| 293 | {
|
---|
| 294 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
| 295 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
| 296 | return aResult;
|
---|
| 297 | }
|
---|
[18] | 298 |
|
---|
[24] | 299 | aSize = mHttpResponse.GetContentLength ();
|
---|
| 300 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
| 301 | if (aResult != aSize)
|
---|
| 302 | {
|
---|
| 303 | CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ());
|
---|
| 304 | CRX_ERROR (aResult, "Failed to send to client");
|
---|
| 305 | }
|
---|
| 306 | /*----------------------------------------------------------------*/
|
---|
| 307 |
|
---|
| 308 | return aResult < 0 ? aResult : 0;
|
---|
| 309 | } |
---|