Module: EC2::Common::HTTP
- Defined in:
- lib/ec2/common/http.rb
Defined Under Namespace
Constant Summary collapse
Class Method Summary collapse
-
.delete(url, bucket, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
———————————————————————– Delete the file at the specified url.
-
.get(url, bucket, path = nil, options = {}, user = nil, pass = nil, size = nil, digest = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
———————————————————————– Save the file at specified url, to the local file at specified path.
-
.head(url, bucket, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
———————————————————————– Get the HEAD response for the specified url.
-
.invoke(url, arguments, outfile, debug = false) ⇒ Object
———————————————————————– Invoke curl with arguments given and process results of output file.
-
.put(url, bucket, path, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
———————————————————————– Put the file at the specified path to the specified url.
-
.putdir(url, bucket, constraint, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
———————————————————————– Create a bucket using the specified url and constraints (either a file with location string, or the string itself).
- .putdir_binary_data(url, bucket, binary_data, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
- .putdir_file(url, bucket, path, options = {}, user = nil, pass = nil, debug = false) ⇒ Object
Class Method Details
.delete(url, bucket, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
Delete the file at the specified url.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ec2/common/http.rb', line 128 def HTTP::delete(url, bucket, ={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION) raise ArgumentError.new('Bad options in HTTP::delete') unless .is_a? Hash begin output = Tempfile.new('ec2-delete-response') output.close arguments = ['-X DELETE'] arguments << get_arguments(url, bucket, 'DELETE', , user, pass, sigv, region) arguments << '-o ' + output.path response = HTTP::invoke(url, arguments, output.path, debug) return EC2::Common::HTTP::Response.new(response.code, response.type) ensure output.close(true) GC.start end end |
.get(url, bucket, path = nil, options = {}, user = nil, pass = nil, size = nil, digest = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
Save the file at specified url, to the local file at specified path. The local file will be created if necessary, or overwritten already existing. If specified, the expected digest is compare to that of the retrieved file which gets deleted if the calculated digest does not meet expectations. If no path is specified, and the response is a 200 OK, the content of the response will be returned as a String
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/ec2/common/http.rb', line 238 def HTTP::get(url, bucket, path=nil, ={}, user=nil, pass=nil, size=nil, digest=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION) raise ArgumentError.new('Bad options in HTTP::get') unless .is_a? Hash buffer = nil if path.nil? buffer = Tempfile.new('ec2-get-response') buffer.close path = buffer.path else directory = File.dirname(path) FileUtils.mkdir_p(directory) unless File.exist?(directory) end arguments = [get_arguments(url, bucket, 'GET', , user, pass, sigv, region)] arguments << "--max-filesize #{size}" if size arguments << '-o ' + path begin FileUtils.touch path response = HTTP::invoke(url, arguments, path, debug) body = nil if response.success? if digest obtained = IO.popen("openssl sha1 #{path}") { |io| io.readline.split(/\s+/).last.strip } unless digest == obtained File.delete(path) if File.exists?(path) and not buffer.is_a? Tempfile raise Error::BadDigest.new(path, digest, obtained) end end if buffer.is_a? Tempfile buffer.open; body = buffer.read end else File.delete(path) if File.exist?(path) and not buffer.is_a? Tempfile end return EC2::Common::HTTP::Response.new(response.code, response.type, body) rescue Error::Transfer => e File::delete(path) if File::exist?(path) and not buffer.is_a? Tempfile raise Error::Retrieve.new(e., e.code) ensure if buffer.is_a? Tempfile buffer.close buffer.unlink if File.exists? path GC.start end end end |
.head(url, bucket, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
Get the HEAD response for the specified url.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/ec2/common/http.rb', line 290 def HTTP::head(url, bucket, ={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION) raise ArgumentError.new('Bad options in HTTP::head') unless .is_a? Hash begin output = Tempfile.new('ec2-head-response') output.close arguments = ['--head'] arguments << get_arguments(url, bucket, 'HEAD', , user, pass, sigv, region) arguments << '-o ' + output.path response = HTTP::invoke(url, arguments, output.path, debug) return EC2::Common::HTTP::Response.new(response.code, response.type) rescue Error::Transfer => e raise Error::Retrieve.new(e., e.code) ensure output.close(true) GC.start end end |
.invoke(url, arguments, outfile, debug = false) ⇒ Object
Invoke curl with arguments given and process results of output file
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ec2/common/http.rb', line 72 def HTTP::invoke(url, arguments, outfile, debug=false) begin raise ArgumentError.new(outfile) unless File.exists? outfile result = EC2::Common::Curl.execute("#{arguments.join(' ')} '#{url}'", debug) if result.success? if result.response.success? return result.response else synopsis= 'Server.Error(' + result.response.code.to_s + '): ' = result.stderr + ' ' if result.response.type == 'application/xml' require 'rexml/document' doc = REXML::Document.new(IO.read(outfile)) if doc.root if result.response.redirect? endpoint = REXML::XPath.first(doc, '/Error/Endpoint').text raise Error::Redirect.new(result.response.code, endpoint) end content = REXML::XPath.first(doc, '/Error/Code') unless content.nil? or content.text.empty? synopsis= 'Server.'+ content.text + '(' + result.response.code.to_s + '): ' end content = REXML::XPath.first(doc, '/Error/Message') = content.text unless content.nil? end else if result.response.type =~ /text/ << IO.read(outfile) end end raise Error::Transfer.new(synopsis + , result.response.code) end else synopsis= 'Curl.Error(' + result.status.to_s + '): ' = result.stderr.split("\n").map { |line| if (m = /^curl:\s+(?:\(\d{1,2}\)\s+)*(.*)$/.match(line)) (m.captures[0] == "try 'curl --help' for more information") ? '' : m.captures[0] else line.strip end }.join("\n") output = result.stdout.chomp if debug and not output.empty? << "\nCurl.Output: " + output.gsub("\n", "\nCurl.Output: ") end raise Error::Transfer.new(synopsis + .strip + '.', result.status) end rescue EC2::Common::Curl::Error => e raise Error::Transfer.new(e., e.code) end end |
.put(url, bucket, path, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
Put the file at the specified path to the specified url. The content of the options hash will be passed as HTTP headers. If the username and password options are specified, then the headers will be signed.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/ec2/common/http.rb', line 151 def HTTP::put(url, bucket, path, ={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION) path ||= "/dev/null" raise Error::PathInvalid.new(path) unless path and File::exist?(path) raise ArgumentError.new('Bad options in HTTP::put') unless .is_a? Hash begin output = Tempfile.new('ec2-put-response') output.close arguments = [get_arguments(url, bucket, 'PUT', , user, pass, sigv, region, open(path))] arguments << '-T ' + path arguments << '-o ' + output.path response = HTTP::invoke(url, arguments, output.path, debug) return EC2::Common::HTTP::Response.new(response.code, response.type) ensure output.close(true) GC.start end end |
.putdir(url, bucket, constraint, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
Create a bucket using the specified url and constraints (either a file with location string, or the string itself). The content of the options hash will be passed as HTTP headers. If the username and password options are specified, then the headers will be signed.
177 178 179 180 181 182 183 |
# File 'lib/ec2/common/http.rb', line 177 def HTTP::putdir(url, bucket, constraint, ={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION) if constraint and File::exists?(constraint) putdir_file(url, bucket, constraint, , user, pass, debug) else putdir_binary_data(url, bucket, constraint, , user, pass, debug, sigv, region) end end |
.putdir_binary_data(url, bucket, binary_data, options = {}, user = nil, pass = nil, debug = false, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/ec2/common/http.rb', line 211 def HTTP::putdir_binary_data(url, bucket, binary_data, ={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION) raise ArgumentError.new('Bad options in HTTP::putdir_binary_data') unless .is_a? Hash begin output = Tempfile.new('ec2-put-response') output.close arguments = ["-X PUT"] arguments << "--data-binary \"#{binary_data}\"" arguments << get_arguments(url, bucket, 'PUT', , user, pass, sigv, region, binary_data) arguments << '-o ' + output.path response = HTTP::invoke(url, arguments, output.path, debug) return EC2::Common::HTTP::Response.new(response.code, response.type) ensure output.close(true) GC.start end end |
.putdir_file(url, bucket, path, options = {}, user = nil, pass = nil, debug = false) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/ec2/common/http.rb', line 185 def HTTP::putdir_file(url, bucket, path, ={}, user=nil, pass=nil, debug=false) raise Error::PathInvalid.new(path) unless path and File::exist?(path) raise ArgumentError.new('Bad options in HTTP::putdir_file') unless .is_a? Hash begin output = Tempfile.new('ec2-put-response') output.close arguments = [] headers = EC2::Common::Headers.new('PUT') .each do |name, value| headers.add(name, value) end headers.sign(user, pass, url, bucket) if user and pass arguments << headers.get.map { |name, value| "-H \"#{name}:#{value}\""}.join(' ') arguments << '-T - ' arguments << '-o ' + output.path arguments << " < #{path}" response = HTTP::invoke(url, arguments, output.path, debug) return EC2::Common::HTTP::Response.new(response.code, response.type) ensure output.close(true) GC.start end end |