class Connection
Represents the connection HTTP header, which controls options for the current connection.
The connection header is used to specify control options such as whether the connection should be kept alive, closed, or upgraded to a different protocol.
Definitions
KEEP_ALIVE = "keep-alive"
The keep-alive directive indicates that the connection should remain open for future requests or responses, avoiding the overhead of opening a new connection.
CLOSE = "close"
The close directive indicates that the connection should be closed after the current request and response are complete.
UPGRADE = "upgrade"
The upgrade directive indicates that the connection should be upgraded to a different protocol, as specified in the Upgrade header.
def initialize(value = nil)
Initializes the connection header with the given value. The value is expected to be a comma-separated string of directives.
Signature
-
parameter
valueString | Nil the raw
connectionheader value.
Implementation
def initialize(value = nil)
super(value&.downcase)
end
def <<(value)
Adds a directive to the connection header. The value will be normalized to lowercase before being added.
Signature
-
parameter
valueString the directive to add.
Implementation
def << value
super(value.downcase)
end
def keep_alive?
Signature
-
returns
Boolean whether the
keep-alivedirective is present and the connection is not marked for closure with theclosedirective.
Implementation
def keep_alive?
self.include?(KEEP_ALIVE) && !close?
end
def close?
Signature
-
returns
Boolean whether the
closedirective is present, indicating that the connection should be closed after the current request and response.
Implementation
def close?
self.include?(CLOSE)
end
def upgrade?
Signature
-
returns
Boolean whether the
upgradedirective is present, indicating that the connection should be upgraded to a different protocol.
Implementation
def upgrade?
self.include?(UPGRADE)
end
def self.trailer?
Whether this header is acceptable in HTTP trailers. Connection headers control the current connection and must not appear in trailers.
Signature
-
returns
Boolean false, as connection headers are hop-by-hop and forbidden in trailers.
Implementation
def self.trailer?
false
end