]> git.mxchange.org Git - simgear.git/blob - simgear/io/httpget.cxx
Drop explicit SDK setting on Mac
[simgear.git] / simgear / io / httpget.cxx
1
2 #include <cstdio>
3 #include <cstring>
4 #include <signal.h>
5
6 #include <iostream>
7 #include <boost/foreach.hpp>
8
9
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>
16
17 using namespace simgear;
18 using std::cout;
19 using std::endl;
20 using std::cerr;
21 using std::string;
22
23 class ARequest : public HTTP::Request
24 {
25 public:
26     ARequest(string& url) :
27         Request(url),
28         _complete(false),
29         _file(NULL)
30     {
31         
32     }
33     
34     void setFile(SGFile* f)
35     {
36         _file = f;
37     }
38     
39     bool complete() const
40         { return _complete; }
41         
42     void addHeader(const string& h)
43     {
44         int colonPos = h.find(':');
45         if (colonPos < 0) {
46             cerr << "malformed header: " << h << endl;
47             return;
48         }
49         
50         string key = h.substr(0, colonPos);
51         requestHeader(key) = h.substr(colonPos + 1);
52     }
53     
54 protected:
55     virtual void onDone()
56     {
57         _complete = true;
58     }  
59
60     virtual void gotBodyData(const char* s, int n)
61     {
62         _file->write(s, n);
63     }
64 private:    
65     bool _complete;
66     SGFile* _file;
67 };
68
69 int main(int argc, char* argv[])
70 {
71     HTTP::Client cl;
72     SGFile* outFile = 0;
73     string proxy, proxyAuth;
74     string_list headers;
75     string url;
76     
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")) {
82                 proxy = argv[++a];
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;
89                     return EXIT_FAILURE;
90                 }
91             } else if (!strcmp(argv[a], "--header")) {
92                 headers.push_back(argv[++a]);
93             }
94         } else { // of argument starts with a hyphen
95             url = argv[a];
96         }
97     } // of arguments iteration
98
99     if (!proxy.empty()) {
100         int colonPos = proxy.find(':');
101         string proxyHost = proxy;
102         int proxyPort = 8800;
103         if (colonPos >= 0) {
104             proxyHost = proxy.substr(0, colonPos);
105             proxyPort = strutils::to_int(proxy.substr(colonPos + 1));
106         }
107         
108         cl.setProxy(proxyHost, proxyPort, proxyAuth);
109     }
110
111 #ifndef WIN32
112     signal(SIGPIPE, SIG_IGN);
113 #endif
114
115     if (!outFile) {
116         outFile = new SGFile(fileno(stdout));
117     }
118
119     if (url.empty()) {
120         cerr << "no URL argument specificed" << endl;
121         return EXIT_FAILURE;
122     }
123
124     ARequest* req = new ARequest(url);
125     BOOST_FOREACH(string h, headers) {
126         req->addHeader(h);
127     }
128     
129     req->setFile(outFile);
130     cl.makeRequest(req);
131     
132     while (!req->complete()) {
133         cl.update();
134         SGTimeStamp::sleepForMSec(100);
135     }
136         
137     if (req->responseCode() != 200) {
138         cerr << "got response:" << req->responseCode() << endl;
139         cerr << "\treason:" << req->responseReason() << endl;
140         return EXIT_FAILURE;
141     }
142     
143     return EXIT_SUCCESS;
144 }