diff --git a/Tranga/Server.cs b/Tranga/Server.cs index 9052b9e..4109fc2 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -1,7 +1,9 @@ using System.Net; using System.Runtime.InteropServices; using System.Text; +using System.Text.RegularExpressions; using Newtonsoft.Json; +using Tranga.MangaConnectors; namespace Tranga; @@ -32,10 +34,7 @@ public class Server : GlobalBase Log($"{context.Request.HttpMethod} {context.Request.Url} {context.Request.UserAgent}"); Task t = new(() => { - if(context.Request.HttpMethod == "OPTIONS") - SendResponse(HttpStatusCode.OK, context.Response); - else - HandleRequest(context); + HandleRequest(context); }); t.Start(); } @@ -45,15 +44,83 @@ public class Server : GlobalBase { HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; + if(request.HttpMethod == "OPTIONS") + SendResponse(HttpStatusCode.OK, context.Response); if(request.Url!.LocalPath.Contains("favicon")) SendResponse(HttpStatusCode.NoContent, response); + + switch (request.HttpMethod) + { + case "GET": + HandleGet(request, request.InputStream, response); + break; + case "POST": + HandlePost(request, request.InputStream, response); + break; + case "DELETE": + HandleDelete(request, request.InputStream, response); + break; + default: + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + } + + private Dictionary GetRequestVariables(string query) + { + Dictionary ret = new(); + Regex queryRex = new (@"\?{1}&?([A-z0-9-=]+=[A-z0-9-=]+)+(&[A-z0-9-=]+=[A-z0-9-=]+)*"); + if (!queryRex.IsMatch(query)) + return ret; + query = query.Substring(1); + foreach (string kvpair in query.Split('&').Where(str => str.Length >= 3)) + { + string var = kvpair.Split('=')[0]; + string val = Regex.Replace(kvpair.Substring(var.Length + 1), "%20", " "); + val = Regex.Replace(val, "%[0-9]{2}", ""); + ret.Add(var, val); + } + return ret; + } + + private void HandleGet(HttpListenerRequest request, Stream content, HttpListenerResponse response) + { + Dictionary requestVariables = GetRequestVariables(request.Url!.Query); + switch (request.Url!.LocalPath[1..]) + { + case "Connectors": + SendResponse(HttpStatusCode.OK, response, _parent.GetConnectors().Select(connector => connector.name).ToArray()); + break; + case "Publications/FromConnector": + if (requestVariables.TryGetValue("connector", out string? connectorName) && + requestVariables.TryGetValue("title", out string? title)) + { + MangaConnector? connector = _parent.GetConnector(connectorName); + if (connector is null || title.Length < 4) + { + SendResponse(HttpStatusCode.BadRequest, response); + return; + } + SendResponse(HttpStatusCode.OK, response, connector.GetPublications(title)); + }else + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + } + + private void HandlePost(HttpListenerRequest request, Stream content, HttpListenerResponse response) + { + + } + + private void HandleDelete(HttpListenerRequest request, Stream content, HttpListenerResponse response) + { - SendResponse(HttpStatusCode.NotFound, response); } private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null) { - //logger?.WriteLine(this.GetType().ToString(), $"Sending response: {statusCode}"); + Log($"Response: {statusCode} {content}"); response.StatusCode = (int)statusCode; response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE");