Class TransferEncodingChunkedParser

Parses the Transfer-Encoding: chunked header.

namespace System.Net.Http.Headers
public static class TransferEncodingChunkedParser

Summary

This static class provides methods for parsing and formatting the Transfer-Encoding: chunked HTTP header. Chunked transfer encoding is a method of transferring HTTP message body content in a series of chunks. This is particularly useful when the total size of the message body is not known in advance.

Methods

Parse(string input, out int requiredLength)

public static bool Parse(string input, out int requiredLength)

Parses the specified input string as a chunked transfer encoding header. This method is primarily used internally by the .NET framework to process HTTP requests and responses.

Parameters

Return Value

true if the input string is a valid chunked transfer encoding header; otherwise, false.

ToString(int length)

public static string ToString(int length)

Formats the specified length into a string representation suitable for the Transfer-Encoding: chunked header. This method is also primarily for internal use.

Parameters

Return Value

A string representing the formatted length for a chunked transfer encoding header.

Remarks

The TransferEncodingChunkedParser class is a low-level utility for handling the specific format of the Transfer-Encoding: chunked header. It is not intended to be instantiated or used directly in application code. For most HTTP communication scenarios, the HttpClient and related classes in System.Net.Http handle chunked encoding automatically.

Chunked transfer encoding defines a way to send a message body in chunks, where each chunk is preceded by its size in hexadecimal. A final chunk with a size of zero indicates the end of the message body. This is documented in RFC 7230 Section 3.3.1.

Examples

While direct usage is not recommended, here's a conceptual illustration of how the parser might be used internally:


// This is an illustrative example and not intended for direct use.
using System.Net.Http.Headers;

string headerValue = "chunked";
int requiredLength;

bool isValid = TransferEncodingChunkedParser.Parse(headerValue, out requiredLength);

if (isValid)
{
    Console.WriteLine($"Header '{headerValue}' is valid. Required length: {requiredLength}");
    // Further processing would occur here.
}
else
{
    Console.WriteLine($"Header '{headerValue}' is invalid.");
}

// Formatting example (again, for illustration)
int dataSize = 1024;
string formattedSize = TransferEncodingChunkedParser.ToString(dataSize);
Console.WriteLine($"Formatted size for {dataSize} bytes: {formattedSize}");
// This would likely output something like "400" (hexadecimal for 1024) followed by CRLF.
        

See Also