curlApi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * curlApi.c
  3. *
  4. * Created on: 2021/6/1
  5. * Author: foluswen
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <termios.h>
  28. #include <errno.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <signal.h>
  35. #include <curl/curl.h>
  36. #include <curl/easy.h>
  37. #include "SystemLogMessage.h"
  38. #include "curlApi.h"
  39. struct MemoryStruct
  40. {
  41. uint8_t memory[4096];
  42. size_t size;
  43. };
  44. struct MemoryStruct chunk;
  45. /**
  46. * HTTP get/post data response callback
  47. * @param contents
  48. * @param size
  49. * @param nmemb
  50. * @param userp
  51. */
  52. static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  53. {
  54. size_t realsize = size*nmemb;
  55. struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  56. mem->size = realsize;;
  57. memset(mem->memory, 0x00, ARRAY_SIZE(mem->memory));
  58. chunk.size = realsize;
  59. memcpy(mem->memory, contents, realsize);
  60. return realsize;
  61. }
  62. /**
  63. * HTTP get request/response
  64. * @param url
  65. * @param responseData
  66. * @return
  67. */
  68. int httpGet(uint8_t *url, uint8_t *responseData)
  69. {
  70. int result = FAIL;
  71. CURL *curl;
  72. CURLcode res;
  73. curl_global_init(CURL_GLOBAL_ALL);
  74. curl = curl_easy_init();
  75. if(curl)
  76. {
  77. curl_easy_setopt(curl, CURLOPT_URL, (char*)url);
  78. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  79. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  80. //curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  81. //curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen((char*)postData));
  82. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  83. //curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
  84. //curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
  85. res = curl_easy_perform(curl);
  86. if(res != CURLE_OK)
  87. {
  88. DEBUG_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  89. }
  90. else
  91. {
  92. if(chunk.size > 0)
  93. {
  94. //DEBUG_INFO("Response content: %s\n", chunk.memory);
  95. memcpy(responseData, chunk.memory, ARRAY_SIZE(chunk.memory));
  96. }
  97. result = PASS;
  98. }
  99. curl_easy_cleanup(curl);
  100. }
  101. curl_global_cleanup();
  102. return result;
  103. }
  104. /**
  105. * HTTP post request/response
  106. * @param url
  107. * @param postData
  108. * @return
  109. */
  110. int httpPost(uint8_t *url, uint8_t *postData, uint8_t *responseData)
  111. {
  112. int result = FAIL;
  113. CURL *curl;
  114. CURLcode res;
  115. curl_global_init(CURL_GLOBAL_ALL);
  116. curl = curl_easy_init();
  117. if(curl)
  118. {
  119. curl_easy_setopt(curl, CURLOPT_URL, (char*)url);
  120. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  121. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  122. //curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  123. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (char*)postData);
  124. //curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen((char*)postData));
  125. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  126. //curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
  127. //curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
  128. res = curl_easy_perform(curl);
  129. if(res != CURLE_OK)
  130. {
  131. DEBUG_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  132. }
  133. else
  134. {
  135. if(chunk.size > 0)
  136. {
  137. //DEBUG_INFO("Response content: %s\n", chunk.memory);
  138. memcpy(responseData, chunk.memory, ARRAY_SIZE(chunk.memory));
  139. }
  140. result = PASS;
  141. }
  142. curl_easy_cleanup(curl);
  143. }
  144. curl_global_cleanup();
  145. return result;
  146. }
  147. /**
  148. * HTTP put request
  149. * @param url
  150. * @param postData
  151. * @return
  152. */
  153. int httpPut(uint8_t *url, uint8_t *postData)
  154. {
  155. int result = FAIL;
  156. CURL *curl;
  157. CURLcode res;
  158. curl_global_init(CURL_GLOBAL_ALL);
  159. curl = curl_easy_init();
  160. if(curl)
  161. {
  162. struct curl_slist *headers = NULL;
  163. headers = curl_slist_append(headers, "Content-Type: literature/classic");
  164. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  165. curl_easy_setopt(curl, CURLOPT_URL, (char*)url);
  166. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (char*)postData);
  167. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
  168. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  169. //curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
  170. //curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
  171. res = curl_easy_perform(curl);
  172. if(res != CURLE_OK)
  173. {
  174. DEBUG_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  175. }
  176. else
  177. {
  178. result = PASS;
  179. }
  180. curl_easy_cleanup(curl);
  181. curl_slist_free_all(headers);
  182. }
  183. curl_global_cleanup();
  184. return result;
  185. }
  186. /**
  187. * HTTP download response callback
  188. * @param ptr
  189. * @param size
  190. * @param nmemb
  191. * @param stream
  192. * @return
  193. */
  194. static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
  195. {
  196. size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  197. return written;
  198. }
  199. /**
  200. * Curl download request
  201. * @param url
  202. * @return
  203. */
  204. int curlDownload(uint8_t *url, uint8_t *filename)
  205. {
  206. int result = FAIL;
  207. CURL *curl;
  208. CURLcode res;
  209. FILE *downloadFile;
  210. curl_global_init(CURL_GLOBAL_ALL);
  211. curl = curl_easy_init();
  212. if(curl)
  213. {
  214. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  215. curl_easy_setopt(curl, CURLOPT_URL, (char*)url);
  216. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  217. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  218. //curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
  219. //curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
  220. downloadFile = fopen((char*)filename, "wb");
  221. if(downloadFile)
  222. {
  223. curl_easy_setopt(curl, CURLOPT_WRITEDATA, downloadFile);
  224. res = curl_easy_perform(curl);
  225. if(res != CURLE_OK)
  226. {
  227. DEBUG_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  228. }
  229. else
  230. {
  231. //DEBUG_INFO("Download %s success.\n", url);
  232. result = PASS;
  233. }
  234. fclose(downloadFile);
  235. }
  236. curl_easy_cleanup(curl);
  237. }
  238. curl_global_cleanup();
  239. return result;
  240. }
  241. /**
  242. * HTTP upload request
  243. * @param url
  244. * @param filename
  245. * @return
  246. */
  247. int httpUpload(uint8_t *url, uint8_t *filename)
  248. {
  249. int result = FAIL;
  250. CURL *curl;
  251. CURLcode res;
  252. FILE *upFile;
  253. struct stat file_info;
  254. curl_off_t speed_upload, total_time;
  255. if((upFile = fopen((char*)filename, "rb")))
  256. {
  257. if(fstat(fileno(upFile), &file_info) == 0)
  258. {
  259. curl = curl_easy_init();
  260. if(curl)
  261. {
  262. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  263. curl_easy_setopt(curl, CURLOPT_URL, (char*)url);
  264. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  265. curl_easy_setopt(curl, CURLOPT_READDATA, upFile);
  266. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
  267. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  268. //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  269. //curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
  270. //curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
  271. res = curl_easy_perform(curl);
  272. if(res != CURLE_OK)
  273. {
  274. DEBUG_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  275. }
  276. else
  277. {
  278. curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
  279. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
  280. /*DEBUG_INFO("Upload %s to %s success.\n", filename, url);
  281. DEBUG_INFO("Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during %"
  282. CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",
  283. speed_upload,
  284. (total_time / 1000000), (long)(total_time % 1000000));*/
  285. result = PASS;
  286. }
  287. fclose(upFile);
  288. curl_easy_cleanup(curl);
  289. }
  290. }
  291. }
  292. return result;
  293. }
  294. /**
  295. * File read callback
  296. * @param ptr
  297. * @param size
  298. * @param nmemb
  299. * @param stream
  300. * @return
  301. */
  302. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  303. {
  304. curl_off_t nread;
  305. size_t retcode = fread(ptr, size, nmemb, stream);
  306. nread = (curl_off_t)retcode;
  307. DEBUG_INFO("Read %" CURL_FORMAT_CURL_OFF_T " bytes from file\n", nread);
  308. return retcode;
  309. }
  310. /**
  311. * FTP upload request
  312. * @param url
  313. * @param filename
  314. * @return
  315. */
  316. int ftpUpload(uint8_t *url, uint8_t *filename)
  317. {
  318. int result = FAIL;
  319. CURL *curl;
  320. CURLcode res;
  321. FILE *upFile;
  322. struct stat file_info;
  323. curl_off_t fsize;
  324. curl_global_init(CURL_GLOBAL_ALL);
  325. if(!stat((char*)filename, &file_info))
  326. {
  327. fsize = (curl_off_t)file_info.st_size;
  328. DEBUG_INFO("%s size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", filename, fsize);
  329. if((upFile = fopen((char*)filename, "rb")))
  330. {
  331. curl = curl_easy_init();
  332. if(curl)
  333. {
  334. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  335. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  336. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  337. curl_easy_setopt(curl, CURLOPT_URL, url);
  338. curl_easy_setopt(curl, CURLOPT_READDATA, upFile);
  339. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize);
  340. res = curl_easy_perform(curl);
  341. if(res != CURLE_OK)
  342. {
  343. DEBUG_ERROR("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  344. }
  345. else
  346. {
  347. //DEBUG_INFO("Upload %s success.\n", url);
  348. }
  349. curl_easy_cleanup(curl);
  350. }
  351. fclose(upFile);
  352. }
  353. }
  354. curl_global_cleanup();
  355. return result;
  356. }