Compare commits

...

23 commits

Author SHA1 Message Date
Josh Sherman
b12a319ebb Consider STRINGPOINT as string_options
URL changed from being an OBJECT to being a STRINGPOINT so when building
URL was being thrown as an invalid option. This resolves that but has
left me with a segfault :-/
2016-10-12 15:28:50 -05:00
Josh Sherman
98bd3a7e61 Updated to more recent variable name 2016-10-12 14:32:05 -05:00
Miao Jiang
288400f616 Bump version to 0.3.3 2014-09-03 15:41:04 +08:00
Miao Jiang
ce4a60be91 Merge branch 'master' of git://github.com/briancsparks/node-curl 2014-09-03 15:32:32 +08:00
Miao Jiang
1f3a2f1bad Fixes free incorrect variable. 2014-09-03 15:31:35 +08:00
Brian C Sparks
5e2abee688 Fix for 64-bit segfault 2014-06-12 14:20:58 -07:00
Miao Jiang
171f5bc3a6 Update package version. 2013-05-25 16:19:28 +08:00
Miao Jiang
05b8345650 Update README. 2013-05-25 16:19:07 +08:00
Miao Jiang
f02c0a6fbf Always make string copy for multi postdata. 2013-05-25 16:07:18 +08:00
Miao Jiang
8de753eec9 Extract header. 2013-05-25 16:00:35 +08:00
Miao Jiang
b77ee78afd Update README. 2013-04-14 18:40:44 +08:00
Miao Jiang
5c6e4b0b49 Bump version to 0.3.1. 2013-04-10 16:36:59 +08:00
Miao Jiang
e9e17fc940 Hotfix, release resource. 2013-04-10 16:36:52 +08:00
Miao Jiang
5e48419e18 Add string.h for strndup. 2013-04-10 03:40:16 +08:00
Miao Jiang
1ba3bb2329 Update travis to test for 0.8, 0.10 2013-04-10 03:31:26 +08:00
Miao Jiang
3a42f7f05e Bump version to 0.3.0. 2013-04-10 03:27:54 +08:00
Miao Jiang
788459bc7d Update README. 2013-04-10 03:22:32 +08:00
Miao Jiang
b337b6d4fb Rename value to contents. 2013-04-10 03:19:00 +08:00
Miao Jiang
9c1cbb81c9 Fix #15, Support multipart post. 2013-04-10 03:09:25 +08:00
Miao Jiang
e318a72006 bump version to 0.2.3 2013-03-01 14:42:56 +08:00
Miao Jiang
8f707b34b1 Catch exception. 2013-02-22 02:07:26 +08:00
Miao Jiang
279e9f04cc bump version to 0.2.2 2013-02-21 19:13:09 +08:00
Miao Jiang
78a53743de Improve code. 2013-02-21 19:05:32 +08:00
18 changed files with 442 additions and 108 deletions

View file

@ -1,5 +1,4 @@
language: node_js
node_js:
- 0.6
- 0.8
- 0.9
- 0.10

View file

@ -17,7 +17,6 @@ Quick Start
console.info(this.info('SIZE_DOWNLOAD'));
});
* with options
curl = require('node-curl')
@ -43,6 +42,7 @@ Usage
members:
status - Http Response code
body - Http body
header - Http header
url - the url set by curl(...)
options - the options set by curl(...)
@ -52,6 +52,10 @@ Usage
methods:
info(name) - Get information of result, see 'info' section
REMARK:
If the http is redirected, then header will contain at least 2 http headers.
* Curl Control
members
@ -86,8 +90,8 @@ Options
node-curl support slist which map to Javascript Array
eg:
HTTP_HEADER: ['FOO', 'BAR']
HTTP_HEADER: 'FOO'
HTTPHEADER: ['FOO', 'BAR']
HTTPHEADER: 'FOO'
any non-array parameter will convert to [ parameter.toString() ]
@ -105,6 +109,25 @@ Infos
slist will be returns in Array
eg: CURLINFO_COOKIELIST
MultiPart Upload
----------------
Use MULTIPART option
There are 4 options in MULTIPART, `name`, `file`, `type`, `contents`
```javascript
curl('127.0.0.1/upload.php', {
MULTIPART: [
{name: 'file', file: '/file/path', type: 'text/html'},
{name: 'sumbit', contents: 'send'}
]
}, function(e) {
console.log(e);
console.log(this.body);
this.close()
});
```
Low Level Curl Usage
--------------------
@ -120,6 +143,7 @@ Methods:
Events:
'data', function(Buffer chunk) {}
'header', function(Buffer chunk) {}
'error', function(Error error) {}
'end', function() {}
@ -140,21 +164,26 @@ Example: examples/low-level.js
// on 'data' must be returns chunk.length, or means interrupt the transfer
curl.on('data', function(chunk) {
p("receive " + chunk.length)
p("receive " + chunk.length);
return chunk.length;
});
curl.on('header', function(chunk) {
p("receive header " + chunk.length);
return chunk.length;
})
// curl.close() should be called in event 'error' and 'end' if the curl won't use any more.
// or the resource will not release until V8 garbage mark sweep.
curl.on('error', function(e) {
p("error: " + e.message)
p("error: " + e.message);
curl.close();
});
curl.on('end', function() {
p('code: ' + curl.getinfo('RESPONSE_CODE'));
p('done.')
p('done.');
curl.close();
});

