5 #include <unistd.h> // for STDOUT_FILENO
7 #include <boost/foreach.hpp>
9 #include <simgear/io/sg_file.hxx>
10 #include <simgear/io/HTTPClient.hxx>
11 #include <simgear/io/HTTPRequest.hxx>
12 #include <simgear/io/sg_netChannel.hxx>
14 using namespace simgear;
20 class ARequest : public HTTP::Request
23 ARequest(string& url) :
31 void setFile(SGFile* f)
39 void addHeader(const string& h)
41 int colonPos = h.find(':');
43 cerr << "malformed header: " << h << endl;
47 string key = h.substr(0, colonPos);
48 _headers[key] = h.substr(colonPos + 1);
51 virtual string_list requestHeaders() const
54 std::map<string, string>::const_iterator it;
55 for (it = _headers.begin(); it != _headers.end(); ++it) {
56 r.push_back(it->first);
62 virtual string header(const string& name) const
64 std::map<string, string>::const_iterator it = _headers.find(name);
65 if (it == _headers.end()) {
72 virtual void responseHeadersComplete()
76 virtual void responseComplete()
81 virtual void gotBodyData(const char* s, int n)
88 std::map<string, string> _headers;
91 int main(int argc, char* argv[])
95 string proxy, proxyAuth;
99 for (int a=0; a<argc;++a) {
100 if (argv[a][0] == '-') {
101 if (!strcmp(argv[a], "--user-agent")) {
102 cl.setUserAgent(argv[++a]);
103 } else if (!strcmp(argv[a], "--proxy")) {
105 } else if (!strcmp(argv[a], "--auth")) {
106 proxyAuth = argv[++a];
107 } else if (!strcmp(argv[a], "-f") || !strcmp(argv[a], "--file")) {
108 outFile = new SGFile(argv[++a]);
109 if (!outFile->open(SG_IO_OUT)) {
110 cerr << "failed to open output for writing:" << outFile->get_file_name() << endl;
113 } else if (!strcmp(argv[a], "--header")) {
114 headers.push_back(argv[++a]);
116 } else { // of argument starts with a hyphen
119 } // of arguments iteration
121 if (!proxy.empty()) {
122 cl.setProxy(proxy, proxyAuth);
126 outFile = new SGFile(STDOUT_FILENO);
130 cerr << "no URL argument specificed" << endl;
134 ARequest* req = new ARequest(url);
135 BOOST_FOREACH(string h, headers) {
139 req->setFile(outFile);
142 while (!req->complete()) {
143 NetChannel::poll(100);
146 if (req->responseCode() != 200) {
147 cerr << "got response:" << req->responseCode() << endl;
148 cerr << "\treason:" << req->responseReason() << endl;