From c7aa0d7b0fa85b802c7441eed1f456044df68a14 Mon Sep 17 00:00:00 2001
From: Azerelat <azerelat@inbox.lv>
Date: Thu, 14 Jan 2016 20:15:10 +0000
Subject: [PATCH] Fix better reverse proxy support

---
 src/Jackett/Content/custom.js                 | 14 +++-
 src/Jackett/Content/index.html                |  2 +-
 .../Utils/WebApiRootRedirectMiddleware.cs     | 73 ++++++++++---------
 3 files changed, 51 insertions(+), 38 deletions(-)

diff --git a/src/Jackett/Content/custom.js b/src/Jackett/Content/custom.js
index f0bb94de..c9dbb49a 100644
--- a/src/Jackett/Content/custom.js
+++ b/src/Jackett/Content/custom.js
@@ -1,4 +1,4 @@
-var basePath = "";
+var basePath = '';
 
 $(document).ready(function () {
     $.ajaxSetup({ cache: false });
@@ -7,7 +7,7 @@ $(document).ready(function () {
 
     bindUIButtons();
     loadJackettSettings();
-    reloadIndexers();
+   
 });
 
 function getJackettConfig(callback) {
@@ -26,6 +26,10 @@ function loadJackettSettings() {
         $("#jackett-port").val(data.config.port);
         $("#jackett-basepathoverride").val(data.config.basepathoverride);
         basePath = data.config.basepathoverride;
+        if (basePath === null || basePath === undefined) {
+            basePath = '';
+        }
+       
         $("#jackett-savedir").val(data.config.blackholedir);
         $("#jackett-allowext").attr('checked', data.config.external);
         $("#jackett-allowupdate").attr('checked', data.config.updatedisabled);
@@ -36,6 +40,8 @@ function loadJackettSettings() {
         if (password != null && password != '') {
             $("#logoutBtn").show();
         }
+
+        reloadIndexers();
     });
 }
 
@@ -56,8 +62,8 @@ function displayIndexers(items) {
     $('#unconfigured-indexers-template').empty();
     for (var i = 0; i < items.length; i++) {
         var item = items[i];
-        item.torznab_host = resolveUrl("/" + basePath + "/torznab/" + item.id);
-        item.potato_host = resolveUrl("/" + basePath + "/potato/" + item.id);
+        item.torznab_host = resolveUrl(basePath + "/torznab/" + item.id);
+        item.potato_host = resolveUrl(basePath + "/potato/" + item.id);
         if (item.configured)
             $('#indexers').append(indexerTemplate(item));
         else
diff --git a/src/Jackett/Content/index.html b/src/Jackett/Content/index.html
index 94fea8d8..04958f2e 100644
--- a/src/Jackett/Content/index.html
+++ b/src/Jackett/Content/index.html
@@ -86,7 +86,7 @@
         </div>
         <div class="input-area">
             <span class="input-header">Base Path Override: </span>
-            <input id="jackett-basepathoverride" class="form-control input-right" type="text" value="" placeholder="jackett/">
+            <input id="jackett-basepathoverride" class="form-control input-right" type="text" value="" placeholder="/jackett/">
         </div>
         <div class="input-area">
             <span class="input-header">Server port: </span>
diff --git a/src/Jackett/Utils/WebApiRootRedirectMiddleware.cs b/src/Jackett/Utils/WebApiRootRedirectMiddleware.cs
index fe4f7ac5..df0aebb5 100644
--- a/src/Jackett/Utils/WebApiRootRedirectMiddleware.cs
+++ b/src/Jackett/Utils/WebApiRootRedirectMiddleware.cs
@@ -1,35 +1,42 @@
 using Jackett.Services;
-using Microsoft.Owin;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Jackett.Utils
-{
-    public class WebApiRootRedirectMiddleware : OwinMiddleware
-    {
-        public WebApiRootRedirectMiddleware(OwinMiddleware next)
-            : base(next)
-        {
-        }
-
-        public async override Task Invoke(IOwinContext context)
-        {
-            var url = context.Request.Uri;
-            if (string.IsNullOrWhiteSpace(url.AbsolutePath) || url.AbsolutePath == "/")
-            {
-                // 301 is the status code of permanent redirect
-                context.Response.StatusCode = 301;
-                var redir = Startup.BasePath + "Admin/Dashboard";
-                Engine.Logger.Info("redirecting to " + redir);
-                context.Response.Headers.Set("Location", redir);
-            }
-            else
-            {
-                await Next.Invoke(context);
-            }
-        }
-    }
+using Microsoft.Owin;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Jackett.Utils
+{
+    public class WebApiRootRedirectMiddleware : OwinMiddleware
+    {
+        public WebApiRootRedirectMiddleware(OwinMiddleware next)
+            : base(next)
+        {
+        }
+
+        public async override Task Invoke(IOwinContext context)
+        {
+            var url = context.Request.Uri;
+            if(context.Request.Path != null && context.Request.Path.HasValue && context.Request.Path.Value.StartsWith(Startup.BasePath))
+            {
+                context.Request.Path = new PathString(context.Request.Path.Value.Substring(Startup.BasePath.Length-1));
+            }
+
+            if (string.IsNullOrWhiteSpace(url.AbsolutePath) || url.AbsolutePath == "/")
+            {
+                // 301 is the status code of permanent redirect
+                context.Response.StatusCode = 301;
+                var redir = Startup.BasePath + "Admin/Dashboard";
+                Engine.Logger.Info("redirecting to " + redir);
+                context.Response.Headers.Set("Location", redir);
+            }
+            else
+            {
+
+
+                await Next.Invoke(context);
+            }
+        }
+    }
 }
\ No newline at end of file
-- 
GitLab