HttpClient.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #include <fstream>
  2. #include <sstream>
  3. #include <boost/algorithm/string.hpp>
  4. #include <curl/curl.h>
  5. #include <openssl/crypto.h>
  6. #include "HttpClientHelpers.h"
  7. #include "HttpClient.h"
  8. namespace PresenceDetection {
  9. namespace Util {
  10. HttpClient::HttpClient()
  11. {
  12. curl_global_init(CURL_GLOBAL_ALL);
  13. m_userAgents.push_back("Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00");
  14. InitializeLocks();
  15. }
  16. HttpClient::~HttpClient()
  17. {
  18. FreeLocks();
  19. curl_global_cleanup();
  20. }
  21. std::string HttpClient::GetUrlContents(const std::string& url) const
  22. {
  23. std::string buffer;
  24. CURL* curl = curl_easy_init();
  25. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  26. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  27. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  28. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  29. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  30. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  31. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  32. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  33. int code = 0;
  34. curl_easy_perform(curl);
  35. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  36. curl_easy_cleanup(curl);
  37. if (code != 200)
  38. {
  39. std::stringstream error;
  40. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  41. error << "Data: " << buffer;
  42. throw std::runtime_error(error.str());
  43. }
  44. return buffer;
  45. }
  46. void HttpClient::GetUrlSilent(const std::string& url) const
  47. {
  48. CURL* curl = curl_easy_init();
  49. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  50. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  51. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  52. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  53. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  54. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  55. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  56. curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
  57. int code = 0;
  58. curl_easy_perform(curl);
  59. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  60. curl_easy_cleanup(curl);
  61. if (code != 200)
  62. {
  63. std::stringstream error;
  64. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  65. throw std::runtime_error(error.str());
  66. }
  67. }
  68. std::string HttpClient::GetUrlContents(const std::string& url, const std::string& cookieFile) const
  69. {
  70. std::string buffer;
  71. CURL* curl = curl_easy_init();
  72. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  73. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  74. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  75. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  76. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  77. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
  78. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
  79. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  80. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  81. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  82. int code = 0;
  83. curl_easy_perform(curl);
  84. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  85. curl_easy_cleanup(curl);
  86. if (code != 200)
  87. {
  88. std::stringstream error;
  89. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  90. error << "Data: " << buffer;
  91. throw std::runtime_error(error.str());
  92. }
  93. return buffer;
  94. }
  95. void HttpClient::GetUrlSilent(const std::string& url, const std::string& cookieFile) const
  96. {
  97. CURL* curl = curl_easy_init();
  98. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  99. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  100. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  101. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  102. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  103. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
  104. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
  105. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  106. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  107. curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
  108. int code = 0;
  109. curl_easy_perform(curl);
  110. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  111. curl_easy_cleanup(curl);
  112. if (code != 200)
  113. {
  114. std::stringstream error;
  115. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  116. throw std::runtime_error(error.str());
  117. }
  118. }
  119. std::string HttpClient::GetUrlPostContents(const std::string& url, const std::string& postData, const std::string& contentType) const
  120. {
  121. std::string buffer;
  122. CURL* curl = curl_easy_init();
  123. std::stringstream contentTypeString;
  124. contentTypeString << "Content-Type: " << contentType;
  125. struct curl_slist *list = NULL;
  126. list = curl_slist_append(list, contentTypeString.str().c_str());
  127. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  128. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  129. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  130. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  131. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  132. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
  133. curl_easy_setopt(curl, CURLOPT_POST, 1);
  134. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
  135. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
  136. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  137. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  138. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  139. int code = 0;
  140. curl_easy_perform(curl);
  141. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  142. curl_easy_cleanup(curl);
  143. if (code != 200)
  144. {
  145. std::stringstream error;
  146. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  147. error << "Data: " << buffer;
  148. throw std::runtime_error(error.str());
  149. }
  150. return buffer;
  151. }
  152. void HttpClient::GetUrlPostSilent(const std::string& url, const std::string& postData, const std::string& contentType) const
  153. {
  154. CURL* curl = curl_easy_init();
  155. std::stringstream contentTypeString;
  156. contentTypeString << "Content-Type: " << contentType;
  157. struct curl_slist *list = NULL;
  158. list = curl_slist_append(list, contentTypeString.str().c_str());
  159. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  160. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  161. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  162. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  163. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  164. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
  165. curl_easy_setopt(curl, CURLOPT_POST, 1);
  166. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
  167. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
  168. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  169. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  170. curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
  171. int code = 0;
  172. curl_easy_perform(curl);
  173. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  174. curl_easy_cleanup(curl);
  175. if (code != 200)
  176. {
  177. std::stringstream error;
  178. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  179. throw std::runtime_error(error.str());
  180. }
  181. }
  182. std::string HttpClient::GetUrlPostContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const
  183. {
  184. std::string buffer;
  185. CURL* curl = curl_easy_init();
  186. std::stringstream contentTypeString;
  187. contentTypeString << "Content-Type: " << contentType;
  188. struct curl_slist *list = NULL;
  189. list = curl_slist_append(list, contentTypeString.str().c_str());
  190. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  191. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  192. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  193. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  194. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  195. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
  196. curl_easy_setopt(curl, CURLOPT_POST, 1);
  197. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
  198. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
  199. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
  200. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
  201. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  202. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  203. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  204. int code = 0;
  205. curl_easy_perform(curl);
  206. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  207. curl_easy_cleanup(curl);
  208. if (code != 200)
  209. {
  210. std::stringstream error;
  211. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  212. error << "Data: " << buffer;
  213. throw std::runtime_error(error.str());
  214. }
  215. return buffer;
  216. }
  217. void HttpClient::GetUrlPostSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const
  218. {
  219. CURL* curl = curl_easy_init();
  220. std::stringstream contentTypeString;
  221. contentTypeString << "Content-Type: " << contentType;
  222. struct curl_slist *list = NULL;
  223. list = curl_slist_append(list, contentTypeString.str().c_str());
  224. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  225. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  226. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  227. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  228. curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
  229. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
  230. curl_easy_setopt(curl, CURLOPT_POST, 1);
  231. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
  232. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
  233. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
  234. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
  235. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  236. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  237. curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
  238. int code = 0;
  239. curl_easy_perform(curl);
  240. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  241. curl_easy_cleanup(curl);
  242. if (code != 200)
  243. {
  244. std::stringstream error;
  245. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  246. throw std::runtime_error(error.str());
  247. }
  248. }
  249. std::string HttpClient::GetUrlPostAttachmentContents(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
  250. {
  251. std::string buffer;
  252. std::string contents;
  253. std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
  254. if (fileStream)
  255. {
  256. fileStream.seekg(0, std::ios::end);
  257. contents.resize(fileStream.tellg());
  258. fileStream.seekg(0, std::ios::beg);
  259. fileStream.read(&contents[0], contents.size());
  260. fileStream.close();
  261. }
  262. CURL* curl = curl_easy_init();
  263. CURLcode result;
  264. struct curl_httppost *formpost = nullptr;
  265. struct curl_httppost *lastptr = nullptr;
  266. struct curl_slist *headerlist = nullptr;
  267. static const char headerBuffer[] = "Expect:";
  268. curl_global_init(CURL_GLOBAL_ALL);
  269. curl_formadd(&formpost, &lastptr,
  270. CURLFORM_COPYNAME, "cache-control:",
  271. CURLFORM_COPYCONTENTS, "no-cache",
  272. CURLFORM_END);
  273. curl_formadd(&formpost, &lastptr,
  274. CURLFORM_COPYNAME, "content-type:",
  275. CURLFORM_COPYCONTENTS, "multipart/form-data",
  276. CURLFORM_END);
  277. std::vector<std::string> postTokens;
  278. boost::split(postTokens, postData, boost::is_any_of("&"));
  279. for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
  280. {
  281. std::vector<std::string> tokens;
  282. boost::split(tokens, *it, boost::is_any_of("="));
  283. curl_formadd(&formpost, &lastptr,
  284. CURLFORM_COPYNAME, tokens[0].c_str(),
  285. CURLFORM_COPYCONTENTS, tokens[1].c_str(),
  286. CURLFORM_END);
  287. }
  288. curl_formadd(&formpost, &lastptr,
  289. CURLFORM_COPYNAME, fileFieldname.c_str(),
  290. CURLFORM_BUFFER, "data",
  291. CURLFORM_BUFFERPTR, contents.data(),
  292. CURLFORM_BUFFERLENGTH, contents.size(),
  293. CURLFORM_END);
  294. headerlist = curl_slist_append(headerlist, headerBuffer);
  295. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  296. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  297. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  298. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  299. int code = 0;
  300. curl_easy_perform(curl);
  301. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  302. curl_easy_cleanup(curl);
  303. curl_formfree(formpost);
  304. curl_slist_free_all(headerlist);
  305. if (code != 200)
  306. {
  307. std::stringstream error;
  308. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  309. error << "Data: " << buffer;
  310. throw std::runtime_error(error.str());
  311. }
  312. return buffer;
  313. }
  314. void HttpClient::GetUrlPostAttachmentSilent(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
  315. {
  316. std::string contents;
  317. std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
  318. if (fileStream)
  319. {
  320. fileStream.seekg(0, std::ios::end);
  321. contents.resize(fileStream.tellg());
  322. fileStream.seekg(0, std::ios::beg);
  323. fileStream.read(&contents[0], contents.size());
  324. fileStream.close();
  325. }
  326. CURL* curl = curl_easy_init();
  327. CURLcode result;
  328. struct curl_httppost *formpost = nullptr;
  329. struct curl_httppost *lastptr = nullptr;
  330. struct curl_slist *headerlist = nullptr;
  331. static const char headerBuffer[] = "Expect:";
  332. curl_global_init(CURL_GLOBAL_ALL);
  333. curl_formadd(&formpost, &lastptr,
  334. CURLFORM_COPYNAME, "cache-control:",
  335. CURLFORM_COPYCONTENTS, "no-cache",
  336. CURLFORM_END);
  337. curl_formadd(&formpost, &lastptr,
  338. CURLFORM_COPYNAME, "content-type:",
  339. CURLFORM_COPYCONTENTS, "multipart/form-data",
  340. CURLFORM_END);
  341. std::vector<std::string> postTokens;
  342. boost::split(postTokens, postData, boost::is_any_of("&"));
  343. for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
  344. {
  345. std::vector<std::string> tokens;
  346. boost::split(tokens, *it, boost::is_any_of("="));
  347. curl_formadd(&formpost, &lastptr,
  348. CURLFORM_COPYNAME, tokens[0].c_str(),
  349. CURLFORM_COPYCONTENTS, tokens[1].c_str(),
  350. CURLFORM_END);
  351. }
  352. curl_formadd(&formpost, &lastptr,
  353. CURLFORM_COPYNAME, fileFieldname.c_str(),
  354. CURLFORM_BUFFER, "data",
  355. CURLFORM_BUFFERPTR, contents.data(),
  356. CURLFORM_BUFFERLENGTH, contents.size(),
  357. CURLFORM_END);
  358. headerlist = curl_slist_append(headerlist, headerBuffer);
  359. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  360. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  361. int code = 0;
  362. curl_easy_perform(curl);
  363. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  364. curl_easy_cleanup(curl);
  365. curl_formfree(formpost);
  366. curl_slist_free_all(headerlist);
  367. if (code != 200)
  368. {
  369. std::stringstream error;
  370. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  371. throw std::runtime_error(error.str());
  372. }
  373. }
  374. std::string HttpClient::GetUrlPostAttachmentContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
  375. {
  376. std::string buffer;
  377. std::string contents;
  378. std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
  379. if (fileStream)
  380. {
  381. fileStream.seekg(0, std::ios::end);
  382. contents.resize(fileStream.tellg());
  383. fileStream.seekg(0, std::ios::beg);
  384. fileStream.read(&contents[0], contents.size());
  385. fileStream.close();
  386. }
  387. CURL* curl = curl_easy_init();
  388. CURLcode result;
  389. struct curl_httppost *formpost = nullptr;
  390. struct curl_httppost *lastptr = nullptr;
  391. struct curl_slist *headerlist = nullptr;
  392. static const char headerBuffer[] = "Expect:";
  393. curl_global_init(CURL_GLOBAL_ALL);
  394. curl_formadd(&formpost, &lastptr,
  395. CURLFORM_COPYNAME, "cache-control:",
  396. CURLFORM_COPYCONTENTS, "no-cache",
  397. CURLFORM_END);
  398. curl_formadd(&formpost, &lastptr,
  399. CURLFORM_COPYNAME, "content-type:",
  400. CURLFORM_COPYCONTENTS, "multipart/form-data",
  401. CURLFORM_END);
  402. std::vector<std::string> postTokens;
  403. boost::split(postTokens, postData, boost::is_any_of("&"));
  404. for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
  405. {
  406. std::vector<std::string> tokens;
  407. boost::split(tokens, *it, boost::is_any_of("="));
  408. curl_formadd(&formpost, &lastptr,
  409. CURLFORM_COPYNAME, tokens[0].c_str(),
  410. CURLFORM_COPYCONTENTS, tokens[1].c_str(),
  411. CURLFORM_END);
  412. }
  413. curl_formadd(&formpost, &lastptr,
  414. CURLFORM_COPYNAME, fileFieldname.c_str(),
  415. CURLFORM_BUFFER, "data",
  416. CURLFORM_BUFFERPTR, contents.data(),
  417. CURLFORM_BUFFERLENGTH, contents.size(),
  418. CURLFORM_END);
  419. headerlist = curl_slist_append(headerlist, headerBuffer);
  420. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  421. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  422. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
  423. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
  424. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
  425. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  426. int code = 0;
  427. curl_easy_perform(curl);
  428. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  429. curl_easy_cleanup(curl);
  430. curl_formfree(formpost);
  431. curl_slist_free_all(headerlist);
  432. if (code != 200)
  433. {
  434. std::stringstream error;
  435. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  436. error << "Data: " << buffer;
  437. throw std::runtime_error(error.str());
  438. }
  439. return buffer;
  440. }
  441. void HttpClient::GetUrlPostAttachmentSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
  442. {
  443. std::string contents;
  444. std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
  445. if (fileStream)
  446. {
  447. fileStream.seekg(0, std::ios::end);
  448. contents.resize(fileStream.tellg());
  449. fileStream.seekg(0, std::ios::beg);
  450. fileStream.read(&contents[0], contents.size());
  451. fileStream.close();
  452. }
  453. CURL* curl = curl_easy_init();
  454. CURLcode result;
  455. struct curl_httppost *formpost = nullptr;
  456. struct curl_httppost *lastptr = nullptr;
  457. struct curl_slist *headerlist = nullptr;
  458. static const char headerBuffer[] = "Expect:";
  459. curl_global_init(CURL_GLOBAL_ALL);
  460. curl_formadd(&formpost, &lastptr,
  461. CURLFORM_COPYNAME, "cache-control:",
  462. CURLFORM_COPYCONTENTS, "no-cache",
  463. CURLFORM_END);
  464. curl_formadd(&formpost, &lastptr,
  465. CURLFORM_COPYNAME, "content-type:",
  466. CURLFORM_COPYCONTENTS, "multipart/form-data",
  467. CURLFORM_END);
  468. std::vector<std::string> postTokens;
  469. boost::split(postTokens, postData, boost::is_any_of("&"));
  470. for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
  471. {
  472. std::vector<std::string> tokens;
  473. boost::split(tokens, *it, boost::is_any_of("="));
  474. curl_formadd(&formpost, &lastptr,
  475. CURLFORM_COPYNAME, tokens[0].c_str(),
  476. CURLFORM_COPYCONTENTS, tokens[1].c_str(),
  477. CURLFORM_END);
  478. }
  479. curl_formadd(&formpost, &lastptr,
  480. CURLFORM_COPYNAME, fileFieldname.c_str(),
  481. CURLFORM_BUFFER, "data",
  482. CURLFORM_BUFFERPTR, contents.data(),
  483. CURLFORM_BUFFERLENGTH, contents.size(),
  484. CURLFORM_END);
  485. headerlist = curl_slist_append(headerlist, headerBuffer);
  486. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  487. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  488. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
  489. curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
  490. int code = 0;
  491. curl_easy_perform(curl);
  492. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  493. curl_easy_cleanup(curl);
  494. curl_formfree(formpost);
  495. curl_slist_free_all(headerlist);
  496. if (code != 200)
  497. {
  498. std::stringstream error;
  499. error << "Error (" << code << ") encountered while retrieving " << url << "\n";
  500. throw std::runtime_error(error.str());
  501. }
  502. }
  503. size_t HttpClient::WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData)
  504. {
  505. if (writerData == nullptr)
  506. return size * nmemb;
  507. writerData->append(data, size * nmemb);
  508. return size * nmemb;
  509. }
  510. } // namespace Util
  511. } // namespace PresenceDetection