Skip to content

Commit

Permalink
More url wrangling
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldgray committed May 22, 2024
1 parent d014883 commit 01b1c78
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ namespace Preservation.API.Controllers;

[Route("[controller]/{*path}")]
[ApiController]
public class RepositoryController(
IPreservation preservation,
ModelConverter modelConverter,
ILogger<RepositoryController> logger) : Controller
public class RepositoryController(IPreservation preservation, ModelConverter modelConverter) : Controller
{
/// <summary>
/// Browse underlying repository for Container, DigitalObject or Binary.
Expand All @@ -31,7 +28,7 @@ public async Task<IActionResult> 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);
}

Expand All @@ -50,24 +47,7 @@ public async Task<IActionResult> 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;
}
}
2 changes: 1 addition & 1 deletion LeedsExperiment/Preservation.API/Models/UriGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 01b1c78

Please sign in to comment.