Skip to content

Commit

Permalink
core: refactor http webclient part 4 #8529 (#7653)
Browse files Browse the repository at this point in the history
Change byte result name to ContentBytes in preparation for merge
  • Loading branch information
cadatoiva authored and ngosang committed Sep 19, 2020
1 parent 823ed85 commit c605c9a
Show file tree
Hide file tree
Showing 18 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Jackett.Common/Indexers/Abstract/XtremeZoneTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public override async Task<byte[]> Download(Uri link)
response = await RequestBytesWithCookies(link.ToString(), headers: GetSearchHeaders());
}
else if (response.Status != HttpStatusCode.OK)
throw new Exception($"Unknown error in download: {response.Content}");
return response.Content;
throw new Exception($"Unknown error in download: {response.ContentBytes}");
return response.ContentBytes;
}

private Dictionary<string, string> GetSearchHeaders() => new Dictionary<string, string>
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/BakaBT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public override async Task<byte[]> Download(Uri link)
throw new Exception("Unable to find download link.");

var response = await RequestBytesWithCookies(SiteLink + downloadLink);
return response.Content;
return response.ContentBytes;
}
}
}
6 changes: 3 additions & 3 deletions src/Jackett.Common/Indexers/BaseIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,12 @@ protected async Task<byte[]> Download(Uri link, RequestType method, string refer
if (response.Status != System.Net.HttpStatusCode.OK && response.Status != System.Net.HttpStatusCode.Continue && response.Status != System.Net.HttpStatusCode.PartialContent)
{
logger.Error("Failed download cookies: " + CookieHeader);
if (response.Content != null)
logger.Error("Failed download response:\n" + Encoding.UTF8.GetString(response.Content));
if (response.ContentBytes != null)
logger.Error("Failed download response:\n" + Encoding.UTF8.GetString(response.ContentBytes));
throw new Exception($"Remote server returned {response.Status.ToString()}" + (response.IsRedirect ? " => " + response.RedirectingTo : ""));
}

return response.Content;
return response.ContentBytes;
}

