Http.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #include "Http.hpp"
  2. #include <cstdlib>
  3. #include <functional>
  4. #include <thread>
  5. #include <deque>
  6. #include <sstream>
  7. #include <exception>
  8. #include <boost/filesystem/fstream.hpp>
  9. #include <boost/filesystem/path.hpp>
  10. #include <boost/filesystem.hpp>
  11. #include <boost/format.hpp>
  12. #include <boost/log/trivial.hpp>
  13. #include <curl/curl.h>
  14. #ifdef OPENSSL_CERT_OVERRIDE
  15. #include <openssl/x509.h>
  16. #endif
  17. #include "libslic3r/libslic3r.h"
  18. #include "libslic3r/Utils.hpp"
  19. namespace fs = boost::filesystem;
  20. namespace Slic3r {
  21. // Private
  22. struct CurlGlobalInit
  23. {
  24. static std::unique_ptr<CurlGlobalInit> instance;
  25. CurlGlobalInit()
  26. {
  27. #ifdef OPENSSL_CERT_OVERRIDE // defined if SLIC3R_STATIC=ON
  28. // Look for a set of distro specific directories. Don't change the
  29. // order: https://bugzilla.redhat.com/show_bug.cgi?id=1053882
  30. static const char * CA_BUNDLES[] = {
  31. "/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6
  32. "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
  33. "/usr/share/ssl/certs/ca-bundle.crt",
  34. "/usr/local/share/certs/ca-root-nss.crt", // FreeBSD
  35. "/etc/ssl/cert.pem",
  36. "/etc/ssl/ca-bundle.pem" // OpenSUSE Tumbleweed
  37. };
  38. namespace fs = boost::filesystem;
  39. // Env var name for the OpenSSL CA bundle (SSL_CERT_FILE nomally)
  40. const char *const SSL_CA_FILE = X509_get_default_cert_file_env();
  41. const char * ssl_cafile = ::getenv(SSL_CA_FILE);
  42. if (!ssl_cafile)
  43. ssl_cafile = X509_get_default_cert_file();
  44. int replace = true;
  45. if (!ssl_cafile || !fs::exists(fs::path(ssl_cafile)))
  46. for (const char * bundle : CA_BUNDLES) {
  47. if (fs::exists(fs::path(bundle))) {
  48. ::setenv(SSL_CA_FILE, bundle, replace);
  49. break;
  50. }
  51. }
  52. BOOST_LOG_TRIVIAL(info)
  53. << "Detected OpenSSL root CA store: " << ::getenv(SSL_CA_FILE);
  54. #endif
  55. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  56. }
  57. ~CurlGlobalInit() { ::curl_global_cleanup(); }
  58. };
  59. std::unique_ptr<CurlGlobalInit> CurlGlobalInit::instance;
  60. struct Http::priv
  61. {
  62. enum {
  63. DEFAULT_TIMEOUT_CONNECT = 10,
  64. DEFAULT_SIZE_LIMIT = 5 * 1024 * 1024,
  65. };
  66. ::CURL *curl;
  67. ::curl_httppost *form;
  68. ::curl_httppost *form_end;
  69. ::curl_slist *headerlist;
  70. // Used for reading the body
  71. std::string buffer;
  72. // Used for storing file streams added as multipart form parts
  73. // Using a deque here because unlike vector it doesn't ivalidate pointers on insertion
  74. std::deque<fs::ifstream> form_files;
  75. std::string postfields;
  76. std::string error_buffer; // Used for CURLOPT_ERRORBUFFER
  77. size_t limit;
  78. bool cancel;
  79. std::thread io_thread;
  80. Http::CompleteFn completefn;
  81. Http::ErrorFn errorfn;
  82. Http::ProgressFn progressfn;
  83. priv(const std::string &url);
  84. ~priv();
  85. static bool ca_file_supported(::CURL *curl);
  86. static size_t writecb(void *data, size_t size, size_t nmemb, void *userp);
  87. static int xfercb(void *userp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
  88. static int xfercb_legacy(void *userp, double dltotal, double dlnow, double ultotal, double ulnow);
  89. static size_t form_file_read_cb(char *buffer, size_t size, size_t nitems, void *userp);
  90. void set_timeout_connect(long timeout);
  91. void form_add_file(const char *name, const fs::path &path, const char* filename);
  92. void set_post_body(const fs::path &path);
  93. std::string curl_error(CURLcode curlcode);
  94. std::string body_size_error();
  95. void http_perform();
  96. };
  97. Http::priv::priv(const std::string &url)
  98. : curl(::curl_easy_init())
  99. , form(nullptr)
  100. , form_end(nullptr)
  101. , headerlist(nullptr)
  102. , error_buffer(CURL_ERROR_SIZE + 1, '\0')
  103. , limit(0)
  104. , cancel(false)
  105. {
  106. if (!CurlGlobalInit::instance)
  107. CurlGlobalInit::instance = std::make_unique<CurlGlobalInit>();
  108. if (curl == nullptr) {
  109. throw std::runtime_error(std::string("Could not construct Curl object"));
  110. }
  111. set_timeout_connect(DEFAULT_TIMEOUT_CONNECT);
  112. ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // curl makes a copy internally
  113. ::curl_easy_setopt(curl, CURLOPT_USERAGENT, SLIC3R_APP_NAME "/" SLIC3R_VERSION);
  114. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer.front());
  115. }
  116. Http::priv::~priv()
  117. {
  118. ::curl_easy_cleanup(curl);
  119. ::curl_formfree(form);
  120. ::curl_slist_free_all(headerlist);
  121. }
  122. bool Http::priv::ca_file_supported(::CURL *curl)
  123. {
  124. #ifdef _WIN32
  125. bool res = false;
  126. #else
  127. bool res = true;
  128. #endif
  129. if (curl == nullptr) { return res; }
  130. #if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 48
  131. ::curl_tlssessioninfo *tls;
  132. if (::curl_easy_getinfo(curl, CURLINFO_TLS_SSL_PTR, &tls) == CURLE_OK) {
  133. if (tls->backend == CURLSSLBACKEND_SCHANNEL || tls->backend == CURLSSLBACKEND_DARWINSSL) {
  134. // With Windows and OS X native SSL support, cert files cannot be set
  135. res = false;
  136. }
  137. }
  138. #endif
  139. return res;
  140. }
  141. size_t Http::priv::writecb(void *data, size_t size, size_t nmemb, void *userp)
  142. {
  143. auto self = static_cast<priv*>(userp);
  144. const char *cdata = static_cast<char*>(data);
  145. const size_t realsize = size * nmemb;
  146. const size_t limit = self->limit > 0 ? self->limit : DEFAULT_SIZE_LIMIT;
  147. if (self->buffer.size() + realsize > limit) {
  148. // This makes curl_easy_perform return CURLE_WRITE_ERROR
  149. return 0;
  150. }
  151. self->buffer.append(cdata, realsize);
  152. return realsize;
  153. }
  154. int Http::priv::xfercb(void *userp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
  155. {
  156. auto self = static_cast<priv*>(userp);
  157. bool cb_cancel = false;
  158. if (self->progressfn) {
  159. Progress progress(dltotal, dlnow, ultotal, ulnow);
  160. self->progressfn(progress, cb_cancel);
  161. }
  162. if (cb_cancel) { self->cancel = true; }
  163. return self->cancel;
  164. }
  165. int Http::priv::xfercb_legacy(void *userp, double dltotal, double dlnow, double ultotal, double ulnow)
  166. {
  167. return xfercb(userp, dltotal, dlnow, ultotal, ulnow);
  168. }
  169. size_t Http::priv::form_file_read_cb(char *buffer, size_t size, size_t nitems, void *userp)
  170. {
  171. auto stream = reinterpret_cast<fs::ifstream*>(userp);
  172. try {
  173. stream->read(buffer, size * nitems);
  174. } catch (const std::exception &) {
  175. return CURL_READFUNC_ABORT;
  176. }
  177. return stream->gcount();
  178. }
  179. void Http::priv::set_timeout_connect(long timeout)
  180. {
  181. ::curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
  182. }
  183. void Http::priv::form_add_file(const char *name, const fs::path &path, const char* filename)
  184. {
  185. // We can't use CURLFORM_FILECONTENT, because curl doesn't support Unicode filenames on Windows
  186. // and so we use CURLFORM_STREAM with boost ifstream to read the file.
  187. if (filename == nullptr) {
  188. filename = path.string().c_str();
  189. }
  190. form_files.emplace_back(path, std::ios::in | std::ios::binary);
  191. auto &stream = form_files.back();
  192. stream.seekg(0, std::ios::end);
  193. size_t size = stream.tellg();
  194. stream.seekg(0);
  195. if (filename != nullptr) {
  196. ::curl_formadd(&form, &form_end,
  197. CURLFORM_COPYNAME, name,
  198. CURLFORM_FILENAME, filename,
  199. CURLFORM_CONTENTTYPE, "application/octet-stream",
  200. CURLFORM_STREAM, static_cast<void*>(&stream),
  201. CURLFORM_CONTENTSLENGTH, static_cast<long>(size),
  202. CURLFORM_END
  203. );
  204. }
  205. }
  206. void Http::priv::set_post_body(const fs::path &path)
  207. {
  208. std::ifstream file(path.string());
  209. std::string file_content { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() };
  210. postfields = file_content;
  211. }
  212. std::string Http::priv::curl_error(CURLcode curlcode)
  213. {
  214. return (boost::format("%1%:\n%2%\n[Error %3%]")
  215. % ::curl_easy_strerror(curlcode)
  216. % error_buffer.c_str()
  217. % curlcode
  218. ).str();
  219. }
  220. std::string Http::priv::body_size_error()
  221. {
  222. return (boost::format("HTTP body data size exceeded limit (%1% bytes)") % limit).str();
  223. }
  224. void Http::priv::http_perform()
  225. {
  226. ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  227. ::curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
  228. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
  229. ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<void*>(this));
  230. ::curl_easy_setopt(curl, CURLOPT_READFUNCTION, form_file_read_cb);
  231. ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  232. #if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 32
  233. ::curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xfercb);
  234. ::curl_easy_setopt(curl, CURLOPT_XFERINFODATA, static_cast<void*>(this));
  235. #ifndef _WIN32
  236. (void)xfercb_legacy; // prevent unused function warning
  237. #endif
  238. #else
  239. ::curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, xfercb);
  240. ::curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, static_cast<void*>(this));
  241. #endif
  242. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, get_logging_level() >= 5);
  243. if (headerlist != nullptr) {
  244. ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  245. }
  246. if (form != nullptr) {
  247. ::curl_easy_setopt(curl, CURLOPT_HTTPPOST, form);
  248. }
  249. if (!postfields.empty()) {
  250. ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfields.c_str());
  251. ::curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, postfields.size());
  252. }
  253. CURLcode res = ::curl_easy_perform(curl);
  254. if (res != CURLE_OK) {
  255. if (res == CURLE_ABORTED_BY_CALLBACK) {
  256. if (cancel) {
  257. // The abort comes from the request being cancelled programatically
  258. Progress dummyprogress(0, 0, 0, 0);
  259. bool cancel = true;
  260. if (progressfn) { progressfn(dummyprogress, cancel); }
  261. } else {
  262. // The abort comes from the CURLOPT_READFUNCTION callback, which means reading file failed
  263. if (errorfn) { errorfn(std::move(buffer), "Error reading file for file upload", 0); }
  264. }
  265. }
  266. else if (res == CURLE_WRITE_ERROR) {
  267. if (errorfn) { errorfn(std::move(buffer), body_size_error(), 0); }
  268. } else {
  269. if (errorfn) { errorfn(std::move(buffer), curl_error(res), 0); }
  270. };
  271. } else {
  272. long http_status = 0;
  273. ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status);
  274. if (http_status >= 400) {
  275. if (errorfn) { errorfn(std::move(buffer), std::string(), http_status); }
  276. } else {
  277. if (completefn) { completefn(std::move(buffer), http_status); }
  278. }
  279. }
  280. }
  281. Http::Http(const std::string &url) : p(new priv(url)) {}
  282. // Public
  283. Http::Http(Http &&other) : p(std::move(other.p)) {}
  284. Http::~Http()
  285. {
  286. if (p && p->io_thread.joinable()) {
  287. p->io_thread.detach();
  288. }
  289. }
  290. Http& Http::timeout_connect(long timeout)
  291. {
  292. if (timeout < 1) { timeout = priv::DEFAULT_TIMEOUT_CONNECT; }
  293. if (p) { p->set_timeout_connect(timeout); }
  294. return *this;
  295. }
  296. Http& Http::size_limit(size_t sizeLimit)
  297. {
  298. if (p) { p->limit = sizeLimit; }
  299. return *this;
  300. }
  301. Http& Http::header(std::string name, const std::string &value)
  302. {
  303. if (!p) { return * this; }
  304. if (name.size() > 0) {
  305. name.append(": ").append(value);
  306. } else {
  307. name.push_back(':');
  308. }
  309. p->headerlist = curl_slist_append(p->headerlist, name.c_str());
  310. return *this;
  311. }
  312. Http& Http::remove_header(std::string name)
  313. {
  314. if (p) {
  315. name.push_back(':');
  316. p->headerlist = curl_slist_append(p->headerlist, name.c_str());
  317. }
  318. return *this;
  319. }
  320. Http& Http::ca_file(const std::string &name)
  321. {
  322. if (p && priv::ca_file_supported(p->curl)) {
  323. ::curl_easy_setopt(p->curl, CURLOPT_CAINFO, name.c_str());
  324. }
  325. return *this;
  326. }
  327. Http& Http::form_add(const std::string &name, const std::string &contents)
  328. {
  329. if (p) {
  330. ::curl_formadd(&p->form, &p->form_end,
  331. CURLFORM_COPYNAME, name.c_str(),
  332. CURLFORM_COPYCONTENTS, contents.c_str(),
  333. CURLFORM_END
  334. );
  335. }
  336. return *this;
  337. }
  338. Http& Http::form_add_file(const std::string &name, const fs::path &path)
  339. {
  340. if (p) { p->form_add_file(name.c_str(), path.c_str(), nullptr); }
  341. return *this;
  342. }
  343. Http& Http::form_add_file(const std::string &name, const fs::path &path, const std::string &filename)
  344. {
  345. if (p) { p->form_add_file(name.c_str(), path.c_str(), filename.c_str()); }
  346. return *this;
  347. }
  348. Http& Http::set_post_body(const fs::path &path)
  349. {
  350. if (p) { p->set_post_body(path);}
  351. return *this;
  352. }
  353. Http& Http::on_complete(CompleteFn fn)
  354. {
  355. if (p) { p->completefn = std::move(fn); }
  356. return *this;
  357. }
  358. Http& Http::on_error(ErrorFn fn)
  359. {
  360. if (p) { p->errorfn = std::move(fn); }
  361. return *this;
  362. }
  363. Http& Http::on_progress(ProgressFn fn)
  364. {
  365. if (p) { p->progressfn = std::move(fn); }
  366. return *this;
  367. }
  368. Http::Ptr Http::perform()
  369. {
  370. auto self = std::make_shared<Http>(std::move(*this));
  371. if (self->p) {
  372. auto io_thread = std::thread([self](){
  373. self->p->http_perform();
  374. });
  375. self->p->io_thread = std::move(io_thread);
  376. }
  377. return self;
  378. }
  379. void Http::perform_sync()
  380. {
  381. if (p) { p->http_perform(); }
  382. }
  383. void Http::cancel()
  384. {
  385. if (p) { p->cancel = true; }
  386. }
  387. Http Http::get(std::string url)
  388. {
  389. return std::move(Http{std::move(url)});
  390. }
  391. Http Http::post(std::string url)
  392. {
  393. Http http{std::move(url)};
  394. curl_easy_setopt(http.p->curl, CURLOPT_POST, 1L);
  395. return http;
  396. }
  397. bool Http::ca_file_supported()
  398. {
  399. ::CURL *curl = ::curl_easy_init();
  400. bool res = priv::ca_file_supported(curl);
  401. if (curl != nullptr) { ::curl_easy_cleanup(curl); }
  402. return res;
  403. }
  404. std::string Http::url_encode(const std::string &str)
  405. {
  406. ::CURL *curl = ::curl_easy_init();
  407. if (curl == nullptr) {
  408. return str;
  409. }
  410. char *ce = ::curl_easy_escape(curl, str.c_str(), str.length());
  411. std::string encoded = std::string(ce);
  412. ::curl_free(ce);
  413. ::curl_easy_cleanup(curl);
  414. return encoded;
  415. }
  416. std::ostream& operator<<(std::ostream &os, const Http::Progress &progress)
  417. {
  418. os << "Http::Progress("
  419. << "dltotal = " << progress.dltotal
  420. << ", dlnow = " << progress.dlnow
  421. << ", ultotal = " << progress.ultotal
  422. << ", ulnow = " << progress.ulnow
  423. << ")";
  424. return os;
  425. }
  426. }