Class: Aws::Plugins::ChecksumAlgorithm::AwsChunkedTrailerDigestIO Private

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-sdk-core/plugins/checksum_algorithm.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wrapper for request body that implements application-layer chunking with Digest computed on chunks + added as a trailer

Constant Summary collapse

CHUNK_SIZE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

16_384

Instance Method Summary collapse

Constructor Details

#initialize(io, algorithm, location_name) ⇒ AwsChunkedTrailerDigestIO

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AwsChunkedTrailerDigestIO.



471
472
473
474
475
476
477
# File 'lib/aws-sdk-core/plugins/checksum_algorithm.rb', line 471

def initialize(io, algorithm, location_name)
  @io = io
  @location_name = location_name
  @algorithm = algorithm
  @digest = ChecksumAlgorithm.digest_for_algorithm(algorithm)
  @trailer_io = nil
end

Instance Method Details

#read(length, buf = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/aws-sdk-core/plugins/checksum_algorithm.rb', line 496

def read(length, buf = nil)
  # account for possible leftover bytes at the end, if we have trailer bytes, send them
  if @trailer_io
    return @trailer_io.read(length, buf)
  end

  chunk = @io.read(length)
  if chunk
    @digest.update(chunk)
    application_chunked = "#{chunk.bytesize.to_s(16)}\r\n#{chunk}\r\n"
    return StringIO.new(application_chunked).read(application_chunked.size, buf)
  else
    trailers = {}
    trailers[@location_name] = @digest.base64digest
    trailers = trailers.map { |k,v| "#{k}:#{v}" }.join("\r\n")
    @trailer_io = StringIO.new("0\r\n#{trailers}\r\n\r\n")
    chunk = @trailer_io.read(length, buf)
  end
  chunk
end

#rewindObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



492
493
494
# File 'lib/aws-sdk-core/plugins/checksum_algorithm.rb', line 492

def rewind
  @io.rewind
end

#sizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

the size of the application layer aws-chunked + trailer body



480
481
482
483
484
485
486
487
488
489
490
# File 'lib/aws-sdk-core/plugins/checksum_algorithm.rb', line 480

def size
  # compute the number of chunks
  # a full chunk has 4 + 4 bytes overhead, a partial chunk is len.to_s(16).size + 4
  orig_body_size = @io.size
  n_full_chunks = orig_body_size / CHUNK_SIZE
  partial_bytes = orig_body_size % CHUNK_SIZE
  chunked_body_size = n_full_chunks * (CHUNK_SIZE + 8)
  chunked_body_size += partial_bytes.to_s(16).size + partial_bytes + 4 unless  partial_bytes.zero?
  trailer_size = ChecksumAlgorithm.trailer_length(@algorithm, @location_name)
  chunked_body_size + trailer_size
end