From 02ba0f25d35224a73403d575980ae609bba8f31c Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Tue, 19 Feb 2013 20:12:33 +0800 Subject: [PATCH] expose low level curl. --- examples/low-level.js | 28 ++++++++++++++++++++++++++++ index.js | 7 +++++-- lib/Curl.toffee | 3 +++ lib/CurlInstance.js | 6 ------ src/node-curl.h | 16 ++++++++++++---- 5 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 examples/low-level.js delete mode 100644 lib/CurlInstance.js diff --git a/examples/low-level.js b/examples/low-level.js new file mode 100644 index 0000000..5da8e8e --- /dev/null +++ b/examples/low-level.js @@ -0,0 +1,28 @@ +var Curl = require('../lib/Curl') + +var p = console.log; +var url = process.argv[2]; + +var curl = new Curl(); + +if (!url) +url = 'www.yahoo.com'; + +curl.setopt('URL', url); +curl.setopt('CONNECTTIMEOUT', 2); + +// on 'data' must be returns chunk.length, or means interrupt the transfer +curl.on('data', function(chunk) { + return chunk.length; +}); + +curl.on('error', function(e) { + curl.close(); +}); + + +curl.on('end', function() { + curl.close(); +}); + +curl.perform(); diff --git a/index.js b/index.js index 92a1ac4..0e3a56d 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ -// Generated by CoffeeScript 1.1.4-3 +// Generated by ToffeeScript 1.4.0 (function() { + var CurlBuilder; - module.exports = require('./lib/curl'); + CurlBuilder = require('./lib/CurlBuilder'); + + module.exports = CurlBuilder.create(); }).call(this); diff --git a/lib/Curl.toffee b/lib/Curl.toffee index 246975f..c80814e 100644 --- a/lib/Curl.toffee +++ b/lib/Curl.toffee @@ -21,6 +21,7 @@ Curl::setopt = (ooption, value) -> @setopt_str_ option_id, value.toString() else throw new Error("unsupported option #{option}") + @ Curl::getinfo = (oinfo) -> info = oinfo.toUpperCase() @@ -34,6 +35,7 @@ Curl::getinfo = (oinfo) -> @getinfo_double_(info_id) else throw new Error("unsupported info #{oinfo}") + @ Curl.user_options = RAW: 'RAW' @@ -58,6 +60,7 @@ Curl::on = (event, callback) -> Curl::perform = -> @perform_() Curl.process() + @ Curl.process = -> if Curl.in_process diff --git a/lib/CurlInstance.js b/lib/CurlInstance.js deleted file mode 100644 index 9883a7c..0000000 --- a/lib/CurlInstance.js +++ /dev/null @@ -1,6 +0,0 @@ -// Generated by ToffeeScript 1.4.0 -(function() { - - - -}).call(this); diff --git a/src/node-curl.h b/src/node-curl.h index ae39340..f0331e9 100644 --- a/src/node-curl.h +++ b/src/node-curl.h @@ -27,6 +27,9 @@ class NodeCurl CURL * curl; v8::Persistent handle; + + // keep the object alive when performing + v8::Persistent refer; bool in_curlm; std::vector slists; std::map strings; @@ -312,8 +315,6 @@ class NodeCurl if (msg->msg == CURLMSG_DONE) { NodeCurl * curl = curls[msg->easy_handle]; - // copy CURLMsg - // on_error may delete the whole NodeCurl CURLMsg msg_copy = *msg; code = curl_multi_remove_handle(curlm, msg->easy_handle); curl->in_curlm = false; @@ -322,12 +323,13 @@ class NodeCurl return raise("curl_multi_remove_handle failed", curl_multi_strerror(code)); } - // ensure curl still exists, - // gc will delete the curl if there is no reference. if (msg_copy.data.result == CURLE_OK) curl->on_end(&msg_copy); else curl->on_error(&msg_copy); + + curl->refer.Dispose(); + curl->refer.Clear(); } } } @@ -338,6 +340,12 @@ class NodeCurl static v8::Handle perform(const v8::Arguments & args) { NodeCurl *curl = unwrap(args.This()); + + if (!curl->refer.IsEmpty()) + return raise("The curl session is running, use curl.create() to create another session."); + + curl->refer = v8::Persistent::New(args.This()); + CURLMcode code = curl_multi_add_handle(curlm, curl->curl); if (code != CURLM_OK) {