jericho - HttpMultipartParser 8.2.0-beta0005

A C# Http Multipart/form-data parser that works correctly on binary data and very large files.

PM> Install-Package HttpMultipartParser -Version 8.2.0-beta0005 -Source https://www.myget.org/F/jericho/api/v3/index.json

Copy to clipboard

> nuget.exe install HttpMultipartParser -Version 8.2.0-beta0005 -Source https://www.myget.org/F/jericho/api/v3/index.json

Copy to clipboard

> dotnet add package HttpMultipartParser --version 8.2.0-beta0005 --source https://www.myget.org/F/jericho/api/v3/index.json

Copy to clipboard
<PackageReference Include="HttpMultipartParser" Version="8.2.0-beta0005" />
Copy to clipboard
source https://www.myget.org/F/jericho/api/v3/index.json

nuget HttpMultipartParser  ~> 8.2.0-beta0005
Copy to clipboard

> choco install HttpMultipartParser --version 8.2.0-beta0005 --source https://www.myget.org/F/jericho/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "jericho" -SourceLocation "https://www.myget.org/F/jericho/api/v2"
Install-Module -Name "HttpMultipartParser" -RequiredVersion "8.2.0-beta0005" -Repository "jericho" -AllowPreRelease
Copy to clipboard

Http Multipart Parser

License Sourcelink Build status tests Coverage Status CodeFactor

Release Notes NuGet (stable) MyGet (prerelease)
GitHub release Nuget MyGet Pre Release

About

The Http Multipart Parser does it exactly what it claims on the tin: parses multipart/form-data. This particular parser is well suited to parsing large data from streams as it doesn't attempt to read the entire stream at once and procudes a set of streams for file data.

Installation

The easiest way to include HttpMultipartParser in your project is by adding the nuget package to your project:

PM> Install-Package HttpMultipartParser

.NET framework suport

  • The parser was built for and tested on .NET 4.8, .NET standard 2.1, .NET 5.0 and .NET 6.0.
  • Version 5.1.0 was the last version that supported .NET 4.6.1, NET 4.7.2 and .NET standard 2.0.
  • Version 2.2.4 was the last version that supported older .NET platforms such as .NET 4.5 and .NET standard 1.3.

Usage

Non-Streaming (Simple, don't use on very large files)

  1. Parse the stream containing the multipart/form-data by invoking MultipartFormDataParser.Parse (or it's asynchronous counterpart MultipartFormDataParser.ParseAsync).
  2. Access the data through the parser.

Streaming (Handles large files)

  1. Create a new StreamingMultipartFormDataParser with the stream containing the multipart/form-data
  2. Set up the ParameterHandler and FileHandler delegates
  3. Call parser.Run() (or it's asynchronous counterpart parser.RunAsync())
  4. The delegates will be called as data streams in.

Examples

Single file

// stream:
-----------------------------41952539122868
Content-Disposition: form-data; name="username"

example
-----------------------------41952539122868
Content-Disposition: form-data; name="email"

example@data.com
-----------------------------41952539122868
Content-Disposition: form-data; name="files[]"; filename="photo1.jpg"
Content-Type: image/jpeg

ExampleBinaryData012031203
-----------------------------41952539122868--
// ===== Simple Parsing ====
// You can parse synchronously:
var parser = MultipartFormDataParser.Parse(stream);

// Or you can parse asynchronously:
var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);

// From this point the data is parsed, we can retrieve the
// form data using the GetParameterValue method.
var username = parser.GetParameterValue("username");
var email = parser.GetParameterValue("email");

// Files are stored in a list:
var file = parser.Files.First();
string filename = file.FileName;
Stream data = file.Data;

// ==== Advanced Parsing ====
var parser = new StreamingMultipartFormDataParser(stream);
parser.ParameterHandler += parameter => DoSomethingWithParameter(parameter);
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) =>
{
    // Write the part of the file we've received to a file stream. (Or do something else)
    filestream.Write(buffer, 0, bytes);
}

// You can parse synchronously:
parser.Run();

// Or you can parse asynchronously:
await parser.RunAsync().ConfigureAwait(false);

Multiple Parameters

// stream:
-----------------------------41952539122868
Content-Disposition: form-data; name="checkbox"

likes_cake
-----------------------------41952539122868
Content-Disposition: form-data; name="checkbox"

likes_cookies
-----------------------------41952539122868--
// ===== Simple Parsing ====
// You can parse synchronously:
var parser = MultipartFormDataParser.Parse(stream);

// Or you can parse asynchronously:
var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);

// From this point the data is parsed, we can retrieve the
// form data from the GetParameterValues method
var checkboxResponses = parser.GetParameterValues("checkbox");
foreach(var parameter in checkboxResponses)
{
    Console.WriteLine("Parameter {0} is {1}", parameter.Name, parameter.Data)
}

Multiple Files

// stream:
-----------------------------41111539122868
Content-Disposition: form-data; name="files[]"; filename="photo1.jpg"
Content-Type: image/jpeg

MoreBinaryData
-----------------------------41111539122868
Content-Disposition: form-data; name="files[]"; filename="photo2.jpg"
Content-Type: image/jpeg

ImagineLotsOfBinaryData
-----------------------------41111539122868--
// ===== Simple Parsing ====
// You can parse synchronously:
var parser = MultipartFormDataParser.Parse(stream);

