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