Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-11-02
12:50
Merge trunk (Update http version number to 2.8.10, due to [838e99a76d]) check-in: 1caeb0f5b9 user: jan.nijtmans tags: novem
12:44
Update http version number to 2.8.10, due to [838e99a76d] check-in: 47bd2d0bbb user: jan.nijtmans tags: core-8-6-branch
12:41
Update http version number to 2.7.14, due to [838e99a76d] check-in: 9a63070856 user: jan.nijtmans tags: core-8-5-branch
2016-10-08
16:43 • Closed ticket [838e99a76d]: treat application/xml as text plus 7 other chang... artifact: 0ef55940fe user: dkf
16:41
[838e99a76d] Ensure that encodings are handled with application/xml and friends. check-in: e3de800d7d user: dkf tags: trunk
16:40
[838e99a76d] Ensure that encodings are handled with application/xml and friends. check-in: 041dc66019 user: dkf tags: core-8-6-branch
16:38
[838e99a76d] Ensure that encodings are handled with application/xml and friends. check-in: 07c269e62a user: dkf tags: core-8-5-branch
15:54 • Ticket [838e99a76d] treat application/xml as text status still Open with 3 o... artifact: 99901ceea6 user: dkf
15:53 • Ticket [838e99a76d]: 4 changes artifact: 9c0ed57e45 user: dkf
15:45 • Ticket [838e99a76d]: 4 changes artifact: 9b4eb1a626 user: dkf
2016-09-26
14:34 • Ticket [838e99a76d]: 4 changes artifact: 85a248f65c user: jan.nijtmans
2016-09-25
16:24 • Ticket [838e99a76d]: 4 changes artifact: 2f37f5bdbc user: gahr
16:05 • Ticket [838e99a76d]: 4 changes artifact: 1daae07366 user: gahr
2016-09-24
19:00 • New ticket [838e99a76d]. artifact: 09db90cdff user: gahr

treat application/xml as text

Closed / Fixed838e99a76dd54f1961b8b5c4bdb3da114bf5b6f8Created 10 years agoUpdated 10 years ago
Descriptionplain2016-09-24 19:00:46
The http package converts the body of the response to the charset specified in the response headers only if content-type matches text*. This patch proposes to treat application/xml in the same way. The rationale is that xml is encoded in utf-8 by default and carries human readable information.
Currently, anybody receiving */xml should decode the stream himself, as suggested in [1733261fffffffffffff]. I find this counterintuitive.

