1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2019-2025, by Samuel Williams.
# Copyright, 2024, by Thomas Morgan.
require_relative "split"
module Protocol
module HTTP
module Header
# 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.
class Connection < Split
# The `keep-alive` directive indicates that the connection should remain open for future requests or responses, avoiding the overhead of opening a new connection.
KEEP_ALIVE = "keep-alive"
# The `close` directive indicates that the connection should be closed after the current request and response are complete.
CLOSE = "close"
# The `upgrade` directive indicates that the connection should be upgraded to a different protocol, as specified in the `Upgrade` header.
UPGRADE = "upgrade"
# Initializes the connection header with the given value. The value is expected to be a comma-separated string of directives.
#
# @parameter value [String | Nil] the raw `connection` header value.
def initialize(value = nil)
super(value&.downcase)
end
# Adds a directive to the `connection` header. The value will be normalized to lowercase before being added.
#
# @parameter value [String] the directive to add.
def << value
super(value.downcase)
end
# @returns [Boolean] whether the `keep-alive` directive is present and the connection is not marked for closure with the `close` directive.
def keep_alive?
self.include?(KEEP_ALIVE) && !close?
end
# @returns [Boolean] whether the `close` directive is present, indicating that the connection should be closed after the current request and response.
def close?
self.include?(CLOSE)
end
# @returns [Boolean] whether the `upgrade` directive is present, indicating that the connection should be upgraded to a different protocol.
def upgrade?
self.include?(UPGRADE)
end
# Whether this header is acceptable in HTTP trailers.
# Connection headers control the current connection and must not appear in trailers.
# @returns [Boolean] `false`, as connection headers are hop-by-hop and forbidden in trailers.
def self.trailer?
false
end
end
end
end
end
|