View file

@ -2,8 +2,8 @@
'targets': [
{
'target_name': 'node-curl',
'cflags': ['-Wall', '-O1', '-fno-inline-functions'],
'cflags_cc': ['-Wall', '-O1', '-fno-inline-functions'],
'cflags': ['-Wall', '-O1', '-g', '-fno-inline-functions'],
'cflags_cc': ['-Wall', '-O1', '-g', '-fno-inline-functions'],
'sources': ['src/node-curl.cc'],
'libraries': ['-lcurl']
}

View file

@ -1,6 +1,6 @@
// Generated by ToffeeScript 1.4.0
// Generated by ToffeeScript 1.6.2-5
(function() {
var curl, p,
var curl, err, p, res,
_this = this;
curl = require('../index');
@ -9,11 +9,10 @@
curl(process.argv[2], {
VERBOSE: 1,
DEBUG: 1
}, function(_$$_err, _$$_res) {
var err, res;
err = _$$_err;
res = _$$_res;
DEBUG: 1,
FOLLOWLOCATION: 1
}, function() {
err = arguments[0], res = arguments[1];
if (err) {
return p(err);
} else {

View file

@ -1,7 +1,7 @@
curl = require '../index'
p = console.info
err, res = curl! process.argv[2], {VERBOSE: 1, DEBUG: 1}
err, res = curl! process.argv[2], {VERBOSE: 1, DEBUG: 1, FOLLOWLOCATION: 1}
if err
p err
else

View file

@ -1,4 +1,4 @@
var Curl = require('../lib/Curl')
var Curl = require('../lib/Curl');
var p = console.log;
var url = process.argv[2];
@ -14,21 +14,27 @@ curl.setopt('VERBOSE', 1);
// on 'data' must be returns chunk.length, or means interrupt the transfer
curl.on('data', function(chunk) {
p("receive " + chunk.length)
p("receive " + chunk.length);
return chunk.length;
});
curl.on('header', function(chunk) {
p("receive header " + chunk.length);
return chunk.length;
})
// curl.close() should be called in event 'error' and 'end' if the curl won't use any more.
// or the resource will not release until V8 garbage mark sweep.
curl.on('error', function(e) {
p("error: " + e.message)
p("error: " + e.message);
curl.close();
once();
});
curl.on('end', function() {
p("code: " + curl.getinfo('RESPONSE_CODE'));
curl.close();
p('code: ' + curl.getinfo('RESPONSE_CODE'));
p('done.');
curl.close();
});
curl.perform();

View file

@ -0,0 +1,28 @@
// Generated by ToffeeScript 1.6.2-5
(function() {
var curl, p;
curl = require('../index');
p = console.log;
p('start');
curl('127.0.0.1/upload.php', {
multipart: [
{
name: 'file',
file: '/home/miao/test.js',
type: 'text/html'
}, {
name: 'sumbit',
contents: 'send'
}
]
}, function(e) {
console.log(e);
console.log(this.body);
return this.close();
});
}).call(this);

View file

@ -0,0 +1,13 @@
curl = require '../index'
p = console.log
p 'start'
curl '127.0.0.1/upload.php', {
multipart: [
{name: 'file', file: '/home/miao/test.js', type: 'text/html'},
{name: 'sumbit', contents: 'send'}
],
}, (e) ->
console.log e
console.log @body
@close()

View file

@ -1,4 +1,4 @@
// Generated by ToffeeScript 1.4.0
// Generated by ToffeeScript 1.6.2-5
(function() {
var Curl, assert, i, j, next, _fn, _i;
@ -17,16 +17,15 @@
var curl, once;
curl = Curl.create();
return (once = function() {
var _this = this;
return curl('localhost/test.html', function(_$$_err, _$$_res) {
var err, res;
err = _$$_err;
res = _$$_res;
var err, res,
_this = this;
curl('localhost/test.html', function() {
err = arguments[0], res = arguments[1];
assert.equal(res.body.length, 1468);
if (++j % 100 === 0) {
console.info(j);
}
if (j < 5000) {
if (j < 500000) {
return once();
}
});

View file

@ -13,6 +13,6 @@ for i in [1..100]
assert.equal res.body.length, 1468
if ++j % 100 == 0
console.info j
if j < 5000
if j < 500000
once()
# res.close()

View file

@ -1,6 +1,6 @@
// Generated by ToffeeScript 1.4.0
// Generated by ToffeeScript 1.6.2-5
(function() {
var cookieFile, curl, fs, options, p, util,
var cookieFile, curl, err, fs, options, p, stream, util,
_this = this;
curl = require('../index');
@ -25,14 +25,12 @@
curl.setDefaultOptions(options);
curl('www.google.com', function(_$$_err) {
var err;
err = _$$_err;
curl('www.google.com', function() {
err = arguments[0];
p("\x1b[33m" + util.inspect(curl.info('COOKIELIST')) + "\x1b[0m");
curl.reset();
return curl('www.yahoo.com', function(_$$_err) {
var stream;
err = _$$_err;
curl('www.yahoo.com', function() {
err = arguments[0];
p("\x1b[33m" + util.inspect(curl.info('COOKIELIST')) + "\x1b[0m");
p("body length " + curl.body.length);
p("\x1b[33mText in " + cookieFile + "\x1b[0m");
@ -44,7 +42,7 @@
p("----");
curl.close();
p("deleting " + cookieFile);
return fs.unlink(cookieFile, function() {
fs.unlink(cookieFile, function() {
return p("done.");
});
});

View file

@ -1,26 +1,58 @@
// Generated by ToffeeScript 1.4.0
// Generated by ToffeeScript 1.6.2-5
(function() {
var Curl, curls, id, m, p;
var Curl, curls, e, id, m, p,
__hasProp = {}.hasOwnProperty;
try {
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
} catch (e) {
} catch (_error) {
e = _error;
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;
};
Curl.prototype.setopt_httppost = function(rows) {
var cols, k, option_id, row, v;
this.httppost = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = rows.length; _i < _len; _i++) {
row = rows[_i];
cols = [];
for (k in row) {
if (!__hasProp.call(row, k)) continue;
v = row[k];
k = k.toUpperCase();
if ((option_id = Curl.httppost_options[k]) != null) {
cols.push(option_id);
if (!(v instanceof Buffer)) {
v = new Buffer(v.toString());
}
cols.push(v);
} else {
throw new Error("invalid http post option " + k);
}
}
_results.push(cols);
}
return _results;
})();
this.setopt_httppost_(this.httppost);
return this;
};
Curl.prototype.setopt = function(option_name, value) {
var option, option_id;
option = option_name.toUpperCase();
if ((option_id = Curl.user_options[option]) != null) {
this.setopt_user_(option_id, value);
if (option === 'MULTIPART') {
this.setopt_httppost(value);
} else {
this.setopt_user_(option_id, value);
}
} else if ((option_id = Curl.slist_options[option]) != null) {
this.setopt_slist_(option_id, value);
} else if ((option_id = Curl.integer_options[option]) != null) {
@ -54,29 +86,37 @@
Curl.user_options = {
RAW: 'RAW',
DEBUG: 'DEBUG'
DEBUG: 'DEBUG',
MULTIPART: 'MULTIPART'
};
id = 0;
curls = {};
Curl.prototype.on = function(event, callback) {
var _this = this;
switch (event) {
case 'data':
this.on_write = callback;
this.on_write = function(chunk) {
return callback.call(_this, chunk);
};
break;
case 'header':
this.on_header = function(chunk) {
return callback.call(_this, chunk);
};
break;
case 'error':
this.on_error = function() {
var rt;
rt = callback();
curls[_this.id] = null;
return rt;
this.on_error = function(e) {
delete curls[_this.id];
return callback.call(_this, e);
};
break;
case 'end':
this.on_end = function() {
var rt;
rt = callback();
curls[_this.id] = null;
return rt;
delete curls[_this.id];
return callback.call(_this);
};
break;
default:
@ -85,6 +125,11 @@
return this;
};
Curl.prototype.close = function() {
delete curls[this.id];
return this.close_();
};
Curl.prototype.perform = function() {
this.id = ++id;
curls[this.id] = this;

View file

@ -3,18 +3,41 @@ try
catch e
{Curl} = require __dirname + '/../build/default/node-curl'
curls = {}
id = 0
Curl::setopt_user_ = (option_id, value) ->
@options[option_id] = value
Curl::setopt_httppost = (rows) ->
# convert object-rows to array-rows
# format
# [
# [OPTION_ID, VALUE]
# ]
@httppost = for row in rows
cols = []
for own k, v of row
k = k.toUpperCase()
if (option_id = Curl.httppost_options[k])?
cols.push option_id
unless v instanceof Buffer
v = new Buffer(v.toString())
cols.push v
else
throw new Error("invalid http post option #{k}")
cols
@setopt_httppost_(@httppost)
@
Curl::setopt = (option_name, value) ->
option = option_name.toUpperCase()
# slist must be at the top of condition
# the option exists in string_options too
if (option_id = Curl.user_options[option])?
@setopt_user_ option_id, value
if (option == 'MULTIPART')
@setopt_httppost value
else
@setopt_user_ option_id, value
else if (option_id = Curl.slist_options[option])?
@setopt_slist_ option_id, value
else if (option_id = Curl.integer_options[option])?
@ -43,30 +66,39 @@ Curl::getinfo = (oinfo) ->
Curl.user_options =
RAW: 'RAW'
DEBUG: 'DEBUG'
MULTIPART: 'MULTIPART'
id = 0
curls = {}
# on 'data' must be returns the chunk length
Curl::on = (event, callback) ->
switch event
when 'data'
# (Buffer chunk) ->
@on_write = callback
@on_write = (chunk) =>
callback.call @, chunk
when 'header'
@on_header = (chunk) =>
callback.call @, chunk
when 'error'
# (Error error) ->
@on_error = =>
rt = callback()
curls[@id] = null
rt
@on_error = (e) =>
delete curls[@id]
callback.call @, e
when 'end'
# () ->
@on_end = =>
rt = callback()
curls[@id] = null
rt
delete curls[@id]
callback.call @
else
throw new Error("invalid event type #{event}")
@
Curl::close = () ->
delete curls[@id]
@close_()
Curl::perform = ->
@id = ++id
curls[@id] = @
@ -96,5 +128,6 @@ Curl.process = ->
Curl.in_process = false
module.exports = Curl
# vim: sw=2 ts=2 sts=2 expandtab :

View file

@ -1,17 +1,29 @@
// Generated by ToffeeScript 1.4.0
// Generated by ToffeeScript 1.6.2-5
(function() {
var Curl, CurlBuilder,
var Curl, CurlBuilder, e,
__hasProp = {}.hasOwnProperty,
__slice = [].slice;
try {
Curl = require(__dirname + '/Curl');
} catch (e) {
} catch (_error) {
e = _error;
Curl = require(__dirname + '/Curl');
}
CurlBuilder = (function() {
function merge_chunks(chunks, length) {
var chunk, data, position, _i, _len;
data = new Buffer(length);
position = 0;
for (_i = 0, _len = chunks.length; _i < _len; _i++) {
chunk = chunks[_i];
chunk.copy(data, position);
position += chunk.length;
}
return data;
};
CurlBuilder = (function() {
function CurlBuilder() {}
CurlBuilder.curls = {};
@ -31,12 +43,11 @@
};
CurlBuilder.create = function(defaultOptions) {
var curl;
curl = function() {
function curl() {
return curl.perform.apply(curl, arguments);
};
curl.perform = function() {
var args, c, cb, k, length, v, _ref, _ref1, _ref2, _ref3, _ref4;
var args, c, cb, header_length, k, length, v, _ref, _ref1, _ref2, _ref3, _ref4;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (this.running) {
throw new Error('the cURL session is busy, use curl.create to create another cURL Session');
@ -50,9 +61,8 @@
if ((_ref = this.options) == null) {
this.options = {};
}
c = this.curl_;
c.chunks = [];
length = 0;
header_length = 0;
this.debug = (_ref1 = (_ref2 = this.defaultOptions.DEBUG) != null ? _ref2 : this.options.DEBUG) != null ? _ref1 : this.debug;
this.effectiveOptions = {};
_ref3 = this.defaultOptions;
@ -69,33 +79,39 @@
this.setOptions({
URL: this.url
});
c = this.curl_;
c.chunks = [];
c.header_chunks = [];
c.on('data', function(chunk) {
curl.log("receive " + chunk.length + " bytes");
c.chunks.push(chunk);
length += chunk.length;
return chunk.length;
});
c.on('header', function(chunk) {
curl.log("receive " + chunk.length + " header");
c.header_chunks.push(chunk);
header_length += chunk.length;
return chunk.length;
});
c.on('end', function() {
var chunk, data, position, _i, _len, _ref5,
var data, header,
_this = this;
curl.log("receive succeeded.");
curl.running = false;
data = new Buffer(length);
position = 0;
_ref5 = c.chunks;
for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
chunk = _ref5[_i];
chunk.copy(data, position);
position += chunk.length;
}
data = merge_chunks(c.chunks, length);
header = merge_chunks(c.header_chunks, header_length);
c.chunks = [];
c.header_chunks = [];
if (c.options.RAW) {
curl.body = data;
curl.header = header;
} else {
curl.body = data.toString();
curl.header = header.toString();
}
curl.status = curl.code = c.getinfo('RESPONSE_CODE');
return process.nextTick(function() {
process.nextTick(function() {
return cb.call(curl, null, curl);
});
});
@ -103,7 +119,7 @@
var _this = this;
curl.log("receive failed: " + err.message);
curl.running = false;
return process.nextTick(function() {
process.nextTick(function() {
return cb.call(curl, err, null);
});
});

View file

@ -3,6 +3,14 @@ try
catch e
Curl = require __dirname + '/Curl'
merge_chunks = (chunks, length) ->
data = new Buffer(length)
position = 0
for chunk in chunks
chunk.copy data, position
position += chunk.length
data
class CurlBuilder
@curls: {}
@id: 0
@ -30,13 +38,10 @@ class CurlBuilder
[@url, @options] = args
@options ?= {}
c = @curl_
c.chunks = []
length = 0
header_length = 0
@debug = @defaultOptions.DEBUG ? @options.DEBUG ? @debug
@effectiveOptions = {}
for k, v of @defaultOptions
@effectiveOptions[k] = v
@ -47,26 +52,35 @@ class CurlBuilder
@setOptions @effectiveOptions
@setOptions {URL: @url}
c = @curl_
c.chunks = []
c.header_chunks = []
c.on 'data', (chunk) ->
curl.log "receive #{chunk.length} bytes"
c.chunks.push chunk
length += chunk.length
chunk.length
c.on 'header', (chunk) ->
curl.log "receive #{chunk.length} header"
c.header_chunks.push chunk
header_length += chunk.length
chunk.length
c.on 'end', ->
curl.log "receive succeeded."
curl.running = false
data = new Buffer(length)
position = 0
for chunk in c.chunks
chunk.copy data, position
position += chunk.length
data = merge_chunks(c.chunks, length)
header = merge_chunks(c.header_chunks, header_length)
c.chunks = []
c.header_chunks = []
if c.options.RAW
curl.body = data
curl.header = header
else
curl.body = data.toString()
curl.header = header.toString()
curl.status = curl.code = c.getinfo('RESPONSE_CODE')
# if curl returns to fast, avoid cb recursive call

View file

@ -1,6 +1,6 @@
{
"name": "node-curl",
"version": "0.2.1",
"version": "0.3.3",
"author" : "Jiang Miao <jiangfriend@gmail.com>",
"description": "node wrapper for multi curl, fully implemented.",
"keywords" : ["node-curl", "curl", "multi-curl", "mcurl"],

View file

@ -30,7 +30,7 @@ generate() {
) > $root/$name.h
}
generate integer_options 'CINIT\((\w+).*LONG' OPT
generate string_options 'CINIT\((\w+).*OBJECT' OPT
generate string_options 'CINIT\((\w+).*(STRINGPOINT|OBJECT)' OPT
generate integer_infos 'CURLINFO_(\w+).*LONG' INFO
generate string_infos 'CURLINFO_(\w+).*STRING' INFO

View file

@ -8,9 +8,92 @@
#include <map>
#include <vector>
#include <string>
#include <stdlib.h>
#include <string.h>
#define NODE_CURL_EXPORT(name) export_curl_options(t, #name, name, sizeof(name) / sizeof(CurlOption));
class NodeCurlHttppost
{
public:
curl_httppost *first;
curl_httppost *last;
public:
NodeCurlHttppost()
: first(NULL), last(NULL)
{
reset();
}
~NodeCurlHttppost()
{
reset();
}
void reset()
{
curl_httppost *cur = first;
while (cur) {
curl_httppost *next = cur->next;
if (cur->contenttype)
free(cur->contenttype);
if (cur->contents)
free(cur->contents);
if (cur->buffer)
free(cur->buffer);
if (cur->name)
free(cur->name);
free(cur);
cur = next;
}
first = NULL;
last = NULL;
}
void append()
{
if (!first) {
first = (curl_httppost*)calloc(1, sizeof(curl_httppost));
last = first;
} else {
last->next = (curl_httppost*)calloc(1, sizeof(curl_httppost));
last = last->next;
}
}
enum {
NAME,
FILE,
CONTENTS,
TYPE
};
void set(int field, char *value, long length)
{
value = strndup(value, length);
switch (field) {
case NAME:
last->name = value;
last->namelength = length;
break;
case TYPE:
last->contenttype = value;
break;
case FILE:
last->flags |= CURL_HTTPPOST_FILENAME;
case CONTENTS:
last->contents = value;
last->contentslength = length;
break;
default:
// `default` should never be reached.
free(value);
break;
}
}
};
class NodeCurl
{
struct CurlOption
@ -32,6 +115,7 @@ class NodeCurl
bool in_curlm;
std::vector<curl_slist*> slists;
std::map<int, std::string> strings;
NodeCurlHttppost httppost;
NodeCurl(v8::Handle<v8::Object> object)
: in_curlm(false)
@ -50,6 +134,8 @@ class NodeCurl
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_function);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, this);
curls[curl] = this;
}
@ -111,6 +197,13 @@ class NodeCurl
return nodecurl->on_write(ptr, size * nmemb);
}
static size_t header_function(char *ptr, size_t size, size_t nmemb, void *userdata)
{
transfered += size * nmemb;
NodeCurl *nodecurl = (NodeCurl*)userdata;
return nodecurl->on_header(ptr, size * nmemb);
}
size_t on_write(char *data, size_t n)
{
static v8::Persistent<v8::String> SYM_ON_WRITE = v8::Persistent<v8::String>::New(v8::String::NewSymbol("on_write"));
@ -119,7 +212,28 @@ class NodeCurl
{
node::Buffer * buffer = node::Buffer::New(data, n);
v8::Handle<v8::Value> argv[] = { buffer->handle_ };
n = cb->ToObject()->CallAsFunction(handle, 1, argv)->Int32Value();
v8::Handle<v8::Value> rt = cb->ToObject()->CallAsFunction(handle, 1, argv);
if (rt.IsEmpty())
return 0;
else
return rt->Int32Value();
}
return n;
}
size_t on_header(char *data, size_t n)
{
static v8::Persistent<v8::String> SYM_ON_HEADER = v8::Persistent<v8::String>::New(v8::String::NewSymbol("on_header"));
v8::Handle<v8::Value> cb = handle->Get(SYM_ON_HEADER);
if (cb->IsFunction())
{
node::Buffer * buffer = node::Buffer::New(data, n);
v8::Handle<v8::Value> argv[] = { buffer->handle_ };
v8::Handle<v8::Value> rt = cb->ToObject()->CallAsFunction(handle, 1, argv);
if (rt.IsEmpty())
return 0;
else
return rt->Int32Value();
}
return n;
}
@ -163,7 +277,7 @@ class NodeCurl
static v8::Handle<v8::Value> getinfo_int(const v8::Arguments & args)
{
return getinfo<int, v8::Integer>(args);
return getinfo<long, v8::Integer>(args);
}
static v8::Handle<v8::Value> getinfo_str(const v8::Arguments & args)
@ -238,6 +352,30 @@ class NodeCurl
return node_curl->setopt(args[0], slist);
}
static v8::Handle<v8::Value> setopt_httppost(const v8::Arguments & args)
{
NodeCurl * node_curl = unwrap(args.This());
NodeCurlHttppost &httppost = node_curl->httppost;
v8::Handle<v8::Array> rows = v8::Handle<v8::Array>::Cast(args[0]);
httppost.reset();
for (uint32_t i=0, len = rows->Length(); i<len; ++i)
{
v8::Handle<v8::Array> cols = v8::Handle<v8::Array>::Cast(rows->Get(i));
uint32_t j=0, cols_len = cols->Length();
httppost.append();
while (j<cols_len)
{
int field = cols->Get(j++)->Int32Value();
v8::Handle<v8::Object> buffer = cols->Get(j++)->ToObject();
char *value = node::Buffer::Data(buffer);
int length = node::Buffer::Length(buffer);
httppost.set(field, value, length);
}
}
curl_easy_setopt(node_curl->curl, CURLOPT_HTTPPOST, node_curl->httppost.first);
return args.This();
}
static curl_slist * value_to_slist(v8::Handle<v8::Value> value)
{
curl_slist * slist = NULL;
@ -328,8 +466,6 @@ class NodeCurl
curl->on_end(&msg_copy);
else
curl->on_error(&msg_copy);
// Handle should not released in curl.process, or will cause segment fault.
}
}
}
@ -340,6 +476,12 @@ class NodeCurl
static v8::Handle<v8::Value> perform(const v8::Arguments & args)
{
NodeCurl *curl = unwrap(args.This());
if (!curl)
return raise("curl is closed.");
if (curl->in_curlm)
return raise("curl session is running.");
CURLMcode code = curl_multi_add_handle(curlm, curl->curl);
if (code != CURLM_OK)
{
@ -347,6 +489,7 @@ class NodeCurl
}
curl->in_curlm = true;
++running_handles;
return args.This();
}
@ -380,13 +523,14 @@ class NodeCurl
NODE_SET_PROTOTYPE_METHOD(t , "setopt_int_" , setopt_int);
NODE_SET_PROTOTYPE_METHOD(t , "setopt_str_" , setopt_str);
NODE_SET_PROTOTYPE_METHOD(t , "setopt_slist_" , setopt_slist);
NODE_SET_PROTOTYPE_METHOD(t , "setopt_httppost_" , setopt_httppost);
NODE_SET_PROTOTYPE_METHOD(t , "getinfo_int_" , getinfo_int);
NODE_SET_PROTOTYPE_METHOD(t , "getinfo_str_" , getinfo_str);
NODE_SET_PROTOTYPE_METHOD(t , "getinfo_double_" , getinfo_double);
NODE_SET_PROTOTYPE_METHOD(t , "getinfo_slist_" , getinfo_slist);
NODE_SET_PROTOTYPE_METHOD(t, "close", close);
NODE_SET_PROTOTYPE_METHOD(t, "close_", close);
NODE_SET_METHOD(t , "process_" , process);
NODE_SET_METHOD(t , "get_count" , get_count);
@ -425,6 +569,15 @@ class NodeCurl
X(SSL_ENGINES),
X(COOKIELIST)
};
#undef X
#define X(name) {#name, NodeCurlHttppost::name}
CurlOption httppost_options[] = {
X(NAME),
X(FILE),
X(CONTENTS),
X(TYPE)
};
#undef X
NODE_CURL_EXPORT(string_options);
@ -436,6 +589,8 @@ class NodeCurl
NODE_CURL_EXPORT(double_infos);
NODE_CURL_EXPORT(slist_infos);
NODE_CURL_EXPORT(httppost_options);
target->Set(v8::String::NewSymbol("Curl"), t->GetFunction());
return target;
}