Skip to content

Commit

Permalink
Revert "core: refactor http webclient part 7 #8529 (#7689)"
Browse files Browse the repository at this point in the history
This reverts commit 79fa3b9.
  • Loading branch information
ngosang committed Sep 21, 2020
1 parent 08c41c6 commit 5f457fc
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 43 deletions.
50 changes: 7 additions & 43 deletions src/Jackett.Common/Utils/Clients/BaseWebResult.cs
@@ -1,58 +1,22 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Jackett.Common.Utils.Clients
{
public abstract class BaseWebResult
{
private Encoding _encoding;

public Encoding Encoding
{
get
{
if (_encoding != null)
return _encoding;
if (Request.Encoding != null)
_encoding = Request.Encoding;
else if (Headers.ContainsKey("content-type"))
{
var charsetRegexMatch = Regex.Match(Headers["content-type"][0], @"charset=([\w-]+)", RegexOptions.Compiled);
if (charsetRegexMatch.Success)
{
var charset = charsetRegexMatch.Groups[1].Value;
try
{
_encoding = Encoding.GetEncoding(charset);
}
catch (ArgumentException)
{
// Encoding not found or not enabled on current machine.
}
}
}

_encoding ??= Encoding.UTF8;

return _encoding;
}
set => _encoding = value;
}

public Encoding Encoding { get; set; }
public HttpStatusCode Status { get; set; }
public string Cookies { get; set; }
public string RedirectingTo { get; set; }
public WebRequest Request { get; set; }
public Dictionary<string, string[]> Headers { get; protected set; } =
new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, string[]> Headers = new Dictionary<string, string[]>();

public bool IsRedirect => Status == HttpStatusCode.Redirect ||
Status == HttpStatusCode.RedirectKeepVerb ||
Status == HttpStatusCode.RedirectMethod ||
Status == HttpStatusCode.Found ||
Status == HttpStatusCode.MovedPermanently;
public bool IsRedirect => Status == System.Net.HttpStatusCode.Redirect ||
Status == System.Net.HttpStatusCode.RedirectKeepVerb ||
Status == System.Net.HttpStatusCode.RedirectMethod ||
Status == System.Net.HttpStatusCode.Found ||
Status == System.Net.HttpStatusCode.MovedPermanently;
}
}
34 changes: 34 additions & 0 deletions src/Jackett.Common/Utils/Clients/HttpWebClient.cs
Expand Up @@ -294,6 +294,40 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
result.Cookies = cookieBuilder.ToString().Trim();
}
ServerUtil.ResureRedirectIsFullyQualified(webRequest, result);
Encoding encoding = null;
if (webRequest.Encoding != null)
{
encoding = webRequest.Encoding;
}
else if (result.Headers.ContainsKey("content-type"))
{
var CharsetRegex = new Regex(@"charset=([\w-]+)", RegexOptions.Compiled);
var CharsetRegexMatch = CharsetRegex.Match(result.Headers["content-type"][0]);
if (CharsetRegexMatch.Success)
{
var charset = CharsetRegexMatch.Groups[1].Value;
try
{
encoding = Encoding.GetEncoding(charset);
}
catch (Exception ex)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Error loading encoding {2} based on header {3}: {4}", ClientType, webRequest.Url, charset, result.Headers["content-type"][0], ex));
}
}
else
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Got header without charset: {2}", ClientType, webRequest.Url, result.Headers["content-type"][0]));
}
}

if (encoding == null)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): No encoding detected, defaulting to UTF-8", ClientType, webRequest.Url));
encoding = Encoding.UTF8;
}

