1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 90 91 92 93 94 95 96 97
|
# frozen_string_literal: true
require 'uri'
require 'cgi'
module Aws
module S3
module Plugins
# This plugin auto-populates the `:encoding_type` request parameter
# to all calls made to Amazon S3 that accept it.
#
# This enables Amazon S3 to return object keys that might contain
# invalid XML characters as URL encoded strings. This plugin also
# automatically decodes these keys so that the key management is
# transparent to the user.
#
# If you specify the `:encoding_type` parameter, then this plugin
# will be disabled, and you will need to decode the keys yourself.
#
# The following operations are managed:
#
# * {S3::Client#list_objects}
# * {S3::Client#list_multipart_uploads}
# * {S3::Client#list_object_versions}
#
class UrlEncodedKeys < Seahorse::Client::Plugin
class Handler < Seahorse::Client::Handler
def call(context)
if context.params.key?(:encoding_type)
@handler.call(context) # user managed
else
manage_keys(context)
end
end
private
def manage_keys(context)
context.params[:encoding_type] = 'url'
@handler.call(context).on_success do |resp|
send("decode_#{resp.context.operation_name}_keys", resp.data)
end
end
def decode_list_objects_keys(data)
decode(:marker, data)
decode(:next_marker, data)
decode(:prefix, data)
decode(:delimiter, data)
data.contents.each { |o| decode(:key, o) } if data.contents
data.common_prefixes.each { |o| decode(:prefix, o) } if data.common_prefixes
end
def decode_list_object_versions_keys(data)
decode(:key_marker, data)
decode(:next_key_marker, data)
decode(:prefix, data)
decode(:delimiter, data)
data.versions.each { |o| decode(:key, o) } if data.versions
data.delete_markers.each { |o| decode(:key, o) } if data.delete_markers
data.common_prefixes.each { |o| decode(:prefix, o) } if data.common_prefixes
end
def decode_list_multipart_uploads_keys(data)
decode(:key_marker, data)
decode(:next_key_marker, data)
decode(:prefix, data)
decode(:delimiter, data)
data.uploads.each { |o| decode(:key, o) } if data.uploads
data.common_prefixes.each { |o| decode(:prefix, o) } if data.common_prefixes
end
def decode(member, struct)
if struct[member]
struct[member] = CGI.unescape(struct[member])
end
end
end
handler(Handler,
step: :validate,
priority: 0,
operations: [
:list_objects,
:list_object_versions,
:list_multipart_uploads,
]
)
end
end
end
end
|