From 01b1c783423c8c4a9a37441467643d9a7c38d872 Mon Sep 17 00:00:00 2001 From: Donald Gray Date: Wed, 22 May 2024 17:04:36 +0100 Subject: [PATCH] More url wrangling --- .../Controllers/RepositoryController.cs | 26 +++---------------- .../Preservation.API/Models/UriGenerator.cs | 2 +- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/LeedsExperiment/Preservation.API/Controllers/RepositoryController.cs b/LeedsExperiment/Preservation.API/Controllers/RepositoryController.cs index eb58f6f..fd618a6 100644 --- a/LeedsExperiment/Preservation.API/Controllers/RepositoryController.cs +++ b/LeedsExperiment/Preservation.API/Controllers/RepositoryController.cs @@ -6,10 +6,7 @@ namespace Preservation.API.Controllers; [Route("[controller]/{*path}")] [ApiController] -public class RepositoryController( - IPreservation preservation, - ModelConverter modelConverter, - ILogger logger) : Controller +public class RepositoryController(IPreservation preservation, ModelConverter modelConverter) : Controller { /// /// Browse underlying repository for Container, DigitalObject or Binary. @@ -31,7 +28,7 @@ public async Task Browse([FromRoute] string path, [FromQuery] str if (storageResource == null) return NotFound(); var preservationResource = - modelConverter.ToPreservationResource(storageResource, GetCorrectDisplayUrl()); + modelConverter.ToPreservationResource(storageResource, new Uri(HttpContext.Request.GetDisplayUrl())); return Ok(preservationResource); } @@ -50,24 +47,7 @@ public async Task CreateContainer([FromRoute] string path) var unEscapedPath = Uri.UnescapeDataString(path); var storageContainer = await preservation.CreateContainer(unEscapedPath); - var container = modelConverter.ToPreservationResource(storageContainer, GetCorrectDisplayUrl()); + var container = modelConverter.ToPreservationResource(storageContainer, new Uri(HttpContext.Request.GetDisplayUrl())); return CreatedAtAction("Browse", new { path }, container); } - - public Uri GetCorrectDisplayUrl() - { - // HACK avoid `https://uol.digirati:80` when behind ELB - var displayUrl = HttpContext.Request.GetDisplayUrl(); - logger.LogInformation("DisplayUrl {UrlAsString}", displayUrl); - var uri = new Uri(displayUrl); - - logger.LogInformation("Url Port and Scheme: {Port} and {Scheme}", uri.Scheme, uri.Port); - if (uri is { Scheme: "https", Port: -1 or 80 }) - { - var uriBuilder = new UriBuilder(uri) { Port = 443 }; - return uriBuilder.Uri; - } - - return uri; - } } \ No newline at end of file diff --git a/LeedsExperiment/Preservation.API/Models/UriGenerator.cs b/LeedsExperiment/Preservation.API/Models/UriGenerator.cs index 5f5276d..83cee1f 100644 --- a/LeedsExperiment/Preservation.API/Models/UriGenerator.cs +++ b/LeedsExperiment/Preservation.API/Models/UriGenerator.cs @@ -52,9 +52,9 @@ private UriBuilder GetUriBuilderForCurrentHost(string path) { var uriBuilderFromContext = new UriBuilder(request.Scheme, request.Host.Host) { - Port = request.Host.Port ?? 80, Path = path }; + if (request.Host.Port.HasValue) uriBuilderFromContext.Port = request.Host.Port.Value; return uriBuilderFromContext; } else