From 2e98e144d8a89c0c0a8a8482fb6c4ee7bfaf0ec8 Mon Sep 17 00:00:00 2001 From: Jonathan Magnan Date: Tue, 18 Jul 2023 22:52:43 -0400 Subject: [PATCH] Add optional `int?` change for `_maxAutoRedirects` --- .../HtmlNode.Encapsulator.cs | 47 +++++++++++-------- src/HtmlAgilityPack.Shared/HtmlWeb.cs | 17 +++++-- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs b/src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs index b1ed0c2e..cf755505 100644 --- a/src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs +++ b/src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs @@ -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 @@ -29,7 +29,7 @@ public partial class HtmlNode /// Why it's thrown. /// Why it's thrown. /// Why it's thrown. - /// Why it's thrown. + /// Why it's thrown. /// Why it's thrown. public T GetEncapsulatedData() { @@ -50,7 +50,7 @@ public T GetEncapsulatedData() /// Why it's thrown. /// Why it's thrown. /// Why it's thrown. - /// Why it's thrown. + /// Why it's thrown. /// Why it's thrown. public T GetEncapsulatedData(HtmlDocument htmlDocument) { @@ -72,7 +72,7 @@ public T GetEncapsulatedData(HtmlDocument htmlDocument) /// Why it's thrown. /// Why it's thrown. /// Why it's thrown. - /// Why it's thrown. + /// Why it's thrown. /// Why it's thrown. public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = null) { @@ -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)); @@ -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(htmlNode, xPathAttribute); } @@ -381,6 +381,9 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n } #endregion targetObject_NOTDefined_XPath } + + + } @@ -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; } @@ -724,7 +729,7 @@ internal static int CountOfIEnumerable(this IEnumerable source) counter++; } return counter; - } + } /// /// Return html part of based on @@ -747,6 +752,8 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString()); }; } + + } @@ -756,7 +763,7 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT public enum ReturnType { /// - /// The text between the start and end tags of the object. + /// The text between the start and end tags of the object. /// InnerText, @@ -855,18 +862,18 @@ public sealed class SkipNodeNotFoundAttribute : Attribute public class NodeNotFoundException : Exception { /// - /// + /// /// public NodeNotFoundException() { } /// - /// + /// /// /// public NodeNotFoundException(string message) : base(message) { } /// - /// + /// /// /// /// @@ -880,18 +887,18 @@ public NodeNotFoundException(string message, Exception inner) : base(message, in public class NodeAttributeNotFoundException : Exception { /// - /// + /// /// public NodeAttributeNotFoundException() { } /// - /// + /// /// /// public NodeAttributeNotFoundException(string message) : base(message) { } /// - /// + /// /// /// /// @@ -906,18 +913,18 @@ public class MissingXPathException : Exception { /// - /// + /// /// public MissingXPathException() { } /// - /// + /// /// /// public MissingXPathException(string message) : base(message) { } /// - /// + /// /// /// /// @@ -926,7 +933,7 @@ public MissingXPathException(string message, Exception inner) : base(message, in } -#if FX20 +#if FX20 namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Method | diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index 81247119..af7c1eaf 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -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; @@ -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; /// /// Occurs after an HTTP request has been executed. @@ -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'. /// - /// Must be greater than 0, Default is 50. - public int MaxAutoRedirects + /// Must be greater than 0. + public int? MaxAutoRedirects { set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } } get { return _maxAutoRedirects; } @@ -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; @@ -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) {