shoko/Protocols/HttpProtoHandler.cs

38 lines
1.2 KiB
C#
Raw Permalink Normal View History

2023-10-02 18:49:24 +00:00
using System.Reflection;
2023-10-20 04:59:28 +00:00
using System;
2023-10-02 18:49:24 +00:00
namespace Shoko;
[Protocol("http")]
[Protocol("https")]
class HttpProtoHandler : ProtoHandler
{
public HttpProtoHandler(Uri url)
{
URL = url;
}
2023-10-20 04:59:28 +00:00
long _totalBytes = -1;
public override long TotalBytes => _totalBytes;
2023-10-02 18:49:24 +00:00
2023-10-20 04:59:28 +00:00
public override async Task Load()
2023-10-02 18:49:24 +00:00
{
2023-10-20 04:59:28 +00:00
var client = new HttpClient(){
2023-10-02 18:49:24 +00:00
BaseAddress = URL
};
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 shoko/" + Assembly.GetExecutingAssembly().GetName().Version.ToString());
2023-10-20 04:59:28 +00:00
var response = await client.GetAsync(URL, HttpCompletionOption.ResponseHeadersRead);
2023-10-02 18:49:24 +00:00
Headers = response.Content.Headers;
2023-10-20 04:59:28 +00:00
_totalBytes = response.Content.Headers.ContentLength ?? -1;
2023-10-02 18:49:24 +00:00
MediaType = response.Content.Headers.ContentType.MediaType ?? "text/html";
MediaTypeParams = response.Content.Headers.ContentType.Parameters.Select(x => new KeyValuePair<string, string>(x.Name, x.Value));
Status = string.Format("{0} {1}", (int)response.StatusCode, response.StatusCode.ToString());
2023-10-20 04:59:28 +00:00
var download = await response.Content.ReadAsStreamAsync();
Content = await Download(download);
OnLoaded();
2023-10-02 18:49:24 +00:00
}
public override void Render()
{
}
}