package io.undertow.protocols.http2;
import java.nio.ByteBuffer;
import io.undertow.UndertowMessages;
import io.undertow.util.HttpString;
import static io.undertow.protocols.http2.Hpack.HeaderField;
public class HpackDecoder {
private static final int DEFAULT_RING_BUFFER_SIZE = 10;
private HeaderEmitter ;
private HeaderField[] ;
private int firstSlotPosition = 0;
private int filledTableSlots = 0;
private int currentMemorySize = 0;
private int specifiedMemorySize;
private final int maxAllowedMemorySize;
private boolean first = true;
private final StringBuilder stringBuilder = new StringBuilder();
public HpackDecoder(int maxAllowedMemorySize) {
this.specifiedMemorySize = Math.min(Hpack.DEFAULT_TABLE_SIZE, maxAllowedMemorySize);
this.maxAllowedMemorySize = maxAllowedMemorySize;
headerTable = new HeaderField[DEFAULT_RING_BUFFER_SIZE];
}
public HpackDecoder() {
this(Hpack.DEFAULT_TABLE_SIZE);
}
public void decode(ByteBuffer buffer, boolean moreData) throws HpackException {
while (buffer.hasRemaining()) {
int originalPos = buffer.position();
byte b = buffer.get();
if ((b & 0b10000000) != 0) {
first = false;
buffer.position(buffer.position() - 1);
int index = Hpack.decodeInteger(buffer, 7);
if (index == -1) {
if(!moreData) {
throw UndertowMessages.MESSAGES.hpackFailed();
}
buffer.position(originalPos);
return;
} else if(index == 0) {
throw UndertowMessages.MESSAGES.zeroNotValidHeaderTableIndex();
}
handleIndex(index);
} else if ((b & 0b01000000) != 0) {
first = false;
HttpString headerName = readHeaderName(buffer, 6);
if (headerName == null) {
if(!moreData) {
throw UndertowMessages.MESSAGES.hpackFailed();
}
buffer.position(originalPos);
return;
}
String headerValue = readHpackString(buffer);
if (headerValue == null) {
if(!moreData) {
throw UndertowMessages.MESSAGES.hpackFailed();
}
buffer.position(originalPos);
return;
}
headerEmitter.emitHeader(headerName, headerValue, false);
addEntryToHeaderTable(new HeaderField(headerName, headerValue));
} else if ((b & 0b11110000) == 0) {
first = false;
HttpString headerName = readHeaderName(buffer, 4);
if (headerName == null) {
if(!moreData) {
throw UndertowMessages.MESSAGES.hpackFailed();
}
buffer.position(originalPos);
return;
}
String headerValue = readHpackString(buffer);
if (headerValue == null) {
if(!moreData) {
throw UndertowMessages.MESSAGES.hpackFailed();
}
buffer.position(originalPos);
return;
}
headerEmitter.emitHeader(headerName, headerValue, false);
} else if ((b & 0b11110000) == 0b00010000) {
first = false;
HttpString headerName = readHeaderName(buffer, 4);
if (headerName == null) {
buffer.position(originalPos);
return;
}
String headerValue = readHpackString(buffer);
if (headerValue == null) {
if(!moreData) {
throw UndertowMessages.MESSAGES.hpackFailed();
}
buffer.position(originalPos);
return;
}
headerEmitter.emitHeader(headerName, headerValue, true);
} else if ((b & 0b11100000) == 0b00100000) {
if(!first) {
throw new HpackException();
}
if (!handleMaxMemorySizeChange(buffer, originalPos)) {
return;
}
} else {
throw UndertowMessages.MESSAGES.invalidHpackEncoding(b);
}
}
if(!moreData) {
first = true;
}
}
private boolean handleMaxMemorySizeChange(ByteBuffer buffer, int originalPos) throws HpackException {
buffer.position(buffer.position() - 1);
int size = Hpack.decodeInteger(buffer, 5);
if (size == -1) {
buffer.position(originalPos);
return false;
}
if(size > maxAllowedMemorySize) {
throw new HpackException(Http2Channel.ERROR_PROTOCOL_ERROR);
}
specifiedMemorySize = size;
if (currentMemorySize > specifiedMemorySize) {
int newTableSlots = filledTableSlots;
int tableLength = headerTable.length;
int newSize = currentMemorySize;
while (newSize > specifiedMemorySize) {
int clearIndex = firstSlotPosition;
firstSlotPosition++;
if (firstSlotPosition == tableLength) {
firstSlotPosition = 0;
}
HeaderField oldData = headerTable[clearIndex];
headerTable[clearIndex] = null;
newSize -= oldData.size;
newTableSlots--;
}
this.filledTableSlots = newTableSlots;
currentMemorySize = newSize;
}
return true;
}
private HttpString (ByteBuffer buffer, int prefixLength) throws HpackException {
buffer.position(buffer.position() - 1);
int index = Hpack.decodeInteger(buffer, prefixLength);
if (index == -1) {
return null;
} else if (index != 0) {
return handleIndexedHeaderName(index);
} else {
String string = readHpackString(buffer);
if (string == null) {
return null;
}
return new HttpString(string);
}
}
private String readHpackString(ByteBuffer buffer) throws HpackException {
if (!buffer.hasRemaining()) {
return null;
}
byte data = buffer.get(buffer.position());
int length = Hpack.decodeInteger(buffer, 7);
if (buffer.remaining() < length || length == -1) {
return null;
}
boolean huffman = (data & 0b10000000) != 0;
if (huffman) {
return readHuffmanString(length, buffer);
}
for (int i = 0; i < length; ++i) {
stringBuilder.append((char) buffer.get());
}
String ret = stringBuilder.toString();
stringBuilder.setLength(0);
return ret;
}
private String readHuffmanString(int length, ByteBuffer buffer) throws HpackException {
HPackHuffman.decode(buffer, length, stringBuilder);
String ret = stringBuilder.toString();
stringBuilder.setLength(0);
return ret;
}
private HttpString handleIndexedHeaderName(int index) throws HpackException {
if (index <= Hpack.STATIC_TABLE_LENGTH) {
return Hpack.STATIC_TABLE[index].name;
} else {
if (index >= Hpack.STATIC_TABLE_LENGTH + filledTableSlots) {
throw new HpackException();
}
int adjustedIndex = getRealIndex(index - Hpack.STATIC_TABLE_LENGTH);
HeaderField res = headerTable[adjustedIndex];
if (res == null) {
throw new HpackException();
}
return res.name;
}
}
private void handleIndex(int index) throws HpackException {
if (index <= Hpack.STATIC_TABLE_LENGTH) {
addStaticTableEntry(index);
} else {
int adjustedIndex = getRealIndex(index - Hpack.STATIC_TABLE_LENGTH);
HeaderField headerField = headerTable[adjustedIndex];
headerEmitter.emitHeader(headerField.name, headerField.value, false);
}
}
int getRealIndex(int index) throws HpackException {
int newIndex = (firstSlotPosition + (filledTableSlots - index)) % headerTable.length;
if(newIndex < 0) {
throw UndertowMessages.MESSAGES.invalidHpackIndex(index);
}
return newIndex;
}
private void addStaticTableEntry(int index) throws HpackException {
HeaderField entry = Hpack.STATIC_TABLE[index];
headerEmitter.emitHeader(entry.name, entry.value == null ? "" : entry.value, false);
}
private void (HeaderField entry) {
if (entry.size > specifiedMemorySize) {
while (filledTableSlots > 0) {
headerTable[firstSlotPosition] = null;
firstSlotPosition++;
if (firstSlotPosition == headerTable.length) {
firstSlotPosition = 0;
}
filledTableSlots--;
}
currentMemorySize = 0;
return;
}
resizeIfRequired();
int newTableSlots = filledTableSlots + 1;
int tableLength = headerTable.length;
int index = (firstSlotPosition + filledTableSlots) % tableLength;
headerTable[index] = entry;
int newSize = currentMemorySize + entry.size;
while (newSize > specifiedMemorySize) {
int clearIndex = firstSlotPosition;
firstSlotPosition++;
if (firstSlotPosition == tableLength) {
firstSlotPosition = 0;
}
HeaderField oldData = headerTable[clearIndex];
headerTable[clearIndex] = null;
newSize -= oldData.size;
newTableSlots--;
}
this.filledTableSlots = newTableSlots;
currentMemorySize = newSize;
}
private void resizeIfRequired() {
if(filledTableSlots == headerTable.length) {
HeaderField[] newArray = new HeaderField[headerTable.length + 10];
for(int i = 0; i < headerTable.length; ++i) {
newArray[i] = headerTable[(firstSlotPosition + i) % headerTable.length];
}
firstSlotPosition = 0;
headerTable = newArray;
}
}
public interface {
void (HttpString name, String value, boolean neverIndex) throws HpackException;
}
public HeaderEmitter () {
return headerEmitter;
}
public void (HeaderEmitter headerEmitter) {
this.headerEmitter = headerEmitter;
}
int getFirstSlotPosition() {
return firstSlotPosition;
}
HeaderField[] () {
return headerTable;
}
int getFilledTableSlots() {
return filledTableSlots;
}
int getCurrentMemorySize() {
return currentMemorySize;
}
int getSpecifiedMemorySize() {
return specifiedMemorySize;
}
}