Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #include <node.h>
  2. #include <curl/curl.h>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. using namespace std;
  10.  
  11. class CURLplusplus
  12. {
  13. private:
  14. CURL* curl;
  15. stringstream ss;
  16. long http_code;
  17. public:
  18. CURLplusplus()
  19. : curl(curl_easy_init())
  20. , http_code(0)
  21. {
  22.  
  23. }
  24. ~CURLplusplus()
  25. {
  26. if (curl) curl_easy_cleanup(curl);
  27. }
  28. std::string Get(const std::string& url)
  29. {
  30. CURLcode res;
  31. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  32. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  33. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  34. curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
  35.  
  36. ss.str("");
  37. http_code = 0;
  38. res = curl_easy_perform(curl);
  39. if (res != CURLE_OK)
  40. {
  41. throw std::runtime_error(curl_easy_strerror(res));
  42. }
  43. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
  44. return ss.str();
  45. }
  46. long GetHttpCode()
  47. {
  48. return http_code;
  49. }
  50. private:
  51. static size_t write_data(void* buffer, size_t size, size_t nmemb, void* userp)
  52. {
  53. return static_cast<CURLplusplus*>(userp)->Write(buffer, size, nmemb);
  54. }
  55. size_t Write(void* buffer, size_t size, size_t nmemb)
  56. {
  57. ss.write((const char*)buffer, size * nmemb);
  58. return size * nmemb;
  59. }
  60. };
  61.  
  62. struct MemoryStruct {
  63. char* memory;
  64. size_t size;
  65. };
  66.  
  67. static size_t
  68. WriteMemoryCallback(void* contents, size_t size, size_t nmemb, void* userp)
  69. {
  70. size_t realsize = size * nmemb;
  71. struct MemoryStruct* mem = (struct MemoryStruct*)userp;
  72.  
  73. char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
  74. if (ptr == NULL) {
  75. /* out of memory! */
  76. printf("not enough memory (realloc returned NULL)\n");
  77. return 0;
  78. }
  79.  
  80. mem->memory = ptr;
  81. memcpy(&(mem->memory[mem->size]), contents, realsize);
  82. mem->size += realsize;
  83. mem->memory[mem->size] = 0;
  84.  
  85. return realsize;
  86. }
  87.  
  88. namespace Internal {
  89.  
  90. using v8::FunctionCallbackInfo;
  91. using v8::Isolate;
  92. using v8::Local;
  93. using v8::Object;
  94. using v8::String;
  95. using v8::Value;
  96.  
  97. void Inject(const FunctionCallbackInfo<Value>& args) {
  98. Isolate* isolate = args.GetIsolate();
  99.  
  100. CURL* curl_handle;
  101. CURLcode res;
  102. char *c_url;
  103.  
  104. v8::String::Utf8Value url(args[0]->ToString());
  105. c_url = *url;
  106.  
  107. struct MemoryStruct chunk;
  108.  
  109. chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
  110. chunk.size = 0; /* no data at this point */
  111.  
  112. curl_global_init(CURL_GLOBAL_ALL);
  113.  
  114. /* init the curl session */
  115. curl_handle = curl_easy_init();
  116.  
  117. /* specify URL to get */
  118. curl_easy_setopt(curl_handle, CURLOPT_URL, c_url);
  119.  
  120. /* send all data to this function */
  121. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  122.  
  123. /* we pass our 'chunk' struct to the callback function */
  124. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)&chunk);
  125.  
  126. /* some servers don't like requests that are made without a user-agent
  127. field, so we provide one */
  128. curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "");
  129.  
  130. /* get it! */
  131. res = curl_easy_perform(curl_handle);
  132.  
  133. /* check for errors */
  134. if (res != CURLE_OK) {
  135. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  136. curl_easy_strerror(res));
  137. }
  138. else {
  139. /*
  140. * Now, our chunk.memory points to a memory block that is chunk.size
  141. * bytes big and contains the remote file.
  142. *
  143. * Do something nice with it!
  144. */
  145.  
  146. printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
  147. }
  148.  
  149. /* cleanup curl stuff */
  150. curl_easy_cleanup(curl_handle);
  151.  
  152. free(chunk.memory);
  153.  
  154. /* we're done with libcurl, so clean it up */
  155. curl_global_cleanup();
  156.  
  157. char *success = "OK";
  158. args.GetReturnValue().Set(String::NewFromUtf8(isolate, success).ToLocalChecked());
  159. }
  160.  
  161. void init(Local<Object> exports) {
  162. NODE_SET_METHOD(exports, "Inject", Inject);
  163. }
  164.  
  165. NODE_MODULE(addon, init)
  166.  
  167. } // namespace demo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement