Implement encoder
This commit is contained in:
parent
3f2ac922f1
commit
742ef421dc
1 changed files with 29 additions and 1 deletions
|
@ -11,6 +11,34 @@ public class Codec : ICodec {
|
|||
}
|
||||
|
||||
public byte[] Encode(Document document) {
|
||||
return document.Data;
|
||||
var dataLength = document.Data.Length;
|
||||
var contentLength = new Dictionary<string, string> () {
|
||||
{ "content-length", dataLength.ToString() }
|
||||
};
|
||||
var headers = document.Metadata.Union(contentLength);
|
||||
|
||||
var packedHeaders = packHeaders(headers);
|
||||
var headerLength = Convert.ToUInt16(packedHeaders.Length);
|
||||
|
||||
using var output = new MemoryStream();
|
||||
output.WriteByte(0x01);
|
||||
output.Write(BitConverter.GetBytes(headerLength));
|
||||
output.Write(packedHeaders);
|
||||
output.Write(document.Data);
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
private byte[] packHeaders(IEnumerable<KeyValuePair<string, string>> headers) {
|
||||
var encoder = new hpack.Encoder(0); //0 will disable dynamic table that we don't need anyways
|
||||
|
||||
using var output = new MemoryStream();
|
||||
using var writer = new BinaryWriter(output);
|
||||
|
||||
foreach(var (name, value) in headers) {
|
||||
encoder.EncodeHeader(writer, name, value);
|
||||
}
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue