HttpServerBoxed 0.0.11 alpha
A simple http server for C# and .NET
Loading...
Searching...
No Matches
HSB.Components Namespace Reference

Classes

class  FilePart
 Represents a file of a multipart form. More...
 
class  Form
 
class  FormPart
 Represents a generic part of a multipart form. More...
 

Functions

class MultiPartFormData (byte[] body, string boundary)
 

Function Documentation

◆ MultiPartFormData()

class HSB.Components.MultiPartFormData ( byte[]  body,
string  boundary 
)

Returns all parts of the forms which are not files

Returns

Definition at line 5 of file MulitPartFormData.cs.

6{
7 private readonly List<FormPart> parts = [];
8 private byte[] body = body;
9 private readonly string Boundary = boundary;
10
11 private void ExtractParts()
12 {
13 var boundaryBytes = Encoding.UTF8.GetBytes("--" + Boundary);
14
15 body = body[..^2];//remove last \r\n
16 body = body[..^2]; //remove last "--"
17 body = body[..^boundaryBytes.Length]; //remove trailing boundary
18
19 var partsData = body.Split(boundaryBytes);
20 partsData.RemoveAt(0);//skip first byte array, it's empty
21 foreach (var part in partsData)
22 {
23 parts.Add(FormPart.Build(part[2..])); //remove \r\n at start
24 }
25
26 body = [];
27
28 }
33 public List<FormPart> GetParts()
34 {
35 if (body.Length > 0)
36 ExtractParts();
37 return parts
38 .Where(p => p is not FilePart)
39 .ToList();
40 }
41
42 public List<FilePart> GetFiles()
43 {
44 if (body.Length > 0)
45 ExtractParts();
46 return
47 parts
48 .Where(p => p is FilePart)
49 .Select(p => (FilePart)p) //filter and cast to FilePart
50 .ToList();
51 }
52
53}
Represents a generic part of a multipart form.
Definition FormPart.cs:10