expose low level curl.

This commit is contained in:
Miao Jiang 2013-02-19 20:12:33 +08:00
parent d25438bdf0
commit 02ba0f25d3
5 changed files with 48 additions and 12 deletions

28
examples/low-level.js Normal file
View file

@ -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();

View file

@ -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);

View file

@ -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

View file

@ -1,6 +0,0 @@
// Generated by ToffeeScript 1.4.0
(function() {
}).call(this);

View file

@ -27,6 +27,9 @@ class NodeCurl
CURL * curl;
v8::Persistent<v8::Object> handle;
// keep the object alive when performing
v8::Persistent<v8::Object> refer;
bool in_curlm;
std::vector<curl_slist*> slists;
std::map<int, std::string> 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<v8::Value> 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<v8::Object>::New(args.This());
CURLMcode code = curl_multi_add_handle(curlm, curl->curl);
if (code != CURLM_OK)
{