Bug 15555 - XMLHttpRequest does not support charset "x-user-defined", which can facilitate loading of binary data
Summary: XMLHttpRequest does not support charset "x-user-defined", which can facilitat...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: 523.x (Safari 3)
Hardware: Mac OS X 10.4
: P2 Normal
Assignee: Alexey Proskuryakov
URL: http://tlrobinson.net/misc/webkit/bin...
Keywords:
Depends on:
Blocks:
 
Reported: 2007-10-18 02:43 PDT by Tom Robinson
Modified: 2007-10-27 01:52 PDT (History)
1 user (show)

See Also:


Attachments
proposed fix (17.62 KB, patch)
2007-10-25 11:24 PDT, Alexey Proskuryakov
darin: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Robinson 2007-10-18 02:43:22 PDT
Firefox's XMLHttpRequest can load binary data by overriding the MIME type to use "x-user-defined":

req.overrideMimeType("text/plain; charset=x-user-defined");

(http://mgran.blogspot.com/2006/08/downloading-binary-streams-with.html)

WebKit's XMLHttpRequest does not support this charset, so it defaults to another, which results in some of the binary values greater than 127 being mapped to incorrect values (see the red boxes in the test case).
Comment 1 Alexey Proskuryakov 2007-10-18 02:51:30 PDT
I'm wondering if this encoding is of any use outside XHR - most likely, Firefox supports it for all page loads.
Comment 2 Alexey Proskuryakov 2007-10-25 11:24:46 PDT
Created attachment 16860 [details]
proposed fix

This matches Firefox implementation, since I couldn't find MSIE one documented anywhere (this charset is not supposed to be used on the Web, according to MSDN).
Comment 3 Tom Robinson 2007-10-25 11:42:41 PDT
But should it actually match Firefox's implementation? I'm wondering why Firefox returns the correct value OR'd with 0xF700 for values >= 0x80? Is there a good reason for this?
Comment 4 Alexey Proskuryakov 2007-10-25 11:56:09 PDT
So, what does IE do? Is its implementation of x-user-defined different in some way?
Comment 5 Alexey Proskuryakov 2007-10-25 22:41:58 PDT
In IE, responseText is trimmed at null bytes, so it apparently isn't suitable for binary data regardless of its encoding.
Comment 6 Darin Adler 2007-10-26 00:15:01 PDT
Comment on attachment 16860 [details]
proposed fix

+        unsigned char c = bytes[i];
+        characters[i] = (c < 0x80) ? c : 0xf700 + c;

If you used "signed char" you wouldn't need to do as much math:

        signed char signedByte = bytes[i];
        characters[i] = signedByte & 0xF7FF;

+        UChar32 highBits = c & 0xffffff80;
+        if (!highBits || highBits == 0xf780)
+            bytes[resultLength++] = static_cast<char>(c);

And I'd just write this as:

    signed char signedByte = c;
    if (signedByte & 0xF7FF == c)
        bytes[resultLength++] = signedByte;

r=me with or without my suggested optimization
Comment 7 Alexey Proskuryakov 2007-10-27 01:52:39 PDT
Committed revision 27145 with suggested optimizations.

I guess it remains to be seen how (and whether) this change affects HTML documents...