Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions Usenet/Nntp/Parsers/ArticleResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public NntpArticleResponse Parse(int code, string message, IEnumerable<string> d
return new NntpArticleResponse(code, message, true, new NntpArticle(number, messageId, null, null, null));
}

using (IEnumerator<string> enumerator = dataBlock.GetEnumerator())
// Materialize lines to avoid returning a body tied to a disposed enumerator
var lines = dataBlock?.ToList();

using (IEnumerator<string> enumerator = (lines ?? Enumerable.Empty<string>()).GetEnumerator())
{
// get headers if requested
MultiValueDictionary<string, string> headers = (requestType & ArticleRequestType.Head) == ArticleRequestType.Head
Expand All @@ -83,15 +86,8 @@ public NntpArticleResponse Parse(int code, string message, IEnumerable<string> d

// get body if requested
IEnumerable<string> bodyLines = (requestType & ArticleRequestType.Body) == ArticleRequestType.Body
? EnumerateBodyLines(enumerator)
: new string[0];

if (dataBlock is ICollection<string>)
{
// no need to keep enumerator if input is not a stream
// memoize the body lines
bodyLines = bodyLines.ToList();
}
? EnumerateBodyLines(enumerator).ToList()
: new List<string>(0);

return new NntpArticleResponse(
code, message, true,
Expand Down