// Or you can parse asynchronously:
var parser = await MultipartFormDataParser.ParseAsync(stream).ConfigureAwait(false);

// Loop through all the files
foreach(var file in parser.Files)
{
    Stream data = file.Data;

    // Do stuff with the data.
}

// ==== Advanced Parsing ====
var parser = new StreamingMultipartFormDataParser(stream);
parser.ParameterHandler += parameter => DoSomethingWithParameter(parameter);
parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) =>
{
    // Write the part of the file we've received to a file stream. (Or do something else)
    // Assume that filesreamsByName is a Dictionary<string, FileStream> of all the files
    // we are writing.
    filestreamsByName[name].Write(buffer, 0, bytes);
};
parser.StreamClosedHandler += () 
{
    // Do things when my input stream is closed
};

// You can parse synchronously:
parser.Run();

// Or you can parse asynchronously:
await parser.RunAsync().ConfigureAwait(false);

Licensing

This project is licensed under MIT.

  • .NETFramework 4.8
    • Microsoft.IO.RecyclableMemoryStream (>= 2.2.1)
    • System.Buffers (>= 4.5.1)
  • .NETFramework 6.0
    • Microsoft.IO.RecyclableMemoryStream (>= 2.2.1)
    • System.Buffers (>= 4.5.1)
  • .NETFramework 7.0
    • Microsoft.IO.RecyclableMemoryStream (>= 2.2.1)
    • System.Buffers (>= 4.5.1)
  • .NETStandard 2.1
    • Microsoft.CSharp (>= 4.7.0)
    • Microsoft.IO.RecyclableMemoryStream (>= 2.2.1)
    • System.Buffers (>= 4.5.1)
  • .NETFramework 4.8: 4.8.0.0
  • .NETFramework 6.0: 6.0.0.0
  • .NETFramework 7.0: 7.0.0.0
  • .NETStandard 2.1: 2.1.0.0

Owners

Jericho

Authors

Jake Woods, Jeremie Desautels

Project URL

https://github.com/Http-Multipart-Data-Parser/Http-Multipart-Data-Parser

License

Unknown

Tags

http multipart parser form data

Info

62 total downloads
2 downloads for version 8.2.0-beta0005
Download (121.45 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
8.3.0-beta0013 121.85 KB Thu, 22 Jun 2023 14:23:44 GMT 2
8.2.0 121.74 KB Thu, 22 Jun 2023 14:33:51 GMT 2
8.2.0-beta0015 121.84 KB Thu, 22 Jun 2023 14:05:24 GMT 2
8.2.0-beta0011 121.31 KB Wed, 21 Jun 2023 20:07:34 GMT 2
8.2.0-beta0005 121.45 KB Sun, 05 Feb 2023 20:51:55 GMT 2
8.1.0 121.37 KB Sun, 05 Feb 2023 21:20:32 GMT 2
8.1.0-beta0014 121.46 KB Sat, 04 Feb 2023 19:36:27 GMT 3
8.1.0-beta0013 121.3 KB Sun, 08 Jan 2023 14:49:21 GMT 2
8.0.0 121.2 KB Sun, 08 Jan 2023 15:04:07 GMT 2
7.2.0-beta0016 121.32 KB Sat, 07 Jan 2023 18:54:32 GMT 2
7.2.0-beta0014 121.35 KB Sat, 07 Jan 2023 18:49:02 GMT 2
7.2.0-beta0008 107.33 KB Fri, 09 Dec 2022 17:57:56 GMT 2
7.2.0-beta0006 107.31 KB Sun, 30 Oct 2022 16:20:25 GMT 1
7.1.0 107.22 KB Sun, 30 Oct 2022 16:28:08 GMT 2
7.1.0-beta0011 107.32 KB Sun, 30 Oct 2022 16:10:42 GMT 2
7.1.0-beta0008 107.33 KB Thu, 07 Jul 2022 14:43:34 GMT 2
7.0.0 107.25 KB Thu, 07 Jul 2022 14:52:53 GMT 2
6.1.0-ignore-invalid-p0025 107.49 KB Sat, 02 Jul 2022 15:33:46 GMT 5
6.1.0-beta0022 102.93 KB Tue, 31 May 2022 17:15:42 GMT 2
6.1.0-beta0020 102.95 KB Tue, 31 May 2022 17:07:14 GMT 2
5.2.0-beta0025 102.92 KB Mon, 30 May 2022 14:04:45 GMT 2
5.2.0-beta0023 102.46 KB Sat, 05 Mar 2022 17:07:30 GMT 2
5.2.0-beta0020 100.39 KB Sat, 05 Mar 2022 16:55:40 GMT 2
5.2.0-beta0019 100.42 KB Sat, 05 Mar 2022 16:21:02 GMT 2
5.1.0-beta0052 100.41 KB Sat, 05 Mar 2022 16:05:53 GMT 2
5.1.0-beta0049 100.41 KB Sat, 05 Mar 2022 15:07:31 GMT 2
5.1.0-alpha0048 100.31 KB Sat, 05 Mar 2022 15:01:01 GMT 2
5.1.0-alpha0044 99.63 KB Sat, 25 Sep 2021 14:57:50 GMT 2
5.1.0-alpha0043 99.72 KB Wed, 14 Jul 2021 17:40:10 GMT 2
5.1.0-alpha0041 98.67 KB Wed, 14 Jul 2021 16:03:15 GMT 1