use c++ instead of c++0x to compatible with old compiler

This commit is contained in:
jiangfriend@gmail.com 2012-02-14 21:10:45 +08:00
parent f8f934a119
commit 314d43203f
11 changed files with 454 additions and 106 deletions

View file

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

View file

@ -3,9 +3,19 @@ try
catch e
{Curl} = require __dirname + '/../build/default/node-curl'
Curl::setopt_user_ = (option_id, value) ->
@options[option_id] = value
Curl::setopt = (ooption, value) ->
option = ooption.toUpperCase()
if (option_id = Curl.integer_options[option])?
# 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
else if (option_id = Curl.slist_options[option])?
@setopt_slist_ option_id, value
else if (option_id = Curl.integer_options[option])?
@setopt_int_ option_id, value >> 0
else if (option_id = Curl.string_options[option])?
@setopt_str_ option_id, value.toString()
@ -14,19 +24,24 @@ Curl::setopt = (ooption, value) ->
Curl::getinfo = (oinfo) ->
info = oinfo.toUpperCase()
if (info_id = Curl.integer_infos[info])?
if (info_id = Curl.slist_infos[info])?
@getinfo_slist_(info_id)
else if (info_id = Curl.integer_infos[info])?
@getinfo_int_(info_id)
else if (info_id = Curl.string_infos[info])?
@getinfo_str_(info_id)
else if (info_id = Curl.double_infos[info])?
@getinfo_double_(info_id)
else
throw new Error("unsupproted info #{oinfo}")
throw new Error("unsupported info #{oinfo}")
Curl::perform = ->
@perform_()
Curl.process()
Curl.user_options =
RAW: 'RAW'
Curl.process = ->
if Curl.in_process
return
@ -40,56 +55,75 @@ Curl.process = ->
# url, [options], cb
curl_id = 0
curls = {}
curl = (args...) ->
cb = args.pop()
[url, options] = args
options ?= {}
c = new Curl()
c.options = {}
c.id = ++curl_id
# after the scope c will not valid any more, so add to curls to keep c alive
curls[c.id] = c
c.setopt 'FOLLOWLOCATION', 1
c.setopt 'ACCEPT_ENCODING', 'gzip'
chunks = []
c.chunks = []
length = 0
res = {}
for own k, v of options
c.setopt k, v
c.on_write = (chunk) ->
chunks.push chunk
c.chunks.push chunk
length += chunk.length
console.info "on_write #{c.id} #{chunk.length}"
c.on_end = ->
data = new Buffer(length)
position = 0
for chunk in chunks
for chunk in c.chunks
chunk.copy data, position
position += chunk.length
# Strange Issue
# use data.toString() will cause parallel http request terminated? eg yahoo.com
st = Date.now()
i = 0
while Date.now() - st < 500
++i
c.chunks = []
res = {}
# now the c is in res.curl, delete curl in curls
# if res destruct, curl will be destruct after gc
res.curl_ = c
delete curls[c.id]
res.body = data #.toString()
if c.options.RAW
res.body = data
else
res.body = data.toString() #.toString()
res.status = res.code = c.getinfo('RESPONSE_CODE')
res.info = (info)->
c.getinfo(info)
# 当curl返回过快且cb循环调用回导致堆栈溢出
# process.nextTick!
console.info "id: #{c.id}"
unless @curl_?
throw new Error('curl is closed')
@curl_.getinfo(info)
res.close = ->
@curl_.close() if @curl_?
@curl_ = null
@body = null
# if curl returns to fast, avoid cb recursive call
process.nextTick!
cb null, res
c.on_error = (err)->
curls[c.id].close()
delete curls[c.id]
process.nextTick!
cb err, null
c.setopt('URL', url)
c.perform()
c
curl.Curl = Curl
curl.get_count = ->
Curl.get_count()
module.exports = curl