Permalink
Browse files

expose low level curl.

  • Loading branch information...
1 parent d25438b commit 02ba0f25d35224a73403d575980ae609bba8f31c @jiangmiao committed Feb 19, 2013
Showing with 48 additions and 12 deletions.
  1. +28 −0 examples/low-level.js
  2. +5 −2 index.js
  3. +3 −0 lib/Curl.toffee
  4. +0 −6 lib/CurlInstance.js
  5. +12 −4 src/node-curl.h
View
@@ -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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -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)
{

0 comments on commit 02ba0f2

Please sign in to comment.