Class: CryptKeeperHttp

Inherits:
Object
  • Object
show all
Defined in:
lib/crypt_keeper_http.rb

Constant Summary collapse

GET =

Request Types

"GET"
PUT =
"PUT"
POST =
"POST"
HEAD =
"HEAD"
DELETE =
"DELETE"

Instance Method Summary collapse

Constructor Details

#initialize(address, access_key, secret_key, signature_id) ⇒ CryptKeeperHttp

Returns a new instance of CryptKeeperHttp.



13
14
15
16
17
18
# File 'lib/crypt_keeper_http.rb', line 13

def initialize(address, access_key, secret_key, signature_id)
  @address = address
  @access_key = access_key
  @secret_key = secret_key
  @signature_id = signature_id
end

Instance Method Details

#delete(path, bucket_name, additional_headers = {}) ⇒ Object



35
36
37
# File 'lib/crypt_keeper_http.rb', line 35

def delete(path, bucket_name, additional_headers={})
  request(DELETE, path, bucket_name, nil, additional_headers)
end

#get(path, bucket_name = '', additional_headers = {}) ⇒ Object



27
28
29
# File 'lib/crypt_keeper_http.rb', line 27

def get(path, bucket_name='', additional_headers={})
  request(GET, path, bucket_name, nil, additional_headers)
end

#put(path, bucket_name = '', body = nil, additional_headers = {}) ⇒ Object



31
32
33
# File 'lib/crypt_keeper_http.rb', line 31

def put(path, bucket_name='', body=nil, additional_headers={})
  request(PUT, path, bucket_name, body, additional_headers)
end

#request(type, path, bucket_name = '', body = nil, additional_headers = {}) ⇒ Object



39
40
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
89
# File 'lib/crypt_keeper_http.rb', line 39

def request(type, path, bucket_name='', body=nil, additional_headers={})
  body.rewind if body && body.respond_to?(:rewind)
  bucket_name = "#{bucket_name}." if !bucket_name.empty?

  res = nil
  Net::HTTP.start(@address) do |http|
    req = Net::HTTP.const_get(type.capitalize).new(path)

    content_md5 = ''
    content_type = ''
    x_headers = ''

    if !additional_headers.empty?
      additional_headers.each do |k, v|
        req.add_field(k, v)
        x_headers << "#{k}:#{v}\n" if k[0..5] == "x-goog"
        if type==PUT
          case k
            when 'Content-MD5'
              content_md5 = v
            when 'Content-Type'
              content_type = v
          end
        end
      end
    end

    content_length = 0
    if body
      if body.respond_to? :lstat
        content_length = body.stat.size
        req.body_stream = body
      else
        content_length = body.size
        req.body = body
      end
    end

    time = Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")

    sig_path = !bucket_name.empty? ? "/#{bucket_name.gsub(/[\.]$/, path.gsub(/\?.*$/, ''))}" : "/"
    req.add_field('Host', "#{bucket_name}#{@address}")
    req.add_field('Date', time)
    req.add_field('Content-length', content_length)
    sig = "#{type}\n#{content_md5}\n#{content_type}\n#{time}\n#{x_headers}#{sig_path}"
    req.add_field('Authorization', "#{@signature_id} #{@access_key}:#{signature(sig)}")

    res = http.request(req)
  end
  res ? res.body : nil
end

#signature(message) ⇒ Object



20
21
22
23
24
25
# File 'lib/crypt_keeper_http.rb', line 20

def signature(message)
  utf8_message = message.unpack("C*").pack("U*")
  utf8_key = @secret_key.unpack("C*").pack("U*")
  sha1 = HMAC::SHA1.digest(utf8_key, utf8_message)
  Base64.encode64(sha1).strip
end