|
| 1 | +require(File.expand_path(File.join(File.dirname(__FILE__), 'core'))) |
| 2 | + |
| 3 | +module Fog |
| 4 | + module RiakCS |
| 5 | + |
| 6 | + module MultipartUtils |
| 7 | + require 'net/http' |
| 8 | + |
| 9 | + class Headers |
| 10 | + include Net::HTTPHeader |
| 11 | + |
| 12 | + def initialize |
| 13 | + initialize_http_header({}) |
| 14 | + end |
| 15 | + |
| 16 | + # Parse a single header line into its key and value |
| 17 | + # @param [String] chunk a single header line |
| 18 | + def self.parse(chunk) |
| 19 | + line = chunk.strip |
| 20 | + # thanks Net::HTTPResponse |
| 21 | + return [nil,nil] if chunk =~ /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)\s*(.*)\z/in |
| 22 | + m = /\A([^:]+):\s*/.match(line) |
| 23 | + [m[1], m.post_match] rescue [nil, nil] |
| 24 | + end |
| 25 | + |
| 26 | + # Parses a header line and adds it to the header collection |
| 27 | + # @param [String] chunk a single header line |
| 28 | + def parse(chunk) |
| 29 | + key, value = self.class.parse(chunk) |
| 30 | + add_field(key, value) if key && value |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + def parse(data, boundary) |
| 35 | + contents = data.match(end_boundary_regex(boundary)).pre_match rescue "" |
| 36 | + contents.split(inner_boundary_regex(boundary)).reject(&:empty?).map do |part| |
| 37 | + parse_multipart_section(part) |
| 38 | + end.compact |
| 39 | + end |
| 40 | + |
| 41 | + def extract_boundary(header_string) |
| 42 | + $1 if header_string =~ /boundary=([A-Za-z0-9\'()+_,-.\/:=?]+)/ |
| 43 | + end |
| 44 | + |
| 45 | + private |
| 46 | + def end_boundary_regex(boundary) |
| 47 | + /\r?\n--#{Regexp.escape(boundary)}--\r?\n?/ |
| 48 | + end |
| 49 | + |
| 50 | + def inner_boundary_regex(boundary) |
| 51 | + /\r?\n--#{Regexp.escape(boundary)}\r?\n/ |
| 52 | + end |
| 53 | + |
| 54 | + def parse_multipart_section(part) |
| 55 | + headers = Headers.new |
| 56 | + if md = part.match(/\r?\n\r?\n/) |
| 57 | + body = md.post_match |
| 58 | + md.pre_match.split(/\r?\n/).each do |line| |
| 59 | + headers.parse(line) |
| 60 | + end |
| 61 | + |
| 62 | + if headers["content-type"] =~ /multipart\/mixed/ |
| 63 | + boundary = extract_boundary(headers.to_hash["content-type"].first) |
| 64 | + parse(body, boundary) |
| 65 | + else |
| 66 | + {:headers => headers.to_hash, :body => body} |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + module UserUtils |
| 73 | + def update_riakcs_user(key_id, user) |
| 74 | + response = @s3_connection.put_object('riak-cs', "user/#{key_id}", MultiJson.encode(user), { 'Content-Type' => 'application/json' }) |
| 75 | + if !response.body.empty? |
| 76 | + response.body = MultiJson.decode(response.body) |
| 77 | + end |
| 78 | + response |
| 79 | + end |
| 80 | + |
| 81 | + def update_mock_user(key_id, user) |
| 82 | + if data[key_id] |
| 83 | + if status = user[:status] |
| 84 | + data[key_id][:status] = status |
| 85 | + end |
| 86 | + |
| 87 | + if regrant = user[:new_key_secret] |
| 88 | + data[key_id][:key_secret] = rand(100).to_s |
| 89 | + end |
| 90 | + |
| 91 | + Excon::Response.new.tap do |response| |
| 92 | + response.status = 200 |
| 93 | + response.body = data[key_id] |
| 94 | + end |
| 95 | + else |
| 96 | + Excon::Response.new.tap do |response| |
| 97 | + response.status = 403 |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + module Utils |
| 104 | + def configure_uri_options(options = {}) |
| 105 | + @host = options[:host] || 'localhost' |
| 106 | + @persistent = options[:persistent] || true |
| 107 | + @port = options[:port] || 8080 |
| 108 | + @scheme = options[:scheme] || 'http' |
| 109 | + end |
| 110 | + |
| 111 | + def riakcs_uri |
| 112 | + "#{@scheme}://#{@host}:#{@port}" |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + extend Fog::Provider |
| 117 | + |
| 118 | + service(:provisioning, 'riakcs/provisioning', 'Provisioning') |
| 119 | + service(:usage, 'riakcs/usage', 'Usage') |
| 120 | + |
| 121 | + end |
| 122 | +end |
0 commit comments