Module: HTTP::Accept::QuotedString

Defined in:
lib/http/accept/quoted_string.rb

Class Method Summary collapse

Class Method Details

.quote(value, force = false) ⇒ Object

Quote a string if required. Doesn’t handle newlines correctly currently.



29
30
31
32
33
34
35
# File 'lib/http/accept/quoted_string.rb', line 29

def self.quote(value, force = false)
	if value =~ /"/ or force
		"\"#{value.gsub(/["\\]/, "\\\\\\0")}\""
	else
		return 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.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/http/accept/quoted_string.rb', line 15

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