Save url, options, defaultOptions in curl.

This commit is contained in:
Miao Jiang 2012-11-29 16:33:10 +08:00
parent 8e0d95d812
commit 13f1f69dbc
2 changed files with 45 additions and 24 deletions

View file

@ -1,4 +1,4 @@
// Generated by ToffeeScript 1.3.3
// Generated by ToffeeScript 1.4.0
(function() {
var Curl, CurlBuilder,
__hasProp = {}.hasOwnProperty,
@ -36,7 +36,7 @@
return curl.perform.apply(curl, arguments);
};
curl.perform = function() {
var args, c, cb, length, options, url;
var args, c, cb, 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 CurlBuilder.create to create another cURL Session');
@ -46,30 +46,42 @@
}
this.running = true;
cb = args.pop();
url = args[0], options = args[1];
if (options == null) {
options = {};
this.url = args[0], this.options = args[1];
if ((_ref = this.options) == null) {
this.options = {};
}
c = this.curl_;
options['URL'] = url;
c.chunks = [];
length = 0;
this.setOptions(options);
this.debug = (_ref1 = (_ref2 = this.defaultOptions.DEBUG) != null ? _ref2 : this.options.DEBUG) != null ? _ref1 : this.debug;
this.effective_options = {};
_ref3 = this.defaultOptions;
for (k in _ref3) {
v = _ref3[k];
this.effective_options[k] = v;
}
_ref4 = this.options;
for (k in _ref4) {
v = _ref4[k];
this.effective_options[k] = v;
}
this.setOptions(this.effective_options);
this.curl_.setopt('URL', this.url);
c.on_write = function(chunk) {
curl.log("receive " + chunk.length + " bytes");
c.chunks.push(chunk);
return length += chunk.length;
};
c.on_end = function() {
var chunk, data, position, _i, _len, _ref,
var chunk, data, position, _i, _len, _ref5,
_this = this;
curl.log("receive succeeded.");
curl.running = false;
data = new Buffer(length);
position = 0;
_ref = c.chunks;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
chunk = _ref[_i];
_ref5 = c.chunks;
for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
chunk = _ref5[_i];
chunk.copy(data, position);
position += chunk.length;
}
@ -102,7 +114,7 @@
if (reset == null) {
reset = true;
}
defaultOptions = options;
this.defaultOptions = options;
if (reset) {
this.log('Set default options and reset cURL');
return this.reset();
@ -157,7 +169,7 @@
this.log("opening.");
this.curl_ = new Curl();
this.curl_.options = {};
this.setOptions(defaultOptions);
this.defaultOptions = defaultOptions != null ? defaultOptions : {};
CurlBuilder.curls[curl.id] = curl;
return this.log("opened.");
};
@ -168,8 +180,6 @@
}
return this.open();
};
curl.Curl = Curl;
curl.Builder = CurlBuilder;
curl.create = function(defaultOptions) {
return CurlBuilder.create(defaultOptions);
};