Class: Protocol::HTTP1::Body::Remainder

Inherits:
HTTP::Body::Readable
  • Object
show all
Defined in:
lib/protocol/http1/body/remainder.rb

Overview

Represents the remainder of the body, which reads all the data from the connection until it is finished.

Constant Summary collapse

BLOCK_SIZE =
1024 * 64

Instance Method Summary collapse

Constructor Details

#initialize(connection, block_size: BLOCK_SIZE) ⇒ Remainder

Initialize the body with the given connection.



18
19
20
21
# File 'lib/protocol/http1/body/remainder.rb', line 18

def initialize(connection, block_size: BLOCK_SIZE)
	@connection = connection
	@block_size = block_size
end

Instance Method Details

#as_jsonObject



67
68
69
70
71
72
# File 'lib/protocol/http1/body/remainder.rb', line 67

def as_json(...)
	super.merge(
		block_size: @block_size,
		state: @connection ? "open" : "closed"
	)
end

#close(error = nil) ⇒ Object

Close the connection.



41
42
43
44
45
# File 'lib/protocol/http1/body/remainder.rb', line 41

def close(error = nil)
	self.discard
	
	super
end

#discardObject

Discard the body, which will close the connection and prevent further reads.



29
30
31
32
33
34
35
36
# File 'lib/protocol/http1/body/remainder.rb', line 29

def discard
	if connection = @connection
		@connection = nil
		
		# Ensure no further requests can be read from the connection, as we are discarding the body which may not be fully read:
		connection.close_read
	end
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/protocol/http1/body/remainder.rb', line 24

def empty?
	@connection.nil?
end

#inspectObject



62
63
64
# File 'lib/protocol/http1/body/remainder.rb', line 62

def inspect
	"#<#{self.class} #{@block_size} byte blocks, #{empty? ? 'finished' : 'reading'}>"
end

#readObject

Read a chunk of data.



50
51
52
53
54
55
56
57
58
59
# File 'lib/protocol/http1/body/remainder.rb', line 50

def read
	@connection&.readpartial(@block_size)
rescue EOFError
	if connection = @connection
		@connection = nil
		connection.receive_end_stream!
	end
	
	return nil
end