Skip to content

Commit

Permalink
Merge branch 'zzzprojects:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
rwecho authored Jul 25, 2023
2 parents 0451ed8 + 2e98e14 commit aa54c1b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 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 @@ -30,7 +30,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 @@ -51,7 +51,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 @@ -73,7 +73,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 @@ -176,7 +176,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,
Expand All @@ -195,7 +195,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 @@ -386,6 +386,9 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
}
#endregion targetObject_NOTDefined_XPath
}



}


Expand Down Expand Up @@ -629,10 +632,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 @@ -729,7 +734,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 @@ -752,6 +757,8 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString());
};
}


}


Expand All @@ -761,7 +768,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 @@ -885,18 +892,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 @@ -910,18 +917,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 @@ -936,18 +943,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 @@ -956,7 +963,7 @@ public MissingXPathException(string message, Exception inner) : base(message, in

}

#if FX20
#if FX20
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method |
Expand Down
21 changes: 21 additions & 0 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,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 @@ -802,6 +803,17 @@ internal static HttpClient GetSharedHttpClient(string userAgent)
/// <value>The automatic decompression.</value>
public DecompressionMethods AutomaticDecompression { get; set; }

/// <summary>
/// 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.</value>
public int? MaxAutoRedirects
{
set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } }
get { return _maxAutoRedirects; }
}

/// <summary>
/// Gets or sets the timeout value in milliseconds. Must be greater than zero. A value of -1 sets the timeout to be infinite.
/// </summary>
Expand Down Expand Up @@ -1581,6 +1593,10 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
bool oldFile = false;

req = WebRequest.Create(uri) as HttpWebRequest;
if (MaxAutoRedirects.HasValue)
{
req.MaximumAutomaticRedirections = MaxAutoRedirects.Value;
}
req.Timeout = Timeout;
req.Method = method;
req.UserAgent = UserAgent;
Expand Down Expand Up @@ -1854,6 +1870,11 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout);

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

if(CaptureRedirect)
{
handler.AllowAutoRedirect = false;
Expand Down

0 comments on commit aa54c1b

Please sign in to comment.