result.Encoding = encoding;
return result;
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/Jackett.Common/Utils/Clients/HttpWebClient2.cs
Expand Up @@ -314,6 +314,40 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
result.Cookies = cookieBuilder.ToString().Trim();
}
ServerUtil.ResureRedirectIsFullyQualified(webRequest, result);
Encoding encoding = null;
if (webRequest.Encoding != null)
{
encoding = webRequest.Encoding;
}
else if (result.Headers.ContainsKey("content-type"))
{
var CharsetRegex = new Regex(@"charset=([\w-]+)", RegexOptions.Compiled);
var CharsetRegexMatch = CharsetRegex.Match(result.Headers["content-type"][0]);
if (CharsetRegexMatch.Success)
{
var charset = CharsetRegexMatch.Groups[1].Value;
try
{
encoding = Encoding.GetEncoding(charset);
}
catch (Exception ex)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Error loading encoding {2} based on header {3}: {4}", ClientType, webRequest.Url, charset, result.Headers["content-type"][0], ex));
}
}
else
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Got header without charset: {2}", ClientType, webRequest.Url, result.Headers["content-type"][0]));
}
}

if (encoding == null)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): No encoding detected, defaulting to UTF-8", ClientType, webRequest.Url));
encoding = Encoding.UTF8;
}

result.Encoding = encoding;
return result;
}

Expand Down
34 changes: 34 additions & 0 deletions src/Jackett.Common/Utils/Clients/HttpWebClient2NetCore.cs
Expand Up @@ -314,6 +314,40 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
result.Cookies = cookieBuilder.ToString().Trim();
}
ServerUtil.ResureRedirectIsFullyQualified(webRequest, result);
Encoding encoding = null;
if (webRequest.Encoding != null)
{
encoding = webRequest.Encoding;
}
else if (result.Headers.ContainsKey("content-type"))
{
var CharsetRegex = new Regex(@"charset=([\w-]+)", RegexOptions.Compiled);
var CharsetRegexMatch = CharsetRegex.Match(result.Headers["content-type"][0]);
if (CharsetRegexMatch.Success)
{
var charset = CharsetRegexMatch.Groups[1].Value;
try
{
encoding = Encoding.GetEncoding(charset);
}
catch (Exception ex)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Error loading encoding {2} based on header {3}: {4}", ClientType, webRequest.Url, charset, result.Headers["content-type"][0], ex));
}
}
else
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Got header without charset: {2}", ClientType, webRequest.Url, result.Headers["content-type"][0]));
}
}

if (encoding == null)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): No encoding detected, defaulting to UTF-8", ClientType, webRequest.Url));
encoding = Encoding.UTF8;
}

result.Encoding = encoding;
return result;
}

Expand Down
34 changes: 34 additions & 0 deletions src/Jackett.Common/Utils/Clients/HttpWebClientNetCore.cs
Expand Up @@ -297,6 +297,40 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
result.Cookies = cookieBuilder.ToString().Trim();
}
ServerUtil.ResureRedirectIsFullyQualified(webRequest, result);
Encoding encoding = null;
if (webRequest.Encoding != null)
{
encoding = webRequest.Encoding;
}
else if (result.Headers.ContainsKey("content-type"))
{
var CharsetRegex = new Regex(@"charset=([\w-]+)", RegexOptions.Compiled);
var CharsetRegexMatch = CharsetRegex.Match(result.Headers["content-type"][0]);
if (CharsetRegexMatch.Success)
{
var charset = CharsetRegexMatch.Groups[1].Value;
try
{
encoding = Encoding.GetEncoding(charset);
}
catch (Exception ex)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Error loading encoding {2} based on header {3}: {4}", ClientType, webRequest.Url, charset, result.Headers["content-type"][0], ex));
}
}
else
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): Got header without charset: {2}", ClientType, webRequest.Url, result.Headers["content-type"][0]));
}
}

if (encoding == null)
{
logger.Error(string.Format("WebClient({0}).GetString(Url:{1}): No encoding detected, defaulting to UTF-8", ClientType, webRequest.Url));
encoding = Encoding.UTF8;
}

result.Encoding = encoding;
return result;
}
}
Expand Down

0 comments on commit 5f457fc

Please sign in to comment.