Re: Special special chars in XML Response

Gaetano Giunta <[email protected]> Thu, 20 Sep 2007 00:12:27 +0200
Newsgroups gmane.comp.php.xml-rpc
Message-ID <[email protected]>
Ok, I have seen that line 922 is actually line 932 on my version of the l=
ib.

This hints to the fact that you are writing an xmlrpcserver.=20
xmlrpc_defencoding has nothing to do with the problem.

The patch I would recommend to xmlrpcs.inc is the following:
            if (!in_array($GLOBALS['xmlrpc_internalencoding'],=20
array('UTF-8', 'ISO-8859-1', 'US-ASCII')))
            {
                xml_parser_set_option($parser,=20
XML_OPTION_TARGET_ENCODING, 'UTF-8');
            }
            else
            {
                xml_parser_set_option($parser,=20
XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']);
            }

What this patch does is that
- it makes sure that no warning is emitted
- most importantly, it makes sure the charset encoding of the data as=20
seen by the user code is not dependent on the encoding of data received=20
over the net (as opposed to just prepending an @ in front of =20
xml_parser_set_option)
- it picks the charset encoding with the widest range, to avoid data loss

This means that, when $xmlrpc_internalencoding is set to a charset other=20
than the 3 allowed, incoming data will always be in UTF8.
It is up to your code to treat it appropriately in xmlrpc method=20
handlers (eg. via utf8_decode or using mbstring for UTF8 -> CP1252=20
translation).

Bye
Gaetano
> The answer was clearly given without enough thinking...
>
> The first question is: are you using the lib to write the client, the=20
> server or both?
>
> Then some explanations:
> - on line 922, if the server has received some CP1252 text, it should=20
> default to $GLOBALS['xmlrpc_defencoding']=3D'UTF-8'. Did you also chang=
e=20
> that variable? otherwise I cannot explain it...
> - are you using php 4 or 5? there are some differences between the xml=20
> parser use by php
> - there is some more work surely to be done for everything to work=20
> fine. Setting internalencoding to CP1252 before emitting (encoding)=20
> data is fine, but, as you have seen, it cannot be used when decoding=20
> it. And both server and client decode data (request and response,=20
> respectively). Since cp1252 is not supported by the php4 xml parser,=20
> we have to find some workaround
>
> Bye
> Gaetano
>
>> Hi Gaetano,
>>
>> thank you for your fast reply and advice! I implemented the steps as=20
>> you described, but when setting=20
>> $GLOBALS['xmlrpc_internalencoding']=3D'CP1252'; I am now getting the=20
>> following error:
>>
>> Warning:  xml_parser_set_option() [function.xml-parser-set-option]:=20
>> Unsupported target encoding &quot;CP1252&quot; in=20
>> ...\module_xmlrpc\lib\xmlrpcs.inc on line 922
>>
>> The PHP documentation says the only support ISO-8859-1, US-ASCII and=20
>> UTF-8: http://de3.php.net/xml_parser_set_option
>>
>> How can I further tackle this issue?
>>
>> Thanks and best regards,
>> Matthias Korn
>>
>> Gaetano Giunta schrieb:
>>> The characters you are sending are very likely part of  the windows =20
>>> charset, aka, cp 1252.
>>> There is no support for that right now, but it is quite is easy to=20
>>> add it:
>>>
>>> in xmlrpc.inc, on line  152,  an array is already defined with the=20
>>> necessary translation. Using array_keys() and array_values() on it,=20
>>> you can modify function xmlrpc_encode_entitites(), adding a new case:
>>> case 'CP1252_US-ASCII':
>>>                $escaped_data =3D str_replace(array('&', '"', "'", '<'=
,=20
>>> '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
>>>                $escaped_data =3D=20
>>> str_replace($GLOBALS['xml_iso88591_Entities']['in'],=20
>>> $GLOBALS['xml_iso88591_Entities']['out'], $escaped_data);
>>>                $escaped_data =3D=20
>>> str_replace(array_keys(array_keys($GLOBALS['$cp1252_to_xmlent'])),=20
>>> array_values($GLOBALS['$cp1252_to_xmlent']), $escaped_data);
>>>                break;
>>>
>>> then of course you have to declare your internal encoding as CP1252
>>> ... and maybe check out if there is any decoding function to be=20
>>> patched...
>>>
>>> bye
>>> Gaetano
>>>
>>>> Hi,
>>>>
>>>> I have an encoding problem of some sort. The data (strings) I'm=20
>>>> sending through xmlresp contains some really nasty characters (e.g.=20
>>>> =E2=80=A2 =E2=80=9E =E2=80=9C =E2=80=A6) and breaks the XML parser o=
n the client side. Most of the=20
>>>> characters get automatically converted to their corresponding XML=20
>>>> entities by you library, but not those listed above.
>>>>
>>>> How can I convert them so that my XML parser doesn't break? (I can=20
>>>> verify it's broken in Internet Explorer, which probably uses the=20
>>>> same parser)
>>>>
>>>>
>>>> Best regards,
>>>> Matthias Korn
>>>> _______________________________________________
>>>> phpxmlrpc mailing list
>>>> [email protected]
>>>> http://lists.usefulinc.com/cgi-bin/mailman/listinfo/phpxmlrpc
>>>>
>>>
>>
>>
>
>