diff --git a/examples/low-level.js b/examples/low-level.js index e274660..b5df695 100644 --- a/examples/low-level.js +++ b/examples/low-level.js @@ -20,13 +20,14 @@ curl.on('data', function(chunk) { curl.on('error', function(e) { p("error: " + e.message) curl.close(); + once(); }); curl.on('end', function() { - p('done.'); - p('code: ' + curl.getinfo('RESPONSE_CODE')); + curl.getinfo('RESPONSE_CODE'); curl.close(); + p('done.'); }); curl.perform(); diff --git a/lib/Curl.js b/lib/Curl.js index 6253d1e..1565368 100644 --- a/lib/Curl.js +++ b/lib/Curl.js @@ -1,6 +1,6 @@ // Generated by ToffeeScript 1.4.0 (function() { - var Curl, m, p; + var Curl, curls, id, m, p; try { Curl = require(__dirname + '/../build/Release/node-curl').Curl; @@ -8,6 +8,10 @@ Curl = require(__dirname + '/../build/default/node-curl').Curl; } + curls = {}; + + id = 0; + Curl.prototype.setopt_user_ = function(option_id, value) { return this.options[option_id] = value; }; @@ -54,15 +58,26 @@ }; Curl.prototype.on = function(event, callback) { + var _this = this; switch (event) { case 'data': this.on_write = callback; break; case 'error': - this.on_error = callback; + this.on_error = function() { + var rt; + rt = callback(); + curls[_this.id] = null; + return rt; + }; break; case 'end': - this.on_end = callback; + this.on_end = function() { + var rt; + rt = callback(); + curls[_this.id] = null; + return rt; + }; break; default: throw new Error("invalid event type " + event); @@ -71,6 +86,8 @@ }; Curl.prototype.perform = function() { + this.id = ++id; + curls[this.id] = this; this.perform_(); Curl.process(); return this; @@ -95,7 +112,7 @@ return process.nextTick(once); } else { m = 0; - w = (8192 - n) * 80 / 8192; + w = (8192 - n) * 80 / 8192 >> 0; if (w < 0) { w = 0; } diff --git a/lib/Curl.toffee b/lib/Curl.toffee index 69185e3..338a1ee 100644 --- a/lib/Curl.toffee +++ b/lib/Curl.toffee @@ -3,6 +3,8 @@ try catch e {Curl} = require __dirname + '/../build/default/node-curl' +curls = {} +id = 0 Curl::setopt_user_ = (option_id, value) -> @options[option_id] = value @@ -50,15 +52,24 @@ Curl::on = (event, callback) -> @on_write = callback when 'error' # (Error error) -> - @on_error = callback + @on_error = => + rt = callback() + curls[@id] = null + rt + when 'end' # () -> - @on_end = callback + @on_end = => + rt = callback() + curls[@id] = null + rt else throw new Error("invalid event type #{event}") @ Curl::perform = -> + @id = ++id + curls[@id] = @ @perform_() Curl.process() @ @@ -77,7 +88,7 @@ Curl.process = -> process.nextTick once else m = 0 - w = (8192 - n) * 80 / 8192 + w = (8192 - n) * 80 / 8192 >> 0 if w < 0 w = 0 setTimeout once, w diff --git a/src/node-curl.h b/src/node-curl.h index ea0e593..9079c2e 100644 --- a/src/node-curl.h +++ b/src/node-curl.h @@ -29,8 +29,6 @@ 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; @@ -331,8 +329,7 @@ class NodeCurl else curl->on_error(&msg_copy); - curl->refer.Dispose(); - curl->refer.Clear(); + // Handle should not released in curl.process, or will cause segment fault. } } } @@ -343,12 +340,6 @@ 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) {