REST2: fix Content-Length truncation for non-ASCII JSON responses#419
Closed
marek-veber wants to merge 1 commit into
Closed
REST2: fix Content-Length truncation for non-ASCII JSON responses#419marek-veber wants to merge 1 commit into
marek-veber wants to merge 1 commit into
Conversation
JSON::to_json without utf8=>1 returns a Perl wide-character string. Web::Machine then computes Content-Length via length(), which counts characters rather than bytes. Plack::Handler::FCGI subsequently encodes the wide string to UTF-8 for output (triggering a deprecation warning), producing more bytes than Content-Length declared. HTTP clients reading exactly Content-Length bytes receive a truncated JSON body, causing "Unterminated string" parse errors. Fix: pass utf8=>1 so JSON::to_json returns a UTF-8 byte string. length() then returns the correct byte count, Content-Length matches the actual body size, and no wide-character warning is emitted. Affected: - Resource/Message.pm: comment/correspond success response - Resource/Record/Readable.pm: GET record response Other callers (Util.pm, Record/Writable.pm) already use encode_json, which encodes to UTF-8 bytes by default.
Member
|
The issue is confirmed. We fixed it with a slightly different change. @marek-veber could you confirm this patch also fixes the issue for you? https://github.com/bestpractical/rt/commit/ecf2371323b83d21c350afc51435ad97a5fdd48d.patch The full branch with a test is here: https://github.com/bestpractical/rt/tree/5.0/rest2-content-length-utf8 |
Member
|
5.0/rest2-content-length-utf8 has been merged into 5.0-trunk, closing. thanks for the report! |
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.
Problem
JSON::to_jsonwithoututf8 => 1returns a Perl wide-character string(Unicode code points). Web::Machine computes
Content-Lengthby callinglength()on this string, which counts characters, not bytes. WhenPlack::Handler::FCGI outputs the response it encodes the wide string to
UTF-8, producing more bytes than
Content-Lengthdeclared.HTTP clients reading exactly
Content-Lengthbytes receive a truncatedJSON body. For example, a Czech-language RT installation returns
["Komentáře přidány"](27 characters, 31 UTF-8 bytes) withContent-Length: 27, causing clients to get an unterminated JSON string.Plack::Handler::FCGI emits a deprecation warning for each such response:
Fix
Pass
utf8 => 1toJSON::to_jsonso it returns a UTF-8 byte string.length()then counts bytes correctly,Content-Lengthmatches the actualbody, and no wide-character warning is emitted.
Two callsites affected:
Resource/Message.pm: success response for comment/correspond POSTResource/Record/Readable.pm: GET response for individual recordsOther callers (
Util.pm,Record/Writable.pm) already useencode_json,which encodes to UTF-8 bytes by default — those are correct.
Testing
Reproduced with an RT instance with Czech locale (
Lang: cs). After thefix,
POST /REST/2.0/ticket/:id/commentreturns a complete JSON body andthe FCGI wide-character warning disappears.