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.
- Host: GitHub
- URL: https://github.com/jchristn/chunkdecoder
- Owner: jchristn
- License: mit
- Created: 2019-06-23T01:30:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-07T16:01:41.000Z (almost 4 years ago)
- Last Synced: 2025-04-24T00:07:29.124Z (8 months ago)
- Topics: chunk, chunk-data, decoder, http, http-server, https, https-server, nuget, server, web
- Language: C#
- Size: 255 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

# ChunkDecoder
[](https://www.nuget.org/packages/ChunkDecoder/) [](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