HttpMachine.PCL 6.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package HttpMachine.PCL --version 6.1.0
                    
NuGet\Install-Package HttpMachine.PCL -Version 6.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HttpMachine.PCL" Version="6.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HttpMachine.PCL" Version="6.1.0" />
                    
Directory.Packages.props
<PackageReference Include="HttpMachine.PCL" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HttpMachine.PCL --version 6.1.0
                    
#r "nuget: HttpMachine.PCL, 6.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HttpMachine.PCL@6.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HttpMachine.PCL&version=6.1.0
                    
Install as a Cake Addin
#tool nuget:?package=HttpMachine.PCL&version=6.1.0
                    
Install as a Cake Tool

HttpMachine

NuGet NuGet Downloads Build and Test License: MIT

.NET .NET Standard

A combined C# HTTP request/response parser, implemented as a state machine built with Adrian Thurston's excellent state machine compiler, Ragel.

Please star this project if you find it useful. Thank you.

When is this library useful?

Most .NET code should reach for HttpClient or Kestrel — they manage connections, TLS and newer protocol versions for you. HttpMachine solves a different problem: it parses HTTP messages from raw bytes, with no socket, connection or transport attached. You hand it a buffer, it hands you back the parsed message.

That makes it a good fit when:

  • The HTTP message doesn't arrive over an HTTP connection. SSDP/UPnP discovery is the classic case — NOTIFY and M-SEARCH messages multicast over UDP, which no HTTP client will parse for you.
  • You need the handshake, not the protocol. Reading a WebSocket upgrade request or response.
  • You are implementing the endpoint yourself — a custom or embedded server, a proxy or gateway, or a device speaking HTTP over an unusual transport.
  • You are inspecting or testing traffic — sniffers, protocol test harnesses, fuzzing, or replaying captured bytes.
  • Data arrives in fragments. Messages may be split across buffers at any byte, and Execute(ReadOnlySpan<byte>) reads pooled or sliced buffers without copying.

Targeting .NET Standard 2.0 also means it runs where modern HTTP stacks don't — older runtimes, Xamarin, Unity and constrained devices.

If you need HTTP/2 or HTTP/3, see Why HTTP/1.x only? at the end.

Background

This project began in May 2016 as a fork of the great work done by Benjamin van der Veen, the original HttpMachine, which was no longer being maintained. It has been developed independently ever since, and by now differs substantially from its origin: a combined request/response parser, chunked transfer encoding, zero-length header support, a span-based parsing API, and continuous modernization of the target frameworks — see the version history below.

The original HttpMachine is Copyright (c) 2011 Benjamin van der Veen and is licensed under the MIT License, as is this fork. See LICENCE.md.

Features

  • HTTP/1.1 and 1.0
  • Parses requests and responses with one combined parser — filter on MessageType to see which one was parsed
  • Supports pipelined messages
  • Tells your server if it should keep the connection alive (ShouldKeepAlive)
  • Extracts the length of the entity body (Content-Length)
  • Supports chunked Transfer-Encoding
  • Handles zero-length headers (for example EXT: as used in UPnP)
  • Header values are collected in an IEnumerable<string> per header name (repeated headers accumulate)

Usage

using System;
using System.IO;
using System.Linq;
using System.Text;
using HttpMachine;

var data = Encoding.UTF8.GetBytes(
    "HTTP/1.1 200 OK\r\n" +
    "Content-Type: text/plain\r\n" +
    "Content-Length: 14\r\n" +
    "\r\n" +
    "This is a test");

using var handler = new HttpParserDelegate();
using var parser = new HttpCombinedParser(handler);

if (parser.Execute(data) != data.Length)
{
    Console.WriteLine("Parse failed.");
    return;
}

var result = handler.HttpRequestResponse;

Console.WriteLine($"Type: {result.MessageType}");     // Response
Console.WriteLine($"Status: {result.StatusCode}");    // 200
Console.WriteLine($"Keep-alive: {result.ShouldKeepAlive}");

foreach (var header in result.Headers)
{
    Console.WriteLine($"{header.Key}: {string.Join(", ", header.Value)}");
}

result.Body.Position = 0;
Console.WriteLine($"Body: {new StreamReader(result.Body).ReadToEnd()}");

See the console test project for a complete example including chunked transfer encoding, and the test suite for more usage patterns.

