Skip to content

Commit cf31764

Browse files
committed
Send single-line UTF8 strings as string when permitted.
Sending as literal requires counting bytes, which is a pain while testing. And it causes an unnecessary slowdown due to the server roundtrip.
1 parent a00e2e3 commit cf31764

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/net/imap.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ module Net
491491
#
492492
# ==== RFC6855: <tt>UTF8=ACCEPT</tt>, <tt>UTF8=ONLY</tt>
493493
#
494-
# - See #enable for information about support foi UTF-8 string encoding.
494+
# - See #enable for information about support for UTF-8 string encoding.
495495
#
496496
#--
497497
# ==== RFC7888: <tt>LITERAL+</tt>, +LITERAL-+
@@ -1974,7 +1974,10 @@ def enable(*capabilities)
19741974
.join(' ')
19751975
synchronize do
19761976
send_command("ENABLE #{capabilities}")
1977-
return @responses.delete("ENABLED")[-1]
1977+
result = @responses.delete("ENABLED")[-1]
1978+
@utf8_strings ||= result.include? "SMTPUTF8"
1979+
@utf8_strings ||= result.include? "IMAP4REV2"
1980+
result
19781981
end
19791982
end
19801983

@@ -2222,6 +2225,7 @@ def initialize(host, port_or_options = {},
22222225
@port = options[:port] || (options[:ssl] ? SSL_PORT : PORT)
22232226
@tag_prefix = "RUBY"
22242227
@tagno = 0
2228+
@utf8_strings = false
22252229
@open_timeout = options[:open_timeout] || 30
22262230
@idle_response_timeout = options[:idle_response_timeout] || 5
22272231
@parser = ResponseParser.new

lib/net/imap/command_data.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ def send_data(data, tag = nil)
5252
end
5353

5454
def send_string_data(str, tag = nil)
55-
case str
56-
when ""
55+
if str.empty?
5756
put_string('""')
58-
when /[\x80-\xff\r\n]/n
59-
# literal
57+
elsif str.match?(/[\r\n]/n)
58+
# literal, because multiline
6059
send_literal(str, tag)
61-
when /[(){ \x00-\x1f\x7f%*"\\]/n
60+
elsif !str.ascii_only?
61+
if @utf8_strings
62+
# quoted string
63+
send_quoted_string(str)
64+
else
65+
# literal, because of non-ASCII bytes
66+
send_literal(str, tag)
67+
end
68+
elsif str.match?(/[(){ \x00-\x1f\x7f%*"\\]/n)
6269
# quoted string
6370
send_quoted_string(str)
6471
else

0 commit comments

Comments
 (0)