http - invalid pixel in Firefox because of content charset setting in Netty server -


i developing http server netty. on occasions, server must answer 1x1 transparent pixel. hard-coded gif transparent pixel in base64, , returned following code :

string pixel_string= new string (base64.decodebase64("r0lgodlhaqabaaaaach5baekaaealaaaaaabaaeaaaictaeaow==")); httpresponse response = new defaulthttpresponse(httpversion.http_1_1, httpresponsestatus.ok); response.setcontent(channelbuffers.copiedbuffer(pixel_string, charsetutil.utf_8)); 

edit : set content-type : response.setheader(httpheaders.names.content_type, "image/gif"); in chrome, fine. however, firefox tells me cannot display pixel (which pretty bad app), pixel data in invalid. after many investigations, figured out fix, changing charset iso-8859-1.

response.setcontent(channelbuffers.copiedbuffer(             responsebuilder.pixel_string, charsetutil.iso_8859_1)); 

i don't understand why works, makes me think may run troubles in cases. tried change firefox preferences (to have utf8 default), doesn't change much.

why firefox accept iso-8859 encoding, , not utf-8 ? can change ? have clue on origin of issue , how sure work whatever user's setting ?

thanks

it's not firefox that's accepting encoding or not. it's server.

when base64 decode produce string contains characters... produced bytes you're thinking of characters somehow. since java string container holds utf-16 string, in practice you're doing taking each byte, treating a 16-bit integer , constructing utf-16 "string" made of code units.

but when want put on network, have convert string bytes, , argument copiedbuffer says how that. if converting utf-8, character came byte had high bit set end getting encoded two-byte utf-8 sequence. on other hand, if converting iso-8859-1, conversion drops high byte of each utf-16 code unit (which in case 0 anyway).

so conversion iso-8859-1 produces actual byte array got out of base64-decoding, while conversion utf-8 produces.... else may or may not make sense depending on exact byte values.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -