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
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.
module Protocol
module HTTP
# According to https://tools.ietf.org/html/rfc7231#appendix-C
TOKEN = /[!#$%&'*+\-.^_`|~0-9A-Z]+/i
QUOTED_STRING = /"(?:.(?!(?<!\\)"))*.?"/
# https://tools.ietf.org/html/rfc7231#section-5.3.1
QVALUE = /0(\.[0-9]{0,3})?|1(\.[0]{0,3})?/
# Handling of HTTP quoted strings.
module QuotedString
# Unquote a "quoted-string" value according to <https://tools.ietf.org/html/rfc7230#section-3.2.6>. It should already match the QUOTED_STRING pattern above by the parser.
def self.unquote(value, normalize_whitespace = true)
value = value[1...-1]
value.gsub!(/\\(.)/, '\1')
if normalize_whitespace
# LWS = [CRLF] 1*( SP | HT )
value.gsub!(/[\r\n]+\s+/, " ")
end
return value
end
QUOTES_REQUIRED = /[()<>@,;:\\"\/\[\]?={} \t]/
# Quote a string for HTTP header values if required.
#
# @raises [ArgumentError] if the value contains invalid characters like control characters or newlines.
def self.quote(value, force = false)
# Check if quoting is required:
if value =~ QUOTES_REQUIRED or force
"\"#{value.gsub(/["\\]/, '\\\\\0')}\""
else
value
end
end
end
end
end
|