Things worth knowing

  • Return value of Execute: the number of bytes consumed. If it is less than the length you passed in, the parser hit a parse error at that position.
  • Feeding data incrementally: call Execute repeatedly as data arrives; messages may be split across buffers at any point.
  • Span support: Execute(ReadOnlySpan<byte>) parses without requiring an array — useful with pooled buffers or PipeReader slices. Body callbacks mirror the input: array-based Execute overloads deliver OnBody(ArraySegment<byte>) over your buffer (as in 5.x); the span overload delivers OnBody(ReadOnlySpan<byte>) to delegates implementing IHttpParserSpanDelegate (which HttpParserDelegate does), or a pooled copy to plain IHttpParserDelegate implementors. Either way the buffer is only valid during the callback — copy it if you keep it.
  • End of stream: when a message has no Content-Length and is not chunked, the body runs until the connection closes. Signal this to the parser by calling Execute with an empty buffer. See Message framing for how bodyless responses are handled and the UnframedResponseMode option.
  • Header keys: stored uppercased; the Headers dictionary uses a case-insensitive comparer, so headers["content-type"] works too.
  • Delegate results: handler.HttpRequestResponse is the snapshot of the most recently completed message. It is null until the first message has been fully parsed. When parsing pipelined messages, read it from OnMessageEnd (override it) if you need every message.
  • Customizing: subclass HttpParserDelegate and override the On... methods you care about, or implement IHttpParserCombinedDelegate from scratch.
  • The HTTP method accepts these additional four characters: $ - , .
  • When both Content-Length and Transfer-Encoding: chunked are present, chunked wins (RFC 9112 6.3).

Message framing (and the one deliberate deviation)

This is a pure byte parser with no connection model — it sees bytes, not sockets. That single fact explains most of its framing behaviour. Following RFC 9112 6.3, the length of a message body is determined, in order, by:

  1. Transfer-Encoding: chunked — the body is delimited by the chunk framing.
  2. Content-Length — exactly that many bytes.
  3. Neither header present:
    • A request has no body (RFC 9112 6.3 rule 6).
    • A response is delimited by connection close — the body runs until the connection ends.

Rule 3 for responses is where "no connection model" bites: the parser can't observe a socket closing. So closing is expressed in bytesyou signal end of stream by calling Execute with an empty buffer. That is the parser's definition of "the connection closed", and it's what makes close-delimited bodies work without the parser knowing anything about transports.

Why bodyless responses have a mode

There is one case where strict HTTP and this library's main use case genuinely disagree: a response with no Content-Length, no Transfer-Encoding, and keep-alive (e.g. a default HTTP/1.1 response). Strict RFC 9112 says such a response is close-delimited. But the traffic this library is most used for — SSDP / UPnP discovery (see the examples above) — is HTTPU: HTTP-shaped messages carried one-per-datagram over UDP, with no body and no connection to close. There, the message is simply complete at the blank line. The same bytes, two correct-but-opposite meanings.

Because no single behaviour serves both, the framing of that specific case is a choice:

UnframedResponseMode A keep-alive response with no framing headers… Good for
CompleteAtHeaders (default) is complete at the end of its headers, with no body HTTPU/SSDP, UPnP, datagram-style messages
CloseDelimited is read until you signal EOF with an empty Execute (RFC 9112 6.3) HTTP/1.x responses over a stream

The default preserves the datagram-friendly behaviour the library has always had. Opt into strict behaviour when you parse streamed responses that may omit Content-Length:

using var parser = new HttpCombinedParser(handler, UnframedResponseMode.CloseDelimited);
parser.Execute(buffer);         // consumes the response headers and any body bytes so far
parser.Execute(Array.Empty<byte>()); // signal connection close -> completes the message

Both modes are identical when the response is framed (Content-Length or chunked) or explicitly carries Connection: close; the mode only affects the ambiguous keep-alive-without-framing case.

Working on the parser (Ragel)

src/main/HttpMachine.netstandard/HttpCombinedParser.cs is generated code — do not edit it by hand. The sources are:

To regenerate, install Ragel 6.x (apt install ragel on Debian/Ubuntu, available via Homebrew and Cygwin elsewhere) and run generate.sh from the rl directory. The script also widens a few generated sbyte[] tables to byte[] — Ragel 6.x picks the element type by table length, not value range, and some tables contain values above 127.

Run the tests after regenerating:

cd src/tests/HttpMachine.Tests
dotnet test

Version history

6.1.0

  • Added UnframedResponseMode and a new HttpCombinedParser(delegate, UnframedResponseMode) constructor overload. CloseDelimited opts into strict RFC 9112 6.3 framing for a response with no Content-Length and no Transfer-Encoding, reading its body until end of stream (signalled by an empty Execute). The default, CompleteAtHeaders, is unchanged and keeps the HTTPU/SSDP behaviour where such a response is complete at the blank line — so existing code is unaffected. See Message framing for the rationale. Addresses the scenario in issue #11.

6.0.1

  • Fixed: a request with neither Content-Length nor Transfer-Encoding is now treated as having no body, per RFC 9112 6.3 rule 6 ("If this is a request message and none of the above are true, then the message body length is zero"). The close-delimited "read until the connection closes" rule applies to responses only. Previously such requests — for example GET / HTTP/1.0 or any request with Connection: close — never completed until Execute was called with an empty buffer, which could hang a server built on the parser, and any following pipelined request was silently consumed as body data.
  • Known issue, unchanged in this release: a response with keep-alive but no framing headers is treated as an immediately complete zero-length message, where RFC 9112 6.3 rule 8 says it should be close-delimited. Correcting this changes behaviour for existing consumers, so it is deferred out of this patch release.

