Skip to content

Instantly share code, notes, and snippets.

@LucaDev
Created August 5, 2017 13:11
Show Gist options
  • Save LucaDev/61c3323dbd5963dcbd36d36b5ca2fc09 to your computer and use it in GitHub Desktop.
Save LucaDev/61c3323dbd5963dcbd36d36b5ca2fc09 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"golang.org/x/net/http2/hpack"
)
func main() {
fmt.Println(EncodeString(os.Args[1]))
}
func EncodeString(in string) string {
hs := hpack.AppendHuffmanString(nil, in)
length := byte(hpack.HuffmanEncodeLength(in) | 0x80)
enc := FormatByte(length)
for _, b := range hs {
enc += FormatByte(b)
}
return fmt.Sprintf("static const u_char nginx[%d] = \"%s\";", len(hs)+1, enc)
}
func FormatByte(b byte) string {
return fmt.Sprintf("\\x%x", b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment