Protocol::HTTPSourceProtocolHTTPHeaderQuotedString

module QuotedString

Handling of HTTP quoted strings.

Definitions

def self.unquote(value, normalize_whitespace = true)

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.

Implementation

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

def self.quote(value, force = false)

Quote a string for HTTP header values if required.

Signature

raises ArgumentError

if the value contains invalid characters like control characters or newlines.

Implementation

def self.quote(value, force = false)
	# Check if quoting is required:
	if value =~ QUOTES_REQUIRED or force
		"\"#{value.gsub(/["\\]/, '\\\\\0')}\""
	else
		value
	end
end