Sha256: ec97b305b3306013642750538b1d2132f90732b06c6b3c240ce1fdb4ac4390b5

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.

require 'protocol/http/body/readable'

module Protocol
	module HTTP1
		module Body
			class Fixed < HTTP::Body::Readable
				def initialize(stream, length)
					@stream = stream
					@length = length
					@remaining = length
				end
				
				attr :length
				attr :remaining
				
				def empty?
					@remaining == 0
				end
				
				def close(error = nil)
					# If we are closing the body without fully reading it, the underlying connection is now in an undefined state.
					if @remaining != 0
						@stream.close
					end
					
					super
				end
				
				# @raises EOFError if the stream is closed before the expected length is read.
				def read
					if @remaining > 0
						# `readpartial` will raise `EOFError` if the stream is closed/finished:
						if chunk = @stream.readpartial(@remaining)
							@remaining -= chunk.bytesize
							
							return chunk
						# else
						# 	raise EOFError, "Stream closed with #{@remaining} bytes remaining!"
						end
					end
				end
				
				def join
					buffer = @stream.read(@remaining)
					
					@remaining = 0
					
					return buffer
				end
				
				def inspect
					"\#<#{self.class} length=#{@length} remaining=#{@remaining}>"
				end
			end
		end
	end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
protocol-http1-0.19.1 lib/protocol/http1/body/fixed.rb
protocol-http1-0.19.0 lib/protocol/http1/body/fixed.rb
protocol-http1-0.18.0 lib/protocol/http1/body/fixed.rb
protocol-http1-0.17.0 lib/protocol/http1/body/fixed.rb
protocol-http1-0.16.1 lib/protocol/http1/body/fixed.rb
protocol-http1-0.16.0 lib/protocol/http1/body/fixed.rb
protocol-http1-0.15.1 lib/protocol/http1/body/fixed.rb
protocol-http1-0.15.0 lib/protocol/http1/body/fixed.rb