HttpClient.cpp 19 KB

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