@@ -13,6 +13,7 @@ class Connection
1313
1414 # Allowed values for CONNECTION header
1515 KEEP_ALIVE = "Keep-Alive"
16+ # Connection: close header value
1617 CLOSE = "close"
1718
1819 # Attempt to read this much data
@@ -113,10 +114,7 @@ def readpartial(size = BUFFER_SIZE)
113114 return chunk if chunk
114115
115116 eof = read_more ( size ) == :eof
116- if eof && !@parser . finished? && body_framed?
117- close
118- raise ConnectionError , "response body ended prematurely"
119- end
117+ check_premature_eof ( eof )
120118
121119 finished = eof || @parser . finished?
122120 chunk = @parser . read ( size )
@@ -221,6 +219,27 @@ def init_state(options)
221219 @parser = Response ::Parser . new
222220 end
223221
222+ # Check for premature end-of-file and raise if detected
223+ #
224+ # @example
225+ # check_premature_eof(:eof)
226+ #
227+ # @return [void]
228+ # @api private
229+ def check_premature_eof ( eof )
230+ return unless eof && !@parser . finished? && body_framed?
231+
232+ close
233+ raise ConnectionError , "response body ended prematurely"
234+ end
235+
236+ # Check if the response body has a known framing mechanism
237+ #
238+ # @example
239+ # body_framed?
240+ #
241+ # @return [Boolean]
242+ # @api private
224243 def body_framed?
225244 @parser . headers . include? ( Headers ::TRANSFER_ENCODING ) ||
226245 @parser . headers . include? ( Headers ::CONTENT_LENGTH )
0 commit comments