Parent

Included Modules

Upr::InputWrapper

the underlying middlware for for wrapping env, this should typically be installed before other middlewares that may wrap env in the middleware chain.

Public Class Methods

new(app, options = {}) click to toggle source
    # 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

Public Instance Methods

_call(env, uid, length) click to toggle source
    # 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
_finish() click to toggle source
    # 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
_incr(nr) click to toggle source
    # 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
call(env) click to toggle source
    # 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
each(&block) click to toggle source
     # 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
gets() click to toggle source
    # 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
read(*args) click to toggle source
     # 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
rewind() click to toggle source
    # File lib/upr/input_wrapper.rb, line 88
88:     def rewind
89:       self.pos = 0
90:       input.rewind
91:     end
size() click to toggle source
    # File lib/upr/input_wrapper.rb, line 77
77:     def size
78:       rv = input.size
79: 
80:       # we had an unknown length and just had to read in everything to get it
81:       if content_length.nil?
82:         _incr(rv - seen)
83:         _finish
84:       end
85:       rv
86:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.