Module: ModPorter::Filter

Defined in:
lib/mod_porter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



35
36
37
38
39
# File 'lib/mod_porter.rb', line 35

def self.included(base)
  base.superclass_delegating_accessor :mod_porter_secret
  base.before_filter :normalize_mod_porters
  base.extend ModPorter::ClassMethods
end

Instance Method Details

#check_signature!(options) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/mod_porter.rb', line 90

def check_signature!(options)
  expected_digest = Digest::SHA1.digest("#{options[:path]}#{self.class.mod_porter_secret}")
  base64_encoded_digest = ActiveSupport::Base64.encode64(expected_digest).chomp

  if options[:signature] != base64_encoded_digest
    raise ModPorter::InvalidSignature.new("#{options[:signature]} != #{base64_encoded_digest}")
  end
end

#normalize_mod_portersObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mod_porter.rb', line 41

def normalize_mod_porters
  x_uploads_header = request.headers["X-Uploads"] || request.headers["HTTP_X_UPLOADS"]
  return if x_uploads_header.blank?

  porter_params = x_uploads_header.split(",").uniq
  logger.info("Processing #{porter_params.inspect}")

  porter_params.each do |file_param|
    s = StringScanner.new(file_param)

    path = []
    path << s.scan(/\w+/).to_sym

    while !s.eos?
      if arr = s.scan(/\[\]/)
        path << [] # We have an array
      elsif key = s.scan(/\[(\w+)\]/)
        path << s[1].to_sym
      else
        raise ModPorter::UnknownError.new("Something went wrong when scaling the file uploads")
      end
    end

    last = path.pop

    h = path.inject(params) do |hash, value|
      if value.is_a?(Array)
        hash
      else
        hash[value]
      end
    end

    if last.is_a?(Array)
      h.map! do |e|
        check_signature!(e)
        UploadedFile.new(e)
      end
    else
      while h.is_a?(Array)
        h = h.first # WTF. file[][some1], file[][some2]. Maybe this work.
      end

      check_signature!(h[last])
      h[last] = UploadedFile.new(h[last])
    end
  end
end