From e6dfd50f78c0eaca4d59e86e1d8511e95ad44ba3 Mon Sep 17 00:00:00 2001 From: Damir Gainetdinov Date: Fri, 19 Apr 2024 14:23:48 +0200 Subject: [PATCH 1/3] Consider extra empty space in BODYSTRUCTURE --- lib/net/imap/response_parser.rb | 1 + .../body_structure_responses.yml | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/net/imap/response_parser.rb b/lib/net/imap/response_parser.rb index 1aab798f..1892a879 100644 --- a/lib/net/imap/response_parser.rb +++ b/lib/net/imap/response_parser.rb @@ -1157,6 +1157,7 @@ def body_fields def body_fld_param return if NIL? param = {} + shift_token if @token.symbol == T_SPACE lpar name = case_insensitive__string; SP!; param[name] = string while SP? diff --git a/test/net/imap/fixtures/response_parser/body_structure_responses.yml b/test/net/imap/fixtures/response_parser/body_structure_responses.yml index 66029bb6..2a3c32fe 100644 --- a/test/net/imap/fixtures/response_parser/body_structure_responses.yml +++ b/test/net/imap/fixtures/response_parser/body_structure_responses.yml @@ -433,6 +433,77 @@ 4383638 NIL (\"attachment\" (\"filename\" \"test.xml\")) NIL NIL) \"mixed\" (\"boundary\" \"001a1137a5047848e405157ddaa3\") NIL))\r\n" + test_bodystructure_extra_space: + :response: "* 1 FETCH (UID 1 BODYSTRUCTURE (((\"text\" \"plain\" (\"charset\" \"UTF-8\") NIL + NIL \"7bit\" 409 7 NIL NIL NIL)(\"text\" \"html\" (\"charset\" \"UTF-8\") NIL NIL \"7bit\" + 592 10 NIL NIL NIL) \"alternative\" (\"boundary\" \"--==_mimepart_5277b641dcc3_57d5887e8325d8\" + \"charset\" \"UTF-8\") NIL NIL) \"mixed\" (\"boundary\" \"--==_mimepart_5277b64110f79_57d5887e832634\" + \"charset\" \"UTF-8\") NIL NIL))\r\n" + + :expected: !ruby/struct:Net::IMAP::UntaggedResponse + name: FETCH + data: !ruby/struct:Net::IMAP::FetchData + seqno: 1 + attr: + UID: 1 + BODYSTRUCTURE: !ruby/struct:Net::IMAP::BodyTypeMultipart + media_type: MULTIPART + subtype: MIXED + parts: + - !ruby/struct:Net::IMAP::BodyTypeMultipart + media_type: MULTIPART + subtype: ALTERNATIVE + parts: + - !ruby/struct:Net::IMAP::BodyTypeText + media_type: TEXT + subtype: PLAIN + param: + CHARSET: UTF-8 + content_id: + description: + encoding: 7BIT + size: 409 + lines: 7 + md5: + disposition: + language: + location: + extension: + - !ruby/struct:Net::IMAP::BodyTypeText + media_type: TEXT + subtype: HTML + param: + CHARSET: UTF-8 + content_id: + description: + encoding: 7BIT + size: 592 + lines: 10 + md5: + disposition: + language: + location: + extension: + param: + BOUNDARY: --==_mimepart_5277b641dcc3_57d5887e8325d8 + CHARSET: UTF-8 + disposition: + language: + location: + extension: + param: + BOUNDARY: --==_mimepart_5277b64110f79_57d5887e832634 + CHARSET: UTF-8 + disposition: + language: + location: + extension: + raw_data: "* 1 FETCH (UID 1 BODYSTRUCTURE (((\"text\" \"plain\" (\"charset\" \"UTF-8\") NIL NIL + \"7bit\" 409 7 NIL NIL NIL)(\"text\" \"html\" (\"charset\" \"UTF-8\") NIL NIL \"7bit\" + 592 10 NIL NIL NIL) \"alternative\" (\"boundary\" \"--==_mimepart_5277b641dcc3_57d5887e8325d8\" + \"charset\" \"UTF-8\") NIL NIL) \"mixed\" (\"boundary\" \"--==_mimepart_5277b64110f79_57d5887e832634\" + \"charset\" \"UTF-8\") NIL NIL))\r\n" + test_bodystructure_mixed_boundary: :response: "* 2688 FETCH (UID 179161 BODYSTRUCTURE ((\"TEXT\" \"PLAIN\" (\"CHARSET\" \"iso-8859-1\") NIL NIL \"QUOTED-PRINTABLE\" 200 4 NIL NIL NIL)(\"MESSAGE\" From a103fee9c6ab78b683c08a2e26a2275c06c0d98c Mon Sep 17 00:00:00 2001 From: Damir Gainetdinov Date: Tue, 23 Apr 2024 13:21:26 +0200 Subject: [PATCH 2/3] Update lib/net/imap/response_parser.rb Co-authored-by: nicholas a. evans --- lib/net/imap/response_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net/imap/response_parser.rb b/lib/net/imap/response_parser.rb index 1892a879..84f6cf79 100644 --- a/lib/net/imap/response_parser.rb +++ b/lib/net/imap/response_parser.rb @@ -1155,9 +1155,9 @@ def body_fields # RFC3501, RFC9051: # body-fld-param = "(" string SP string *(SP string SP string) ")" / nil def body_fld_param + quirky_SP? # See comments on test_bodystructure_extra_space return if NIL? param = {} - shift_token if @token.symbol == T_SPACE lpar name = case_insensitive__string; SP!; param[name] = string while SP? From 3ed083715a775f774594b85f380356271e892a77 Mon Sep 17 00:00:00 2001 From: Damir Gainetdinov Date: Tue, 23 Apr 2024 13:27:35 +0200 Subject: [PATCH 3/3] Add a comment section to the extra space test --- .../fixtures/response_parser/body_structure_responses.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/net/imap/fixtures/response_parser/body_structure_responses.yml b/test/net/imap/fixtures/response_parser/body_structure_responses.yml index 2a3c32fe..24915c3f 100644 --- a/test/net/imap/fixtures/response_parser/body_structure_responses.yml +++ b/test/net/imap/fixtures/response_parser/body_structure_responses.yml @@ -434,6 +434,10 @@ (\"boundary\" \"001a1137a5047848e405157ddaa3\") NIL))\r\n" test_bodystructure_extra_space: + :comments: | + [GH-271] Responses from some IMAP servers contained extra space like this. + The fix has been used since Jan 2014, seems to be easy, and doesn't seem to + cause any harm. :response: "* 1 FETCH (UID 1 BODYSTRUCTURE (((\"text\" \"plain\" (\"charset\" \"UTF-8\") NIL NIL \"7bit\" 409 7 NIL NIL NIL)(\"text\" \"html\" (\"charset\" \"UTF-8\") NIL NIL \"7bit\" 592 10 NIL NIL NIL) \"alternative\" (\"boundary\" \"--==_mimepart_5277b641dcc3_57d5887e8325d8\"