class AWS::S3::Client

def extract_object_headers resp

def extract_object_headers resp
  meta = {}
  resp.http_response.headers.each_pair do |name,value|
    if name =~ /^x-amz-meta-(.+)$/i
      meta[$1] = [value].flatten.join
    end
  end
  resp.data[:meta] = meta
  if expiry = resp.http_response.headers['x-amz-expiration']
    expiry.first =~ /^expiry-date="(.+)", rule-id="(.+)"$/
    exp_date = DateTime.parse($1)
    exp_rule_id = $2
  else
    exp_date = nil
    exp_rule_id = nil
  end
  resp.data[:expiration_date] = exp_date if exp_date
  resp.data[:expiration_rule_id] = exp_rule_id if exp_rule_id
  restoring = false
  restore_date = nil
  if restore = resp.http_response.headers['x-amz-restore']
    if restore.first =~ /ongoing-request="(.+?)", expiry-date="(.+?)"/
      restoring = $1 == "true"
      restore_date = $2 && DateTime.parse($2)
    elsif restore.first =~ /ongoing-request="(.+?)"/
      restoring = $1 == "true"
    end
  end
  resp.data[:restore_in_progress] = restoring
  resp.data[:restore_expiration_date] = restore_date if restore_date
  {
    'x-amz-version-id' => :version_id,
    'content-type' => :content_type,
    'content-encoding' => :content_encoding,
    'cache-control' => :cache_control,
    'expires' => :expires,
    'etag' => :etag,
    'x-amz-website-redirect-location' => :website_redirect_location,
    'accept-ranges' => :accept_ranges,
  }.each_pair do |header,method|
    if value = resp.http_response.header(header)
      resp.data[method] = value
    end
  end
  if time = resp.http_response.header('Last-Modified')
    resp.data[:last_modified] = Time.parse(time)
  end
  if length = resp.http_response.header('content-length')
    resp.data[:content_length] = length.to_i
  end
  if sse = resp.http_response.header('x-amz-server-side-encryption')
    resp.data[:server_side_encryption] = sse.downcase.to_sym
  end
end