Skip to content

header-only c++ implementation of a HTTPv2 HPACK/RFC7541 encoder /decoder

License

Notifications You must be signed in to change notification settings

yadij/hpack-rfc7541

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 

Repository files navigation

hpack

header-only c++/STL-ish implementation of a HTTPv2 HPACK/RFC7541 encoder/decoder.

Encoder

HPACK::encoder_t encoder;
std::vector< uint8_t > d;

// encoder(Name, Value, Use_Huffman_Encoding = true, Use_Never_Index = false)
// typedef std::pair< std::string, std::string > header_t
	encoder.add(HPACK::header_t(":method", "GET"));
	encoder.add(HPACK::header_t(":scheme", "http"));
	encoder.add(HPACK::header_t(":path", "/"));
	encoder.add(HPACK::header_t(":authority", "www.example.com"));
	encoder.data().clear();
	encoder.add(HPACK::header_t(":method", "GET"));
	encoder.add(HPACK::header_t(":scheme", "http"));
	encoder.add(HPACK::header_t(":path", "/"));
	encoder.add(HPACK::header_t(":authority", "www.example.com"), false);
	encoder.add(HPACK::header_t("cache-control", "no-cache"), false);
	encoder.data().clear();
	encoder.add(HPACK::header_t(":method", "GET"));
	encoder.add(HPACK::header_t(":scheme", "https"));
	encoder.add(HPACK::header_t(":path", "/index.html"));
	encoder.add(HPACK::header_t(":authority", "www.example.com"));
	encoder.add(HPACK::header_t("custom-key", "custom-value"), false);

	for (auto& byte : encoder.data())
		std::cout << std::setw(2) << std::setfil('0') << std::hex << 0+byte;
	std::cout << std::endl;

Decoder

	const char x = 			
		"\x82\x86\x84\x41\x8b\x0b\xe2\x5c\x2e\x3c\xb8\x5e\x69\x70\x89"
		"\xef\x58\x86\xa8\xeb\x10\x64\x9c\xbf\x40\x88\x25\xa8\x49\xe9"
		"\x5b\xa9\x7d\x7f\x89\x25\xa8\x49\xe9\x5b\xb8\xe8\xb4\xbf";
	HPACK::decoder_t dec;

	if (true == dec.decode(x)) {
		for (auto& hdr : dec.headers())
			std::cout << hdr.first << ": " << hdr.second << std::endl;
	} else 
		// protocol error

(los angeles)

About

header-only c++ implementation of a HTTPv2 HPACK/RFC7541 encoder /decoder

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%