All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.net.http.common.v2.hpack.HpackFieldPreEncoder Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.http.common.v2.hpack;

import com.fireflysource.common.io.BufferUtils;
import com.fireflysource.net.http.common.codec.HttpFieldPreEncoder;
import com.fireflysource.net.http.common.model.HttpHeader;
import com.fireflysource.net.http.common.model.HttpVersion;

import java.nio.ByteBuffer;

public class HpackFieldPreEncoder implements HttpFieldPreEncoder {

    @Override
    public HttpVersion getHttpVersion() {
        return HttpVersion.HTTP_2;
    }

    @Override
    public byte[] getEncodedField(HttpHeader header, String name, String value) {
        boolean not_indexed = HpackEncoder.DO_NOT_INDEX.contains(header);

        ByteBuffer buffer = ByteBuffer.allocate(name.length() + value.length() + 10);
        boolean huffman;
        int bits;

        if (not_indexed) {
            // Non indexed field
            boolean never_index = HpackEncoder.NEVER_INDEX.contains(header);
            huffman = !HpackEncoder.DO_NOT_HUFFMAN.contains(header);
            buffer.put(never_index ? (byte) 0x10 : (byte) 0x00);
            bits = 4;
        } else if (header == HttpHeader.CONTENT_LENGTH && value.length() > 1) {
            // Non indexed content length for 2 digits or more
            buffer.put((byte) 0x00);
            huffman = true;
            bits = 4;
        } else {
            // indexed
            buffer.put((byte) 0x40);
            huffman = !HpackEncoder.DO_NOT_HUFFMAN.contains(header);
            bits = 6;
        }

        int name_idx = HpackContext.staticIndex(header);
        if (name_idx > 0)
            NBitInteger.encode(buffer, bits, name_idx);
        else {
            buffer.put((byte) 0x80);
            NBitInteger.encode(buffer, 7, Huffman.octetsNeededLC(name));
            Huffman.encodeLC(buffer, name);
        }

        HpackEncoder.encodeValue(buffer, huffman, value);

        buffer.flip();
        return BufferUtils.toArray(buffer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy