shoko/Protocols/GopherProtoHandler.cs

80 lines
1.8 KiB
C#

using System.Net.Sockets;
using System.Text;
using System.Web;
namespace Shoko;
[Protocol("gopher")]
class GopherProtoHandler : ProtoHandler
{
public GopherProtoHandler(Uri url)
{
URL = url;
}
public override async Task Load()
{
var type = "0";
var file = URL.PathAndQuery;
var paths = file.Split("/", StringSplitOptions.RemoveEmptyEntries).ToList();
if(paths.Count > 0)
{
if(paths[0].Length == 1)
{
type = paths[0];
paths.RemoveAt(0);
}
else
{
type = paths[0][0].ToString();
paths[0] = paths[0][1..];
}
}
else
type = "1";
switch(type)
{
case "0":
MediaType = "text/plain";
break;
case "1": // menu
case "3":
case "7":
MediaType = "text/gopher-menu";
break;
case "g":
MediaType = "image/gif";
break;
case "p":
MediaType = "image/png";
break;
case "I":
case ":":
MediaType = "image/*";
break;
case "h":
MediaType = "text/html";
break;
default:
MediaType = "application/octet-stream"; // TODO: mimeguesser?
break;
}
var uri = Encoding.UTF8.GetBytes(HttpUtility.UrlDecode(string.Join("/", paths))+"\r\n");
var tcp = new TcpClient(URL.Host, URL.Port < 0 ? 70 : URL.Port);
var stream = tcp.GetStream();
stream.Write(uri);
Content = await Download(stream);
OnLoaded();
}
public override void Render()
{
}
}