From: Stephen D. <sd...@gm...> - 2005-06-10 15:42:31
|
On 6/10/05, Vlad Seryakov <vl...@cr...> wrote: > I thought about ns_return -binary as well. first -binary flag is > optional so it will be compatible with the old API. Let me try this. >=20 > Zoran Vasiljevic wrote: > > > > Am 10.06.2005 um 15:41 schrieb Vlad Seryakov: > > > >> * Tcl_GetByteArrayFromObj -- > >> * > >> * Attempt to get the array of bytes from the Tcl object. If the > >> * object is not already a ByteArray object, an attempt will be > >> * made to convert it to one. > >> > >> So, if i call ns_return from Tcl and pass data parameter, it will be > >> always type of String. ByteArrays are created from Tcl by binary > >> command only, other commands work with String types, so i need to > >> tell ns_return to treat data as bytearray. Tcl string > >> can contain binary data but it is still String and that check will > >> never work for binary data. > >> > > > > I'm afraid you are right. I also have my own (binary-aware) ns_return > > somewhere > > in my toolbox but never thought it would be of general interest. > > > > OTOH, the "ns_return -binary" would look nicer but I do not > > know if we'd be backwardly-compatible (guess not). The *only* thing a Tcl string can contain is valid UTF8. If you're putting something other than UTF8 in there, you've got big problems... Now, sending a UTF8 string directly to the client as a bytearray is effectively the same as sedinding it in the UTF8 encoding, i.e. the client will recieve exactly what was in memory for that string, because no transcoding to some other character set will be occur.=20 There is API for setting the encoding on a per connection basis. The only way to pass a buffer of arbitrary (non-UTF8) bytes to a command is as a bytearray object. You can create these with the [binary] command, by opening a file with -translation binary, or from some extension such as the image example I gave above. That being the case, you should be able to check the type of the object given to ns_return, and act accordingly It's for these reasons that ns_returnbinary and ns_return -binary are fundamentaly broken. |