Skip to content

Commit

Permalink
Safely encode Strings to ASCII
Browse files Browse the repository at this point in the history
(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
characters with'?'.
  • Loading branch information
trustin committed Mar 18, 2015
1 parent c14e659 commit 42cd55f
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,16 @@ public static void setTransferEncodingChunked(HttpMessage m, boolean chunked) {
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;
}

private HttpHeaderUtil() { }
}

0 comments on commit 42cd55f

Please sign in to comment.