@Test public void decodeShouldSucceed() throws Exception { ByteBuf buf = encode(b(":method"), b("GET"), b("akey"), b("avalue"), randomBytes(), randomBytes()); try { Http2Headers headers = decoder.decodeHeaders(buf); assertEquals(3, headers.size()); assertEquals("GET", headers.method().toString()); assertEquals("avalue", headers.get(new AsciiString("akey")).toString()); } finally { buf.release(); } }
/** * Checks if a new decoder object is needed for the stream identified by {@code streamId}. This * method will modify the {@code content-encoding} header contained in {@code builder}. * * @param streamId The identifier for the headers inside {@code builder} * @param builder Object representing headers which have been read * @param endOfStream Indicates if the stream has ended * @throws Http2Exception If the {@code content-encoding} is not supported */ private void initDecoder(int streamId, Http2Headers headers, boolean endOfStream) throws Http2Exception { // Convert the names into a case-insensitive map. final Http2Stream stream = connection.stream(streamId); if (stream != null) { EmbeddedChannel decoder = stream.decompressor(); if (decoder == null) { if (!endOfStream) { // Determine the content encoding. AsciiString contentEncoding = headers.get(CONTENT_ENCODING_LOWER_CASE); if (contentEncoding == null) { contentEncoding = IDENTITY; } decoder = newContentDecoder(contentEncoding); if (decoder != null) { stream.decompressor(decoder); // Decode the content and remove or replace the existing headers // so that the message looks like a decoded message. AsciiString targetContentEncoding = getTargetContentEncoding(contentEncoding); if (IDENTITY.equalsIgnoreCase(targetContentEncoding)) { headers.remove(CONTENT_ENCODING_LOWER_CASE); } else { headers.set(CONTENT_ENCODING_LOWER_CASE, targetContentEncoding); } } } } else if (endOfStream) { cleanup(stream, decoder); } if (decoder != null) { // The content length will be for the compressed data. Since we will decompress the data // this content-length will not be correct. Instead of queuing messages or delaying sending // header frames...just remove the content-length header headers.remove(CONTENT_LENGTH_LOWER_CASE); } } }