155 lines
4.1 KiB
JavaScript
155 lines
4.1 KiB
JavaScript
// Generated by ToffeeScript 1.2.0-0
|
|
(function() {
|
|
var Curl, curl, curl_id, curls,
|
|
__slice = [].slice,
|
|
__hasProp = {}.hasOwnProperty;
|
|
|
|
try {
|
|
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
|
|
} catch (e) {
|
|
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.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());
|
|
} else {
|
|
throw new Error("unsupported option " + option);
|
|
}
|
|
};
|
|
|
|
Curl.prototype.getinfo = function(oinfo) {
|
|
var info, info_id;
|
|
info = oinfo.toUpperCase();
|
|
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("unsupported info " + oinfo);
|
|
}
|
|
};
|
|
|
|
Curl.prototype.perform = function() {
|
|
this.perform_();
|
|
return Curl.process();
|
|
};
|
|
|
|
Curl.user_options = {
|
|
RAW: 'RAW'
|
|
};
|
|
|
|
Curl.process = function() {
|
|
var once;
|
|
if (Curl.in_process) return;
|
|
return (once = function() {
|
|
var num;
|
|
num = Curl.process_();
|
|
if (num > 0) {
|
|
Curl.in_process = true;
|
|
return setTimeout(once, 80);
|
|
} else {
|
|
return Curl.in_process = false;
|
|
}
|
|
})();
|
|
};
|
|
|
|
curl_id = 0;
|
|
|
|
curls = {};
|
|
|
|
curl = function() {
|
|
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');
|
|
c.chunks = [];
|
|
length = 0;
|
|
for (k in options) {
|
|
if (!__hasProp.call(options, k)) continue;
|
|
v = options[k];
|
|
c.setopt(k, v);
|
|
}
|
|
c.on_write = function(chunk) {
|
|
c.chunks.push(chunk);
|
|
return length += chunk.length;
|
|
};
|
|
c.on_end = function() {
|
|
var chunk, data, position, res, _i, _len, _ref,
|
|
_this = this;
|
|
data = new Buffer(length);
|
|
position = 0;
|
|
_ref = c.chunks;
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
chunk = _ref[_i];
|
|
chunk.copy(data, position);
|
|
position += chunk.length;
|
|
}
|
|
c.chunks = [];
|
|
res = {};
|
|
res.curl_ = c;
|
|
delete curls[c.id];
|
|
if (c.options.RAW) {
|
|
res.body = data;
|
|
} else {
|
|
res.body = data.toString();
|
|
}
|
|
res.status = res.code = c.getinfo('RESPONSE_CODE');
|
|
res.info = function(info) {
|
|
if (this.curl_ == null) throw new Error('curl is closed');
|
|
return this.curl_.getinfo(info);
|
|
};
|
|
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);
|
|
c.perform();
|
|
return c;
|
|
};
|
|
|
|
curl.Curl = Curl;
|
|
|
|
curl.get_count = function() {
|
|
return Curl.get_count();
|
|
};
|
|
|
|
module.exports = curl;
|
|
|
|
}).call(this);
|