Skip to content

Commit

Permalink
fix #172
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Oct 30, 2023
1 parent ece1d9d commit 936b7bb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 80 deletions.
6 changes: 3 additions & 3 deletions ExClient/Client-User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ internal string PassHash
}
}

public Task<UserInfo> FetchCurrentUserInfoAsync()
public async Task<UserInfo> FetchCurrentUserInfoAsync()
{
if (UserId < 0)
throw new InvalidOperationException("Hasn't log in");
return UserInfo.FeachAsync(UserId);
return await UserInfo.FetchAsync(UserId);
}
}
}
}
4 changes: 2 additions & 2 deletions ExClient/Forums/UserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UserInfo
/// <summary>
/// Fetch user info from forum.e-hentai.org/index?showuser={<paramref name="userID"/>}.
/// </summary>
public static Task<UserInfo> FeachAsync(long userID, CancellationToken token = default)
public static Task<UserInfo> FetchAsync(long userID, CancellationToken token = default)
{
if (userID <= 0)
throw new ArgumentOutOfRangeException(nameof(userID));
Expand Down Expand Up @@ -112,4 +112,4 @@ private UserInfo() { }
[JsonProperty]
public Uri Avatar { get; private set; }
}
}
}
121 changes: 46 additions & 75 deletions ExClient/Internal/MyHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

using System;
using System.Collections.Generic;

using System.IO;
using System.Text;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Storage.Streams;
Expand All @@ -17,15 +18,12 @@

using IHttpAsyncOperation = Windows.Foundation.IAsyncOperationWithProgress<Windows.Web.Http.HttpResponseMessage, Windows.Web.Http.HttpProgress>;

