Commit f94cec6f authored by nitroxis's avatar nitroxis

Use X-Forwarded-Proto to determine the request URL scheme.

parent 5dc5c044
......@@ -3,6 +3,7 @@ using System.Diagnostics;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using UrlShortener.Models;
namespace UrlShortener.Controllers
......@@ -66,7 +67,19 @@ namespace UrlShortener.Controllers
private string makeShortUri(string id)
{
return $"{this.HttpContext.Request.Scheme}://{this.HttpContext.Request.Host}/{id}";
string scheme = null;
if (this.HttpContext.Request.Headers.TryGetValue("X-Forwarded-Proto", out StringValues values))
{
scheme = values.FirstOrDefault();
}
if (string.IsNullOrWhiteSpace(scheme))
{
scheme = this.HttpContext.Request.Scheme;
}
return $"{scheme}://{this.HttpContext.Request.Host}/{id}";
}
public IActionResult Index()
......
@{
ViewData["Title"] = "URL shortener";
string scheme = null;
if (Context.Request.Headers.TryGetValue("X-Forwarded-Proto", out StringValues values))
{
scheme = values.FirstOrDefault();
}
if (string.IsNullOrWhiteSpace(scheme))
{
scheme = Context.Request.Scheme;
}
}
@using Microsoft.AspNetCore.Http
@using Microsoft.Extensions.Primitives
@model ShortenerModel
<form action="/" method="POST">
......@@ -33,6 +46,6 @@
}
<p>Enter your long URL and click "Shorten" to get a shortened link which will automatically redirect to the long one. The number of requests you can make is limited to prevent spam. If you see a "Service Temporarily Unavailable" error, try again in a few seconds.</p>
<p>If you want to use this service in Firefox, you can install <a href="https://addons.mozilla.org/en-US/firefox/addon/copy-shorturl/">this add-on</a>. It adds a "Copy ShortURL" entry to the right-click menu. You just have to set "Custom URL" to <code>@Context.Request.Scheme://@Context.Request.Host/Add?%URL%</code> in the add-on settings.</p>
<p>If you want to use this service in Firefox, you can install <a href="https://addons.mozilla.org/en-US/firefox/addon/copy-shorturl/">this add-on</a>. It adds a "Copy ShortURL" entry to the right-click menu. You just have to set "Custom URL" to <code>@scheme://@Context.Request.Host/Add?%URL%</code> in the add-on settings.</p>
<p>Made by <a href="//nxs.re">nitroxis</a>. You can find the source code <a href="https://gitlab.com/nitroxis/urlshortener">here</a> (C#, ASP.NET Core).</p>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment