[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 |
|
---|
[45] | 11 | CRXProxy::CRXProxy (const int aSocket)
|
---|
| 12 | : mServerTimeout (0)
|
---|
[24] | 13 | {
|
---|
| 14 | /*----------------------------------------------------------------*/
|
---|
[45] | 15 | if (aSocket < 0)
|
---|
| 16 | return ;
|
---|
| 17 |
|
---|
| 18 | mClient = aSocket;
|
---|
[24] | 19 | /*----------------------------------------------------------------*/
|
---|
| 20 | }
|
---|
[18] | 21 |
|
---|
[24] | 22 | CRXProxy::~CRXProxy (void)
|
---|
| 23 | {
|
---|
| 24 | /*----------------------------------------------------------------*/
|
---|
| 25 | Close ();
|
---|
| 26 | /*----------------------------------------------------------------*/
|
---|
| 27 | }
|
---|
[18] | 28 |
|
---|
[24] | 29 | void
|
---|
[36] | 30 | CRXProxy::SetServerTimeout (const int aTimeout)
|
---|
| 31 | {
|
---|
| 32 | /*----------------------------------------------------------------*/
|
---|
| 33 | mServerTimeout = aTimeout;
|
---|
| 34 | /*----------------------------------------------------------------*/
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | void
|
---|
[24] | 38 | CRXProxy::Close (void)
|
---|
[20] | 39 | {
|
---|
[24] | 40 | /*----------------------------------------------------------------*/
|
---|
| 41 | mServer.Close ();
|
---|
| 42 | mClient.Close ();
|
---|
| 43 | /*----------------------------------------------------------------*/
|
---|
[20] | 44 | }
|
---|
| 45 |
|
---|
[24] | 46 | int
|
---|
| 47 | CRXProxy::Forward (void)
|
---|
| 48 | {
|
---|
| 49 | int aResult = 0;
|
---|
| 50 |
|
---|
| 51 | /*----------------------------------------------------------------*/
|
---|
| 52 | aResult = ReceiveRequest ();
|
---|
| 53 | if (aResult < 0)
|
---|
[18] | 54 | {
|
---|
[24] | 55 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 56 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
[18] | 57 | }
|
---|
| 58 |
|
---|
[24] | 59 | aResult = SendRequest ();
|
---|
| 60 | if (aResult < 0)
|
---|
| 61 | {
|
---|
| 62 | aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST;
|
---|
[35] | 63 | CRX_ERROR_SET (aResult, "Failed to send to server.");
|
---|
[24] | 64 | return aResult;
|
---|
| 65 | }
|
---|
[23] | 66 |
|
---|
[24] | 67 | aResult = ReceiveResponse ();
|
---|
| 68 | if (aResult < 0)
|
---|
| 69 | {
|
---|
| 70 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE;
|
---|
[35] | 71 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
[24] | 72 | }
|
---|
| 73 |
|
---|
| 74 | aResult = SendResponse ();
|
---|
| 75 | if (aResult < 0)
|
---|
| 76 | {
|
---|
| 77 | aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE;
|
---|
[35] | 78 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 79 | return aResult;
|
---|
| 80 | }
|
---|
[18] | 81 | /*----------------------------------------------------------------*/
|
---|
[24] | 82 |
|
---|
| 83 | return aResult;
|
---|
[18] | 84 | }
|
---|
| 85 |
|
---|
| 86 | int
|
---|
[24] | 87 | CRXProxy::ReceiveRequest (void)
|
---|
| 88 | {
|
---|
[44] | 89 | int aResult = 0;
|
---|
| 90 | char aBuffer;
|
---|
[24] | 91 |
|
---|
[44] | 92 | std::string aHttpHeader;
|
---|
[24] | 93 | /*----------------------------------------------------------------*/
|
---|
[44] | 94 | for (;;)
|
---|
[18] | 95 | {
|
---|
[44] | 96 | aResult = mClient.Receive (&aBuffer, 1);
|
---|
| 97 | if (aResult < 0)
|
---|
| 98 | {
|
---|
| 99 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
| 100 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 101 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
| 102 | return aResult;
|
---|
| 103 | }
|
---|
| 104 | aHttpHeader += aBuffer;
|
---|
| 105 | if (aHttpHeader.rfind (CRLF2) != std::string::npos)
|
---|
| 106 | break;
|
---|
[24] | 107 | }
|
---|
| 108 |
|
---|
[44] | 109 | mHttpRequest.SetHeader (aHttpHeader.c_str ());
|
---|
[24] | 110 | /*----------------------------------------------------------------*/
|
---|
| 111 |
|
---|
| 112 | return aResult < 0 ? aResult : 0;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | int
|
---|
| 116 | CRXProxy::SendRequest (void)
|
---|
| 117 | {
|
---|
| 118 | int aResult = 0;
|
---|
| 119 | int aSize = mHttpRequest.GetHeader ().length ();
|
---|
| 120 |
|
---|
[38] | 121 | struct sockaddr_in aSockName;
|
---|
| 122 | socklen_t aSockLen = sizeof (aSockName);
|
---|
| 123 |
|
---|
[24] | 124 | /*----------------------------------------------------------------*/
|
---|
| 125 | if (!mServer)
|
---|
| 126 | {
|
---|
| 127 | /* connect */
|
---|
[36] | 128 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
|
---|
[18] | 129 | if (aResult < 0)
|
---|
| 130 | {
|
---|
[24] | 131 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
[35] | 132 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 133 | CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
[24] | 134 | return aResult;
|
---|
[18] | 135 | }
|
---|
[24] | 136 | }
|
---|
[18] | 137 |
|
---|
[24] | 138 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
| 139 | if (aResult != aSize)
|
---|
| 140 | {
|
---|
[35] | 141 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 142 | CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
[24] | 143 | }
|
---|
| 144 | /*----------------------------------------------------------------*/
|
---|
[18] | 145 |
|
---|
[24] | 146 | return aResult < 0 ? aResult : 0;
|
---|
| 147 | }
|
---|
[18] | 148 |
|
---|
[24] | 149 | int
|
---|
| 150 | CRXProxy::ReceiveResponse (void)
|
---|
| 151 | {
|
---|
| 152 | int aResult = 0;
|
---|
| 153 |
|
---|
| 154 | const unsigned int aBufferSize = 1024 * 64;
|
---|
| 155 | char aBuffer[aBufferSize + 1] = {0x00, };
|
---|
| 156 |
|
---|
| 157 | /*----------------------------------------------------------------*/
|
---|
| 158 | for (;;)
|
---|
| 159 | {
|
---|
| 160 | memset (aBuffer, 0x00, sizeof (aBuffer));
|
---|
| 161 |
|
---|
| 162 | aResult = mServer.Receive (aBuffer, aBufferSize);
|
---|
| 163 | if (aResult < 0)
|
---|
[18] | 164 | {
|
---|
[24] | 165 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 166 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 167 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
[18] | 168 | return aResult;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[44] | 171 | aResult = mHttpResponse.SetResponseAll (aBuffer, aResult);
|
---|
[45] | 172 | if (aResult < 0)
|
---|
[18] | 173 | {
|
---|
[24] | 174 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
[35] | 175 | CRX_ERROR_SET (aResult, "Failed to set response.");
|
---|
[24] | 176 | return aResult;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | /*----------------------------------------------------------------*/
|
---|
[18] | 180 |
|
---|
[24] | 181 | return aResult < 0 ? aResult : 0;
|
---|
| 182 | }
|
---|
[18] | 183 |
|
---|
[24] | 184 | int
|
---|
| 185 | CRXProxy::SendResponse (void)
|
---|
| 186 | {
|
---|
| 187 | int aResult = 0;
|
---|
| 188 | int aSize = 0;
|
---|
[18] | 189 |
|
---|
| 190 | /*----------------------------------------------------------------*/
|
---|
[24] | 191 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
| 192 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
| 193 | if (aResult != aSize)
|
---|
| 194 | {
|
---|
[35] | 195 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 196 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 197 | return aResult;
|
---|
| 198 | }
|
---|
[18] | 199 |
|
---|
[24] | 200 | aSize = mHttpResponse.GetContentLength ();
|
---|
| 201 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
| 202 | if (aResult != aSize)
|
---|
| 203 | {
|
---|
[35] | 204 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 205 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 206 | }
|
---|
| 207 | /*----------------------------------------------------------------*/
|
---|
| 208 |
|
---|
| 209 | return aResult < 0 ? aResult : 0;
|
---|
[45] | 210 | }
|
---|
| 211 |
|
---|
| 212 | CRXFilter &
|
---|
| 213 | CRXProxy::GetFilter (void) const
|
---|
| 214 | {
|
---|
| 215 | /*----------------------------------------------------------------*/
|
---|
| 216 | /*----------------------------------------------------------------*/
|
---|
| 217 |
|
---|
| 218 | return mFilter;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | CRXHttpRequest &
|
---|
| 222 | CRXProxy::GetHttpRequest (void)
|
---|
| 223 | {
|
---|
| 224 | /*----------------------------------------------------------------*/
|
---|
| 225 | /*----------------------------------------------------------------*/
|
---|
| 226 |
|
---|
| 227 | return mHttpRequest;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | CRXHttpResponse &
|
---|
| 231 | CRXProxy::GetHttpResponse (void)
|
---|
| 232 | {
|
---|
| 233 | /*----------------------------------------------------------------*/
|
---|
| 234 | /*----------------------------------------------------------------*/
|
---|
| 235 |
|
---|
| 236 | return mHttpResponse;
|
---|
[24] | 237 | } |
---|