Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/dns/zone/rr/txt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DNS::Zone::RR::TXT < DNS::Zone::RR::Record

def dump
parts = general_prefix
parts << %Q{"#{text}"}
parts << quote_text(text)
parts.join(' ')
end

Expand All @@ -19,4 +19,17 @@ def load(string, options = {})
self
end

protected

# Quotes the given text, e.g. the content of a TXT or SPF record.
# Respects the rule that a single string may contain at most 255 chars, but
# multiple strings can be used to produce longer content. See also RFC 4408,
# section 3.1.3.
#
# @param text [String] the (potentially long) text
# @return [String] the quoted string or, if needed, several quoted strings
def quote_text(text)
text.chars.each_slice(200).map(&:join).map { |chunk| %Q{"#{chunk}"} }.join(' ')
end

end
13 changes: 13 additions & 0 deletions test/rr/txt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ def test_build_rr__txt
assert_equal 'labelname 2w IN TXT "test text"', rr.dump
end

def test_build_multiple_quoted_strings
rr = DNS::Zone::RR::TXT.new

# set a long text with 300 characters
rr.text = '(10 chars)' * 30

# we expect chunks of at most 200 characters
# (but actually implementations leading to at most 255 chars would also be fine)
chunk1 = %Q{"#{'(10 chars)' * 20}"}
chunk2 = %Q{"#{'(10 chars)' * 10}"}
assert_equal "@ IN TXT #{chunk1} #{chunk2}", rr.dump
end

def test_load_rr__txt
rr = DNS::Zone::RR::TXT.new.load('txtrecord IN TXT "test text"')
assert_equal 'txtrecord', rr.label
Expand Down