class Aws::Plugins::RequestCompression::CompressionHandler
@api private
def call(context)
def call(context) if should_compress?(context) selected_encoding = request_encoding_selection(context) if selected_encoding if streaming?(context.operation.input) process_streaming_compression(selected_encoding, context) elsif context.http_request.body.size >= context.config.request_min_compression_size_bytes process_compression(selected_encoding, context) end end end with_metric(selected_encoding) { @handler.call(context) } end
def gzip_compress(context)
def gzip_compress(context) compressed = StringIO.new compressed.binmode gzip_writer = Zlib::GzipWriter.new(compressed) if context.http_request.body.respond_to?(:read) update_in_chunks(gzip_writer, context.http_request.body) else gzip_writer.write(context.http_request.body) end gzip_writer.close new_body = StringIO.new(compressed.string) context.http_request.body = new_body end
def process_compression(encoding, context)
def process_compression(encoding, context) case encoding when 'gzip' gzip_compress(context) else raise StandardError, "We currently do not support #{encoding} encoding" end update_content_encoding(encoding, context) end
def process_streaming_compression(encoding, context)
def process_streaming_compression(encoding, context) case encoding when 'gzip' context.http_request.body = GzipIO.new(context.http_request.body) else raise StandardError, "We currently do not support #{encoding} encoding" end update_content_encoding(encoding, context) end
def request_encoding_selection(context)
def request_encoding_selection(context) encoding_list = context.operation.request_compression['encodings'] encoding_list.find { |encoding| RequestCompression::SUPPORTED_ENCODINGS.include?(encoding) } end
def should_compress?(context)
def should_compress?(context) context.operation.request_compression && !context.config.disable_request_compression end
def streaming?(input)
def streaming?(input) if payload = input[:payload_member] # checking ref and shape payload['streaming'] || payload.shape['streaming'] else false end end
def update_content_encoding(encoding, context)
def update_content_encoding(encoding, context) headers = context.http_request.headers if headers['Content-Encoding'] headers['Content-Encoding'] += ", #{encoding}" else headers['Content-Encoding'] = encoding end end
def update_in_chunks(compressor, io)
def update_in_chunks(compressor, io) loop do chunk = io.read(CHUNK_SIZE) break unless chunk compressor.write(chunk) end end
def with_metric(encoding, &block)
def with_metric(encoding, &block) case encoding when 'gzip' Aws::Plugins::UserAgent.metric('GZIP_REQUEST_COMPRESSION', &block) else block.call end end