Module: EC2::Common::HTTP

Defined in:
lib/ec2/common/http.rb

Defined Under Namespace

Classes: Error, Response

Constant Summary collapse

DEFAULT_SIGV =
EC2::Common::SIGV4
DEFAULT_REGION =
'us-east-1'

Class Method Summary collapse

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.

Raises:

  • (ArgumentError)


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, options={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION)
  raise ArgumentError.new('Bad options in HTTP::delete') unless options.is_a? Hash
  begin
    output = Tempfile.new('ec2-delete-response')
    output.close
    
    arguments = ['-X DELETE']
    arguments << get_arguments(url, bucket, 'DELETE', options, 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

Raises:

  • (ArgumentError)


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, options={}, 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 options.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', options, 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.message, 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.

Raises:

  • (ArgumentError)


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, options={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION)
  raise ArgumentError.new('Bad options in HTTP::head') unless options.is_a? Hash
  begin
    output = Tempfile.new('ec2-head-response')
    output.close
    
    arguments = ['--head']
    arguments << get_arguments(url, bucket, 'HEAD', options, 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.message, 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 + '): '
        message = 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')
            message = content.text unless content.nil?
          end
        else
          if result.response.type =~ /text/
            message << IO.read(outfile)
          end
        end
        raise Error::Transfer.new(synopsis + message, result.response.code)
      end
    else
      synopsis= 'Curl.Error(' + result.status.to_s + '): '
      message = 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?
        message << "\nCurl.Output: " + output.gsub("\n", "\nCurl.Output: ")
      end
      raise Error::Transfer.new(synopsis + message.strip + '.', result.status)
    end
  rescue EC2::Common::Curl::Error => e
    raise Error::Transfer.new(e.message, 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.

Raises:



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, options={}, 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 options.is_a? Hash
  
  begin
    output = Tempfile.new('ec2-put-response')
    output.close

    arguments = [get_arguments(url, bucket, 'PUT', options, 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, options={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION)
  if constraint and File::exists?(constraint)
    putdir_file(url, bucket, constraint, options, user, pass, debug)
  else
    putdir_binary_data(url, bucket, constraint, options, 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

Raises:

  • (ArgumentError)


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, options={}, user=nil, pass=nil, debug=false, sigv=DEFAULT_SIGV, region=DEFAULT_REGION)
  raise ArgumentError.new('Bad options in HTTP::putdir_binary_data') unless options.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', options, 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

Raises:



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, options={}, 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 options.is_a? Hash

  begin
    output = Tempfile.new('ec2-put-response')
    output.close
    arguments = []

    headers = EC2::Common::Headers.new('PUT')
    options.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