use c++ instead of c++0x to compatible with old compiler
This commit is contained in:
parent
f8f934a119
commit
314d43203f
11 changed files with 454 additions and 106 deletions
81
lib/curl.js
81
lib/curl.js
|
@ -1,6 +1,6 @@
|
|||
// Generated by CoffeeScript 1.1.4-3
|
||||
// Generated by ToffeeScript 1.1.4-4
|
||||
(function() {
|
||||
var Curl, curl, curl_id,
|
||||
var Curl, curl, curl_id, curls,
|
||||
__slice = [].slice,
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
|
@ -10,10 +10,18 @@
|
|||
Curl = require(__dirname + '/../build/default/node-curl').Curl;
|
||||
}
|
||||
|
||||
Curl.prototype.setopt_user_ = function(option_id, value) {
|
||||
return this.options[option_id] = value;
|
||||
};
|
||||
|
||||
Curl.prototype.setopt = function(ooption, value) {
|
||||
var option, option_id;
|
||||
option = ooption.toUpperCase();
|
||||
if ((option_id = Curl.integer_options[option]) != null) {
|
||||
if ((option_id = Curl.user_options[option]) != null) {
|
||||
return this.setopt_user_(option_id, value);
|
||||
} else if ((option_id = Curl.slist_options[option]) != null) {
|
||||
return this.setopt_slist_(option_id, value);
|
||||
} else if ((option_id = Curl.integer_options[option]) != null) {
|
||||
return this.setopt_int_(option_id, value >> 0);
|
||||
} else if ((option_id = Curl.string_options[option]) != null) {
|
||||
return this.setopt_str_(option_id, value.toString());
|
||||
|
@ -25,14 +33,16 @@
|
|||
Curl.prototype.getinfo = function(oinfo) {
|
||||
var info, info_id;
|
||||
info = oinfo.toUpperCase();
|
||||
if ((info_id = Curl.integer_infos[info]) != null) {
|
||||
if ((info_id = Curl.slist_infos[info]) != null) {
|
||||
return this.getinfo_slist_(info_id);
|
||||
} else if ((info_id = Curl.integer_infos[info]) != null) {
|
||||
return this.getinfo_int_(info_id);
|
||||
} else if ((info_id = Curl.string_infos[info]) != null) {
|
||||
return this.getinfo_str_(info_id);
|
||||
} else if ((info_id = Curl.double_infos[info]) != null) {
|
||||
return this.getinfo_double_(info_id);
|
||||
} else {
|
||||
throw new Error("unsupproted info " + oinfo);
|
||||
throw new Error("unsupported info " + oinfo);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -41,6 +51,10 @@
|
|||
return Curl.process();
|
||||
};
|
||||
|
||||
Curl.user_options = {
|
||||
RAW: 'RAW'
|
||||
};
|
||||
|
||||
Curl.process = function() {
|
||||
var once;
|
||||
if (Curl.in_process) return;
|
||||
|
@ -58,63 +72,84 @@
|
|||
|
||||
curl_id = 0;
|
||||
|
||||
curls = {};
|
||||
|
||||
curl = function() {
|
||||
var args, c, cb, chunks, k, length, options, res, url, v;
|
||||
var args, c, cb, k, length, options, url, v;
|
||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||
cb = args.pop();
|
||||
url = args[0], options = args[1];
|
||||
if (options == null) options = {};
|
||||
c = new Curl();
|
||||
c.options = {};
|
||||
c.id = ++curl_id;
|
||||
curls[c.id] = c;
|
||||
c.setopt('FOLLOWLOCATION', 1);
|
||||
c.setopt('ACCEPT_ENCODING', 'gzip');
|
||||
chunks = [];
|
||||
c.chunks = [];
|
||||
length = 0;
|
||||
res = {};
|
||||
for (k in options) {
|
||||
if (!__hasProp.call(options, k)) continue;
|
||||
v = options[k];
|
||||
c.setopt(k, v);
|
||||
}
|
||||
c.on_write = function(chunk) {
|
||||
chunks.push(chunk);
|
||||
length += chunk.length;
|
||||
return console.info("on_write " + c.id + " " + chunk.length);
|
||||
c.chunks.push(chunk);
|
||||
return length += chunk.length;
|
||||
};
|
||||
c.on_end = function() {
|
||||
var chunk, data, i, position, st, _i, _len;
|
||||
var chunk, data, position, res, _i, _len, _ref,
|
||||
_this = this;
|
||||
data = new Buffer(length);
|
||||
position = 0;
|
||||
for (_i = 0, _len = chunks.length; _i < _len; _i++) {
|
||||
chunk = chunks[_i];
|
||||
_ref = c.chunks;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
chunk = _ref[_i];
|
||||
chunk.copy(data, position);
|
||||
position += chunk.length;
|
||||
}
|
||||
st = Date.now();
|
||||
i = 0;
|
||||
while (Date.now() - st < 500) {
|
||||
++i;
|
||||
c.chunks = [];
|
||||
res = {};
|
||||
res.curl_ = c;
|
||||
delete curls[c.id];
|
||||
if (c.options.RAW) {
|
||||
res.body = data;
|
||||
} else {
|
||||
res.body = data.toString();
|
||||
}
|
||||
res.body = data;
|
||||
res.status = res.code = c.getinfo('RESPONSE_CODE');
|
||||
res.info = function(info) {
|
||||
return c.getinfo(info);
|
||||
if (this.curl_ == null) throw new Error('curl is closed');
|
||||
return this.curl_.getinfo(info);
|
||||
};
|
||||
console.info("id: " + c.id);
|
||||
return cb(null, res);
|
||||
res.close = function() {
|
||||
if (this.curl_ != null) this.curl_.close();
|
||||
this.curl_ = null;
|
||||
return this.body = null;
|
||||
};
|
||||
return process.nextTick(function() {
|
||||
return cb(null, res);
|
||||
});
|
||||
};
|
||||
c.on_error = function(err) {
|
||||
var _this = this;
|
||||
curls[c.id].close();
|
||||
delete curls[c.id];
|
||||
return process.nextTick(function() {
|
||||
return cb(err, null);
|
||||
});
|
||||
};
|
||||
c.setopt('URL', url);
|
||||
return c.perform();
|
||||
c.perform();
|
||||
return c;
|
||||
};
|
||||
|
||||
curl.Curl = Curl;
|
||||
|
||||
curl.get_count = function() {
|
||||
return Curl.get_count();
|
||||
};
|
||||
|
||||
module.exports = curl;
|
||||
|
||||
}).call(this);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue