Class: Protocol::HTTP1::Body::Fixed
- Inherits:
-
HTTP::Body::Readable
- Object
- HTTP::Body::Readable
- Protocol::HTTP1::Body::Fixed
- Defined in:
- lib/protocol/http1/body/fixed.rb
Overview
Represents a fixed length body.
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
- #the length of the body.(lengthofthebody.) ⇒ Object readonly
- #the remaining bytes to read.(remainingbytestoread.) ⇒ Object readonly
Instance Method Summary collapse
- #as_json ⇒ Object
-
#close(error = nil) ⇒ Object
Close the connection.
- #empty? ⇒ Boolean
-
#initialize(connection, length) ⇒ Fixed
constructor
Initialize the body with the given connection and length.
- #inspect ⇒ Object
-
#read ⇒ Object
Read a chunk of data.
Constructor Details
#initialize(connection, length) ⇒ Fixed
Initialize the body with the given connection and length.
17 18 19 20 21 22 |
# File 'lib/protocol/http1/body/fixed.rb', line 17 def initialize(connection, length) @connection = connection @length = length @remaining = length end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
25 26 27 |
# File 'lib/protocol/http1/body/fixed.rb', line 25 def length @length end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
28 29 30 |
# File 'lib/protocol/http1/body/fixed.rb', line 28 def remaining @remaining end |
#the length of the body.(lengthofthebody.) ⇒ Object (readonly)
25 |
# File 'lib/protocol/http1/body/fixed.rb', line 25 attr :length |
#the remaining bytes to read.(remainingbytestoread.) ⇒ Object (readonly)
28 |
# File 'lib/protocol/http1/body/fixed.rb', line 28 attr :remaining |
Instance Method Details
#as_json ⇒ Object
81 82 83 84 85 86 |
# File 'lib/protocol/http1/body/fixed.rb', line 81 def as_json(...) super.merge( remaining: @remaining, state: @connection ? "open" : "closed" ) end |
#close(error = nil) ⇒ Object
Close the connection.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/protocol/http1/body/fixed.rb', line 38 def close(error = nil) if connection = @connection @connection = nil unless @remaining == 0 connection.close_read end end super end |
#empty? ⇒ Boolean
31 32 33 |
# File 'lib/protocol/http1/body/fixed.rb', line 31 def empty? @connection.nil? or @remaining == 0 end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/protocol/http1/body/fixed.rb', line 76 def inspect "#<#{self.class} #{@length} bytes, #{@remaining} remaining, #{empty? ? 'finished' : 'reading'}>" end |
#read ⇒ Object
Read a chunk of data.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/protocol/http1/body/fixed.rb', line 54 def read if @remaining > 0 if @connection # `readpartial` will raise `EOFError` if the connection is finished, or `IOError` if the connection is closed. chunk = @connection.readpartial(@remaining) @remaining -= chunk.bytesize if @remaining == 0 @connection.receive_end_stream! @connection = nil end return chunk end # If the connection has been closed before we have read the expected length, raise an error: raise EOFError, "connection closed before expected length was read!" end end |