A real-world use case for this is WebDAV, which has deprecated the use of text/xml in favour of application/xml, see rfc 4918 §8.2 [http://www.webdav.org/specs/rfc4918.html#rfc.section.8.2]

I propose the following patch.

Index: library/http/http.tcl
==================================================================
--- library/http/http.tcl
+++ library/http/http.tcl
@@ -1045,11 +1045,15 @@
 
            # We have to use binary translation to count bytes properly.
            fconfigure $sock -translation binary
 
            if {
-               $state(-binary) || ![string match -nocase text* $state(type)]
+               $state(-binary) ||
+               (
+                   ![string match -nocase text* $state(type)] &&
+                   ![string match -nocase application/xml* $state(type)]
+               )
            } {
                # Turn off conversions for non-text data
                set state(binary) 1
            }
            if {[info exists state(-channel)]} {
Referenced by6
  • 1caeb0f5b9Merge trunk (Update http version number to 2.8.10, due to [838e99a76d]) check-in2016-11-02 12:50:26
  • 47bd2d0bbbUpdate http version number to 2.8.10, due to [838e99a76d] check-in2016-11-02 12:44:32
  • 9a63070856Update http version number to 2.7.14, due to [838e99a76d] check-in2016-11-02 12:41:04
  • e3de800d7d[838e99a76d] Ensure that encodings are handled with application/xml and friends. check-in2016-10-08 16:41:51
  • 041dc66019[838e99a76d] Ensure that encodings are handled with application/xml and friends. check-in2016-10-08 16:40:35
  • 07c269e62a[838e99a76d] Ensure that encodings are handled with application/xml and friends. check-in2016-10-08 16:38:42
gahrplain2016-09-25 16:05:00
Some evidence that the current behaviour is weird, to say the least.

Given these two URLs (available for anyone to check out) and the script below:

https://www.ptrcrt.ch/838e99a76d/utf8.html
https://www.ptrcrt.ch/838e99a76d/utf8.xhtml

==================================================
#!/usr/local/bin/tclsh8.6
package require http 2
package require tls

::http::register https 443 ::tls::socket

puts "Tcl version : [info patchlevel]"
puts "http version: [package present http]"
puts {}

set tok [::http::geturl [lindex $argv 0]]
foreach {k v} [::http::meta $tok] {
    puts "$k: $v"
}
puts {}
puts [::http::data $tok]
==================================================

The two outputs are the following:

==================================================
% tclsh8.6 testurl.tcl https://www.ptrcrt.ch/838e99a76d/utf8.html
Tcl version : 8.6.6
http version: 2.8.9

Server: nginx
Date: Sun, 25 Sep 2016 15:58:52 GMT
Content-Type: text/html; charset=utf-8
Last-Modified: Sun, 25 Sep 2016 15:54:18 GMT
Transfer-Encoding: chunked
Connection: close
ETag: W/"57e7f32a-53"
Content-Encoding: gzip

<!DOCTYPE html>
<html><head><title>üäö</title></head><body>üäö</body></html>

==================================================

and

==================================================
% tclsh8.6 testurl.tcl https://www.ptrcrt.ch/838e99a76d/utf8.xhtml
Tcl version : 8.6.6
http version: 2.8.9

Server: nginx
Date: Sun, 25 Sep 2016 15:59:10 GMT
Content-Type: application/xhtml+xml
Content-Length: 289
Last-Modified: Sun, 25 Sep 2016 15:52:43 GMT
Connection: close
ETag: "57e7f2cb-121"
Accept-Ranges: bytes

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>üäö</title></head><body>üäö</body></html>

==================================================

There is no reason why the application/xhtml+xml content shouldn't be correctly decoded by the http package.
Also, my patch above would need to be modified to match application/xml and application/*+xml.
gahrplain2016-09-25 16:24:19
My current conclusion is that the http package should default to utf-8 when dealing with XML content.

Here's a new version of the patch.

Index: library/http/http.tcl
==================================================================
--- library/http/http.tcl
+++ library/http/http.tcl
@@ -1046,7 +1046,9 @@
            # We have to use binary translation to count bytes properly.
            fconfigure $sock -translation binary
 
-           if {
+           if {[regexp {application/(.*\+)?xml} $state(type)]} {
+               set state(charset) utf-8
+           } elseif {
                $state(-binary) || ![string match -nocase text* $state(type)]
            } {
                # Turn off conversions for non-text data
jan.nijtmansplain2016-09-26 14:34:17
Assigning to the http package expert
dkfwiki2016-10-08 15:45:01

Relevant reading: RFC 3023

dkfwiki2016-10-08 15:53:47

It seems from the RFC that if a format with major type application comes through and the minor type is xml, xml-external-parsed-entity, or xml-dtd, or the minor type matches the glob pattern *+xml, then the data should be treated as textual despite being not generally deemed human-readable. (I'm totally OK with having the -binary switch override. In fact, it probably ought to override both ways so that any other weird cases can be handled by users without patching Tcl.)

We probably ought to factor out this bit of decision making into its own procedure. The submitted patch is too simplistic.

dkf Ticket [838e99a76d]: 3 changes [History diff]2016-10-08 15:54:33
dkfwiki2016-10-08 16:43:39

Fixed this bug (8.5, 8.6 and trunk).

Making the -binary switch override properly is a larger project, FRQ-sized. Project for another time...

Log in (anonymous is fine) to add a comment.