diff --git a/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/framed/Hpack.java b/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/framed/Hpack.java index 3155d6d533a..437a8e093b4 100644 --- a/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/framed/Hpack.java +++ b/okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/framed/Hpack.java @@ -189,7 +189,7 @@ private int evictToRecoverBytes(int bytesToRecover) { int entriesToEvict = 0; if (bytesToRecover > 0) { // determine how many headers need to be evicted. - for (int j = dynamicTable.length - 1; j >= nextDynamicTableIndex && bytesToRecover > 0; j--) { + for (int j = dynamicTable.length - 1; j > nextDynamicTableIndex && bytesToRecover > 0; j--) { bytesToRecover -= dynamicTable[j].hpackSize; dynamicTableByteCount -= dynamicTable[j].hpackSize; dynamicTableHeaderCount--; @@ -197,6 +197,7 @@ private int evictToRecoverBytes(int bytesToRecover) { } System.arraycopy(dynamicTable, nextDynamicTableIndex + 1, dynamicTable, nextDynamicTableIndex + 1 + entriesToEvict, dynamicTableHeaderCount); + Arrays.fill(dynamicTable, nextDynamicTableIndex + 1, nextDynamicTableIndex + 1 + entriesToEvict, null); nextDynamicTableIndex += entriesToEvict; } return entriesToEvict; @@ -489,17 +490,21 @@ void writeHeaders(List headerBlock) throw out.writeByte(0x40); writeByteString(name); writeByteString(value); - insertIntoDynamicTable(header); - } else if (name.startsWith(PSEUDO_PREFIX) && !io.grpc.okhttp.internal.framed.Header.TARGET_AUTHORITY.equals(name)) { - // Follow Chromes lead - only include the :authority pseudo header, but exclude all other - // pseudo headers. Literal Header Field without Indexing - Indexed Name. + insertIntoDynamicTable(new io.grpc.okhttp.internal.framed.Header(name, value)); + } else if (name.startsWith(PSEUDO_PREFIX) + && !io.grpc.okhttp.internal.framed.Header.TARGET_AUTHORITY.equals(name) + && !io.grpc.okhttp.internal.framed.Header.TARGET_PATH.equals(name)) { + // Allow :authority and :path pseudo headers to be indexed. Other pseudo headers are not + // indexed. + // This is a departure from original Chrome behavior, as gRPC paths (ServiceName/MethodName) + // are stable and benefit from indexing. writeInt(headerNameIndex, PREFIX_4_BITS, 0); writeByteString(value); } else { // Literal Header Field with Incremental Indexing - Indexed Name. writeInt(headerNameIndex, PREFIX_6_BITS, 0x40); writeByteString(value); - insertIntoDynamicTable(header); + insertIntoDynamicTable(new io.grpc.okhttp.internal.framed.Header(name, value)); } } } @@ -557,7 +562,7 @@ private int evictToRecoverBytes(int bytesToRecover) { int entriesToEvict = 0; if (bytesToRecover > 0) { // determine how many headers need to be evicted. - for (int j = dynamicTable.length - 1; j >= nextDynamicTableIndex && bytesToRecover > 0; j--) { + for (int j = dynamicTable.length - 1; j > nextDynamicTableIndex && bytesToRecover > 0; j--) { bytesToRecover -= dynamicTable[j].hpackSize; dynamicTableByteCount -= dynamicTable[j].hpackSize; dynamicTableHeaderCount--; @@ -565,6 +570,7 @@ private int evictToRecoverBytes(int bytesToRecover) { } System.arraycopy(dynamicTable, nextDynamicTableIndex + 1, dynamicTable, nextDynamicTableIndex + 1 + entriesToEvict, dynamicTableHeaderCount); + Arrays.fill(dynamicTable, nextDynamicTableIndex + 1, nextDynamicTableIndex + 1 + entriesToEvict, null); nextDynamicTableIndex += entriesToEvict; } return entriesToEvict; diff --git a/okhttp/third_party/okhttp/test/java/io/grpc/okhttp/internal/framed/HpackTest.java b/okhttp/third_party/okhttp/test/java/io/grpc/okhttp/internal/framed/HpackTest.java index dc5e030810f..2a023a098a4 100644 --- a/okhttp/third_party/okhttp/test/java/io/grpc/okhttp/internal/framed/HpackTest.java +++ b/okhttp/third_party/okhttp/test/java/io/grpc/okhttp/internal/framed/HpackTest.java @@ -274,12 +274,12 @@ public void readerEviction() throws IOException { * http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-12#appendix-C.2.2 */ @Test public void literalHeaderFieldWithoutIndexingIndexedName() throws IOException { - List
headerBlock = headerEntries(":path", "/sample/path"); + List
headerBlock = headerEntries(":method", "PUT"); - bytesIn.writeByte(0x04); // == Literal not indexed == - // Indexed name (idx = 4) -> :path - bytesIn.writeByte(0x0c); // Literal value (len = 12) - bytesIn.writeUtf8("/sample/path"); + bytesIn.writeByte(0x02); // == Literal not indexed == + // Indexed name (idx = 2) -> :method + bytesIn.writeByte(0x03); // Literal value (len = 3) + bytesIn.writeUtf8("PUT"); hpackWriter.writeHeaders(headerBlock); assertEquals(bytesIn, bytesOut); @@ -1108,30 +1108,47 @@ public void doNotIndexPseudoHeaders() throws IOException { hpackWriter.writeHeaders(headerEntries(":method", "PUT")); assertBytes(0x02, 3, 'P', 'U', 'T'); assertEquals(0, hpackWriter.dynamicTableHeaderCount); - - hpackWriter.writeHeaders(headerEntries(":path", "/okhttp")); - assertBytes(0x04, 7, '/', 'o', 'k', 'h', 't', 't', 'p'); - assertEquals(0, hpackWriter.dynamicTableHeaderCount); } @Test - public void incrementalIndexingWithAuthorityPseudoHeader() throws IOException { - hpackWriter.writeHeaders(headerEntries(":authority", "foo.com")); - assertBytes(0x41, 7, 'f', 'o', 'o', '.', 'c', 'o', 'm'); + public void pseudoHeaderIndexingForPathAndAuthority() throws IOException { + // :path should now be indexed + hpackWriter.writeHeaders(headerEntries(":path", "/okhttp")); + assertBytes(0x44, 7, '/', 'o', 'k', 'h', 't', 't', 'p'); + assertEquals(1, hpackWriter.dynamicTableHeaderCount); + // Second call to same :path should be an index reference byte (0xbe) + hpackWriter.writeHeaders(headerEntries(":path", "/okhttp")); + assertBytes(0xbe); assertEquals(1, hpackWriter.dynamicTableHeaderCount); + // :authority should be indexed + hpackWriter.writeHeaders(headerEntries(":authority", "foo.com")); + assertBytes(0x41, 7, 'f', 'o', 'o', '.', 'c', 'o', 'm'); + assertEquals(2, hpackWriter.dynamicTableHeaderCount); + // Second call to same :authority should be an index reference byte (0xbe) hpackWriter.writeHeaders(headerEntries(":authority", "foo.com")); assertBytes(0xbe); - assertEquals(1, hpackWriter.dynamicTableHeaderCount); + assertEquals(2, hpackWriter.dynamicTableHeaderCount); - // If the :authority header somehow changes, it should be re-added to the dynamic table. + // If the :authority header value changes, it should be added as a new dynamic table entry. hpackWriter.writeHeaders(headerEntries(":authority", "bar.com")); assertBytes(0x41, 7, 'b', 'a', 'r', '.', 'c', 'o', 'm'); - assertEquals(2, hpackWriter.dynamicTableHeaderCount); + assertEquals(3, hpackWriter.dynamicTableHeaderCount); hpackWriter.writeHeaders(headerEntries(":authority", "bar.com")); assertBytes(0xbe); - assertEquals(2, hpackWriter.dynamicTableHeaderCount); + assertEquals(3, hpackWriter.dynamicTableHeaderCount); + } + + @Test + public void evictToRecoverBytesDoesNotNpeWhenBytesToRecoverExceedsTable() throws IOException { + hpackWriter.writeHeaders(headerEntries("custom-key", "custom-value")); + assertEquals(1, hpackWriter.dynamicTableHeaderCount); + + // Force resize to table size 0, requiring total eviction exceeding table capacity + hpackWriter.resizeHeaderTable(0); + + assertEquals(0, hpackWriter.dynamicTableHeaderCount); } @Test