Artifacts Associated With Ticket 2da8d6fb3d
Ticket change [c8a90c49f9] (rid 167226) by andrew.brooks on 2018-11-08 18:08:26:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
If a HTTP request made with the http package receives a reply with an explicit charset in the Content-Type header, the charset will only be respected when decoding the reply for certain Content-Types (text/* and several xml types). This behavior is at odds with RFC2068, section 3.7.1 ('HTTP/1.1 recipients MUST respect the charset label provided by the sender; and those user agents that have a provision to "guess" a charset MUST use the charset from the content-type field if they support that charset'). The problem is easy to reproduce: 1. Make a ::http::geturl request to a server that sends a reply with header 'Content-Type: application/json;charset=UTF-8' and a body containing a UTF-8 wide character. 2. Check the response body (the wide character will be mis-encoded). Note that this does not happen if the Content-Type header is 'text;charset=UTF-8' or 'application/xml;charset=UTF-8'. I first noticed this behavior in 8.6.8, but I imagine that it has been around for a while. This patch (written against trunk) for the http package corrects the problem: --- tcl/library/http/http.tcl 2018-11-08 11:11:36.000000000 -0600 +++ fixed/library/http/http.tcl 2018-11-08 11:11:31.000000000 -0600 @@ -748,6 +748,7 @@ -validate boolean } set state(charset) $defaultCharset + set state(explicit_charset) 0 set options { -binary -blocksize -channel -command -handler -headers -keepalive -method -myaddr -progress -protocol -query -queryblocksize @@ -2710,9 +2711,11 @@ {charset\s*=\s*\"((?:[^""]|\\\")*)\"} \ $state(type) -> cs]} { set state(charset) [string map {{\"} \"} $cs] + set state(explicit_charset) 1 } else { - regexp -nocase {charset\s*=\s*(\S+?);?} \ - $state(type) -> state(charset) + set state(explicit_charset) \ + [regexp -nocase {charset\s*=\s*(\S+?);?} \ + $state(type) -> state(charset)] } } content-length { @@ -3320,11 +3323,13 @@ return } - if {!$state(binary)} { + if {!$state(binary) || $state(explicit_charset)} { # If we are getting text, set the incoming channel's encoding # correctly. iso8859-1 is the RFC default, but this could be any - # IANA charset. However, we only know how to convert what we have - # encodings for. + # IANA charset. If we were given an explicit charset in the + # Content-Type header, RFC2068 3.7.1 demands that we respect the + # explicit charset if possible. However, we only know how to + # convert what we have encodings for. set enc [CharsetToEncoding $state(charset)] if {$enc ne "binary"} { - foundin initialized to: "8.6.8"
- is_private initialized to: "0"
- login: "andrew.brooks"
- priority initialized to: "5 Medium"
- private_contact initialized to: "d2db30f3ad8e328ce0ed2fb6de7c0b52c59b2acd"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "andrew.brooks"
- subsystem initialized to: "29. http Package"
- title initialized to:
http doesn't respect charset for some content-types
- type initialized to: "Bug"
Add attachment "http.tcl.patch" [c69d46faa7] (rid 167227) by andrew.brooks on 2018-11-08 18:11:15.
Ticket change [22a7f497a9] (rid 167228) by andrew.brooks on 2018-11-08 18:12:31:
- comment changed to:
If a HTTP request made with the http package receives a reply with an explicit charset in the Content-Type header, the charset will only be respected when decoding the reply for certain Content-Types (text/* and several xml types). This behavior is at odds with RFC2068, section 3.7.1 ('HTTP/1.1 recipients MUST respect the charset label provided by the sender; and those user agents that have a provision to "guess" a charset MUST use the charset from the content-type field if they support that charset'). The problem is easy to reproduce: 1. Make a ::http::geturl request to a server that sends a reply with header 'Content-Type: application/json;charset=UTF-8' and a body containing a UTF-8 wide character. 2. Check the response body (the wide character will be mis-encoded). Note that this does not happen if the Content-Type header is 'text;charset=UTF-8' or 'application/xml;charset=UTF-8'. I first noticed this behavior in 8.6.8, but I imagine that it has been around for a while. I have attached a patch (written against trunk) for the http package that corrects the problem. - icomment: "moved patch to an attachment"
- login: "andrew.brooks"
- mimetype: "text/plain"
- comment changed to:
Ticket change [2cc4a61371] (rid 185213) by kjnash on 2020-07-24 19:36:30:
- icomment:
The similar bug [13657a2dc35] was fixed in Tcl 8.6.10, http 2.9.1 by identifying Content-Type application/json as not binary. This fixes the present bug for me. If it fixes the problem for you I will close the ticket.
- login: "kjnash"
- mimetype: "text/plain"
- icomment:
Ticket change [140d3fd0d3] (rid 190577) by kjnash on 2020-12-31 15:10:15:
- closedate changed to: "2459215.13211954"
- closer changed to: "kjnash"
- login: "kjnash"
- mimetype: "text/plain"
- resolution changed to: "Duplicate"
- status changed to: "Closed"