An open API service indexing awesome lists of open source software.

https://github.com/jchristn/chunkdecoder

Library for decoding chunk-transfer-encoded data, particularly from HTTP.
https://github.com/jchristn/chunkdecoder

chunk chunk-data decoder http http-server https https-server nuget server web

Last synced: 8 months ago
JSON representation

Library for decoding chunk-transfer-encoded data, particularly from HTTP.

Awesome Lists containing this project

README

          

![alt tag](https://github.com/jchristn/chunkdecoder/blob/master/assets/icon.ico)

# ChunkDecoder

[![NuGet Version](https://img.shields.io/nuget/v/ChunkDecoder.svg?style=flat)](https://www.nuget.org/packages/ChunkDecoder/) [![NuGet](https://img.shields.io/nuget/dt/ChunkDecoder.svg)](https://www.nuget.org/packages/ChunkDecoder)

ChunkDecoder is useful to help decode chunk-transfer-encoded data, particularly from HTTP.

## New in v1.0.4

- XML documentation

## Usage
```csharp
using ChunkDecoder;

public Decoder decoder = new Decoder();

// process a byte array
if (decoder.Decode(data, out outData))
{
// success
}

// process a stream
long outLength = 0;
MemoryStream outStream = null;
if (decoder.Decode(stream, out outLength, out outStream))
{
// success
}
```

## Functionality

Chunk-transfer-encoded data is sent in segments, where each segment is comprised of a line containing the length (in hexadecimal) and the subsequent line containing the payload. Each line ends with a carriage return and line feed .

The final line ends with ```0``` indicating the end of the stream.

For instance:
```
6 // 6 bytes of data will follow.
Hello_ // data. The value is 'Hello_' where _ is actually a space.
5 // 5 bytes of data will follow.
World // data. The value is 'World'.
0 // end of stream.
```
Results in:
```
Hello world
```

For more information, please see:

- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
- https://en.wikipedia.org/wiki/Chunked_transfer_encoding

## Callbacks

In cases where the supplied data has signatures, i.e.
```
6;chunk-signature=foo // 6 bytes of data will follow, and the signature of the chunk is foo.
Hello_ // data. The value is 'Hello ' and _ is actually a space.
5;chunk-signature:bar // 5 bytes of data will follow, and the signature of the chunk is bar.
World // data. The value is 'World'.
0; // end of stream.
```

You can assigned the ```Decoder.ProcessSignature``` callback, which has the following signature:
```csharp
using System.Collections.Generic;

static bool ProcessSignature(KeyValuePair, byte[] data)
{
return true;
}
```

This method should return ```true```, otherwise ChunkDecoder will assume there is an error and terminate.

If you wish to process each chunk as it is read, set the ```Decoder.ProcessChunk``` callback, which has the followning signature:
```csharp
static bool ProcessChunk(byte[] data)
{
return true;
}
```

This method should also return ```true```, for the same reason. ProcessChunk is always called after ProcessSignature.

## Version History

Refer to CHANGELOG.md