Skip to content

Commit

Permalink
Add optional int? change for _maxAutoRedirects
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanMagnan committed Jul 19, 2023
1 parent 2cc71c3 commit 2e98e14
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
47 changes: 27 additions & 20 deletions src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
// Website & Documentation: http://html-agility-pack.net
// Forum & Issues: https://github.com/zzzprojects/html-agility-pack
// License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.

#if !METRO && !NETSTANDARD1_3

Expand All @@ -29,7 +29,7 @@ public partial class HtmlNode
/// <exception cref="XPathException">Why it's thrown.</exception>
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="Exception">Why it's thrown.</exception>
public T GetEncapsulatedData<T>()
{
Expand All @@ -50,7 +50,7 @@ public T GetEncapsulatedData<T>()
/// <exception cref="XPathException">Why it's thrown.</exception>
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="Exception">Why it's thrown.</exception>
public T GetEncapsulatedData<T>(HtmlDocument htmlDocument)
{
Expand All @@ -72,7 +72,7 @@ public T GetEncapsulatedData<T>(HtmlDocument htmlDocument)
/// <exception cref="XPathException">Why it's thrown.</exception>
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="Exception">Why it's thrown.</exception>
public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = null)
{
Expand Down Expand Up @@ -175,7 +175,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
// Property is None-IEnumerable HasXPath-user-defined class
if (propertyInfo.PropertyType.IsDefinedAttribute(typeof(HasXPathAttribute)) == true)
{
HtmlDocument innerHtmlDocument = new HtmlDocument();
HtmlDocument innerHtmlDocument = new HtmlDocument();

innerHtmlDocument.LoadHtml(Tools.GetHtmlForEncapsulation(htmlNode, xPathAttribute.NodeReturnType));

Expand All @@ -192,7 +192,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
{
string result = string.Empty;

if (xPathAttribute.AttributeName == null) // It target value of HTMLTag
if (xPathAttribute.AttributeName == null) // It target value of HTMLTag
{
result = Tools.GetNodeValueBasedOnXPathReturnType<string>(htmlNode, xPathAttribute);
}
Expand Down Expand Up @@ -381,6 +381,9 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
}
#endregion targetObject_NOTDefined_XPath
}



}


Expand Down Expand Up @@ -624,10 +627,12 @@ internal static IList GetNodesValuesBasedOnXPathReturnType(HtmlNodeCollection ht


IList result = listGenericType.CreateIListOfType();

foreach (HtmlNode node in htmlNodeCollection)
{
result.Add(Convert.ChangeType(GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType), listGenericType));
result.Add(Convert.ChangeType(GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType), listGenericType));
}

return result;
}

Expand Down Expand Up @@ -724,7 +729,7 @@ internal static int CountOfIEnumerable<T>(this IEnumerable<T> source)
counter++;
}
return counter;
}
}

/// <summary>
/// Return html part of <see cref="HtmlNode"/> based on <see cref="ReturnType"/>
Expand All @@ -747,6 +752,8 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString());
};
}


}


Expand All @@ -756,7 +763,7 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
public enum ReturnType
{
/// <summary>
/// The text between the start and end tags of the object.
/// The text between the start and end tags of the object.
/// </summary>
InnerText,

Expand Down Expand Up @@ -855,18 +862,18 @@ public sealed class SkipNodeNotFoundAttribute : Attribute
public class NodeNotFoundException : Exception
{
/// <summary>
///
///
/// </summary>
public NodeNotFoundException() { }

/// <summary>
///
///
/// </summary>
/// <param name="message"></param>
public NodeNotFoundException(string message) : base(message) { }

/// <summary>
///
///
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
Expand All @@ -880,18 +887,18 @@ public NodeNotFoundException(string message, Exception inner) : base(message, in
public class NodeAttributeNotFoundException : Exception
{
/// <summary>
///
///
/// </summary>
public NodeAttributeNotFoundException() { }

/// <summary>
///
///
/// </summary>
/// <param name="message"></param>
public NodeAttributeNotFoundException(string message) : base(message) { }

/// <summary>
///
///
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
Expand All @@ -906,18 +913,18 @@ public class MissingXPathException : Exception
{

/// <summary>
///
///
/// </summary>
public MissingXPathException() { }

/// <summary>
///
///
/// </summary>
/// <param name="message"></param>
public MissingXPathException(string message) : base(message) { }

/// <summary>
///
///
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
Expand All @@ -926,7 +933,7 @@ public MissingXPathException(string message, Exception inner) : base(message, in

}

#if FX20
#if FX20
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method |
Expand Down
17 changes: 12 additions & 5 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public partial class HtmlWeb

private string _cachePath;
private bool _fromCache;
private int _maxAutoRedirects = 50;
private int _requestDuration;
private Uri _responseUri;
private HttpStatusCode _statusCode = HttpStatusCode.OK;
Expand All @@ -100,6 +99,7 @@ public partial class HtmlWeb
private bool _usingCacheIfExists;
private string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x";
private int _timeout = 100000;
private int? _maxAutoRedirects;

/// <summary>
/// Occurs after an HTTP request has been executed.
Expand Down Expand Up @@ -807,8 +807,8 @@ internal static HttpClient GetSharedHttpClient(string userAgent)
/// Maximum number of redirects that will be followed.
/// To disable redirects, do not set the value to 0, please set CaptureRedirect to 'true'.
/// </summary>
/// <value>Must be greater than 0, Default is 50.</value>
public int MaxAutoRedirects
/// <value>Must be greater than 0.</value>
public int? MaxAutoRedirects
{
set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } }
get { return _maxAutoRedirects; }
Expand Down Expand Up @@ -1593,7 +1593,10 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
bool oldFile = false;

req = WebRequest.Create(uri) as HttpWebRequest;
req.MaximumAutomaticRedirections = MaxAutoRedirects;
if (MaxAutoRedirects.HasValue)
{
req.MaximumAutomaticRedirections = MaxAutoRedirects.Value;
}
req.Timeout = Timeout;
req.Method = method;
req.UserAgent = UserAgent;
Expand Down Expand Up @@ -1866,7 +1869,11 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout);
handler.MaxAutomaticRedirections = MaxAutoRedirects;

if (MaxAutoRedirects.HasValue)
{
handler.MaxAutomaticRedirections = MaxAutoRedirects.Value;
}

if(CaptureRedirect)
{
Expand Down

0 comments on commit 2e98e14

Please sign in to comment.