]> git.mxchange.org Git - simgear.git/blob - simgear/io/httpget.cxx
Fix a typo
[simgear.git] / simgear / io / httpget.cxx
1
2 #include <cstdio>
3 #include <cstring>
4
5 #include <unistd.h> // for STDOUT_FILENO
6 #include <iostream>
7 #include <boost/foreach.hpp>
8
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>
14
15 using namespace simgear;
16 using std::cout;
17 using std::endl;
18 using std::cerr;
19 using std::string;
20
21 class ARequest : public HTTP::Request
22 {
23 public:
24     ARequest(string& url) :
25         Request(url),
26         _complete(false),
27         _file(NULL)
28     {
29         
30     }
31     
32     void setFile(SGFile* f)
33     {
34         _file = f;
35     }
36     
37     bool complete() const
38         { return _complete; }
39         
40     void addHeader(const string& h)
41     {
42         int colonPos = h.find(':');
43         if (colonPos < 0) {
44             cerr << "malformed header: " << h << endl;
45             return;
46         }
47         
48         string key = h.substr(0, colonPos);
49         _headers[key] = h.substr(colonPos + 1);
50     }
51     
52     virtual string_list requestHeaders() const
53     {
54         string_list r;
55         std::map<string, string>::const_iterator it;
56         for (it = _headers.begin(); it != _headers.end(); ++it) {
57             r.push_back(it->first);
58         }
59         
60         return r;
61     }
62     
63     virtual string header(const string& name) const
64     {
65         std::map<string, string>::const_iterator it = _headers.find(name);
66         if (it == _headers.end()) {
67             return string();
68         }
69         
70         return it->second;
71     }
72 protected:
73     virtual void responseHeadersComplete()
74     {
75     }
76
77     virtual void responseComplete()
78     {
79         _complete = true;
80     }  
81
82     virtual void gotBodyData(const char* s, int n)
83     {
84         _file->write(s, n);
85     }
86 private:    
87     bool _complete;
88     SGFile* _file;
89     std::map<string, string> _headers;
90 };
91
92 int main(int argc, char* argv[])
93 {
94     HTTP::Client cl;
95     SGFile* outFile;
96     string proxy, proxyAuth;
97     string_list headers;
98     string url;
99     
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")) {
105                 proxy = argv[++a];
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;
112                     return EXIT_FAILURE;
113                 }
114             } else if (!strcmp(argv[a], "--header")) {
115                 headers.push_back(argv[++a]);
116             }
117         } else { // of argument starts with a hyphen
118             url = argv[a];
119         }
120     } // of arguments iteration
121
122     if (!proxy.empty()) {
123         int colonPos = proxy.find(':');
124         string proxyHost = proxy;
125         int proxyPort = 8800;
126         if (colonPos >= 0) {
127             proxyHost = proxy.substr(0, colonPos);
128             proxyPort = strutils::to_int(proxy.substr(colonPos + 1));
129             cout << proxyHost << " " << proxyPort << endl;
130         }
131         
132         cl.setProxy(proxyHost, proxyPort, proxyAuth);
133     }
134
135     if (!outFile) {
136         outFile = new SGFile(STDOUT_FILENO);
137     }
138
139     if (url.empty()) {
140         cerr << "no URL argument specificed" << endl;
141         return EXIT_FAILURE;
142     }
143
144     ARequest* req = new ARequest(url);
145     BOOST_FOREACH(string h, headers) {
146         req->addHeader(h);
147     }
148     
149     req->setFile(outFile);
150     cl.makeRequest(req);
151     
152     while (!req->complete()) {
153         NetChannel::poll(100);
154     }
155     
156     if (req->responseCode() != 200) {
157         cerr << "got response:" << req->responseCode() << endl;
158         cerr << "\treason:" << req->responseReason() << endl;
159         return EXIT_FAILURE;
160     }
161     
162     return EXIT_SUCCESS;
163 }