Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public final class HPackEncoder {
private final CharsetEncoder charsetEncoder;
private ByteBuffer tmpBuf;
private int maxTableSize;
private int minTableSize;

HPackEncoder(final OutboundDynamicTable dynamicTable, final CharsetEncoder charsetEncoder) {
this.dynamicTable = Objects.requireNonNull(dynamicTable);
this.huffmanBuf = new ByteArrayBuffer(128);
this.charsetEncoder = charsetEncoder;
this.maxTableSize = this.dynamicTable.getMaxSize();
this.minTableSize = -1;
}

HPackEncoder(final OutboundDynamicTable dynamicTable, final Charset charset) {
Expand Down Expand Up @@ -144,6 +146,19 @@ private void ensureCapacity(final int extra) {
}
}

private void encodeMaxTableSizeUpdates(final ByteArrayBuffer dst) {
if (this.minTableSize == -1) {
return;
}
encodeInt(dst, 5, this.minTableSize, 0x20);
this.dynamicTable.setMaxSize(this.minTableSize);
if (this.maxTableSize != this.minTableSize) {
encodeInt(dst, 5, this.maxTableSize, 0x20);
this.dynamicTable.setMaxSize(this.maxTableSize);
}
this.minTableSize = -1;
}

int encodeString(
final ByteArrayBuffer dst,
final CharSequence charSequence, final int off, final int len,
Expand Down Expand Up @@ -262,11 +277,7 @@ void encodeHeader(
void encodeHeader(
final ByteArrayBuffer dst, final String name, final String value, final boolean sensitive,
final boolean noIndexing, final boolean useHuffman) throws CharacterCodingException {
//send receiver the updated dynamic table size
if (maxTableSize != this.dynamicTable.getMaxSize()) {
encodeInt(dst, 5, maxTableSize, 0x20);
this.dynamicTable.setMaxSize(maxTableSize);
}
encodeMaxTableSizeUpdates(dst);

final HPackRepresentation representation;
if (sensitive) {
Expand Down Expand Up @@ -342,6 +353,13 @@ public int getMaxTableSize() {
public void setMaxTableSize(final int maxTableSize) {
Args.notNegative(maxTableSize, "Max table size");
this.maxTableSize = maxTableSize;
if (this.minTableSize == -1) {
if (maxTableSize != this.dynamicTable.getMaxSize()) {
this.minTableSize = maxTableSize;
}
} else if (maxTableSize < this.minTableSize) {
this.minTableSize = maxTableSize;
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.core5.http2.hpack;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.hc.core5.util.ByteArrayBuffer;
import org.junit.jupiter.api.Test;

class TestHPackEncoder {

@Test
void testMultipleTableSizeUpdatesSignalMinimumFirstThenFinal() throws Exception {
final OutboundDynamicTable dynamicTable = new OutboundDynamicTable(4096);
final HPackEncoder encoder = new HPackEncoder(dynamicTable, StandardCharsets.US_ASCII);
final ByteArrayBuffer dst = new ByteArrayBuffer(128);

encoder.setMaxTableSize(1024);
encoder.setMaxTableSize(2048);

// No local resize until the next header block is encoded.
assertEquals(4096, dynamicTable.getMaxSize());
assertEquals(2048, encoder.getMaxTableSize());

encoder.encodeHeader(dst, "x-test", "abc", false, false, false);

// First 6 bytes must be:
// 1024 => 0x3f 0xe1 0x07
// 2048 => 0x3f 0xe1 0x0f
final byte[] actual = Arrays.copyOf(dst.array(), dst.length());
assertArrayEquals(new byte[]{
0x3f, (byte) 0xe1, 0x07,
0x3f, (byte) 0xe1, 0x0f
}, Arrays.copyOf(actual, 6));

// Local encoder state must also end at the final advertised size.
assertEquals(2048, dynamicTable.getMaxSize());
}

@Test
void testShrinkThenRestoreOriginalStillEmitsTwoUpdates() throws Exception {
final OutboundDynamicTable dynamicTable = new OutboundDynamicTable(4096);
final HPackEncoder encoder = new HPackEncoder(dynamicTable, StandardCharsets.US_ASCII);
final ByteArrayBuffer dst = new ByteArrayBuffer(128);

encoder.setMaxTableSize(1024);
encoder.setMaxTableSize(4096);

encoder.encodeHeader(dst, "x-test", "abc", false, false, false);

final byte[] actual = Arrays.copyOf(dst.array(), dst.length());
assertArrayEquals(new byte[]{
0x3f, (byte) 0xe1, 0x07,
0x3f, (byte) 0xe1, 0x1f
}, Arrays.copyOf(actual, 6));

assertEquals(4096, dynamicTable.getMaxSize());
}

}
Loading