You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __GP_CURL_PROTOTYPE____
  2. #define __GP_CURL_PROTOTYPE____
  3. #include "core.h"
  4. class _GP_CURL
  5. {
  6. public:
  7. char * memory;
  8. CURL * curl;
  9. size_t size;
  10. private:
  11. static size_t WriteMemoryCallback(void * ptr, size_t nsize, size_t nmemb, void * data)
  12. {
  13. size_t realsize = nsize * nmemb;
  14. _GP_CURL * me = (_GP_CURL *) data;
  15. me->memory = (char *) realloc(me->memory, me->size + realsize + 1);
  16. if (me->memory == NULL)
  17. {
  18. /* out of memory! */
  19. return -1;
  20. };
  21. memcpy(&(me->memory[me->size]), ptr, realsize);
  22. me->size += realsize;
  23. me->memory[me->size] = 0;
  24. return realsize;
  25. }
  26. public:
  27. _GP_CURL()
  28. {
  29. memory = NULL;
  30. size = 0;
  31. curl = curl_easy_init();
  32. }
  33. ~_GP_CURL()
  34. {
  35. if (memory)
  36. {
  37. free(memory);
  38. }
  39. }
  40. _GP_CURL(CSTR url)
  41. {
  42. CURLcode res;
  43. curl = curl_easy_init();
  44. memory = NULL;
  45. size = 0;
  46. if (curl)
  47. {
  48. curl_easy_setopt(curl, CURLOPT_URL, url);
  49. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  50. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) this);
  51. curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  52. res = curl_easy_perform(curl);
  53. /* always cleanup */
  54. curl_easy_cleanup(curl);
  55. };
  56. }
  57. void Query(CSTR url)
  58. {
  59. CURLcode res;
  60. if (curl)
  61. {
  62. curl_easy_setopt(curl, CURLOPT_URL, url);
  63. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  64. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) this);
  65. // curl_easy_setopt( curl, CURLOPT_USERAGENT, "libcurl-agent/1.0" );
  66. res = curl_easy_perform(curl);
  67. /* always cleanup */
  68. curl_easy_cleanup(curl);
  69. };
  70. }
  71. size_t getSize(void)
  72. {
  73. return size;
  74. }
  75. CSTR getResult(void)
  76. {
  77. return (CSTR) memory;
  78. }
  79. };
  80. #endif