Skip to content

Commit 14f848f

Browse files
committed
⚡️ Simplify header-fld-name parser
This speeds up my (earlier) benchmarks by ~15-20% over 0.4.3, and by 22-40% over earlier versions. (FYI: I have not re-run the benchmarks against v0.4.16 or v0.5.0-dev.) NOTE: Previously, Net::IMAP recreated the raw original source string. Now, it returns the decoded astring value. Although this is technically incompatible, it should almost never make a difference: all standard header field names are valid atoms. Where it _does_ make a difference, it should simplify client code, by parsing (normalizing) the result and allowing clients to ignore server inconsistency. This will also allow clients to simply rely on FetchData#header for extracting the result. Without this commit, FetchData#header won't work when servers return quoted or literal strings and the user expected an atom. (https://www.iana.org/assignments/message-headers/message-headers.xhtml)
1 parent 34c1064 commit 14f848f

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

lib/net/imap/response_parser.rb

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,31 +1317,19 @@ def header_list
13171317
# header-fld-name = astring
13181318
#
13191319
# NOTE: Previously, Net::IMAP recreated the raw original source string.
1320-
# Now, it grabs the raw encoded value using @str and @pos. A future
1321-
# version may simply return the decoded astring value. Although that is
1322-
# technically incompatible, it should almost never make a difference: all
1323-
# standard header field names are valid atoms:
1320+
# Now, it returns the decoded astring value. Although this is technically
1321+
# incompatible, it should almost never make a difference: all standard
1322+
# header field names are valid atoms:
13241323
#
13251324
# https://www.iana.org/assignments/message-headers/message-headers.xhtml
13261325
#
1327-
# Although RFC3501 allows any astring, RFC5322-valid header names are one
1328-
# or more of the printable US-ASCII characters, except SP and colon. So
1329-
# empty string isn't valid, and literals aren't needed and should not be
1330-
# used. This is explicitly unchanged by [I18N-HDRS] (RFC6532).
1331-
#
1332-
# RFC5233:
1326+
# See also RFC5233:
13331327
# optional-field = field-name ":" unstructured CRLF
13341328
# field-name = 1*ftext
13351329
# ftext = %d33-57 / ; Printable US-ASCII
13361330
# %d59-126 ; characters not including
13371331
# ; ":".
1338-
def header_fld_name
1339-
assert_no_lookahead
1340-
start = @pos
1341-
astring
1342-
end_pos = @token ? @pos - 1 : @pos
1343-
@str[start...end_pos]
1344-
end
1332+
alias header_fld_name astring
13451333

13461334
# mailbox-data = "FLAGS" SP flag-list / "LIST" SP mailbox-list /
13471335
# "LSUB" SP mailbox-list / "SEARCH" *(SP nz-number) /

test/net/imap/fixtures/response_parser/fetch_responses.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
data: !ruby/struct:Net::IMAP::FetchData
7171
seqno: 10
7272
attr:
73-
"BODY[HEADER.FIELDS (\"Content-Type\")]": "Content-Type: multipart/alternative;\r\n
73+
"BODY[HEADER.FIELDS (Content-Type)]": "Content-Type: multipart/alternative;\r\n
7474
boundary=\"--==_mimepart_66cfb08b4f249_34306b61811e5\"\r\n\r\n"
7575
raw_data: *test_fetch_msg_att_HEADER_FIELDS_quoted
7676

0 commit comments

Comments
 (0)