shoko/Protocols/SpartanProtoHandler.cs

81 lines
2.1 KiB
C#
Raw Normal View History

2023-10-13 01:20:11 +00:00
using System.Net.Sockets;
using System.Text;
using System.Web;
namespace Shoko;
[Protocol("spartan")]
class SpartanProtoHandler : ProtoHandler
{
public SpartanProtoHandler(Uri url)
{
URL = url;
}
2023-10-20 04:59:28 +00:00
public override async Task Load()
2023-10-13 01:20:11 +00:00
{
var file = URL.AbsolutePath;
if(!file.StartsWith("/")) file = "/"+file;
var data = URL.Query;
if(data.StartsWith("?")) data.Remove(0,1);
var query = Encoding.UTF8.GetBytes(data);
var uri = Encoding.UTF8.GetBytes($"{URL.Host} {HttpUtility.UrlDecode(file)} {query.Length}\r\n");
var tcp = new TcpClient(URL.Host, URL.Port < 0 ? 300 : URL.Port);
var stream = tcp.GetStream();
2023-10-20 04:59:28 +00:00
await stream.WriteAsync(uri);
2023-10-13 01:20:11 +00:00
if(query.Length > 0)
2023-10-20 04:59:28 +00:00
await stream.WriteAsync(query);
2023-10-13 01:20:11 +00:00
var reader = new StreamReader(stream);
var header = reader.ReadLine();
var meta = header.Split(" ", 2, StringSplitOptions.TrimEntries);
Status = meta[0];
MediaType = "text/plain";
if(Status.StartsWith('2'))
{
if(meta.Length > 1)
{
var dict = new Dictionary<string, string>();
var type = meta[1].Split(";", StringSplitOptions.TrimEntries).ToList();
MediaType = type[0];
foreach (var item in type)
{
var val = item.Split("=", 2);
dict[val[0]] = val.Length > 1 ? val[1] : "";
}
MediaTypeParams = dict;
}
2023-10-20 04:59:28 +00:00
Content = await Download(stream);
2023-10-13 01:20:11 +00:00
}
else
{
byte[] content = new byte[]{};
if(meta.Length > 1)
{
if(Status.StartsWith('3'))
{
meta[1] = "=> "+meta[1];
MediaType = "text/gemini";
}
content = Encoding.UTF8.GetBytes(meta[1]);
}
Content = new MemoryStream(content);
}
2023-10-20 04:59:28 +00:00
OnLoaded();
2023-10-13 01:20:11 +00:00
}
public override void Render()
{
}
}