6.0

  • Breaking: removed the unused ParserStatus enum.
  • Added Execute(ReadOnlySpan<byte>) and the optional IHttpParserSpanDelegate interface for zero-copy body delivery; IHttpParserDelegate is unchanged, so existing delegates keep working.
  • Targets .NET 10 instead of .NET 9 (C# 14); .NET Standard 2.0/2.1, .NET 6 and .NET 8 unchanged (System.Memory dependency added for .NET Standard 2.0).
  • Performance: Execute(MemoryStream) no longer copies the stream when its buffer is exposable; chunk sizes and status codes are parsed without intermediate string allocations.
  • Fixed ShouldKeepAlive for HTTP/1.0 messages (Connection: keep-alive/close were inverted).
  • Fixed bodies delimited by connection close (no Content-Length, not chunked); signal EOF with an empty Execute call.
  • Fixed repeated headers separated by other headers (previously failed the whole parse).
  • The same HttpParserDelegate now works for pipelined/keep-alive message sequences.
  • Transfer-Encoding: chunked now takes precedence over Content-Length per RFC 9112.
  • Header dictionary lookups are now case-insensitive.
  • Added an xunit test suite and CI; repository cleanup (removed bundled ragel.exe and stale build scripts).

4.0+

  • .NET Standard 2.0 is now the required minimum version.
  • Refactoring and simplification of use has caused some namespaces to change.
  • It is no longer necessary to implement the interface IHttpCombinedParser; instead simply new up HttpParserDelegate. Methods can be overridden if needed.
  • Header values are collected in an IEnumerable<string>.

3.1.0

  • The parser handler must implement IHttpCombinedParser instead of using HttpCombinedParser.

1.1.1

  • The parser was combined into one request/response parser. Filter on MessageType to see what type was passed.

Why HTTP/1.x only?

HttpMachine parses HTTP/1.0 and HTTP/1.1, and there are no plans to support HTTP/2 or HTTP/3.

What makes this library useful is that it parses HTTP messages straight off a byte stream, with no connection, socket or transport attached — which is what you need for HTTP-shaped traffic that doesn't arrive over a normal HTTP connection: SSDP/UPnP discovery over UDP multicast, WebSocket upgrade handshakes, embedded devices, protocol testing and the like.

HTTP/2 doesn't fit that model. It is a binary, multiplexed protocol that requires an ordered, reliable connection (in practice TLS with ALPN), so the scenarios above can't use it in the first place. It also can't be handled by a parser alone: an endpoint has to acknowledge SETTINGS and send WINDOW_UPDATE frames or the peer stalls, so anything supporting it needs a write path and becomes a connection implementation rather than a parser. Beyond that, HTTP/2 shares no grammar with HTTP/1.x — the Ragel state machine buys you nothing, header compression (HPACK) needs connection-wide state, and stream multiplexing would mean a stream ID on every delegate callback. That is a separate library, not a feature. HTTP/3 adds QUIC on top of all of it.

If you need HTTP/2 or HTTP/3 over a regular connection, .NET already has excellent support: System.Net.Http.HttpClient for clients and Kestrel for servers.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on HttpMachine.PCL:

Package Downloads
WebsocketClientLite.PCL

A simple and light WebSocket client. Can be set to ignore SSL/TLS server certificate issues (use with care!). Easily and effectively observe incoming message using Reactive Extensions (Rx)

SimpleHttpListener

Simple Http Listener This is the legacy version of Simple HTTP Listener. Please see project web site for a new version.

SimpleHttpListener.Rx

A simple Reactive Extensions (Rx) based HTTP listener for TCP, UDP and multicast, supporting HTTP keep-alive and SSDP-style datagrams.

WebsocketClientLite.Rx2

Uses Rx v2.5.5

SwebSocket

An easy to use WebSocket library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.2.0 154 7/28/2026
6.1.0 171 7/25/2026
6.0.1 386 7/23/2026
6.0.0 102 7/23/2026
5.0.1 11,761 7/10/2025
5.0.0 2,068 3/27/2025
4.0.3 60,552 4/20/2022
4.0.2 5,800 3/31/2022
3.1.0 359,084 2/11/2018
Loading failed

Adds UnframedResponseMode: opt into RFC 9112 6.3 close-delimited framing for responses that have no Content-Length and no Transfer-Encoding, via a new HttpCombinedParser constructor overload. The default (CompleteAtHeaders) is unchanged and keeps HTTPU/SSDP behaviour, so this release is fully backward compatible. See README version history.