core: refactor http webclient part 17 #8529 (#9589)

Print HTML in debug mode. Fix bugs in old implementation too
This commit is contained in:
Diego Heras 2020-09-22 03:06:23 +02:00 committed by GitHub
parent 17a32a06fc
commit 24bffe9dae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 6 deletions

View File

@ -159,17 +159,31 @@ namespace Jackett.Common.Utils.Clients
public virtual async Task<WebResult> GetResultAsync(WebRequest request) public virtual async Task<WebResult> GetResultAsync(WebRequest request)
{ {
logger.Debug(string.Format("WebClient({0}).GetResultAsync(Url:{1})", ClientType, request.Url)); logger.Debug($"WebClient({ClientType}).GetResultAsync(Method: {request.Type} Url: {request.Url})");
PrepareRequest(request); PrepareRequest(request);
await DelayRequest(request); await DelayRequest(request);
var result = await Run(request); var result = await Run(request);
lastRequest = DateTime.Now; lastRequest = DateTime.Now;
result.Request = request; result.Request = request;
logger.Debug(
string.Format( if (logger.IsDebugEnabled) // optimization to compute result.ContentString in debug mode only
"WebClient({0}): Returning {1} => {2} bytes", ClientType, result.Status, {
(result.IsRedirect ? result.RedirectingTo + " " : "") + var body = "";
(result.ContentBytes == null ? "<NULL>" : result.ContentBytes.Length.ToString()))); var bodySize = 0;
if (result.ContentBytes != null && result.ContentBytes.Length > 0)
{
bodySize = result.ContentBytes.Length;
var contentString = result.ContentString.Trim();
if (contentString.StartsWith("<") || contentString.StartsWith("{"))
body = "\n" + contentString;
else
body = " <BINARY>";
}
logger.Debug($@"WebClient({ClientType}): Returning {result.Status} => {
(result.IsRedirect ? result.RedirectingTo + " " : "")
}{bodySize} bytes{body}");
}
if (result.Headers.TryGetValue("server", out var server) && server[0] == "cloudflare-nginx") if (result.Headers.TryGetValue("server", out var server) && server[0] == "cloudflare-nginx")
result.ContentString = BrowserUtil.DecodeCloudFlareProtectedEmailFromHTML(result.ContentString); result.ContentString = BrowserUtil.DecodeCloudFlareProtectedEmailFromHTML(result.ContentString);
return result; return result;