diff --git a/src/Jackett.Common/Jackett.Common.csproj b/src/Jackett.Common/Jackett.Common.csproj index 467e6e3e7..b1848da18 100644 --- a/src/Jackett.Common/Jackett.Common.csproj +++ b/src/Jackett.Common/Jackett.Common.csproj @@ -15,6 +15,7 @@ + diff --git a/src/Jackett.Common/Services/Interfaces/IServerService.cs b/src/Jackett.Common/Services/Interfaces/IServerService.cs index 9b8bd529f..010ae3371 100644 --- a/src/Jackett.Common/Services/Interfaces/IServerService.cs +++ b/src/Jackett.Common/Services/Interfaces/IServerService.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using System.Net.Http; +using Microsoft.AspNetCore.Http; namespace Jackett.Common.Services.Interfaces { @@ -12,7 +12,7 @@ namespace Jackett.Common.Services.Interfaces void ReserveUrls(bool doInstall = true); Uri ConvertToProxyLink(Uri link, string serverUrl, string indexerId, string action = "dl", string file = "t"); string BasePath(); - string GetServerUrl(object Request); //TODO: Once Mono is removed, change type to HttpRequest + string GetServerUrl(HttpRequest Request); List notices { get; } string GetBlackholeDirectory(); string GetApiKey(); diff --git a/src/Jackett.Server/Services/ServerService.cs b/src/Jackett.Server/Services/ServerService.cs index 8b1217dc5..060939919 100644 --- a/src/Jackett.Server/Services/ServerService.cs +++ b/src/Jackett.Server/Services/ServerService.cs @@ -314,36 +314,33 @@ namespace Jackett.Server.Services // Only needed for Owin } - public string GetServerUrl(Object obj) + public string GetServerUrl(HttpRequest request) { string serverUrl = ""; - if (obj is HttpRequest request) + var scheme = request.Scheme; + var port = request.HttpContext.Request.Host.Port; + + // Check for protocol headers added by reverse proxys + // X-Forwarded-Proto: A de facto standard for identifying the originating protocol of an HTTP request + var X_Forwarded_Proto = request.Headers.Where(x => x.Key == "X-Forwarded-Proto").Select(x => x.Value).FirstOrDefault(); + if (X_Forwarded_Proto.Count > 0) { - var scheme = request.Scheme; - var port = request.HttpContext.Request.Host.Port; - - // Check for protocol headers added by reverse proxys - // X-Forwarded-Proto: A de facto standard for identifying the originating protocol of an HTTP request - var X_Forwarded_Proto = request.Headers.Where(x => x.Key == "X-Forwarded-Proto").Select(x => x.Value).FirstOrDefault(); - if (X_Forwarded_Proto.Count > 0) - { - scheme = X_Forwarded_Proto.First(); - } - // Front-End-Https: Non-standard header field used by Microsoft applications and load-balancers - else if (request.Headers.Where(x => x.Key == "Front-End-Https" && x.Value.FirstOrDefault() == "on").Any()) - { - scheme = "https"; - } - - //default to 443 if the Host header doesn't contain the port (needed for reverse proxy setups) - if (scheme == "https" && !request.HttpContext.Request.Host.Value.Contains(":")) - { - port = 443; - } - - serverUrl = string.Format("{0}://{1}:{2}{3}/", scheme, request.HttpContext.Request.Host.Host, port, BasePath()); + scheme = X_Forwarded_Proto.First(); } + // Front-End-Https: Non-standard header field used by Microsoft applications and load-balancers + else if (request.Headers.Where(x => x.Key == "Front-End-Https" && x.Value.FirstOrDefault() == "on").Any()) + { + scheme = "https"; + } + + //default to 443 if the Host header doesn't contain the port (needed for reverse proxy setups) + if (scheme == "https" && !request.HttpContext.Request.Host.Value.Contains(":")) + { + port = 443; + } + + serverUrl = string.Format("{0}://{1}:{2}{3}/", scheme, request.HttpContext.Request.Host.Host, port, BasePath()); return serverUrl; }