Skip to content

Commit

Permalink
A fix for #118
Browse files Browse the repository at this point in the history
When the content-length and the actual data available differ (i.e. there are not enough bytes left to read before EOF), which is an error in any case, the response was different between a ZnStringEntity and a ZnByteArrayEntity. Now they behave the same.

Furthermore, in ZnUtils class>>#streamFrom:to:size: there was a read that did not check the amount read.
  • Loading branch information
Sven Van Caekenberghe committed Aug 31, 2023
1 parent a1e4d39 commit f8c23a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
initialize-release
readFrom: stream

self contentLength
ifNil: [
self bytes: (ZnUtils readUpToEnd: stream limit: (ZnCurrentOptions at: #maximumEntitySize)).
self contentLength: self bytes size
]
ifNotNil: [ | byteArray |

self contentLength: self bytes size ]
ifNotNil: [ | byteArray readCount |
self contentLength > (ZnCurrentOptions at: #maximumEntitySize)
ifTrue: [ ZnEntityTooLarge signal ].
byteArray := ByteArray ofSize: self contentLength.
self contentLength > ZnUtils streamingBufferSize
readCount := self contentLength > ZnUtils streamingBufferSize
ifTrue: [ ZnUtils streamFrom: stream to: byteArray writeStream size: self contentLength ]
ifFalse: [ stream next: self contentLength into: byteArray ].
self bytes: byteArray
]
ifFalse: [ stream readInto: byteArray startingAt: 1 count: self contentLength ].
readCount = self contentLength
ifTrue: [ self bytes: byteArray ]
ifFalse: [ self bytes: (byteArray copyFrom: 1 to: readCount); contentLength: readCount ] ]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ readFrom: stream
readStream := total ifNotNil: [ ZnLimitedReadStream on: stream limit: total ] ifNil: [ stream ].
buffer := String new: (ZnUtils streamingBufferSize min: (total ifNil: [ ZnUtils streamingBufferSize ])).
stringStream := nil.
totalRead := 0.
totalRead := read := 0.
self initializeEncoder.
[ readStream atEnd ] whileFalse: [
[ read := encoder readInto: buffer startingAt: 1 count: buffer size fromStream: readStream ]
Expand All @@ -22,7 +22,7 @@ readFrom: stream
ifTrue: [ ZnEntityTooLarge signal ].
stringStream ifNil: [
readStream atEnd
ifTrue: [ ^ self string: (buffer copyFrom: 1 to: read) ]
ifTrue: [ ^ self string: (buffer copyFrom: 1 to: read); computeContentLength ]
ifFalse: [ stringStream := (total ifNil: [ buffer species new ] ifNotNil: [ buffer species new: total ]) writeStream ] ].
stringStream next: read putAll: buffer startingAt: 1.
ZnUtils signalProgress: totalRead total: total ].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ streamFrom: inputStream to: outputStream size: totalSize
bufferSize := self streamingBufferSize min: totalSize.
buffer := (inputStream isBinary ifTrue: [ ByteArray ] ifFalse: [ String ]) new: bufferSize.
leftToRead := totalSize.
[ leftToRead > 0 ]
whileTrue: [ | readCount |
readCount := bufferSize min: leftToRead.
inputStream next: readCount into: buffer.
[ leftToRead > 0 and: [ inputStream atEnd not ] ]
whileTrue: [ | toReadCount readCount |
toReadCount := bufferSize min: leftToRead.
readCount := inputStream readInto: buffer startingAt: 1 count: toReadCount.
leftToRead := leftToRead - readCount.
outputStream next: readCount putAll: buffer startingAt: 1.
leftToRead > 0
ifTrue: [
self signalProgress: (totalSize - leftToRead) total: totalSize.
outputStream flush ] ]
outputStream flush ] ].
^ totalSize - leftToRead

0 comments on commit f8c23a6

Please sign in to comment.