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>
15 #include <simgear/misc/sg_sleep.hxx>
21 using std::stringstream;
23 using namespace simgear;
25 const char* BODY1 = "The quick brown fox jumps over a lazy dog.";
27 const unsigned int body2Size = 8 * 1024;
28 char body2[body2Size];
30 #define COMPARE(a, b) \
32 cerr << "failed:" << #a << " != " << #b << endl; \
33 cerr << "\tgot:" << a << endl; \
39 cerr << "failed:" << #a << endl; \
43 class TestRequest : public HTTP::Request
50 TestRequest(const std::string& url) :
56 std::map<string, string> sendHeaders;
57 std::map<string, string> headers;
59 string_list requestHeaders() const
62 std::map<string, string>::const_iterator it;
63 for (it = sendHeaders.begin(); it != sendHeaders.end(); ++it) {
64 r.push_back(it->first);
69 string header(const string& name) const
71 std::map<string, string>::const_iterator it = sendHeaders.find(name);
72 if (it == sendHeaders.end()) {
79 virtual void responseHeadersComplete()
83 virtual void responseComplete()
88 virtual void failure()
93 virtual void gotBodyData(const char* s, int n)
95 bodyData += string(s, n);
98 virtual void responseHeader(const string& header, const string& value)
100 headers[header] = value;
104 class TestServerChannel : public NetChat
117 setTerminator("\r\n");
120 virtual void collectIncomingData(const char* s, int n)
122 buffer += string(s, n);
125 virtual void foundTerminator(void)
127 if (state == STATE_IDLE) {
128 state = STATE_HEADERS;
129 string_list line = strutils::split(buffer, NULL, 3);
130 if (line.size() < 3) {
131 cerr << "malformed request:" << buffer << endl;
137 httpVersion = line[2];
138 requestHeaders.clear();
140 } else if (state == STATE_HEADERS) {
141 string s = strutils::simplify(buffer);
144 receivedRequestHeaders();
148 int colonPos = buffer.find(':');
150 cerr << "malformed HTTP response header:" << buffer << endl;
155 string key = strutils::simplify(buffer.substr(0, colonPos));
156 string value = strutils::strip(buffer.substr(colonPos + 1));
157 requestHeaders[key] = value;
159 } else if (state == STATE_REQUEST_BODY) {
164 void receivedRequestHeaders()
168 if (path == "/test1") {
169 string contentStr(BODY1);
171 d << "HTTP/1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
172 d << "Content-Length:" << contentStr.size() << "\r\n";
173 d << "\r\n"; // final CRLF to terminate the headers
175 push(d.str().c_str());
176 } else if (path == "/test_headers") {
177 COMPARE(requestHeaders["X-Foo"], string("Bar"));
178 COMPARE(requestHeaders["X-AnotherHeader"], string("A longer value"));
180 string contentStr(BODY1);
182 d << "HTTP/1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
183 d << "Content-Length:" << contentStr.size() << "\r\n";
184 d << "\r\n"; // final CRLF to terminate the headers
186 push(d.str().c_str());
187 } else if (path == "/test2") {
189 } else if (path == "/testchunked") {
191 d << "HTTP/1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
192 d << "Transfer-Encoding:chunked\r\n";
194 d << "8\r\n"; // first chunk
196 d << "6\r\n"; // second chunk
198 d << "10\r\n"; // third chunk
199 d << "ABCDSTUVABCDSTUV\r\n";
200 d << "0\r\n"; // start of trailer
201 d << "X-Foobar: wibble\r\n"; // trailer data
203 push(d.str().c_str());
204 } else if (path == "http://www.google.com/test2") {
206 if (requestHeaders["Host"] != "www.google.com") {
207 sendErrorResponse(400, true, "bad destination");
210 if (requestHeaders["Proxy-Authorization"] != string()) {
211 sendErrorResponse(401, false, "bad auth"); // shouldn't supply auth
215 } else if (path == "http://www.google.com/test3") {
217 if (requestHeaders["Host"] != "www.google.com") {
218 sendErrorResponse(400, true, "bad destination");
221 if (requestHeaders["Proxy-Authorization"] != "ABCDEF") {
222 sendErrorResponse(401, false, "bad auth"); // forbidden
226 } else if (strutils::starts_with(path, "/test_1_0")) {
227 string contentStr(BODY1);
229 d << "HTTP/1.0 " << 200 << " " << reasonForCode(200) << "\r\n";
230 d << "\r\n"; // final CRLF to terminate the headers
232 push(d.str().c_str());
234 } else if (path == "/test_close") {
235 string contentStr(BODY1);
237 d << "HTTP/1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
238 d << "Connection: close\r\n";
239 d << "\r\n"; // final CRLF to terminate the headers
241 push(d.str().c_str());
244 sendErrorResponse(404, true, "");
251 d << "HTTP/1.1 " << 200 << " " << reasonForCode(200) << "\r\n";
252 d << "Content-Length:" << body2Size << "\r\n";
253 d << "\r\n"; // final CRLF to terminate the headers
254 push(d.str().c_str());
255 bufferSend(body2, body2Size);
258 void sendErrorResponse(int code, bool close, string content)
260 cerr << "sending error " << code << " for " << path << endl;
261 stringstream headerData;
262 headerData << "HTTP/1.1 " << code << " " << reasonForCode(code) << "\r\n";
263 headerData << "Content-Length:" << content.size() << "\r\n";
264 headerData << "\r\n"; // final CRLF to terminate the headers
265 push(headerData.str().c_str());
266 push(content.c_str());
273 string reasonForCode(int code)
276 case 200: return "OK";
277 case 404: return "not found";
278 default: return "unknown code";
287 std::map<string, string> requestHeaders;
290 class TestServer : public NetChannel
296 bind(NULL, 2000); // localhost, any port
300 virtual ~TestServer()
304 virtual bool writable (void) { return false ; }
306 virtual void handleAccept (void)
308 simgear::IPAddress addr ;
309 int handle = accept ( &addr ) ;
310 //cout << "did accept from " << addr.getHost() << ":" << addr.getPort() << endl;
311 TestServerChannel* chan = new TestServerChannel();
312 chan->setHandle(handle);
316 void waitForComplete(HTTP::Client* cl, TestRequest* tr)
318 SGTimeStamp start(SGTimeStamp::now());
319 while (start.elapsedMSec() < 1000) {
327 cerr << "timed out" << endl;
330 void waitForFailed(HTTP::Client* cl, TestRequest* tr)
332 SGTimeStamp start(SGTimeStamp::now());
333 while (start.elapsedMSec() < 1000) {
341 cerr << "timed out waiting for failure" << endl;
344 int main(int argc, char* argv[])
351 TestRequest* tr1 = new TestRequest("http://localhost.woo.zar:2000/test1?foo=bar");
352 COMPARE(tr1->scheme(), "http");
353 COMPARE(tr1->hostAndPort(), "localhost.woo.zar:2000");
354 COMPARE(tr1->host(), "localhost.woo.zar");
355 COMPARE(tr1->port(), 2000);
356 COMPARE(tr1->path(), "/test1");
358 TestRequest* tr2 = new TestRequest("http://192.168.1.1/test1/dir/thing/file.png");
359 COMPARE(tr2->scheme(), "http");
360 COMPARE(tr2->hostAndPort(), "192.168.1.1");
361 COMPARE(tr2->host(), "192.168.1.1");
362 COMPARE(tr2->port(), 80);
363 COMPARE(tr2->path(), "/test1/dir/thing/file.png");
367 TestRequest* tr = new TestRequest("http://localhost:2000/test1");
368 HTTP::Request_ptr own(tr);
371 waitForComplete(&cl, tr);
372 COMPARE(tr->responseCode(), 200);
373 COMPARE(tr->responseReason(), string("OK"));
374 COMPARE(tr->responseLength(), strlen(BODY1));
375 COMPARE(tr->responseBytesReceived(), strlen(BODY1));
376 COMPARE(tr->bodyData, string(BODY1));
380 TestRequest* tr = new TestRequest("http://localhost:2000/test_headers");
381 HTTP::Request_ptr own(tr);
382 tr->sendHeaders["X-Foo"] = "Bar";
383 tr->sendHeaders["X-AnotherHeader"] = "A longer value";
386 waitForComplete(&cl, tr);
387 COMPARE(tr->responseCode(), 200);
388 COMPARE(tr->responseReason(), string("OK"));
389 COMPARE(tr->responseLength(), strlen(BODY1));
390 COMPARE(tr->responseBytesReceived(), strlen(BODY1));
391 COMPARE(tr->bodyData, string(BODY1));
394 // larger get request
395 for (unsigned int i=0; i<body2Size; ++i) {
396 body2[i] = (i << 4) | (i >> 2);
400 TestRequest* tr = new TestRequest("http://localhost:2000/test2");
401 HTTP::Request_ptr own(tr);
403 waitForComplete(&cl, tr);
404 COMPARE(tr->responseCode(), 200);
405 COMPARE(tr->responseBytesReceived(), body2Size);
406 COMPARE(tr->bodyData, string(body2, body2Size));
410 TestRequest* tr = new TestRequest("http://localhost:2000/testchunked");
411 HTTP::Request_ptr own(tr);
414 waitForComplete(&cl, tr);
415 COMPARE(tr->responseCode(), 200);
416 COMPARE(tr->responseReason(), string("OK"));
417 COMPARE(tr->responseBytesReceived(), 30);
418 COMPARE(tr->bodyData, "ABCDEFGHABCDEFABCDSTUVABCDSTUV");
419 // check trailers made it too
420 COMPARE(tr->headers["x-foobar"], string("wibble"));
425 TestRequest* tr = new TestRequest("http://localhost:2000/not-found");
426 HTTP::Request_ptr own(tr);
428 waitForComplete(&cl, tr);
429 COMPARE(tr->responseCode(), 404);
430 COMPARE(tr->responseReason(), string("not found"));
431 COMPARE(tr->responseLength(), 0);
434 cout << "done1" << endl;
437 TestRequest* tr = new TestRequest("http://localhost:2000/test_1_0");
438 HTTP::Request_ptr own(tr);
440 waitForComplete(&cl, tr);
441 COMPARE(tr->responseCode(), 200);
442 COMPARE(tr->responseLength(), strlen(BODY1));
443 COMPARE(tr->bodyData, string(BODY1));
446 cout << "done2" << endl;
447 // test HTTP/1.1 Connection::close
449 TestRequest* tr = new TestRequest("http://localhost:2000/test_close");
450 HTTP::Request_ptr own(tr);
452 waitForComplete(&cl, tr);
453 COMPARE(tr->responseCode(), 200);
454 COMPARE(tr->responseLength(), strlen(BODY1));
455 COMPARE(tr->bodyData, string(BODY1));
457 cout << "done3" << endl;
458 // test connectToHost failure
461 TestRequest* tr = new TestRequest("http://not.found/something");
462 HTTP::Request_ptr own(tr);
465 COMPARE(tr->responseCode(), -1);
470 cl.setProxy("localhost", 2000);
471 TestRequest* tr = new TestRequest("http://www.google.com/test2");
472 HTTP::Request_ptr own(tr);
474 waitForComplete(&cl, tr);
475 COMPARE(tr->responseCode(), 200);
476 COMPARE(tr->responseLength(), body2Size);
477 COMPARE(tr->bodyData, string(body2, body2Size));
481 cl.setProxy("localhost", 2000, "ABCDEF");
482 TestRequest* tr = new TestRequest("http://www.google.com/test3");
483 HTTP::Request_ptr own(tr);
485 waitForComplete(&cl, tr);
486 COMPARE(tr->responseCode(), 200);
487 COMPARE(tr->responseBytesReceived(), body2Size);
488 COMPARE(tr->bodyData, string(body2, body2Size));
494 TestRequest* tr = new TestRequest("http://localhost:2000/test1");
495 HTTP::Request_ptr own(tr);
499 TestRequest* tr2 = new TestRequest("http://localhost:2000/test1");
500 HTTP::Request_ptr own2(tr2);
503 TestRequest* tr3 = new TestRequest("http://localhost:2000/test1");
504 HTTP::Request_ptr own3(tr3);
507 waitForComplete(&cl, tr3);
508 VERIFY(tr->complete);
509 VERIFY(tr2->complete);
510 COMPARE(tr->bodyData, string(BODY1));
511 COMPARE(tr2->bodyData, string(BODY1));
512 COMPARE(tr3->bodyData, string(BODY1));
515 // multiple requests with an HTTP 1.0 server
517 cout << "http 1.0 multiple requests" << endl;
520 TestRequest* tr = new TestRequest("http://localhost:2000/test_1_0/A");
521 HTTP::Request_ptr own(tr);
524 TestRequest* tr2 = new TestRequest("http://localhost:2000/test_1_0/B");
525 HTTP::Request_ptr own2(tr2);
528 TestRequest* tr3 = new TestRequest("http://localhost:2000/test_1_0/C");
529 HTTP::Request_ptr own3(tr3);
532 waitForComplete(&cl, tr3);
533 VERIFY(tr->complete);
534 VERIFY(tr2->complete);
535 COMPARE(tr->bodyData, string(BODY1));
536 COMPARE(tr2->bodyData, string(BODY1));
537 COMPARE(tr3->bodyData, string(BODY1));
540 cout << "all tests passed ok" << endl;