protected async Task<WebClientByteResult> RequestBytesWithCookiesAndRetry(string url, string cookieOverride = null, RequestType method = RequestType.GET, string referer = null, IEnumerable<KeyValuePair<string, string>> data = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/CardigannIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ public async Task<ConfigurationData> GetConfigurationForSetup(bool automaticlogi
var CaptchaImage = new ImageItem { Name = "Captcha Image" };
var CaptchaText = new StringItem { Name = "Captcha Text" };

CaptchaImage.Value = captchaImageData.Content;
CaptchaImage.Value = captchaImageData.ContentBytes;

configData.AddDynamic("CaptchaImage", CaptchaImage);
configData.AddDynamic("CaptchaText", CaptchaText);
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/LostFilm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public override async Task<ConfigurationData> GetConfigurationForSetup()
{
var captchaUrl = SiteLink + qCaptchaImg.GetAttribute("src");
var captchaImage = await RequestBytesWithCookies(captchaUrl, loginPage.Cookies);
configData.CaptchaImage.Value = captchaImage.Content;
configData.CaptchaImage.Value = captchaImage.ContentBytes;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/PixelHD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override async Task<ConfigurationData> GetConfigurationForSetup()
input_captcha = catchaInput.GetAttribute("name");

var captchaImage = await RequestBytesWithCookies(SiteLink + catchaImg.GetAttribute("src"), loginPage.Cookies, RequestType.GET, LoginUrl);
configData.CaptchaImage.Value = captchaImage.Content;
configData.CaptchaImage.Value = captchaImage.ContentBytes;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/PornoLab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public override async Task<ConfigurationData> GetConfigurationForSetup()
if (captchaimg != null)
{
var captchaImage = await RequestBytesWithCookies("https:" + captchaimg.GetAttribute("src"));
configData.CaptchaImage.Value = captchaImage.Content;
configData.CaptchaImage.Value = captchaImage.ContentBytes;

var codefield = LoginResultDocument.QuerySelector("input[name^=\"cap_code_\"]");
cap_code_field = codefield.GetAttribute("name");
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/RuTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ public override async Task<ConfigurationData> GetConfigurationForSetup()
if (captchaimg != null)
{
var captchaImage = await RequestBytesWithCookies(captchaimg.GetAttribute("src"));
configData.CaptchaImage.Value = captchaImage.Content;
configData.CaptchaImage.Value = captchaImage.ContentBytes;

var codefield = doc.QuerySelector("input[name^=\"cap_code_\"]");
_capCodeField = codefield.GetAttribute("name");
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/TorrentHeaven.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public override async Task<ConfigurationData> GetConfigurationForSetup()
{
var captchaUrl = SiteLink + qCaptchaImg.GetAttribute("src");
var captchaImage = await RequestBytesWithCookies(captchaUrl, loginPage.Cookies);
configData.CaptchaImage.Value = captchaImage.Content;
configData.CaptchaImage.Value = captchaImage.ContentBytes;
}
else
configData.CaptchaImage.Value = Array.Empty<byte>();
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Indexers/XSpeeds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public override async Task<ConfigurationData> GetConfigurationForSetup()
var captchaImageResponse = await RequestBytesWithCookies(captchaUrl, loginPage.Cookies, RequestType.GET, LandingUrl);

var captchaText = new StringItem { Name = "Captcha Text" };
var captchaImage = new ImageItem {Name = "Captcha Image", Value = captchaImageResponse.Content};
var captchaImage = new ImageItem {Name = "Captcha Image", Value = captchaImageResponse.ContentBytes};

configData.AddDynamic("CaptchaText", captchaText);
configData.AddDynamic("CaptchaImage", captchaImage);
Expand Down
4 changes: 2 additions & 2 deletions src/Jackett.Common/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ private async Task<string> DownloadRelease(List<Asset> assets, bool isWindows, s
if (isWindows)
{
var zipPath = Path.Combine(tempDir, "Update.zip");
File.WriteAllBytes(zipPath, data.Content);
File.WriteAllBytes(zipPath, data.ContentBytes);
var fastZip = new FastZip();
fastZip.ExtractZip(zipPath, tempDir, null);
}
else
{
var gzPath = Path.Combine(tempDir, "Update.tar.gz");
File.WriteAllBytes(gzPath, data.Content);
File.WriteAllBytes(gzPath, data.ContentBytes);
Stream inStream = File.OpenRead(gzPath);
Stream gzipStream = new GZipInputStream(inStream);

Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Utils/Clients/HttpWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
{
var result = new WebClientByteResult
{
Content = await response.Content.ReadAsByteArrayAsync()
ContentBytes = await response.Content.ReadAsByteArrayAsync()
};

foreach (var header in response.Headers)
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Utils/Clients/HttpWebClient2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)

var result = new WebClientByteResult
{
Content = await response.Content.ReadAsByteArrayAsync()
ContentBytes = await response.Content.ReadAsByteArrayAsync()
};

foreach (var header in response.Headers)
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Utils/Clients/HttpWebClient2NetCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)

var result = new WebClientByteResult
{
Content = await response.Content.ReadAsByteArrayAsync()
ContentBytes = await response.Content.ReadAsByteArrayAsync()
};

foreach (var header in response.Headers)
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Utils/Clients/HttpWebClientNetCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
{
var result = new WebClientByteResult
{
Content = await response.Content.ReadAsByteArrayAsync()
ContentBytes = await response.Content.ReadAsByteArrayAsync()
};

foreach (var header in response.Headers)
Expand Down
2 changes: 1 addition & 1 deletion src/Jackett.Common/Utils/Clients/WebByteResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Jackett.Common.Utils.Clients
{
public class WebClientByteResult : BaseWebResult
{
public byte[] Content { get; set; }
public byte[] ContentBytes { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Jackett.Common/Utils/Clients/WebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public virtual async Task<WebClientByteResult> GetBytes(WebRequest request)
var result = await Run(request);
lastRequest = DateTime.Now;
result.Request = request;
logger.Debug(string.Format("WebClient({0}): Returning {1} => {2} bytes", ClientType, result.Status, (result.IsRedirect ? result.RedirectingTo + " " : "") + (result.Content == null ? "<NULL>" : result.Content.Length.ToString())));
logger.Debug(string.Format("WebClient({0}): Returning {1} => {2} bytes", ClientType, result.Status, (result.IsRedirect ? result.RedirectingTo + " " : "") + (result.ContentBytes == null ? "<NULL>" : result.ContentBytes.Length.ToString())));
return result;
}

Expand All @@ -117,8 +117,8 @@ public virtual async Task<WebClientStringResult> GetString(WebRequest request)
var stringResult = Mapper.Map<WebClientStringResult>(result);

string decodedContent = null;
if (result.Content != null)
decodedContent = result.Encoding.GetString(result.Content);
if (result.ContentBytes != null)
decodedContent = result.Encoding.GetString(result.ContentBytes);

stringResult.ContentString = decodedContent;
logger.Debug(string.Format("WebClient({0}): Returning {1} => {2}", ClientType, result.Status, (result.IsRedirect ? result.RedirectingTo + " " : "") + (decodedContent == null ? "<NULL>" : decodedContent)));
Expand Down
6 changes: 3 additions & 3 deletions src/Jackett.Server/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ private static void InitAutomapper()
cfg.CreateMap<WebClientByteResult, WebClientStringResult>().ForMember(x => x.ContentString, opt => opt.Ignore()).AfterMap((be, str) =>
{
var encoding = be.Request.Encoding ?? Encoding.UTF8;
str.ContentString = encoding.GetString(be.Content);
str.ContentString = encoding.GetString(be.ContentBytes);
});
cfg.CreateMap<WebClientStringResult, WebClientByteResult>().ForMember(x => x.Content, opt => opt.Ignore()).AfterMap((str, be) =>
cfg.CreateMap<WebClientStringResult, WebClientByteResult>().ForMember(x => x.ContentBytes, opt => opt.Ignore()).AfterMap((str, be) =>
{
if (!string.IsNullOrEmpty(str.ContentString))
{
var encoding = str.Request.Encoding ?? Encoding.UTF8;
be.Content = encoding.GetBytes(str.ContentString);
be.ContentBytes = encoding.GetBytes(str.ContentString);
}
});
Expand Down

0 comments on commit c605c9a

Please sign in to comment.