Safely encode Strings to ASCII

(Ported @luciferous's changes against 3.10)

Motivation:

The current implementation of the encoder writes each character of the
String as a single byte to the buffer, however not all characters are
mappable to a single byte.

Modifications:

If a character is outside the ASCII range, it's converted to '?'.

Result:

A safer encoder for String to ASCII, which substitutes unmappable
This commit is contained in:
Trustin Lee 2015-03-18 15:57:31 +09:00
parent abcb2b3661
commit c1ac64fb82

View File

@ -1383,10 +1383,17 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
static void encodeAscii0(CharSequence seq, ByteBuf buf) {
int length = seq.length();
for (int i = 0 ; i < length; i++) {
buf.writeByte((byte) seq.charAt(i));
buf.writeByte(c2b(seq.charAt(i)));
}
}
private static byte c2b(char c) {
if (c > 255) {
return '?';
}
return (byte) c;
}
/**
* Create a new {@link CharSequence} which is optimized for reuse as {@link HttpHeaders} name or value.
* So if you have a Header name or value that you want to reuse you should make use of this.