-
Notifications
You must be signed in to change notification settings - Fork 73
Add HTTP#force_response_body_encoding for forcing response body encoding #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jeremyevans
merged 3 commits into
ruby:master
from
jeremyevans:force_response_body_encoding-2567
Apr 11, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -84,6 +84,7 @@ def initialize(httpv, code, msg) #:nodoc: internal use only | |||||
@read = false | ||||||
@uri = nil | ||||||
@decode_content = false | ||||||
@body_encoding = false | ||||||
end | ||||||
|
||||||
# The HTTP version supported by the server. | ||||||
|
@@ -106,6 +107,18 @@ def initialize(httpv, code, msg) #:nodoc: internal use only | |||||
# Accept-Encoding header from the user. | ||||||
attr_accessor :decode_content | ||||||
|
||||||
# The encoding to use for the response body. If Encoding, use that encoding. | ||||||
# If other true value, attempt to detect the appropriate encoding, and use | ||||||
# that. | ||||||
attr_reader :body_encoding | ||||||
|
||||||
# Set the encoding to use for the response body. If given a String, find | ||||||
# the related Encoding. | ||||||
def body_encoding=(value) | ||||||
value = Encoding.find(value) if value.is_a?(String) | ||||||
@body_encoding = value | ||||||
end | ||||||
|
||||||
def inspect | ||||||
"#<#{self.class} #{@code} #{@message} readbody=#{@read}>" | ||||||
end | ||||||
|
@@ -214,6 +227,17 @@ def read_body(dest = nil, &block) | |||||
end | ||||||
@read = true | ||||||
|
||||||
case enc = @body_encoding | ||||||
when Encoding, false, nil | ||||||
# Encoding: force given encoding | ||||||
# false/nil: do not force encoding | ||||||
else | ||||||
# other value: detect encoding from body | ||||||
enc = detect_encoding(@body) | ||||||
end | ||||||
|
||||||
@body.force_encoding(enc) if enc | ||||||
|
||||||
@body | ||||||
end | ||||||
|
||||||
|
@@ -245,6 +269,141 @@ def body=(value) | |||||
|
||||||
private | ||||||
|
||||||
# :nodoc: | ||||||
def detect_encoding(str, encoding=nil) | ||||||
if encoding | ||||||
elsif encoding = type_params['charset'] | ||||||
elsif encoding = check_bom(str) | ||||||
else | ||||||
encoding = case content_type&.downcase | ||||||
when %r{text/x(?:ht)?ml|application/(?:[^+]+\+)?xml} | ||||||
/\A<xml[ \t\r\n]+ | ||||||
version[ \t\r\n]*=[ \t\r\n]*(?:"[0-9.]+"|'[0-9.]*')[ \t\r\n]+ | ||||||
encoding[ \t\r\n]*=[ \t\r\n]* | ||||||
(?:"([A-Za-z][\-A-Za-z0-9._]*)"|'([A-Za-z][\-A-Za-z0-9._]*)')/x =~ str | ||||||
encoding = $1 || $2 || Encoding::UTF_8 | ||||||
when %r{text/html.*} | ||||||
sniff_encoding(str) | ||||||
end | ||||||
end | ||||||
return encoding | ||||||
end | ||||||
|
||||||
# :nodoc: | ||||||
def sniff_encoding(str, encoding=nil) | ||||||
# the encoding sniffing algorithm | ||||||
# http://www.w3.org/TR/html5/parsing.html#determining-the-character-encoding | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Algorithm also should be updated if needed |
||||||
if enc = scanning_meta(str) | ||||||
enc | ||||||
# 6. last visited page or something | ||||||
# 7. frequency | ||||||
elsif str.ascii_only? | ||||||
Encoding::US_ASCII | ||||||
elsif str.dup.force_encoding(Encoding::UTF_8).valid_encoding? | ||||||
Encoding::UTF_8 | ||||||
end | ||||||
# 8. implementation-defined or user-specified | ||||||
end | ||||||
|
||||||
# :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 | ||||||
end | ||||||
nil | ||||||
end | ||||||
|
||||||
# :nodoc: | ||||||
def scanning_meta(str) | ||||||
require 'strscan' | ||||||
ss = StringScanner.new(str) | ||||||
if ss.scan_until(/<meta[\t\n\f\r ]*/) | ||||||
attrs = {} # attribute_list | ||||||
got_pragma = false | ||||||
need_pragma = nil | ||||||
charset = nil | ||||||
|
||||||
# step: Attributes | ||||||
while attr = get_attribute(ss) | ||||||
name, value = *attr | ||||||
next if attrs[name] | ||||||
attrs[name] = true | ||||||
case name | ||||||
when 'http-equiv' | ||||||
got_pragma = true if value == 'content-type' | ||||||
when 'content' | ||||||
encoding = extracting_encodings_from_meta_elements(value) | ||||||
unless charset | ||||||
charset = encoding | ||||||
end | ||||||
need_pragma = true | ||||||
when 'charset' | ||||||
need_pragma = false | ||||||
charset = value | ||||||
end | ||||||
end | ||||||
|
||||||
# step: Processing | ||||||
return if need_pragma.nil? | ||||||
return if need_pragma && !got_pragma | ||||||
|
||||||
charset = Encoding.find(charset) rescue nil | ||||||
return unless charset | ||||||
charset = Encoding::UTF_8 if charset == Encoding::UTF_16 | ||||||
return charset # tentative | ||||||
end | ||||||
nil | ||||||
end | ||||||
|
||||||
def get_attribute(ss) | ||||||
ss.scan(/[\t\n\f\r \/]*/) | ||||||
if ss.peek(1) == '>' | ||||||
ss.getch | ||||||
return nil | ||||||
end | ||||||
name = ss.scan(/[^=\t\n\f\r \/>]*/) | ||||||
name.downcase! | ||||||
raise if name.empty? | ||||||
ss.skip(/[\t\n\f\r ]*/) | ||||||
if ss.getch != '=' | ||||||
value = '' | ||||||
return [name, value] | ||||||
end | ||||||
ss.skip(/[\t\n\f\r ]*/) | ||||||
case ss.peek(1) | ||||||
when '"' | ||||||
ss.getch | ||||||
value = ss.scan(/[^"]+/) | ||||||
value.downcase! | ||||||
ss.getch | ||||||
when "'" | ||||||
ss.getch | ||||||
value = ss.scan(/[^']+/) | ||||||
value.downcase! | ||||||
ss.getch | ||||||
when '>' | ||||||
value = '' | ||||||
else | ||||||
value = ss.scan(/[^\t\n\f\r >]+/) | ||||||
value.downcase! | ||||||
end | ||||||
[name, value] | ||||||
end | ||||||
|
||||||
def extracting_encodings_from_meta_elements(value) | ||||||
# http://dev.w3.org/html5/spec/fetching-resources.html#algorithm-for-extracting-an-encoding-from-a-meta-element | ||||||
if /charset[\t\n\f\r ]*=(?:"([^"]*)"|'([^']*)'|["']|\z|([^\t\n\f\r ;]+))/i =~ value | ||||||
return $1 || $2 || $3 | ||||||
end | ||||||
return nil | ||||||
end | ||||||
|
||||||
## | ||||||
# Checks for a supported Content-Encoding header and yields an Inflate | ||||||
# wrapper for this response's socket when zlib is present. If the | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name
force_encoding
is intended to indicate that the method is a dirty, low-level operation. On the other hand, I think this method is highly recommended. The name should reflect the difference.Also maybe the given String should be verified when it was set instead of the time when it calls
force_encoding
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added commits to fix both of these issues. I can update NEWS for ruby after this is merged.