Skip to content

Commit 5dc45b7

Browse files
committed
Add tolerant parse mode for torrents
Using the tolerant mode skips validation of required fields
1 parent 29dafd6 commit 5dc45b7

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

BencodeNET/Parsing/BObjectParserList.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using BencodeNET.Objects;
56

67
namespace BencodeNET.Parsing
@@ -118,6 +119,16 @@ public IBObjectParser<T> Get<T>() where T : IBObject
118119
return Get(typeof(T)) as IBObjectParser<T>;
119120
}
120121

122+
/// <summary>
123+
/// Gets the specific parser of the type specified or null if not found.
124+
/// </summary>
125+
/// <typeparam name="T">The parser type to get.</typeparam>
126+
/// <returns>The parser of the specified type or null if there isn't one.</returns>
127+
public T GetSpecific<T>() where T : class, IBObjectParser
128+
{
129+
return Parsers.FirstOrDefault(x => x.Value is T).Value as T;
130+
}
131+
121132
/// <summary>
122133
/// Removes the parser for the specified type.
123134
/// </summary>
@@ -137,7 +148,6 @@ public IBObjectParser<T> Get<T>() where T : IBObject
137148
/// </summary>
138149
public void Clear() => Parsers.Clear();
139150

140-
141151
/// <summary>
142152
/// Returns an enumerator that iterates through the collection.
143153
/// </summary>

BencodeNET/Parsing/TorrentParser.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,34 @@ namespace BencodeNET.Parsing
1414
/// </summary>
1515
public class TorrentParser : BObjectParser<Torrent>
1616
{
17+
/// <summary>
18+
/// The parsing mode used when parsing torrents.
19+
/// You can use <see cref="TorrentParserMode.Tolerant"/> if your torrent files are not always following the torrent specification.
20+
/// </summary>
21+
public TorrentParserMode ParseMode { get; set; }
22+
1723
/// <summary>
1824
/// Creates an instance using the specified <see cref="IBencodeParser"/> for parsing
1925
/// the torrent <see cref="BDictionary"/>.
2026
/// </summary>
2127
/// <param name="bencodeParser">The parser used for parsing the torrent <see cref="BDictionary"/>.</param>
2228
public TorrentParser(IBencodeParser bencodeParser)
29+
: this(bencodeParser, TorrentParserMode.Strict)
30+
{
31+
}
32+
33+
/// <summary>
34+
/// Creates an instance using the specified <see cref="IBencodeParser"/> for parsing
35+
/// the torrent <see cref="BDictionary"/>.
36+
/// </summary>
37+
/// <param name="bencodeParser">The parser used for parsing the torrent <see cref="BDictionary"/>.</param>
38+
/// <param name="torrentParserMode">The parsing mode to use.</param>
39+
public TorrentParser(IBencodeParser bencodeParser, TorrentParserMode torrentParserMode)
2340
{
2441
if (bencodeParser == null) throw new ArgumentNullException(nameof(bencodeParser));
2542

2643
BencodeParser = bencodeParser;
44+
ParseMode = torrentParserMode;
2745
}
2846

2947
/// <summary>
@@ -54,7 +72,8 @@ public override Torrent Parse(BencodeStream stream)
5472
/// <returns>A <see cref="Torrent"/> matching the input.</returns>
5573
protected Torrent CreateTorrent(BDictionary data)
5674
{
57-
EnsureValidTorrentData(data);
75+
if (ParseMode == TorrentParserMode.Strict)
76+
EnsureValidTorrentData(data);
5877

5978
var info = data.Get<BDictionary>(TorrentFields.Info);
6079

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace BencodeNET.Parsing
2+
{
3+
/// <summary>
4+
/// Determines how strict to be when parsing torrent files.
5+
/// </summary>
6+
public enum TorrentParserMode
7+
{
8+
/// <summary>
9+
/// The parser will throw an exception if some parts of the torrent specification is not followed.
10+
/// </summary>
11+
Strict,
12+
13+
/// <summary>
14+
/// The parser will ignore stuff that doesn't follow the torrent specifications.
15+
/// </summary>
16+
Tolerant
17+
}
18+
}

0 commit comments

Comments
 (0)