Save url, options, defaultOptions in curl.
This commit is contained in:
parent
8e0d95d812
commit
13f1f69dbc
2 changed files with 45 additions and 24 deletions
|
@ -1,4 +1,4 @@
|
||||||
// Generated by ToffeeScript 1.3.3
|
// Generated by ToffeeScript 1.4.0
|
||||||
(function() {
|
(function() {
|
||||||
var Curl, CurlBuilder,
|
var Curl, CurlBuilder,
|
||||||
__hasProp = {}.hasOwnProperty,
|
__hasProp = {}.hasOwnProperty,
|
||||||
|
@ -36,7 +36,7 @@
|
||||||
return curl.perform.apply(curl, arguments);
|
return curl.perform.apply(curl, arguments);
|
||||||
};
|
};
|
||||||
curl.perform = function() {
|
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) : [];
|
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
||||||
if (this.running) {
|
if (this.running) {
|
||||||
throw new Error('the cURL session is busy, use CurlBuilder.create to create another cURL Session');
|
throw new Error('the cURL session is busy, use CurlBuilder.create to create another cURL Session');
|
||||||
|
@ -46,30 +46,42 @@
|
||||||
}
|
}
|
||||||
this.running = true;
|
this.running = true;
|
||||||
cb = args.pop();
|
cb = args.pop();
|
||||||
url = args[0], options = args[1];
|
this.url = args[0], this.options = args[1];
|
||||||
if (options == null) {
|
if ((_ref = this.options) == null) {
|
||||||
options = {};
|
this.options = {};
|
||||||
}
|
}
|
||||||
c = this.curl_;
|
c = this.curl_;
|
||||||
options['URL'] = url;
|
|
||||||
c.chunks = [];
|
c.chunks = [];
|
||||||
length = 0;
|
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) {
|
c.on_write = function(chunk) {
|
||||||
curl.log("receive " + chunk.length + " bytes");
|
curl.log("receive " + chunk.length + " bytes");
|
||||||
c.chunks.push(chunk);
|
c.chunks.push(chunk);
|
||||||
return length += chunk.length;
|
return length += chunk.length;
|
||||||
};
|
};
|
||||||
c.on_end = function() {
|
c.on_end = function() {
|
||||||
var chunk, data, position, _i, _len, _ref,
|
var chunk, data, position, _i, _len, _ref5,
|
||||||
_this = this;
|
_this = this;
|
||||||
curl.log("receive succeeded.");
|
curl.log("receive succeeded.");
|
||||||
curl.running = false;
|
curl.running = false;
|
||||||
data = new Buffer(length);
|
data = new Buffer(length);
|
||||||
position = 0;
|
position = 0;
|
||||||
_ref = c.chunks;
|
_ref5 = c.chunks;
|
||||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
|
||||||
chunk = _ref[_i];
|
chunk = _ref5[_i];
|
||||||
chunk.copy(data, position);
|
chunk.copy(data, position);
|
||||||
position += chunk.length;
|
position += chunk.length;
|
||||||
}
|
}
|
||||||
|
@ -102,7 +114,7 @@
|
||||||
if (reset == null) {
|
if (reset == null) {
|
||||||
reset = true;
|
reset = true;
|
||||||
}
|
}
|
||||||
defaultOptions = options;
|
this.defaultOptions = options;
|
||||||
if (reset) {
|
if (reset) {
|
||||||
this.log('Set default options and reset cURL');
|
this.log('Set default options and reset cURL');
|
||||||
return this.reset();
|
return this.reset();
|
||||||
|
@ -157,7 +169,7 @@
|
||||||
this.log("opening.");
|
this.log("opening.");
|
||||||
this.curl_ = new Curl();
|
this.curl_ = new Curl();
|
||||||
this.curl_.options = {};
|
this.curl_.options = {};
|
||||||
this.setOptions(defaultOptions);
|
this.defaultOptions = defaultOptions != null ? defaultOptions : {};
|
||||||
CurlBuilder.curls[curl.id] = curl;
|
CurlBuilder.curls[curl.id] = curl;
|
||||||
return this.log("opened.");
|
return this.log("opened.");
|
||||||
};
|
};
|
||||||
|
@ -168,8 +180,6 @@
|
||||||
}
|
}
|
||||||
return this.open();
|
return this.open();
|
||||||
};
|
};
|
||||||
curl.Curl = Curl;
|
|
||||||
curl.Builder = CurlBuilder;
|
|
||||||
curl.create = function(defaultOptions) {
|
curl.create = function(defaultOptions) {
|
||||||
return CurlBuilder.create(defaultOptions);
|
return CurlBuilder.create(defaultOptions);
|
||||||
};
|
};
|
||||||
|
|
|
@ -24,17 +24,28 @@ class CurlBuilder
|
||||||
throw new Error 'the cURL is closed.'
|
throw new Error 'the cURL is closed.'
|
||||||
|
|
||||||
@running = true
|
@running = true
|
||||||
|
|
||||||
|
# pop arguments (url, [options = {}], callback)
|
||||||
cb = args.pop()
|
cb = args.pop()
|
||||||
[url, options] = args
|
[@url, @options] = args
|
||||||
@url = url
|
@options ?= {}
|
||||||
options ?= {}
|
|
||||||
|
|
||||||
c = @curl_
|
c = @curl_
|
||||||
options['URL'] = url
|
|
||||||
c.chunks = []
|
c.chunks = []
|
||||||
length = 0
|
length = 0
|
||||||
|
|
||||||
@setOptions options
|
@debug = @defaultOptions.DEBUG ? @options.DEBUG ? @debug
|
||||||
|
|
||||||
|
|
||||||
|
@effective_options = {}
|
||||||
|
for k, v of @defaultOptions
|
||||||
|
@effective_options[k] = v
|
||||||
|
for k, v of @options
|
||||||
|
@effective_options[k] = v
|
||||||
|
|
||||||
|
@setOptions @effective_options
|
||||||
|
|
||||||
|
@curl_.setopt('URL', @url)
|
||||||
|
|
||||||
c.on_write = (chunk) ->
|
c.on_write = (chunk) ->
|
||||||
curl.log "receive #{chunk.length} bytes"
|
curl.log "receive #{chunk.length} bytes"
|
||||||
|
@ -73,7 +84,7 @@ class CurlBuilder
|
||||||
|
|
||||||
|
|
||||||
curl.setDefaultOptions = (options = {}, reset = true) ->
|
curl.setDefaultOptions = (options = {}, reset = true) ->
|
||||||
defaultOptions = options
|
@defaultOptions = options
|
||||||
if reset
|
if reset
|
||||||
@log 'Set default options and reset cURL'
|
@log 'Set default options and reset cURL'
|
||||||
@reset()
|
@reset()
|
||||||
|
@ -81,6 +92,7 @@ class CurlBuilder
|
||||||
curl.log = (text) ->
|
curl.log = (text) ->
|
||||||
if @debug
|
if @debug
|
||||||
console.info "[cURL #{@id}] " + text
|
console.info "[cURL #{@id}] " + text
|
||||||
|
|
||||||
curl.setOptions = (options = {}) ->
|
curl.setOptions = (options = {}) ->
|
||||||
for own k, v of options
|
for own k, v of options
|
||||||
@log "Set option '#{k}' to '#{v}'"
|
@log "Set option '#{k}' to '#{v}'"
|
||||||
|
@ -111,7 +123,7 @@ class CurlBuilder
|
||||||
@log "opening."
|
@log "opening."
|
||||||
@curl_ = new Curl()
|
@curl_ = new Curl()
|
||||||
@curl_.options = {}
|
@curl_.options = {}
|
||||||
@setOptions defaultOptions
|
@defaultOptions = defaultOptions ? {}
|
||||||
CurlBuilder.curls[curl.id] = curl
|
CurlBuilder.curls[curl.id] = curl
|
||||||
@log "opened."
|
@log "opened."
|
||||||
|
|
||||||
|
@ -121,10 +133,9 @@ class CurlBuilder
|
||||||
@end()
|
@end()
|
||||||
@open()
|
@open()
|
||||||
|
|
||||||
curl.Curl = Curl
|
|
||||||
curl.Builder = CurlBuilder
|
|
||||||
curl.create = (defaultOptions) ->
|
curl.create = (defaultOptions) ->
|
||||||
CurlBuilder.create(defaultOptions)
|
CurlBuilder.create(defaultOptions)
|
||||||
|
|
||||||
curl.get_count = ->
|
curl.get_count = ->
|
||||||
Curl.get_count()
|
Curl.get_count()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue