Skip to content

Commit dc48fb3

Browse files
committed
(GH-131) Further improve the null-check logic to avoid the problem described in GH-131
Also, avoid validating the options in Multipart parser because they are already validated in the streaming binary parser
1 parent 911398a commit dc48fb3

4 files changed

Lines changed: 28 additions & 44 deletions

File tree

Source/HttpMultipartParser/MultipartFormBinaryDataParser.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,12 @@ public static MultipartFormBinaryDataParser Parse(Stream stream, string boundary
180180
var options = new ParserOptions
181181
{
182182
BinaryBufferSize = binaryBufferSize,
183+
BinaryMimeTypes = binaryMimeTypes ?? Constants.DefaultBinaryMimeTypes,
184+
Boundary = boundary,
185+
Encoding = encoding ?? Constants.DefaultEncoding,
183186
IgnoreInvalidParts = ignoreInvalidParts
184187
};
185188

186-
if (!string.IsNullOrEmpty(boundary)) options.Boundary = boundary;
187-
if (encoding != null) options.Encoding = encoding;
188-
if (binaryMimeTypes != null) options.BinaryMimeTypes = binaryMimeTypes;
189-
190189
return Parse(stream, options);
191190
}
192191

@@ -276,13 +275,12 @@ public static Task<MultipartFormBinaryDataParser> ParseAsync(Stream stream, stri
276275
var options = new ParserOptions
277276
{
278277
BinaryBufferSize = binaryBufferSize,
278+
BinaryMimeTypes = binaryMimeTypes ?? Constants.DefaultBinaryMimeTypes,
279+
Boundary = boundary,
280+
Encoding = encoding ?? Constants.DefaultEncoding,
279281
IgnoreInvalidParts = ignoreInvalidParts
280282
};
281283

282-
if (!string.IsNullOrEmpty(boundary)) options.Boundary = boundary;
283-
if (encoding != null) options.Encoding = encoding;
284-
if (binaryMimeTypes != null) options.BinaryMimeTypes = binaryMimeTypes;
285-
286284
return ParseAsync(stream, options, cancellationToken);
287285
}
288286

Source/HttpMultipartParser/MultipartFormDataParser.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// <author>Jake Woods</author>
2121
// --------------------------------------------------------------------------------------------------------------------
2222

23+
using System;
2324
using System.Collections.Generic;
2425
using System.IO;
2526
using System.Text;
@@ -180,13 +181,12 @@ public static MultipartFormDataParser Parse(Stream stream, string boundary = nul
180181
var options = new ParserOptions
181182
{
182183
BinaryBufferSize = binaryBufferSize,
184+
BinaryMimeTypes = binaryMimeTypes ?? Constants.DefaultBinaryMimeTypes,
185+
Boundary = boundary,
186+
Encoding = encoding ?? Constants.DefaultEncoding,
183187
IgnoreInvalidParts = ignoreInvalidParts
184188
};
185189

186-
if (!string.IsNullOrEmpty(boundary)) options.Boundary = boundary;
187-
if (encoding != null) options.Encoding = encoding;
188-
if (binaryMimeTypes != null) options.BinaryMimeTypes = binaryMimeTypes;
189-
190190
return Parse(stream, options);
191191
}
192192

@@ -276,13 +276,12 @@ public static Task<MultipartFormDataParser> ParseAsync(Stream stream, string bou
276276
var options = new ParserOptions
277277
{
278278
BinaryBufferSize = binaryBufferSize,
279+
BinaryMimeTypes = binaryMimeTypes ?? Constants.DefaultBinaryMimeTypes,
280+
Boundary = boundary,
281+
Encoding = encoding ?? Constants.DefaultEncoding,
279282
IgnoreInvalidParts = ignoreInvalidParts
280283
};
281284

282-
if (!string.IsNullOrEmpty(boundary)) options.Boundary = boundary;
283-
if (encoding != null) options.Encoding = encoding;
284-
if (binaryMimeTypes != null) options.BinaryMimeTypes = binaryMimeTypes;
285-
286285
return ParseAsync(stream, options, cancellationToken);
287286
}
288287

Source/HttpMultipartParser/StreamingBinaryMultipartFormDataParser.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,15 @@ public StreamingBinaryMultipartFormDataParser(Stream stream, Encoding encoding,
135135
/// By default the parser will throw an exception if it encounters an invalid part. set this to true to ignore invalid parts.
136136
/// </param>
137137
public StreamingBinaryMultipartFormDataParser(Stream stream, string boundary = null, Encoding encoding = null, int binaryBufferSize = Constants.DefaultBufferSize, string[] binaryMimeTypes = null, bool ignoreInvalidParts = false)
138-
{
139-
if (stream == null || stream == Stream.Null) { throw new ArgumentNullException(nameof(stream)); }
140-
141-
_options = new ParserOptions
138+
: this(stream, new ParserOptions
142139
{
143140
BinaryBufferSize = binaryBufferSize,
141+
BinaryMimeTypes = binaryMimeTypes ?? Constants.DefaultBinaryMimeTypes,
142+
Boundary = boundary,
143+
Encoding = encoding ?? Constants.DefaultEncoding,
144144
IgnoreInvalidParts = ignoreInvalidParts
145-
};
146-
147-
if (!string.IsNullOrEmpty(boundary)) _options.Boundary = boundary;
148-
if (encoding != null) _options.Encoding = encoding;
149-
if (binaryMimeTypes != null) _options.BinaryMimeTypes = binaryMimeTypes;
150-
151-
_stream = stream;
152-
readEndBoundary = false;
145+
})
146+
{
153147
}
154148

155149
/// <summary>
@@ -164,8 +158,9 @@ public StreamingBinaryMultipartFormDataParser(Stream stream, string boundary = n
164158
/// </param>
165159
public StreamingBinaryMultipartFormDataParser(Stream stream, ParserOptions options)
166160
{
167-
if (stream == null || stream == Stream.Null) { throw new ArgumentNullException(nameof(stream)); }
168-
161+
if (stream == null || stream == Stream.Null) throw new ArgumentNullException(nameof(stream));
162+
_stream = stream;
163+
169164
if (options == null)
170165
{
171166
_options = new ParserOptions();
@@ -179,7 +174,6 @@ public StreamingBinaryMultipartFormDataParser(Stream stream, ParserOptions optio
179174
_options = options;
180175
}
181176

182-
_stream = stream;
183177
readEndBoundary = false;
184178
}
185179

Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.IO;
32
using System.Text;
43
using System.Threading;
@@ -100,19 +99,15 @@ public StreamingMultipartFormDataParser(Stream stream, Encoding encoding, int bi
10099
/// By default the parser will throw an exception if it encounters an invalid part. set this to true to ignore invalid parts.
101100
/// </param>
102101
public StreamingMultipartFormDataParser(Stream stream, string boundary = null, Encoding encoding = null, int binaryBufferSize = Constants.DefaultBufferSize, string[] binaryMimeTypes = null, bool ignoreInvalidParts = false)
103-
{
104-
if (stream == null || stream == Stream.Null) { throw new ArgumentNullException(nameof(stream)); }
105-
106-
_options = new ParserOptions
102+
: this(stream, new ParserOptions
107103
{
108-
Boundary = boundary,
109-
Encoding = encoding ?? Constants.DefaultEncoding,
110104
BinaryBufferSize = binaryBufferSize,
111105
BinaryMimeTypes = binaryMimeTypes ?? Constants.DefaultBinaryMimeTypes,
106+
Boundary = boundary,
107+
Encoding = encoding ?? Constants.DefaultEncoding,
112108
IgnoreInvalidParts = ignoreInvalidParts
113-
};
114-
115-
_stream = stream;
109+
})
110+
{
116111
}
117112

118113
/// <summary>
@@ -127,10 +122,8 @@ public StreamingMultipartFormDataParser(Stream stream, string boundary = null, E
127122
/// </param>
128123
public StreamingMultipartFormDataParser(Stream stream, ParserOptions options)
129124
{
130-
if (stream == null || stream == Stream.Null) { throw new ArgumentNullException(nameof(stream)); }
131-
132-
_options = options ?? new ParserOptions();
133125
_stream = stream;
126+
_options = options;
134127
}
135128

136129
/// <summary>

0 commit comments

Comments
 (0)