Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 144 lines (125 sloc) 5.339 kb
8096d5f @miloops Initial commit.
authored
1 # This software code is made available "AS IS" without warranties of any
2 # kind. You may copy, display, modify and redistribute the software
3 # code either by itself or as incorporated into your code; provided that
4 # you do not remove any proprietary notices. Your use of this software
5 # code is at your own risk and you waive any claim against Amazon
6 # Digital Services, Inc. or its affiliates with respect to your use of
7 # this software code. (c) 2006 Amazon Digital Services, Inc. or its
8 # affiliates.
9 #
10 # This software code is made available "AS IS" without warranties of any
11 # kind. You may copy, display, modify and redistribute the software
12 # code either by itself or as incorporated into your code; provided that
13 # you do not remove any proprietary notices. Your use of this software
14 # code is at your own risk and you waive any claim against the author
15 # with respect to your use of this software code.
16 # (c) 2007 s3sync.net
17 #
18 require 'S3'
19 require 'HTTPStreaming'
20
21 # The purpose of this file is to overlay the S3 library from AWS
22 # to add some functionality
23 # (without changing the file itself or requiring a specific version)
24 # It still isn't perfectly robust, i.e. if radical changes are made
25 # to the underlying lib this stuff will need updating.
26
27 module S3
28 class AWSAuthConnection
29
30 def make_http(bucket='', host='', proxy_host=nil, proxy_port=nil, proxy_user=nil, proxy_pass=nil)
31
32 # build the domain based on the calling format
33 server = ''
34 if host != ''
35 server = host
36 elsif bucket.empty?
37 # for a bucketless request (i.e. list all buckets)
38 # revert to regular domain case since this operation
39 # does not make sense for vanity domains
40 server = @server
41 elsif @calling_format == CallingFormat::SUBDOMAIN
42 server = "#{bucket}.#{@server}"
43 elsif @calling_format == CallingFormat::VANITY
44 server = bucket
45 else
46 server = @server
47 end
48 # automatically does the right thing when no proxy
49 http = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).new(server, @port)
50 #http = Net::HTTP.new(server, @port)
51 http.use_ssl = @is_secure
52 http.verify_mode=@verify_mode
53 http.ca_file=@ca_file
54 http.ca_path=@ca_path
55 http.start
56 return http
57 end
58
59 # add support for streaming the response body to an IO stream
60 alias __make_request__ make_request
61 def make_request(method, bucket='', key='', path_args={}, headers={}, data='', metadata={}, streamOut=nil)
62 # build the path based on the calling format
63 path = ''
64 if (not bucket.empty?) and (@calling_format == CallingFormat::REGULAR)
65 path << "/#{bucket}"
66 end
67 # add the slash after the bucket regardless
68 # the key will be appended if it is non-empty
69 path << "/#{key}"
70
71 # build the path_argument string
72 # add the ? in all cases since
73 # signature and credentials follow path args
74 path << '?'
75 path << S3.path_args_hash_to_string(path_args)
76
77 req = method_to_request_class(method).new("#{path}")
78
79 set_headers(req, headers)
80 set_headers(req, metadata, METADATA_PREFIX)
81 set_headers(req, {'Connection' => 'keep-alive', 'Keep-Alive' => '300'})
82
83 set_aws_auth_header(req, @aws_access_key_id, @aws_secret_access_key, bucket, key, path_args)
84
85 http = $S3syncHttp
86
87 if req.request_body_permitted?
88 http.request(req, data, streamOut)
89 else
90 http.request(req, nil, streamOut)
91 end
92 end
93
94 # a "get" operation that sends the body to an IO stream
95 def get_stream(bucket, key, headers={}, streamOut=nil)
96 return GetResponse.new(make_request('GET', bucket, CGI::escape(key), {}, headers, '', {}, streamOut))
97 end
98
99 # a "get" operation that sends the body to an IO stream
100 def get_query_stream(bucket, key, path_args={}, headers={}, streamOut=nil)
101 return GetResponse.new(make_request('GET', bucket, CGI::escape(key), path_args, headers, '', {}, streamOut))
102 end
103
104 def head(bucket, key=nil, headers={})
105 return GetResponse.new(make_request('HEAD', bucket, CGI::escape(key), {}, headers, '', {}))
106 end
107 undef create_bucket
108 def create_bucket(bucket, object)
109 object = S3Object.new(object) if not object.instance_of? S3Object
110 return Response.new(
111 make_request('PUT', bucket, '', {}, {}, object.data, object.metadata)
112 )
113 end
114 # no, because internally the library does not support the header,wait,body paradigm, so this is useless
115 #alias __put__ put
116 #def put(bucket, key, object, headers={})
117 # headers['Expect'] = "100-continue"
118 # __put__(bucket, key, object, headers)
119 #end
120
121
122 # allow ssl validation
123 attr_accessor :verify_mode
124 attr_accessor :ca_path
125 attr_accessor :ca_file
126
127 end
128 module CallingFormat
129 def CallingFormat.string_to_format(s)
130 case s
131 when 'REGULAR'
132 return CallingFormat::REGULAR
133 when 'SUBDOMAIN'
134 return CallingFormat::SUBDOMAIN
135 when 'VANITY'
136 return CallingFormat::VANITY
137 else
138 raise "Unsupported calling format #{s}"
139 end
140 end
141 end
142
143 end
Something went wrong with that request. Please try again.