namespace ExClient.Internal
{
namespace ExClient.Internal {
/*
* 由于使用了自定义 Filter 后发生异常会丢失异常详细信息,故使用此类封装,以保留异常信息。
* */
internal class MyHttpClient : IDisposable
{
private void _CheckStringResponse(string responseString)
{
internal class MyHttpClient : IDisposable {
private void _CheckStringResponse(string responseString) {
if (responseString.Length > 200)
return;
if (responseString.Contains("This gallery is currently unavailable."))
Expand All @@ -34,28 +32,23 @@ private void _CheckStringResponse(string responseString)
throw new InvalidOperationException(LocalizedStrings.Resources.IPBannedOfPageLoad);
if (responseString.Contains("This page is currently not available, as your account has been suspended."))
throw new InvalidOperationException(LocalizedStrings.Resources.AccountSuspended);
if (responseString.Contains("https://exhentai.org/img/kokomade.jpg"))
{
if (responseString.Contains("https://exhentai.org/img/kokomade.jpg")) {
_ = _Owner.ResetExCookieAsync();
throw new InvalidOperationException(LocalizedStrings.Resources.Kokomade);
}
}

private void _CheckSadPanda(HttpResponseMessage response)
{
if (response.Content.Headers.ContentDisposition?.FileName == "sadpanda.jpg")
{
private void _CheckSadPanda(HttpResponseMessage response) {
if (response.Content.Headers.ContentDisposition?.FileName == "sadpanda.jpg") {
_ = _Owner.ResetExCookieAsync();
throw new InvalidOperationException(LocalizedStrings.Resources.SadPanda);
}
}

public MyHttpClient(Client owner, HttpClient inner)
{
public MyHttpClient(Client owner, HttpClient inner) {
_Inner = inner;
_Owner = owner;
_Nocookie = new HttpClient(new HttpBaseProtocolFilter
{
_Nocookie = new HttpClient(new HttpBaseProtocolFilter {
CookieUsageBehavior = HttpCookieUsageBehavior.NoCookies,
});

Expand All @@ -64,8 +57,7 @@ public MyHttpClient(Client owner, HttpClient inner)
_Nocookie.DefaultRequestHeaders.UserAgent.Add(ua);
}

private void _ReformUri(ref Uri uri)
{
private void _ReformUri(ref Uri uri) {
if (!uri.IsAbsoluteUri)
uri = new Uri(_Owner.Uris.RootUri, uri);
}
Expand All @@ -76,37 +68,30 @@ private void _ReformUri(ref Uri uri)

public HttpRequestHeaderCollection DefaultRequestHeaders => _Inner.DefaultRequestHeaders;

public IHttpAsyncOperation GetAsync(Uri uri, HttpCompletionOption completionOption, bool checkStatusCode)
{
public IHttpAsyncOperation GetAsync(Uri uri, HttpCompletionOption completionOption, bool checkStatusCode) {
_ReformUri(ref uri);
var client = uri.Host.EndsWith("hentai.org") ? _Inner : _Nocookie;
var request = client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
if (completionOption == HttpCompletionOption.ResponseHeadersRead)
{
if (completionOption == HttpCompletionOption.ResponseHeadersRead) {
return request;
}

return Run<HttpResponseMessage, HttpProgress>(async (token, progress) =>
{
return Run<HttpResponseMessage, HttpProgress>(async (token, progress) => {
token.Register(request.Cancel);
request.Progress = (t, p) => progress.Report(p);
var response = await request;
_CheckSadPanda(response);
if (checkStatusCode)
{
if (checkStatusCode) {
response.EnsureSuccessStatusCode();
}

var buffer = response.Content.BufferAllAsync();
if (!response.Content.TryComputeLength(out var length))
{
if (!response.Content.TryComputeLength(out var length)) {
var contentLength = response.Content.Headers.ContentLength;
length = contentLength ?? ulong.MaxValue;
}
buffer.Progress = (t, p) =>
{
progress.Report(new HttpProgress
{
buffer.Progress = (t, p) => {
progress.Report(new HttpProgress {
TotalBytesToReceive = length,
BytesReceived = p,
Stage = HttpProgressStage.ReceivingContent
Expand All @@ -117,16 +102,13 @@ public IHttpAsyncOperation GetAsync(Uri uri, HttpCompletionOption completionOpti
});
}

public IHttpAsyncOperation GetAsync(Uri uri)
{
public IHttpAsyncOperation GetAsync(Uri uri) {
return GetAsync(uri, HttpCompletionOption.ResponseContentRead, true);
}

public IAsyncOperationWithProgress<IBuffer, HttpProgress> GetBufferAsync(Uri uri)
{
public IAsyncOperationWithProgress<IBuffer, HttpProgress> GetBufferAsync(Uri uri) {
_ReformUri(ref uri);
return Run<IBuffer, HttpProgress>(async (token, progress) =>
{
return Run<IBuffer, HttpProgress>(async (token, progress) => {
var request = GetAsync(uri);
token.Register(request.Cancel);
request.Progress = (t, p) => progress.Report(p);
Expand All @@ -135,11 +117,9 @@ public IAsyncOperationWithProgress<IBuffer, HttpProgress> GetBufferAsync(Uri uri
});
}

public IAsyncOperationWithProgress<IInputStream, HttpProgress> GetInputStreamAsync(Uri uri)
{
public IAsyncOperationWithProgress<IInputStream, HttpProgress> GetInputStreamAsync(Uri uri) {
_ReformUri(ref uri);
return Run<IInputStream, HttpProgress>(async (token, progress) =>
{
return Run<IInputStream, HttpProgress>(async (token, progress) => {
var request = GetAsync(uri);
token.Register(request.Cancel);
request.Progress = (t, p) => progress.Report(p);
Expand All @@ -148,11 +128,9 @@ public IAsyncOperationWithProgress<IInputStream, HttpProgress> GetInputStreamAsy
});
}

public IAsyncOperationWithProgress<string, HttpProgress> GetStringAsync(Uri uri)
{
public IAsyncOperationWithProgress<string, HttpProgress> GetStringAsync(Uri uri) {
_ReformUri(ref uri);
return Run<string, HttpProgress>(async (token, progress) =>
{
return Run<string, HttpProgress>(async (token, progress) => {
var request = GetAsync(uri);
token.Register(request.Cancel);
request.Progress = (t, p) => progress.Report(p);
Expand All @@ -163,24 +141,23 @@ public IAsyncOperationWithProgress<string, HttpProgress> GetStringAsync(Uri uri)
});
}

public IAsyncOperationWithProgress<HtmlDocument, HttpProgress> GetDocumentAsync(Uri uri)
{
public IAsyncOperationWithProgress<HtmlDocument, HttpProgress> GetDocumentAsync(Uri uri) {
_ReformUri(ref uri);
return Run<HtmlDocument, HttpProgress>(async (token, progress) =>
{
return Run<HtmlDocument, HttpProgress>(async (token, progress) => {
var request = GetAsync(uri, HttpCompletionOption.ResponseHeadersRead, false);
token.Register(request.Cancel);
request.Progress = (t, p) => progress.Report(p);
var doc = new HtmlDocument();
var response = await request;
_CheckSadPanda(response);
var resstr = await response.Content.ReadAsStringAsync();
_CheckStringResponse(resstr);
doc.LoadHtml(resstr);
var resStream = await response.Content.ReadAsInputStreamAsync();
using var reader = new StreamReader(resStream.AsStreamForRead(), Encoding.UTF8);
var resStr = reader.ReadToEnd();
_CheckStringResponse(resStr);
doc.LoadHtml(resStr);
var rootNode = doc.DocumentNode;

do
{
do {
if (response.StatusCode != HttpStatusCode.NotFound)
break;

Expand All @@ -195,43 +172,38 @@ public IAsyncOperationWithProgress<HtmlDocument, HttpProgress> GetDocumentAsync(
break;

var msg = error.GetInnerText();
switch (msg)
{
case "This gallery has been removed or is unavailable.":
throw new InvalidOperationException(LocalizedStrings.Resources.GalleryRemoved);
case "This gallery has been locked for review. Please check back later.":
throw new InvalidOperationException(LocalizedStrings.Resources.GalleryReviewing);
default:
throw new InvalidOperationException(msg);
switch (msg) {
case "This gallery has been removed or is unavailable.":
throw new InvalidOperationException(LocalizedStrings.Resources.GalleryRemoved);
case "This gallery has been locked for review. Please check back later.":
throw new InvalidOperationException(LocalizedStrings.Resources.GalleryReviewing);
default:
throw new InvalidOperationException(msg);
}
} while (false);
response.EnsureSuccessStatusCode();
HentaiVerseInfo.AnalyzePage(doc);
ApiToken.Update(resstr);
ApiToken.Update(resStr);
return doc;
});
}

public IHttpAsyncOperation PostAsync(Uri uri, IHttpContent content)
{
public IHttpAsyncOperation PostAsync(Uri uri, IHttpContent content) {
_ReformUri(ref uri);
return _Inner.PostAsync(uri, content);
}

public IHttpAsyncOperation PostAsync(Uri uri, params KeyValuePair<string, string>[] content)
=> PostAsync(uri, (IEnumerable<KeyValuePair<string, string>>)content);

public IHttpAsyncOperation PostAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> content)
{
public IHttpAsyncOperation PostAsync(Uri uri, IEnumerable<KeyValuePair<string, string>> content) {
_ReformUri(ref uri);
return _Inner.PostAsync(uri, new HttpFormUrlEncodedContent(content));
}

public IAsyncOperationWithProgress<string, HttpProgress> PostStringAsync(Uri uri, IHttpContent content)
{
public IAsyncOperationWithProgress<string, HttpProgress> PostStringAsync(Uri uri, IHttpContent content) {
_ReformUri(ref uri);
return Run<string, HttpProgress>(async (token, progress) =>
{
return Run<string, HttpProgress>(async (token, progress) => {
var op = PostAsync(uri, content);
token.Register(op.Cancel);
op.Progress = (sender, value) => progress.Report(value);
Expand All @@ -243,8 +215,7 @@ public IAsyncOperationWithProgress<string, HttpProgress> PostStringAsync(Uri uri
});
}

public void Dispose()
{
public void Dispose() {
_Inner.Dispose();
_Nocookie.Dispose();
}
Expand Down

0 comments on commit 936b7bb

Please sign in to comment.