Repository navigation
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
fixCharsetwithDetectCharset()relied solely onchardet's statistical detection when theContent-Typeheader has no charset.chardetignores the charset declared in<meta http-equiv="Content-Type">, so pages served as e.g. windows-1251 that declare their encoding only via the meta tag are misdetected and misdecoded (see #777, where a windows-1251 page renders asÑÄÎ «Ïðîìåòåé»).Fix
In
Response.fixCharset, when theContent-Typeheader has no charset, first consultgolang.org/x/net/html/charset.DetermineEncoding, which inspects the BOM, then the HTML<meta>charset declaration, before falling back to its own detection.chardetis kept as a fallback whenDetermineEncodingreturns no name, so the previous behavior is preserved for bodies without any declaration.Testing
New regression test
TestResponseFixCharsetMetaDeclarationinresponse_test.go: a windows-1251 body that declarescharset=windows-1251only via<meta>, with aContent-Type: text/htmlheader (no charset). Before the fix the test fails (body contains mojibakeÐØÕØÝåéì); after the fix it passes (body contains the proper UTF-8 "Прометей").go test . -run TestResponseFixCharset -count=1 -v→ PASSgo test -count=1 ./...→ all packages greengofmt -l .andgo vet ./...→ cleanFixes #777