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 _headers[key] = h.substr(colonPos + 1);
54 virtual string_list requestHeaders() const
57 std::map<string, string>::const_iterator it;
58 for (it = _headers.begin(); it != _headers.end(); ++it) {
59 r.push_back(it->first);
65 virtual string header(const string& name) const
67 std::map<string, string>::const_iterator it = _headers.find(name);
68 if (it == _headers.end()) {
75 virtual void responseHeadersComplete()
79 virtual void responseComplete()
84 virtual void gotBodyData(const char* s, int n)
91 std::map<string, string> _headers;
94 int main(int argc, char* argv[])
98 string proxy, proxyAuth;
102 for (int a=0; a<argc;++a) {
103 if (argv[a][0] == '-') {
104 if (!strcmp(argv[a], "--user-agent")) {
105 cl.setUserAgent(argv[++a]);
106 } else if (!strcmp(argv[a], "--proxy")) {
108 } else if (!strcmp(argv[a], "--auth")) {
109 proxyAuth = argv[++a];
110 } else if (!strcmp(argv[a], "-f") || !strcmp(argv[a], "--file")) {
111 outFile = new SGFile(argv[++a]);
112 if (!outFile->open(SG_IO_OUT)) {
113 cerr << "failed to open output for writing:" << outFile->get_file_name() << endl;
116 } else if (!strcmp(argv[a], "--header")) {
117 headers.push_back(argv[++a]);
119 } else { // of argument starts with a hyphen
122 } // of arguments iteration
124 if (!proxy.empty()) {
125 int colonPos = proxy.find(':');
126 string proxyHost = proxy;
127 int proxyPort = 8800;
129 proxyHost = proxy.substr(0, colonPos);
130 proxyPort = strutils::to_int(proxy.substr(colonPos + 1));
133 cl.setProxy(proxyHost, proxyPort, proxyAuth);
137 signal(SIGPIPE, SIG_IGN);
141 outFile = new SGFile(fileno(stdout));
145 cerr << "no URL argument specificed" << endl;
149 ARequest* req = new ARequest(url);
150 BOOST_FOREACH(string h, headers) {
154 req->setFile(outFile);
157 while (!req->complete()) {
159 SGTimeStamp::sleepForMSec(100);
162 if (req->responseCode() != 200) {
163 cerr << "got response:" << req->responseCode() << endl;
164 cerr << "\treason:" << req->responseReason() << endl;