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>
13 #include <simgear/misc/strutils.hxx>
15 using namespace simgear;
21 class ARequest : public HTTP::Request
24 ARequest(string& url) :
32 void setFile(SGFile* f)
40 void addHeader(const string& h)
42 int colonPos = h.find(':');
44 cerr << "malformed header: " << h << endl;
48 string key = h.substr(0, colonPos);
49 _headers[key] = h.substr(colonPos + 1);
52 virtual string_list requestHeaders() const
55 std::map<string, string>::const_iterator it;
56 for (it = _headers.begin(); it != _headers.end(); ++it) {
57 r.push_back(it->first);
63 virtual string header(const string& name) const
65 std::map<string, string>::const_iterator it = _headers.find(name);
66 if (it == _headers.end()) {
73 virtual void responseHeadersComplete()
77 virtual void responseComplete()
82 virtual void gotBodyData(const char* s, int n)
89 std::map<string, string> _headers;
92 int main(int argc, char* argv[])
96 string proxy, proxyAuth;
100 for (int a=0; a<argc;++a) {
101 if (argv[a][0] == '-') {
102 if (!strcmp(argv[a], "--user-agent")) {
103 cl.setUserAgent(argv[++a]);
104 } else if (!strcmp(argv[a], "--proxy")) {
106 } else if (!strcmp(argv[a], "--auth")) {
107 proxyAuth = argv[++a];
108 } else if (!strcmp(argv[a], "-f") || !strcmp(argv[a], "--file")) {
109 outFile = new SGFile(argv[++a]);
110 if (!outFile->open(SG_IO_OUT)) {
111 cerr << "failed to open output for writing:" << outFile->get_file_name() << endl;
114 } else if (!strcmp(argv[a], "--header")) {
115 headers.push_back(argv[++a]);
117 } else { // of argument starts with a hyphen
120 } // of arguments iteration
122 if (!proxy.empty()) {
123 int colonPos = proxy.find(':');
124 string proxyHost = proxy;
125 int proxyPort = 8800;
127 proxyHost = proxy.substr(0, colonPos);
128 proxyPort = strutils::to_int(proxy.substr(colonPos + 1));
129 cout << proxyHost << " " << proxyPort << endl;
132 cl.setProxy(proxyHost, proxyPort, proxyAuth);
136 outFile = new SGFile(STDOUT_FILENO);
140 cerr << "no URL argument specificed" << endl;
144 ARequest* req = new ARequest(url);
145 BOOST_FOREACH(string h, headers) {
149 req->setFile(outFile);
152 while (!req->complete()) {
153 NetChannel::poll(100);
156 if (req->responseCode() != 200) {
157 cerr << "got response:" << req->responseCode() << endl;
158 cerr << "\treason:" << req->responseReason() << endl;