Skip to content

Commit d9b51ae

Browse files
committed
(GH-116) Skip blank lines when detecting boundary
1 parent a62e1d0 commit d9b51ae

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Source/HttpMultipartParser/StreamingBinaryMultipartFormDataParser.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,16 @@ private static string DetectBoundary(RebufferableBinaryReader reader)
254254
{
255255
// Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
256256
// the front as per the protocol and ||||||||||||| is the part we care about.
257-
var line = reader.ReadLine();
257+
258+
// The following loop ignores blank lines that may be present before the first line of the form.
259+
// It's highly unusual to find blank lines at the start of the data but it's a possible scenario described in GH-116.
260+
// Please note that we intentionally do NOT check for "string.IsNullOrEmpty(line)" because NULL does
261+
// not indicate a blank line. It indicates that we have reached the end of the stream.
262+
var line = string.Empty;
263+
while (line == string.Empty)
264+
{
265+
line = reader.ReadLine();
266+
}
258267

259268
// The line must not be empty and must starts with "--".
260269
if (string.IsNullOrEmpty(line)) throw new MultipartParseException("Unable to determine boundary: either the stream is empty or we reached the end of the stream");
@@ -296,7 +305,16 @@ private static async Task<string> DetectBoundaryAsync(RebufferableBinaryReader r
296305
{
297306
// Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
298307
// the front as per the protocol and ||||||||||||| is the part we care about.
299-
var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
308+
309+
// The following loop ignores blank lines that may be present before the first line of the form.
310+
// It's highly unusual to find blank lines at the start of the data but it's a possible scenario described in GH-116.
311+
// Please note that we intentionally do NOT check for "string.IsNullOrEmpty(line)" because NULL does
312+
// not indicate a blank line. It indicates that we have reached the end of the stream.
313+
var line = string.Empty;
314+
while (line == string.Empty)
315+
{
316+
line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
317+
}
300318

301319
// The line must not be empty and must starts with "--".
302320
if (string.IsNullOrEmpty(line)) throw new MultipartParseException("Unable to determine boundary: either the stream is empty or we reached the end of the stream");

0 commit comments

Comments
 (0)