From f7178045bb11fc3722a98082ed81e1ec39c4940f Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 1 Jan 2024 22:50:32 +1300 Subject: [PATCH] [ruby/openssl] Add support for `gets(chomp: true)`. https://github.com/ruby/openssl/commit/8aa3849cff --- ext/openssl/lib/openssl/buffering.rb | 8 ++++++-- test/openssl/test_pair.rb | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ext/openssl/lib/openssl/buffering.rb b/ext/openssl/lib/openssl/buffering.rb index 9570f14f37..68aa7bc970 100644 --- a/ext/openssl/lib/openssl/buffering.rb +++ b/ext/openssl/lib/openssl/buffering.rb @@ -229,7 +229,7 @@ module OpenSSL::Buffering # # Unlike IO#gets the separator must be provided if a limit is provided. - def gets(eol=$/, limit=nil) + def gets(eol=$/, limit=nil, chomp: false) idx = @rbuffer.index(eol) until @eof break if idx @@ -244,7 +244,11 @@ module OpenSSL::Buffering if size && limit && limit >= 0 size = [size, limit].min end - consume_rbuff(size) + line = consume_rbuff(size) + if chomp && line + line.chomp!(eol) + end + line end ## diff --git a/test/openssl/test_pair.rb b/test/openssl/test_pair.rb index 14786100df..b616883925 100644 --- a/test/openssl/test_pair.rb +++ b/test/openssl/test_pair.rb @@ -115,6 +115,17 @@ module OpenSSL::TestPairM } end + def test_gets_chomp + ssl_pair {|s1, s2| + s1 << "line1\r\nline2\r\nline3\r\n" + s1.close + + assert_equal("line1", s2.gets("\r\n", chomp: true)) + assert_equal("line2\r\n", s2.gets("\r\n", chomp: false)) + assert_equal("line3", s2.gets(chomp: true)) + } + end + def test_gets_eof_limit ssl_pair {|s1, s2| s1.write("hello")