From 13f1f69dbcc2b6355f89acbca1d1c4b83a0e85c7 Mon Sep 17 00:00:00 2001 From: Miao Jiang Date: Thu, 29 Nov 2012 16:33:10 +0800 Subject: [PATCH] Save url, options, defaultOptions in curl. --- lib/CurlBuilder.js | 40 +++++++++++++++++++++++++--------------- lib/CurlBuilder.toffee | 29 ++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/lib/CurlBuilder.js b/lib/CurlBuilder.js index 8a037d5..eae337f 100644 --- a/lib/CurlBuilder.js +++ b/lib/CurlBuilder.js @@ -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); }; diff --git a/lib/CurlBuilder.toffee b/lib/CurlBuilder.toffee index 659486f..0cbdfc2 100644 --- a/lib/CurlBuilder.toffee +++ b/lib/CurlBuilder.toffee @@ -24,17 +24,28 @@ class CurlBuilder throw new Error 'the cURL is closed.' @running = true + + # pop arguments (url, [options = {}], callback) cb = args.pop() - [url, options] = args - @url = url - options ?= {} + [@url, @options] = args + @options ?= {} c = @curl_ - options['URL'] = url c.chunks = [] 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) -> curl.log "receive #{chunk.length} bytes" @@ -73,7 +84,7 @@ class CurlBuilder curl.setDefaultOptions = (options = {}, reset = true) -> - defaultOptions = options + @defaultOptions = options if reset @log 'Set default options and reset cURL' @reset() @@ -81,6 +92,7 @@ class CurlBuilder curl.log = (text) -> if @debug console.info "[cURL #{@id}] " + text + curl.setOptions = (options = {}) -> for own k, v of options @log "Set option '#{k}' to '#{v}'" @@ -111,7 +123,7 @@ class CurlBuilder @log "opening." @curl_ = new Curl() @curl_.options = {} - @setOptions defaultOptions + @defaultOptions = defaultOptions ? {} CurlBuilder.curls[curl.id] = curl @log "opened." @@ -121,10 +133,9 @@ class CurlBuilder @end() @open() - curl.Curl = Curl - curl.Builder = CurlBuilder curl.create = (defaultOptions) -> CurlBuilder.create(defaultOptions) + curl.get_count = -> Curl.get_count()