Module: Protocol::HTTP::Header::QuotedString

Defined in:
lib/protocol/http/header/quoted_string.rb

Overview

Handling of HTTP quoted strings.

Constant Summary collapse

QUOTES_REQUIRED =
/[()<>@,;:\\"\/\[\]?={} \t]/

Class Method Summary collapse

Class Method Details

.quote(value, force = false) ⇒ Object

Quote a string for HTTP header values if required.



38
39
40
41
42
43
44
45
# File 'lib/protocol/http/header/quoted_string.rb', line 38

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

.unquote(value, normalize_whitespace = true) ⇒ Object

Unquote a “quoted-string” value according to <tools.ietf.org/html/rfc7230#section-3.2.6>. It should already match the QUOTED_STRING pattern above by the parser.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/protocol/http/header/quoted_string.rb', line 20

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