File: quoted_string.rb

package info (click to toggle)
ruby-protocol-http 0.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 840 kB
  • sloc: ruby: 6,904; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,315 bytes parent folder | download
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