[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;
|
---|
[46] | 93 |
|
---|
[24] | 94 | /*----------------------------------------------------------------*/
|
---|
[44] | 95 | for (;;)
|
---|
[18] | 96 | {
|
---|
[44] | 97 | aResult = mClient.Receive (&aBuffer, 1);
|
---|
| 98 | if (aResult < 0)
|
---|
| 99 | {
|
---|
| 100 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
| 101 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 102 | CRX_ERROR_SET (aResult, "Failed to receive from client.");
|
---|
| 103 | return aResult;
|
---|
| 104 | }
|
---|
| 105 | aHttpHeader += aBuffer;
|
---|
| 106 | if (aHttpHeader.rfind (CRLF2) != std::string::npos)
|
---|
| 107 | break;
|
---|
[24] | 108 | }
|
---|
| 109 |
|
---|
[44] | 110 | mHttpRequest.SetHeader (aHttpHeader.c_str ());
|
---|
[24] | 111 | /*----------------------------------------------------------------*/
|
---|
| 112 |
|
---|
| 113 | return aResult < 0 ? aResult : 0;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | int
|
---|
| 117 | CRXProxy::SendRequest (void)
|
---|
| 118 | {
|
---|
[46] | 119 | int aResult = 0;
|
---|
| 120 | int aLength = mHttpRequest.GetHeader ().length ();
|
---|
[24] | 121 |
|
---|
| 122 | /*----------------------------------------------------------------*/
|
---|
[46] | 123 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
|
---|
| 124 | if (aResult < 0)
|
---|
[24] | 125 | {
|
---|
[46] | 126 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
| 127 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 128 | CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
| 129 | return aResult;
|
---|
[24] | 130 | }
|
---|
[18] | 131 |
|
---|
[46] | 132 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aLength);
|
---|
| 133 | if (aResult != aLength)
|
---|
[24] | 134 | {
|
---|
[35] | 135 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 136 | CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
[24] | 137 | }
|
---|
| 138 | /*----------------------------------------------------------------*/
|
---|
[18] | 139 |
|
---|
[24] | 140 | return aResult < 0 ? aResult : 0;
|
---|
| 141 | }
|
---|
[18] | 142 |
|
---|
[24] | 143 | int
|
---|
| 144 | CRXProxy::ReceiveResponse (void)
|
---|
| 145 | {
|
---|
[46] | 146 | int aResult = 0;
|
---|
| 147 | char aBuffer;
|
---|
[24] | 148 |
|
---|
[46] | 149 | std::string aHttpHeader;
|
---|
| 150 | bool aRemainHeader = true;
|
---|
[24] | 151 |
|
---|
| 152 | /*----------------------------------------------------------------*/
|
---|
| 153 | for (;;)
|
---|
| 154 | {
|
---|
[46] | 155 | aResult = mServer.Receive (&aBuffer, 1);
|
---|
[24] | 156 | if (aResult < 0)
|
---|
[18] | 157 | {
|
---|
[24] | 158 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
[35] | 159 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 160 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
[18] | 161 | return aResult;
|
---|
| 162 | }
|
---|
[56] | 163 | else if (aResult == 0)
|
---|
| 164 | {
|
---|
| 165 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
| 166 | CRX_ERROR_SET (aResult, "Receive zero-byte from server.");
|
---|
| 167 | break;
|
---|
| 168 | }
|
---|
[18] | 169 |
|
---|
[46] | 170 | if (aRemainHeader)
|
---|
[18] | 171 | {
|
---|
[46] | 172 | aHttpHeader += aBuffer;
|
---|
| 173 | if (aHttpHeader.rfind (CRLF2) != std::string::npos)
|
---|
| 174 | {
|
---|
| 175 | mHttpResponse.SetHeader (aHttpHeader.c_str ());
|
---|
| 176 | aRemainHeader = false;
|
---|
| 177 | }
|
---|
[24] | 178 | }
|
---|
[46] | 179 | else
|
---|
| 180 | {
|
---|
| 181 | aResult = mHttpResponse.SetContent (&aBuffer, aResult);
|
---|
| 182 | if (aResult < 0)
|
---|
| 183 | {
|
---|
| 184 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
| 185 | CRX_ERROR_SET (aResult, "Failed to set response.");
|
---|
| 186 | return aResult;
|
---|
| 187 | }
|
---|
| 188 | else if (aResult == 0)
|
---|
| 189 | break;
|
---|
| 190 | }
|
---|
[24] | 191 | }
|
---|
| 192 | /*----------------------------------------------------------------*/
|
---|
[18] | 193 |
|
---|
[24] | 194 | return aResult < 0 ? aResult : 0;
|
---|
| 195 | }
|
---|
[18] | 196 |
|
---|
[24] | 197 | int
|
---|
| 198 | CRXProxy::SendResponse (void)
|
---|
| 199 | {
|
---|
[46] | 200 | int aResult = 0;
|
---|
| 201 | int aLength = 0;
|
---|
[18] | 202 | /*----------------------------------------------------------------*/
|
---|
[46] | 203 | aLength = mHttpResponse.GetHeader ().length ();
|
---|
| 204 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aLength);
|
---|
| 205 | if (aResult != aLength)
|
---|
[24] | 206 | {
|
---|
[35] | 207 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 208 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 209 | return aResult;
|
---|
| 210 | }
|
---|
[18] | 211 |
|
---|
[46] | 212 | aLength = mHttpResponse.GetContentLength ();
|
---|
| 213 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aLength);
|
---|
| 214 | if (aResult != aLength)
|
---|
[24] | 215 | {
|
---|
[35] | 216 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
| 217 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
[24] | 218 | }
|
---|
| 219 | /*----------------------------------------------------------------*/
|
---|
| 220 |
|
---|
| 221 | return aResult < 0 ? aResult : 0;
|
---|
[45] | 222 | }
|
---|
| 223 |
|
---|
| 224 | CRXFilter &
|
---|
| 225 | CRXProxy::GetFilter (void) const
|
---|
| 226 | {
|
---|
| 227 | /*----------------------------------------------------------------*/
|
---|
| 228 | /*----------------------------------------------------------------*/
|
---|
| 229 |
|
---|
| 230 | return mFilter;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | CRXHttpRequest &
|
---|
| 234 | CRXProxy::GetHttpRequest (void)
|
---|
| 235 | {
|
---|
| 236 | /*----------------------------------------------------------------*/
|
---|
| 237 | /*----------------------------------------------------------------*/
|
---|
| 238 |
|
---|
| 239 | return mHttpRequest;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | CRXHttpResponse &
|
---|
| 243 | CRXProxy::GetHttpResponse (void)
|
---|
| 244 | {
|
---|
| 245 | /*----------------------------------------------------------------*/
|
---|
| 246 | /*----------------------------------------------------------------*/
|
---|
| 247 |
|
---|
| 248 | return mHttpResponse;
|
---|
[24] | 249 | } |
---|