Struct.new(:app, :path_info, :frequency, :backend, :input, :pos, :seen, :content_length, :upload_id, :mtime)
the underlying middlware for for wrapping env, this should typically be installed before other middlewares that may wrap env in the middleware chain.
# File lib/upr/input_wrapper.rb, line 14 14: def initialize(app, options = {}) 15: super(app, 16: Array(options[:path_info] || nil), 17: options[:frequency] || 1, 18: options[:backend]) 19: 20: # support :drb for compatibility with mongrel_upload_progress 21: if options[:drb] 22: backend and raise ArgumentError, ":backend and :drb are incompatible" 23: require 'drb' 24: DRb.start_service 25: self.backend = DRbObject.new(nil, options[:drb]) 26: elsif String === backend 27: # allow people to use strings in case their backend gets 28: # lazy-loaded (like an ActiveRecord model) 29: self.backend = eval(backend) 30: else 31: self.backend ||= Upr::Monitor.new 32: end 33: end
# File lib/upr/input_wrapper.rb, line 51 51: def _call(env, uid, length) 52: self.upload_id = uid 53: self.mtime = self.pos = self.seen = 0 54: self.input = env["rack.input"] 55: env["rack.input"] = self 56: self.content_length = length 57: backend.start(upload_id, length) 58: 59: app.call(env) 60: end
# File lib/upr/input_wrapper.rb, line 72 72: def _finish 73: self.seen = backend.finish(upload_id).seen 74: self.content_length ||= self.seen 75: end
# File lib/upr/input_wrapper.rb, line 62 62: def _incr(nr) 63: self.pos += nr 64: _finish if content_length && pos >= content_length 65: if (nr = pos - seen) > 0 && mtime <= (Time.now.to_i - frequency) 66: backend.incr(upload_id, nr) 67: self.seen = pos 68: self.mtime = Time.now.to_i 69: end 70: end
# File lib/upr/input_wrapper.rb, line 35 35: def call(env) 36: if path_info.empty? || path_info.include?(env["PATH_INFO"]) 37: # benefit curl users... 38: /\A100-continue\z/ =~ env['HTTP_EXPECT'] and return [ 100, {}, [] ] 39: 40: length = env["CONTENT_LENGTH"] and length = length.to_i 41: chunked = env["TRANSFER_ENCODING"] =~ %{\Achunked\z} and length = nil 42: if chunked || (length && length > 0) 43: if uid = extract_upload_id(env) 44: return dup._call(env, uid, length) 45: end 46: end 47: end 48: app.call(env) 49: end
# File lib/upr/input_wrapper.rb, line 105 105: def each(&block) 106: input.each do |chunk| # usually just a line 107: _incr(chunk.size) 108: yield chunk 109: end 110: _finish 111: end
# File lib/upr/input_wrapper.rb, line 93 93: def gets 94: rv = input.gets 95: rv.nil? ? _finish : _incr(rv.size) 96: rv 97: end
# File lib/upr/input_wrapper.rb, line 99 99: def read(*args) 100: rv = input.read(*args) 101: rv.nil? || rv.size == 0 ? _finish : _incr(rv.size) 102: rv 103: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.