lib/protocol/http/body/rewindable.rb in protocol-http-0.16.2 vs lib/protocol/http/body/rewindable.rb in protocol-http-0.16.3
- old
+ new
@@ -31,12 +31,22 @@
def initialize(body)
super(body)
@chunks = []
@index = 0
+ @digest = nil
end
+ def digest
+ if @digest.nil?
+ @digest = Digest::SHA256.new
+ @chunks.each{|chunk| @digest.update(chunk)}
+ end
+
+ @digest.hexdigest
+ end
+
def empty?
(@index >= @chunks.size) && super
end
# A rewindable body wraps some other body. Convert it to a buffered body
@@ -49,9 +59,10 @@
chunk = @chunks[@index]
@index += 1
else
if chunk = super
@chunks << chunk
+ @digest&.update(chunk)
@index += 1
end
end
# We dup them on the way out, so that if someone modifies the string, it won't modify the rewindability.