Overview
| Artifact ID: | c8a90c49f96ff6cdbbcfaa75c9b53775864b512828b5a2b9af3637e4d1413780 |
|---|---|
| Ticket: | 2da8d6fb3d26031a8327b6bcda91f4fd158b71a8
http doesn't respect charset for some content-types |
| User & Date: | andrew.brooks on 2018-11-08 18:08:26 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/plain"
- 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. 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 changed to: "8.6.8"
- is_private changed to: "0"
- login: "andrew.brooks"
- priority changed to: "5 Medium"
- private_contact changed to: "d2db30f3ad8e328ce0ed2fb6de7c0b52c59b2acd"
- resolution changed to: "None"
- severity changed to: "Minor"
- status changed to: "Open"
- submitter changed to: "andrew.brooks"
- subsystem changed to: "29. http Package"
- title changed to:
http doesn't respect charset for some content-types
- type changed to: "Bug"