Skip to content
Tcl Source Code View Ticket
Login

View Ticket

http ignores charset in meta tags

Closed / Wont Fix839289ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-11-10 16:00:39
Many web pages (incl. Tcl'ers Wiki) place charset
information in metatags, like this:

<html>
<head>
<meta http-equiv="Content-type" content="text/html;
charset=utf-8"></meta>
<title>Graffiti</title>

But the http package ignores these and assumes the text
is in iso8859-1.  This means that non-ascii characters
are mangled with something as simple as:

set token [http::geturl
http://alphatcl.sourceforge.net/wikit/10]
set contents [http::data $token]

'$contents' should now contain a spanish n~ and an
o-umlaut.  It actually contains garbage for both instead:

spanish ene: ñ</p><p>o-umlaut: ö
dgpplain2003-11-10 23:56:26
Logged In: YES 
user_id=80530


Isn't this issue something to be
taken care of by the application,
and not the http package?
vincentdarleyplain2003-11-11 01:04:20
Logged In: YES 
user_id=32170

I think it may be too late by the time an application sees
the data.  The data is in utf8 and has been interpreted as
iso8859-1 and then possibly dumped down a channel.  It's not
clear to me that is a non-destructive/reversible operation.

In any case, the http package is documented to handle
charsets and convert to utf-8.  I quote:

"charset 
The value of the charset attribute from the Content-Type
meta-data value. If none was specified, this defaults to the
RFC standard iso8859-1, or the value of
$::http::defaultCharset. Incoming text data will be
automatically converted from this charset to utf-8. "
dgpplain2003-11-11 01:16:57
Logged In: YES 
user_id=80530


Perhaps an application that
wants to support http-equiv
meta tags in HTML content
will need to use the -binary
and -progress options
of [geturl] then?

The documented handling of
charset refers to the HTTP
header, AIUI, not to any special
directive embedded in the content.
Keep in mind that many kinds of
content can be transferred using
the HTTP protocol, not just HTML.
It seems incorrect to me to put
HTML-specific handling into the
http package.
vincentdarleyplain2003-11-11 01:34:52
Logged In: YES 
user_id=32170

You may well be right (my knowledge/understanding of what
ought to be correct here is certainly lacking!), but it
doesn't seem a bit against the spirit of Tcl to have to go
through all that hassle.

If we add '-binary', this also means we are explicitly
disabling the documented charset conversion the package does
via http headers! This would mean an application would
actually need to check both the http headers and the
embedded http-equiv stuff!  Possible, but a bit messy, really.
dkfplain2003-11-11 02:38:39
Logged In: YES 
user_id=79902

All is not lost. Try this:
encoding convertfrom identity \
        {{spanish ene: ñ</p><p>o-umlaut: ö}}
hobbsplain2003-11-11 04:19:50
Logged In: YES 
user_id=72656

The http package handles the charset parameter correct 
when it is in the headers.  The stuff in the doc itself is left up 
to the user, and the correct options will handle it right.
vincentdarleyplain2003-11-11 15:39:30
Logged In: YES 
user_id=32170

Jeff, what do you mean by 'the correct options will handle it right'?  I 
understand now that I was expecting too much from http::geturl, but 
now it's not clear how to get what I want.

In the above example,

upvar #0 $token state
puts $state(charset)

gives iso8859-1 (the default) which I suppose I must ignore. However, 
I also suppose it means the text has been read as iso8859-1 (which 
could surely cause some irretrievable loss, at least where the true 
encoding is something more complex than utf-8).

Anyway, I see three approaches:

(i) take the returned text, scan it for <head><meta> etc, extra the 
charset and convert it (but from what? -- the information in $contents 
is utf-8 which has been read as iso8859-1!!)

(ii) always use -binary 1 with http::geturl and then manually examine 
both the headers and (when those headers indicate a text file) convert 
the text as appropriate

(iii) use a normal http::geturl, but with a callback which looks for <
head><meta> and charset tags in the case where the info is textual, 
and in that case adjusts the encoding of the channel on the fly 
(presumably would need to adjust the encoding of the both the input 
socket and the output channel).

Any advice on how this should work? 

What most concerns me is the aspect of information loss in (i) if the 
original text is in an encoding which gets munged if its bytes are 
interpreted as iso8859-1.
dkfplain2003-11-12 01:30:45
Logged In: YES 
user_id=79902

ISO8859-1 is a single byte encoding that gives a meaning to
every byte, so you've not lost any information *provided*
you can figure out how to un-mess-up the encodings (there is
a bijective - and hence invertible - mapping between
incoming bytes and characters in the string you get out of
the [http::data].)  The first thing to do is to go from the
UTF-8 rep of the 8859-1-encoded data so you end up with a
byte-sequence again.  Once you've done that, you can then
convert from the byte sequence to the real encoding.  This
should work fine for most encodings which map the printable
ASCII subset in similar ways to 8859-1 (i.e. so that you can
work out what the <META> tag says!) but there are outliers
(EBCDIC), but in that case you've got an intractable problem
that should probably be reported to the maintainer of the
website...

IMHO, putting encoding info inside a <META> tag is broken by
design anyway since there's no way to bound the amount of
arbitrarily-encoded information read from the HTTP
connection before the encoding switch happens.  XML does a
more sensible job of this bit at least.

(Understanding <META> is a job for an HTML parser, which the
HTTP package isn't.  You might want to check whether the
HTML parser in tcllib does the right thing though...)
hobbsplain2003-11-12 01:43:35
Logged In: YES 
user_id=72656

The other option is to use -binary, which will ensure no 
encoding translation is done until you do it, which is OK since 
all the charset stuff you want is defined in ASCII-7, which will 
be easy to find.
Log in (anonymous is fine) to add a comment.