Skip to content

Commit 7407787

Browse files
committed
Improve how we detect boundary to handle empty forms (they contain only a "end" boundary without a matching "begin" boundary)
1 parent 7aa597f commit 7407787

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,19 @@ private static string DetectBoundary(RebufferableBinaryReader reader)
351351
// Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
352352
// the front as per the protocol and ||||||||||||| is the part we care about.
353353
string boundary = string.Concat(reader.ReadLine().Skip(2));
354-
reader.Buffer("--" + boundary + "\n");
354+
355+
// If the string ends with '--' it means that we found the "end" boundary and we
356+
// need to trim the two dashes to get the actual boundary
357+
if (boundary.EndsWith("--"))
358+
{
359+
boundary = boundary.Substring(0, boundary.Length - 2);
360+
reader.Buffer($"--{boundary}--\n");
361+
}
362+
else
363+
{
364+
reader.Buffer($"--{boundary}\n");
365+
}
366+
355367
return boundary;
356368
}
357369

@@ -374,8 +386,20 @@ private static async Task<string> DetectBoundaryAsync(RebufferableBinaryReader r
374386
// Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
375387
// the front as per the protocol and ||||||||||||| is the part we care about.
376388
var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
377-
var boundary = string.Concat(line.Skip(2));
378-
reader.Buffer("--" + boundary + "\n");
389+
string boundary = string.Concat(line.Skip(2));
390+
391+
// If the string ends with '--' it means that we found the "end" boundary and we
392+
// need to trim the two dashes to get the actual boundary.
393+
if (boundary.EndsWith("--"))
394+
{
395+
boundary = boundary.Substring(0, boundary.Length - 2);
396+
reader.Buffer($"--{boundary}--\n");
397+
}
398+
else
399+
{
400+
reader.Buffer($"--{boundary}\n");
401+
}
402+
379403
return boundary;
380404
}
381405

0 commit comments

Comments
 (0)