From be4cecc5ffa8d334a58284755feff00c747168d5 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Thu, 27 Aug 2026 23:07:42 +0300 Subject: [PATCH] Detect response byte-order marks independently of string encoding --- lib/net/http/response.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index fb48490..35cbd6b 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -466,14 +466,13 @@ def sniff_encoding(str, encoding=nil) # :nodoc: def check_bom(str) - case str.byteslice(0, 2) - when "\xFE\xFF" - return Encoding::UTF_16BE - when "\xFF\xFE" - return Encoding::UTF_16LE - end - if "\xEF\xBB\xBF" == str.byteslice(0, 3) - return Encoding::UTF_8 + case str.getbyte(0) + when 0xFE + return Encoding::UTF_16BE if str.getbyte(1) == 0xFF + when 0xFF + return Encoding::UTF_16LE if str.getbyte(1) == 0xFE + when 0xEF + return Encoding::UTF_8 if str.getbyte(1) == 0xBB && str.getbyte(2) == 0xBF end nil end