avoid exceptions for non iso characters

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2019-09-11 10:50:42 +10:00
parent bc96561865
commit 90cf7c80bd
4 changed files with 40 additions and 30 deletions

View File

@ -461,6 +461,8 @@ public class HpackContext
if (value != null && value.length() > 0) if (value != null && value.length() > 0)
{ {
int huffmanLen = Huffman.octetsNeeded(value); int huffmanLen = Huffman.octetsNeeded(value);
if (huffmanLen < 0)
throw new IllegalStateException("bad value");
int lenLen = NBitInteger.octectsNeeded(7, huffmanLen); int lenLen = NBitInteger.octectsNeeded(7, huffmanLen);
_huffmanValue = new byte[1 + lenLen + huffmanLen]; _huffmanValue = new byte[1 + lenLen + huffmanLen];
ByteBuffer buffer = ByteBuffer.wrap(_huffmanValue); ByteBuffer buffer = ByteBuffer.wrap(_huffmanValue);

View File

@ -392,18 +392,18 @@ public class HpackEncoder
if (huffman) if (huffman)
{ {
// huffman literal value // huffman literal value
buffer.put((byte)0x80).mark(); buffer.put((byte)0x80);
try int needed = Huffman.octetsNeeded(value);
if (needed >= 0)
{ {
NBitInteger.encode(buffer,7,Huffman.octetsNeeded(value)); NBitInteger.encode(buffer, 7, needed);
Huffman.encode(buffer, value); Huffman.encode(buffer, value);
} }
catch(Throwable th) else
{ {
LOG.ignore(th); // Not iso_8859_1
byte[] bytes = value.getBytes(StandardCharsets.UTF_8); byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
NBitInteger.encode(buffer,7,Huffman.octetsNeeded(bytes)); NBitInteger.encode(buffer,7,Huffman.octetsNeeded(bytes));
Huffman.encode(buffer,bytes); Huffman.encode(buffer,bytes);
} }
@ -418,7 +418,7 @@ public class HpackEncoder
char c = value.charAt(i); char c = value.charAt(i);
if (c < ' ' || c > 127) if (c < ' ' || c > 127)
{ {
// Not iso_8859_1, so re-encode remaining as UTF-8 // Not iso_8859_1, so re-encode as UTF-8
buffer.reset(); buffer.reset();
byte[] bytes = value.getBytes(StandardCharsets.UTF_8); byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
NBitInteger.encode(buffer,7,bytes.length); NBitInteger.encode(buffer,7,bytes.length);

View File

@ -462,7 +462,7 @@ public class Huffman
{ {
char c = s.charAt(i); char c = s.charAt(i);
if (c >= 128 || c < ' ') if (c >= 128 || c < ' ')
throw new IllegalArgumentException(); return -1;
needed += table[c][1]; needed += table[c][1];
} }
@ -481,6 +481,12 @@ public class Huffman
return (needed + 7) / 8; return (needed + 7) / 8;
} }
/**
* @param table The table to encode by
* @param buffer The buffer to encode to
* @param s The string to encode
* @return True if the string could be encoded, false otherwise (and the buffer may have been modified).
*/
private static void encode(final int[][] table, ByteBuffer buffer, String s) private static void encode(final int[][] table, ByteBuffer buffer, String s)
{ {
long current = 0; long current = 0;
@ -512,6 +518,7 @@ public class Huffman
buffer.put((byte)(current)); buffer.put((byte)(current));
} }
} }
private static void encode(final int[][] table,ByteBuffer buffer,byte[] b) private static void encode(final int[][] table,ByteBuffer buffer,byte[] b)
{ {
long current = 0; long current = 0;

View File

@ -25,11 +25,13 @@ import java.util.stream.Stream;
import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.TypeUtil; import org.eclipse.jetty.util.TypeUtil;
import org.hamcrest.Matchers;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -77,8 +79,7 @@ public class HuffmanTest
{ {
String s = "bad '" + bad + "'"; String s = "bad '" + bad + "'";
assertThrows(IllegalArgumentException.class, assertThat(Huffman.octetsNeeded(s), Matchers.is(-1));
() -> Huffman.octetsNeeded(s));
assertThrows(BufferOverflowException.class, assertThrows(BufferOverflowException.class,
() -> Huffman.encode(BufferUtil.allocate(32), s)); () -> Huffman.encode(BufferUtil.allocate(32), s));