Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <node.h>
- #include <curl/curl.h>
- #include <sstream>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- class CURLplusplus
- {
- private:
- CURL* curl;
- stringstream ss;
- long http_code;
- public:
- CURLplusplus()
- : curl(curl_easy_init())
- , http_code(0)
- {
- }
- ~CURLplusplus()
- {
- if (curl) curl_easy_cleanup(curl);
- }
- std::string Get(const std::string& url)
- {
- CURLcode res;
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
- ss.str("");
- http_code = 0;
- res = curl_easy_perform(curl);
- if (res != CURLE_OK)
- {
- throw std::runtime_error(curl_easy_strerror(res));
- }
- curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
- return ss.str();
- }
- long GetHttpCode()
- {
- return http_code;
- }
- private:
- static size_t write_data(void* buffer, size_t size, size_t nmemb, void* userp)
- {
- return static_cast<CURLplusplus*>(userp)->Write(buffer, size, nmemb);
- }
- size_t Write(void* buffer, size_t size, size_t nmemb)
- {
- ss.write((const char*)buffer, size * nmemb);
- return size * nmemb;
- }
- };
- struct MemoryStruct {
- char* memory;
- size_t size;
- };
- static size_t
- WriteMemoryCallback(void* contents, size_t size, size_t nmemb, void* userp)
- {
- size_t realsize = size * nmemb;
- struct MemoryStruct* mem = (struct MemoryStruct*)userp;
- char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
- if (ptr == NULL) {
- /* out of memory! */
- printf("not enough memory (realloc returned NULL)\n");
- return 0;
- }
- mem->memory = ptr;
- memcpy(&(mem->memory[mem->size]), contents, realsize);
- mem->size += realsize;
- mem->memory[mem->size] = 0;
- return realsize;
- }
- namespace Internal {
- using v8::FunctionCallbackInfo;
- using v8::Isolate;
- using v8::Local;
- using v8::Object;
- using v8::String;
- using v8::Value;
- void Inject(const FunctionCallbackInfo<Value>& args) {
- Isolate* isolate = args.GetIsolate();
- CURL* curl_handle;
- CURLcode res;
- char *c_url;
- v8::String::Utf8Value url(args[0]->ToString());
- c_url = *url;
- struct MemoryStruct chunk;
- chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
- chunk.size = 0; /* no data at this point */
- curl_global_init(CURL_GLOBAL_ALL);
- /* init the curl session */
- curl_handle = curl_easy_init();
- /* specify URL to get */
- curl_easy_setopt(curl_handle, CURLOPT_URL, c_url);
- /* send all data to this function */
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- /* we pass our 'chunk' struct to the callback function */
- curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)&chunk);
- /* some servers don't like requests that are made without a user-agent
- field, so we provide one */
- curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "");
- /* get it! */
- res = curl_easy_perform(curl_handle);
- /* check for errors */
- if (res != CURLE_OK) {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- }
- else {
- /*
- * Now, our chunk.memory points to a memory block that is chunk.size
- * bytes big and contains the remote file.
- *
- * Do something nice with it!
- */
- printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
- }
- /* cleanup curl stuff */
- curl_easy_cleanup(curl_handle);
- free(chunk.memory);
- /* we're done with libcurl, so clean it up */
- curl_global_cleanup();
- char *success = "OK";
- args.GetReturnValue().Set(String::NewFromUtf8(isolate, success).ToLocalChecked());
- }
- void init(Local<Object> exports) {
- NODE_SET_METHOD(exports, "Inject", Inject);
- }
- NODE_MODULE(addon, init)
- } // namespace demo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement