hpack 1.4.0

dotnet add package hpack --version 1.4.0                
NuGet\Install-Package hpack -Version 1.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="hpack" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add hpack --version 1.4.0                
#r "nuget: hpack, 1.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install hpack as a Cake Addin
#addin nuget:?package=hpack&version=1.4.0

// Install hpack as a Cake Tool
#tool nuget:?package=hpack&version=1.4.0                

hpack

License: CC0 Workflow: CodeQL NuGet Version Quality Gate Status Coverage

Header Compression for HTTP/2 written in C#

An Example on how to use the hpack.Encoder and hpack.Decoder

First, you have to create a HeaderListener that implements IHeaderListener

using System;
using System.Text;

namespace HTTP2
{
  public class EmitHeaderEventArgs : EventArgs
  {
    private string _name;
    private string _value;
    
    public string Name { get { return this._name; } }
    
    public string Value { get { return this._value; } }
    
    public EmitHeaderEventArgs(byte[] name, byte[] value)
    {
      this._name = Encoding.UTF8.GetString(name);
      this._value = Encoding.UTF8.GetString(value);
    }
    
    public EmitHeaderEventArgs(string name, string value)
    {
      this._name = name;
      this._value = value;
    }
  }
  
  public class HeaderListener : hpack.IHeaderListener
  {
    public delegate void EmitHeaderEventHandler(object sender, EmitHeaderEventArgs e);
    
    public event EmitHeaderEventHandler HeaderEmitted;
    
    public HeaderListener()
    {
      
    }
    
    protected virtual void OnHeaderEmitted(EmitHeaderEventArgs e)
    {
      if (this.HeaderEmitted != null) {
        this.HeaderEmitted(this, e);
      }
    }
    
    public void AddHeader(byte[] name, byte[] value, bool sensitive)
    {
      this.OnHeaderEmitted(new EmitHeaderEventArgs(name, value));
    }
  }
}

Then you can instantiate this HeaderListener and add an EventListener

using System;

namespace HTTP2
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      var headerListener = new HeaderListener();
      headerListener.HeaderEmitted += (object sender, EmitHeaderEventArgs e) {
        Console.WriteLine("header emitted => name: " + e.Name + "; value: " + e.Value);
      };
    }
  }
}

Now you can encode and decode headers

...
hpack.Encoder hpackEncoder = new hpack.Encoder(4096);
hpack.Decoder hpackDecoder = new hpack.Decoder(8192, 4096);

using(var binWriter = new BinaryWriter(new MemoryStream())) {
  hpackEncoder.EncodeHeader(binWriter, ":authority", "localhost:8080");
  hpackEncoder.EncodeHeader(binWriter, ":method", "GET");
  hpackEncoder.EncodeHeader(binWriter, ":path", "/");
  hpackEncoder.EncodeHeader(binWriter, ":scheme", "http");
  
  // send the data (binWriter) to the server!
  
  // wait for receiving headers...
  // for this example the encoded data in binWriter.
  var headerBlockFragment = new byte[binWriter.BaseStream.Length];
  binWriter.BaseStream.Position = 0;
  binWriter.BaseStream.Read(headerBlockFragment, 0, binWriter.BaseStream.Length);
  using(var binReader = new BinaryReader(new MemoryStream(headerBlockFragment))) {
    hpackDecoder.Decode(binReader, headerListener);
    hpackDecoder.EndHeaderBlock(); // this must be called to finalize the decoding process.
    
    // now the decoded headers should be printed from the defined "headerListener.HeaderEmitted" above.
  }
}
...
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on hpack:

Package Downloads
HttpTwo.Net

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 314 11/28/2023
1.3.0 230 8/14/2023
1.2.0 655 6/3/2022
1.1.0 3,232 10/12/2018
1.0.2.29220 42,973 4/24/2016
0.2.5701.39423 1,982 8/11/2015
0.1.5701.14029 1,530 8/11/2015

changed target framework to .NET 6.0 - .NET 8.0