7 #include <boost/foreach.hpp>
10 #include <simgear/io/sg_file.hxx>
11 #include <simgear/io/HTTPClient.hxx>
12 #include <simgear/io/HTTPRequest.hxx>
13 #include <simgear/io/sg_netChannel.hxx>
14 #include <simgear/misc/strutils.hxx>
15 #include <simgear/timing/timestamp.hxx>
17 using namespace simgear;
23 class ARequest : public HTTP::Request
26 ARequest(string& url) :
34 void setFile(SGFile* f)
42 void addHeader(const string& h)
44 int colonPos = h.find(':');
46 cerr << "malformed header: " << h << endl;
50 string key = h.substr(0, colonPos);
51 requestHeader(key) = h.substr(colonPos + 1);
60 virtual void gotBodyData(const char* s, int n)
69 int main(int argc, char* argv[])
73 string proxy, proxyAuth;
77 for (int a=0; a<argc;++a) {
78 if (argv[a][0] == '-') {
79 if (!strcmp(argv[a], "--user-agent")) {
80 cl.setUserAgent(argv[++a]);
81 } else if (!strcmp(argv[a], "--proxy")) {
83 } else if (!strcmp(argv[a], "--auth")) {
84 proxyAuth = argv[++a];
85 } else if (!strcmp(argv[a], "-f") || !strcmp(argv[a], "--file")) {
86 outFile = new SGFile(argv[++a]);
87 if (!outFile->open(SG_IO_OUT)) {
88 cerr << "failed to open output for writing:" << outFile->get_file_name() << endl;
91 } else if (!strcmp(argv[a], "--header")) {
92 headers.push_back(argv[++a]);
94 } else { // of argument starts with a hyphen
97 } // of arguments iteration
100 int colonPos = proxy.find(':');
101 string proxyHost = proxy;
102 int proxyPort = 8800;
104 proxyHost = proxy.substr(0, colonPos);
105 proxyPort = strutils::to_int(proxy.substr(colonPos + 1));
108 cl.setProxy(proxyHost, proxyPort, proxyAuth);
112 signal(SIGPIPE, SIG_IGN);
116 outFile = new SGFile(fileno(stdout));
120 cerr << "no URL argument specificed" << endl;
124 ARequest* req = new ARequest(url);
125 BOOST_FOREACH(string h, headers) {
129 req->setFile(outFile);
132 while (!req->complete()) {
134 SGTimeStamp::sleepForMSec(100);
137 if (req->responseCode() != 200) {
138 cerr << "got response:" << req->responseCode() << endl;
139 cerr << "\treason:" << req->responseReason() << endl;