Fix #15, Support multipart post.

This commit is contained in:
Miao Jiang 2013-04-10 03:09:25 +08:00
parent e318a72006
commit 9c1cbb81c9
10 changed files with 239 additions and 30 deletions

View file

@ -1,10 +1,12 @@
// Generated by ToffeeScript 1.4.0
// Generated by ToffeeScript 1.6.2
(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;
}
@ -12,11 +14,45 @@
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) {
@ -50,7 +86,8 @@
Curl.user_options = {
RAW: 'RAW',
DEBUG: 'DEBUG'
DEBUG: 'DEBUG',
MULTIPART: 'MULTIPART'
};
id = 0;

View file

@ -6,13 +6,38 @@ catch e
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])?
@ -41,6 +66,7 @@ Curl::getinfo = (oinfo) ->
Curl.user_options =
RAW: 'RAW'
DEBUG: 'DEBUG'
MULTIPART: 'MULTIPART'
id = 0
curls = {}

View file

@ -1,17 +1,17 @@
// Generated by ToffeeScript 1.4.0
// Generated by ToffeeScript 1.6.2
(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 CurlBuilder() {}
CurlBuilder.curls = {};
@ -95,7 +95,7 @@
curl.body = data.toString();
}
curl.status = curl.code = c.getinfo('RESPONSE_CODE');
return process.nextTick(function() {
process.nextTick(function() {
return cb.call(curl, null, curl);
});
});
@ -103,7 +103,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);
});
});