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 | for (;;)
|
---|
95 | {
|
---|
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;
|
---|
107 | }
|
---|
108 |
|
---|
109 | mHttpRequest.SetHeader (aHttpHeader.c_str ());
|
---|
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 |
|
---|
121 | struct sockaddr_in aSockName;
|
---|
122 | socklen_t aSockLen = sizeof (aSockName);
|
---|
123 |
|
---|
124 | /*----------------------------------------------------------------*/
|
---|
125 | if (!mServer)
|
---|
126 | {
|
---|
127 | /* connect */
|
---|
128 | aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
|
---|
129 | if (aResult < 0)
|
---|
130 | {
|
---|
131 | aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
|
---|
132 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
133 | CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
134 | return aResult;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
|
---|
139 | if (aResult != aSize)
|
---|
140 | {
|
---|
141 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
142 | CRX_ERROR_SET (aResult, "Failed to send to server <%s>.", mHttpRequest.GetURL ().c_str ());
|
---|
143 | }
|
---|
144 | /*----------------------------------------------------------------*/
|
---|
145 |
|
---|
146 | return aResult < 0 ? aResult : 0;
|
---|
147 | }
|
---|
148 |
|
---|
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)
|
---|
164 | {
|
---|
165 | aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST;
|
---|
166 | CRX_ADD_SUBCLASS_ERROR (mServer);
|
---|
167 | CRX_ERROR_SET (aResult, "Failed to receive from server.");
|
---|
168 | return aResult;
|
---|
169 | }
|
---|
170 |
|
---|
171 | aResult = mHttpResponse.SetResponseAll (aBuffer, aResult);
|
---|
172 | if (aResult < 0)
|
---|
173 | {
|
---|
174 | aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
|
---|
175 | CRX_ERROR_SET (aResult, "Failed to set response.");
|
---|
176 | return aResult;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | /*----------------------------------------------------------------*/
|
---|
180 |
|
---|
181 | return aResult < 0 ? aResult : 0;
|
---|
182 | }
|
---|
183 |
|
---|
184 | int
|
---|
185 | CRXProxy::SendResponse (void)
|
---|
186 | {
|
---|
187 | int aResult = 0;
|
---|
188 | int aSize = 0;
|
---|
189 |
|
---|
190 | /*----------------------------------------------------------------*/
|
---|
191 | aSize = mHttpResponse.GetHeader ().length ();;
|
---|
192 | aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
|
---|
193 | if (aResult != aSize)
|
---|
194 | {
|
---|
195 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
196 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
197 | return aResult;
|
---|
198 | }
|
---|
199 |
|
---|
200 | aSize = mHttpResponse.GetContentLength ();
|
---|
201 | aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
|
---|
202 | if (aResult != aSize)
|
---|
203 | {
|
---|
204 | CRX_ADD_SUBCLASS_ERROR (mClient);
|
---|
205 | CRX_ERROR_SET (aResult, "Failed to send to client.");
|
---|
206 | }
|
---|
207 | /*----------------------------------------------------------------*/
|
---|
208 |
|
---|
209 | return aResult < 0 ? aResult : 0;
|
---|
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;
|
---|
237 | } |
---|