HttpMachine.PCL
6.1.0
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
<PackageReference Include="HttpMachine.PCL" Version="6.1.0" />
<PackageVersion Include="HttpMachine.PCL" Version="6.1.0" />
<PackageReference Include="HttpMachine.PCL" />
paket add HttpMachine.PCL --version 6.1.0
#r "nuget: HttpMachine.PCL, 6.1.0"
#:package HttpMachine.PCL@6.1.0
#addin nuget:?package=HttpMachine.PCL&version=6.1.0
#tool nuget:?package=HttpMachine.PCL&version=6.1.0
HttpMachine
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 —
NOTIFYandM-SEARCHmessages 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
MessageTypeto 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
Executerepeatedly 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 orPipeReaderslices. Body callbacks mirror the input: array-basedExecuteoverloads deliverOnBody(ArraySegment<byte>)over your buffer (as in 5.x); the span overload deliversOnBody(ReadOnlySpan<byte>)to delegates implementingIHttpParserSpanDelegate(whichHttpParserDelegatedoes), or a pooled copy to plainIHttpParserDelegateimplementors. 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-Lengthand is not chunked, the body runs until the connection closes. Signal this to the parser by callingExecutewith an empty buffer. See Message framing for how bodyless responses are handled and theUnframedResponseModeoption. - Header keys: stored uppercased; the
Headersdictionary uses a case-insensitive comparer, soheaders["content-type"]works too. - Delegate results:
handler.HttpRequestResponseis the snapshot of the most recently completed message. It isnulluntil the first message has been fully parsed. When parsing pipelined messages, read it fromOnMessageEnd(override it) if you need every message. - Customizing: subclass
HttpParserDelegateand override theOn...methods you care about, or implementIHttpParserCombinedDelegatefrom scratch. - The HTTP method accepts these additional four characters:
$ - , . - When both
Content-LengthandTransfer-Encoding: chunkedare 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:
Transfer-Encoding: chunked— the body is delimited by the chunk framing.Content-Length— exactly that many bytes.- 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 bytes — you 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:
- HttpParser2-chunked.cs.rl — C# host file with the parser actions
- http-chunked.rl — the HTTP grammar
- uri.rl — the URI grammar
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
UnframedResponseModeand a newHttpCombinedParser(delegate, UnframedResponseMode)constructor overload.CloseDelimitedopts into strict RFC 9112 6.3 framing for a response with noContent-Lengthand noTransfer-Encoding, reading its body until end of stream (signalled by an emptyExecute). 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-LengthnorTransfer-Encodingis 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 exampleGET / HTTP/1.0or any request withConnection: close— never completed untilExecutewas 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
ParserStatusenum. - Added
Execute(ReadOnlySpan<byte>)and the optionalIHttpParserSpanDelegateinterface for zero-copy body delivery;IHttpParserDelegateis 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.Memorydependency 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
ShouldKeepAlivefor HTTP/1.0 messages (Connection: keep-alive/closewere inverted). - Fixed bodies delimited by connection close (no
Content-Length, not chunked); signal EOF with an emptyExecutecall. - Fixed repeated headers separated by other headers (previously failed the whole parse).
- The same
HttpParserDelegatenow works for pipelined/keep-alive message sequences. Transfer-Encoding: chunkednow takes precedence overContent-Lengthper RFC 9112.- Header dictionary lookups are now case-insensitive.
- Added an xunit test suite and CI; repository cleanup (removed bundled
ragel.exeand 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 upHttpParserDelegate. Methods can be overridden if needed. - Header values are collected in an
IEnumerable<string>.
3.1.0
- The parser handler must implement
IHttpCombinedParserinstead of usingHttpCombinedParser.
1.1.1
- The parser was combined into one request/response parser. Filter on
MessageTypeto 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 | Versions 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. |
-
.NETStandard 2.0
- IHttpMachine (>= 6.1.0)
- System.Memory (>= 4.6.3)
-
.NETStandard 2.1
- IHttpMachine (>= 6.1.0)
-
net10.0
- IHttpMachine (>= 6.1.0)
-
net6.0
- IHttpMachine (>= 6.1.0)
-
net8.0
- IHttpMachine (>= 6.1.0)
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.
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.