* updated pciutils/pci.ids (2022-01-21 -> 2022-01-28)
[t2-trunk.git] / source / Curl.hh
blobee8363b56992ae50d6772e9667818e877445c9b7
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
6 #include <string>
7 #include <fstream>
8 #include <sstream>
9 #include <memory>
11 class CurlException {};
12 class TimeoutException : public CurlException {};
13 class UnsupportedProtocolException : public CurlException {};
14 class MalformedUrlException : public CurlException {};
15 class ConnectErrorException : public CurlException {};
16 class AccessDeniedException : public CurlException {};
17 class DoesNotExistException : public CurlException {};
19 class CurlWrapper
21 public:
22 CurlWrapper () {
23 SetConnectTimeout(20);
24 SetMaxTime(-1);
25 char templ[] = "/tmp/curl_downloadXXXXXX";
26 close(mkstemp(templ)); // ReneR: Hack alert!
27 filename = templ;
30 void SetConnectTimeout (int value) {connect_timeout = value;}
31 void SetMaxTime (int value) {max_time = value;}
33 void Download (const std::string& url) {
34 GenParamString ();
35 params << " " << url;
36 ExecCurl ();
39 void Download (std::string url, unsigned int start, unsigned int end) {
40 GenParamString ();
41 params << " -r " << start << "-" << end << " " << url;
42 ExecCurl ();
45 std::string GetCommand () {return params.str();}
47 void SetFile (const std::string& name) {filename = name;}
49 std::auto_ptr<std::ifstream> OpenFile () {
50 std::auto_ptr<std::ifstream> r (new std::ifstream());
51 r->open(filename.c_str());
52 return r;
55 void RemoveFile () {unlink (filename.c_str());}
57 private:
59 void ExecCurl () {
60 std::string cmd = GetCommand();
61 size_t it = cmd.find("&");
62 while (it != std::string::npos) {
63 cmd.replace(it, it, "\\");
64 it = cmd.find("&", it + 2);
67 int ret = system(cmd.c_str());
68 if (ret != 0)
69 switch (ret) {
70 case 1:
71 throw UnsupportedProtocolException();
72 case 3:
73 case 4:
74 case 49:
75 throw MalformedUrlException();
76 case 5:
77 case 6:
78 case 7:
79 case 35:
80 throw ConnectErrorException();
81 case 9:
82 case 10:
83 case 46:
84 throw AccessDeniedException();
85 case 22:
86 throw DoesNotExistException();
87 case 28:
88 throw TimeoutException();
89 default:
90 throw CurlException();
94 void GenParamString () {
95 std::string empty("");
96 params.rdbuf()->str(empty);
97 params << "curl -A T2-updater --disable-epsv --location -f -o " << filename;
98 if (connect_timeout > 0)
99 params << " --connect-timeout " << connect_timeout;
100 if (max_time > 0)
101 params << " --max-time " << max_time;
104 int connect_timeout;
105 int max_time;
106 std::string filename;
108 std::stringstream params;