diff --git a/DOIOU.Server/Controllers/InfoController.cs b/DOIOU.Server/Controllers/InfoController.cs new file mode 100644 index 0000000..54e785a --- /dev/null +++ b/DOIOU.Server/Controllers/InfoController.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace DOIOU.Server.Controllers +{ + [Route("/info")] + public class InfoController : Controller + { + private readonly ILogger _logger; + + public InfoController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IActionResult Index() + { + return View(); + } + + [HttpGet("faq")] + public IActionResult FAQ() + { + return View("FAQ"); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View("Error!"); + } + } +} diff --git a/DOIOU.Server/Controllers/MainController.cs b/DOIOU.Server/Controllers/MainController.cs index 006bcf2..ebb450c 100644 --- a/DOIOU.Server/Controllers/MainController.cs +++ b/DOIOU.Server/Controllers/MainController.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Text.RegularExpressions; using DOIOU.Server.Models; using Microsoft.AspNetCore.Mvc; @@ -21,28 +22,34 @@ public class MainController : Controller } [HttpGet("{*doiou}")] - public IActionResult Get(string doiou) => Resolve(doiou); + public async Task Get(string doiou) => await Resolve(doiou); [HttpPost] - public IActionResult Post([FromForm] string doiou) => Resolve(doiou); + public async Task Post([FromForm] string doiou) => await Resolve(doiou); + + private const string DOI_REGEX = @"^((?[a-z]+):(\/\/)?)?(?[a-z0-9.\-]+\/)?((?\d{1,3})\.(?\d{4,5})\/(?[\S]+[^;,.\s]))$"; [NonAction] - private IActionResult Resolve(string doiou) + private async Task Resolve(string doiou) { - int i = doiou.IndexOf('.'), prefix = 69; + var match = Regex.Match(doiou, DOI_REGEX); - if (i != -1 && !int.TryParse(doiou[..i], out prefix)) + if (!match.Success) { return BadRequest("Invalid format"); } - // TODO further validation + int prefix = int.Parse(match.Groups["prefix"].Value); + int publisher = int.Parse(match.Groups["publisher"].Value); + string id = match.Groups["id"].Value; - // Maybe add other providers if they exist? + string query = $"{prefix}.{publisher}/{id}"; - if (prefix == 69) + // TODO further validation (use schemes?) + + if (prefix == 7 || prefix == 69) { - string? q = _data.Query(doiou); + string? q = _data.Query(query); return q != null ? Redirect(q) : NotFound("DOIOU not found"); } @@ -52,7 +59,22 @@ public class MainController : Controller _ => null }; - return urlBase != null ? Redirect($"{urlBase}/{doiou}") : NotFound($"Unsupported prefix: {prefix}"); + if (urlBase == null) + return BadRequest($"Unsupported prefix: {prefix}"); + + using HttpClient client = new(); + client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "DOIOU v1.0"); + + var response = await client.GetAsync($"{urlBase}/{doiou}"); + if (((int)response.StatusCode / 100) == 3) + { + string location = response.Headers!.Location!.ToString(); + return Redirect(location); + } + + //TODO: cache + + return NotFound("DOIOU not found"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] diff --git a/DOIOU.Server/DOIOU.Server.csproj b/DOIOU.Server/DOIOU.Server.csproj index bfb279d..5d379af 100644 --- a/DOIOU.Server/DOIOU.Server.csproj +++ b/DOIOU.Server/DOIOU.Server.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 enable enable - - - + + + diff --git a/DOIOU.Server/Views/Info/Faq.cshtml b/DOIOU.Server/Views/Info/Faq.cshtml new file mode 100644 index 0000000..e4900c9 --- /dev/null +++ b/DOIOU.Server/Views/Info/Faq.cshtml @@ -0,0 +1,8 @@ +@{ + ViewData["Title"] = "FAQ"; +} + +
+

FAQ

+

something something estradiol

+
diff --git a/DOIOU.Server/Views/Info/Index.cshtml b/DOIOU.Server/Views/Info/Index.cshtml new file mode 100644 index 0000000..1034ea1 --- /dev/null +++ b/DOIOU.Server/Views/Info/Index.cshtml @@ -0,0 +1,8 @@ +@{ + ViewData["Title"] = "Info"; +} + +
+

Info

+

idk

+
diff --git a/DOIOU.Server/Views/Shared/_Layout.cshtml b/DOIOU.Server/Views/Shared/_Layout.cshtml index 826b0ab..43ded3e 100644 --- a/DOIOU.Server/Views/Shared/_Layout.cshtml +++ b/DOIOU.Server/Views/Shared/_Layout.cshtml @@ -12,7 +12,6 @@