7 #include <boost/algorithm/string/case_conv.hpp>
9 #include "HTTPClient.hxx"
10 #include "HTTPRequest.hxx"
12 #include <simgear/io/sg_netChat.hxx>
13 #include <simgear/misc/strutils.hxx>
14 #include <simgear/timing/timestamp.hxx>
20 using std::stringstream;
22 using namespace simgear;
24 const char* BODY1 = "The quick brown fox jumps over a lazy dog.";
26 const unsigned int body2Size = 8 * 1024;
27 char body2[body2Size];
29 #define COMPARE(a, b) \
31 cerr << "failed:" << #a << " != " << #b << endl; \
32 cerr << "\tgot:" << a << endl; \
38 cerr << "failed:" << #a << endl; \
42 class TestRequest : public HTTP::Request
49 TestRequest(const std::string& url) :
56 std::map<string, string> headers;
58 virtual void responseHeadersComplete()
62 virtual void responseComplete()
67 virtual void failure()
72 virtual void gotBodyData(const char* s, int n)
74 bodyData += string(s, n);
77 virtual void responseHeader(const string& header, const string& value)
79 headers[header] = value;
83 class TestServerChannel : public NetChat
96 setTerminator("\r\n");
99 virtual void collectIncomingData(const char* s, int n)
101 buffer += string(s, n);
104 virtual void foundTerminator(void)
106 if (state == STATE_IDLE) {
107 state = STATE_HEADERS;
108 string_list line = strutils::split(buffer, NULL, 3);
109 if (line.size() < 3) {
110 cerr << "malformed request:" << buffer << endl;
116 httpVersion = line[2];
117 requestHeaders.clear();
119 } else if (state == STATE_HEADERS) {
120 string s = strutils::simplify(buffer);
123 receivedRequestHeaders();
127 int colonPos = buffer.find(':');
129 cerr << "malformed HTTP response header:" << buffer << endl;
134 string key = strutils::simplify(buffer.substr(0, colonPos));
135 string lkey = boost::to_lower_copy(key);
136 string value = strutils::strip(buffer.substr(colonPos + 1));
137 requestHeaders[lkey] = value;
139 } else if (state == STATE_REQUEST_BODY) {
144 void receivedRequestHeaders()
147 if (path == "/test1") {
148 string contentStr(BODY1);
150 d << "HTTP1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
151 d << "Content-Length:" << contentStr.size() << "\r\n";
152 d << "\r\n"; // final CRLF to terminate the headers
154 push(d.str().c_str());
155 } else if (path == "/test2") {
157 } else if (path == "/testchunked") {
159 d << "HTTP1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
160 d << "Transfer-Encoding:chunked\r\n";
162 d << "8\r\n"; // first chunk
164 d << "6\r\n"; // second chunk
166 d << "10\r\n"; // third chunk
167 d << "ABCDSTUVABCDSTUV\r\n";
168 d << "0\r\n"; // start of trailer
169 d << "X-Foobar: wibble\r\n"; // trailer data
171 push(d.str().c_str());
172 } else if (path == "http://www.google.com/test2") {
174 if (requestHeaders["host"] != "www.google.com") {
175 sendErrorResponse(400);
178 if (requestHeaders["proxy-authorization"] != string()) {
179 sendErrorResponse(401); // shouldn't supply auth
183 } else if (path == "http://www.google.com/test3") {
185 if (requestHeaders["host"] != "www.google.com") {
186 sendErrorResponse(400);
189 if (requestHeaders["proxy-authorization"] != "ABCDEF") {
190 sendErrorResponse(401); // forbidden
195 sendErrorResponse(404);
202 d << "HTTP1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
203 d << "Content-Length:" << body2Size << "\r\n";
204 d << "\r\n"; // final CRLF to terminate the headers
205 push(d.str().c_str());
206 bufferSend(body2, body2Size);
209 void sendErrorResponse(int code)
211 cerr << "sending error " << code << " for " << path << endl;
212 stringstream headerData;
213 headerData << "HTTP1.1 " << code << " " << reasonForCode(code) << "\r\n";
214 headerData << "\r\n"; // final CRLF to terminate the headers
215 push(headerData.str().c_str());
218 string reasonForCode(int code)
221 case 200: return "OK";
222 case 404: return "not found";
223 default: return "unknown code";
232 std::map<string, string> requestHeaders;
235 class TestServer : public NetChannel
241 bind(NULL, 2000); // localhost, any port
245 virtual ~TestServer()
249 virtual bool writable (void) { return false ; }
251 virtual void handleAccept (void)
253 simgear::IPAddress addr ;
254 int handle = accept ( &addr ) ;
255 cout << "did accept from " << addr.getHost() << ":" << addr.getPort() << endl;
256 TestServerChannel* chan = new TestServerChannel();
257 chan->setHandle(handle);
261 void waitForComplete(TestRequest* tr)
263 SGTimeStamp start(SGTimeStamp::now());
264 while (start.elapsedMSec() < 1000) {
265 NetChannel::poll(10);
271 cerr << "timed out" << endl;
274 void waitForFailed(TestRequest* tr)
276 SGTimeStamp start(SGTimeStamp::now());
277 while (start.elapsedMSec() < 1000) {
278 NetChannel::poll(10);
284 cerr << "timed out waiting for failure" << endl;
287 int main(int argc, char* argv[])
294 TestRequest* tr1 = new TestRequest("http://localhost.woo.zar:2000/test1?foo=bar");
295 COMPARE(tr1->scheme(), "http");
296 COMPARE(tr1->hostAndPort(), "localhost.woo.zar:2000");
297 COMPARE(tr1->host(), "localhost.woo.zar");
298 COMPARE(tr1->port(), 2000);
299 COMPARE(tr1->path(), "/test1");
301 TestRequest* tr2 = new TestRequest("http://192.168.1.1/test1/dir/thing/file.png");
302 COMPARE(tr2->scheme(), "http");
303 COMPARE(tr2->hostAndPort(), "192.168.1.1");
304 COMPARE(tr2->host(), "192.168.1.1");
305 COMPARE(tr2->port(), 80);
306 COMPARE(tr2->path(), "/test1/dir/thing/file.png");
310 TestRequest* tr = new TestRequest("http://localhost:2000/test1");
311 HTTP::Request_ptr own(tr);
315 COMPARE(tr->responseCode(), 200);
316 COMPARE(tr->responseReason(), string("OK"));
317 COMPARE(tr->responseLength(), strlen(BODY1));
318 COMPARE(tr->responseBytesReceived(), strlen(BODY1));
319 COMPARE(tr->bodyData, string(BODY1));
322 // larger get request
323 for (unsigned int i=0; i<body2Size; ++i) {
324 body2[i] = (i << 4) | (i >> 2);
328 TestRequest* tr = new TestRequest("http://localhost:2000/test2");
329 HTTP::Request_ptr own(tr);
332 COMPARE(tr->responseCode(), 200);
333 COMPARE(tr->responseBytesReceived(), body2Size);
334 COMPARE(tr->bodyData, string(body2, body2Size));
338 TestRequest* tr = new TestRequest("http://localhost:2000/testchunked");
339 HTTP::Request_ptr own(tr);
343 COMPARE(tr->responseCode(), 200);
344 COMPARE(tr->responseReason(), string("OK"));
345 COMPARE(tr->responseBytesReceived(), 30);
346 COMPARE(tr->bodyData, "ABCDEFGHABCDEFABCDSTUVABCDSTUV");
347 // check trailers made it too
348 COMPARE(tr->headers["x-foobar"], string("wibble"));
353 TestRequest* tr = new TestRequest("http://localhost:2000/not-found");
354 HTTP::Request_ptr own(tr);
357 COMPARE(tr->responseCode(), 404);
358 COMPARE(tr->responseReason(), string("not found"));
359 COMPARE(tr->responseLength(), 0);
362 // test connectToHost failure
365 TestRequest* tr = new TestRequest("http://not.found/something");
366 HTTP::Request_ptr own(tr);
369 COMPARE(tr->responseCode(), -1);
374 cl.setProxy("localhost", 2000);
375 TestRequest* tr = new TestRequest("http://www.google.com/test2");
376 HTTP::Request_ptr own(tr);
379 COMPARE(tr->responseCode(), 200);
380 COMPARE(tr->responseLength(), body2Size);
381 COMPARE(tr->bodyData, string(body2, body2Size));
385 cl.setProxy("localhost", 2000, "ABCDEF");
386 TestRequest* tr = new TestRequest("http://www.google.com/test3");
387 HTTP::Request_ptr own(tr);
390 COMPARE(tr->responseCode(), 200);
391 COMPARE(tr->responseBytesReceived(), body2Size);
392 COMPARE(tr->bodyData, string(body2, body2Size));
395 cout << "all tests passed ok" << endl;