Skip to content

Commit

Permalink
Add Request.PathBase to Request.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisw-cr committed Oct 20, 2022
1 parent 4478b53 commit 0350dd2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
28 changes: 18 additions & 10 deletions ElmahCore.Mvc/ErrorLogMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal sealed class ErrorLogMiddleware
};

private readonly Func<HttpContext, bool> _checkPermissionAction = context => true;
private readonly string _elmahRoot = @"/elmah";
private readonly string _elmahRoot = @"~/elmah";
private readonly ErrorLog _errorLog;
private readonly List<IErrorFilter> _filters = new List<IErrorFilter>();
private readonly ILogger _logger;
Expand Down Expand Up @@ -91,7 +91,7 @@ internal sealed class ErrorLogMiddleware
if (!string.IsNullOrEmpty(options.Path))
{
_elmahRoot = elmahOptions.Value.Path.ToLower();
if (!_elmahRoot.StartsWith("/")) _elmahRoot = "/" + _elmahRoot;
if (!_elmahRoot.StartsWith("/") && !_elmahRoot.StartsWith("~/")) _elmahRoot = "/" + _elmahRoot;
if (_elmahRoot.EndsWith("/")) _elmahRoot = _elmahRoot.Substring(0, _elmahRoot.Length - 1);
}

Expand Down Expand Up @@ -140,21 +140,25 @@ private void ConfigureFilters(string config)
public async Task InvokeAsync(HttpContext context)
{
string body = null;
var elmahRoot = _elmahRoot;
try
{
if (elmahRoot.StartsWith("~/"))
elmahRoot = context.Request.PathBase + elmahRoot.Substring(1);

context.Features.Set(new ElmahLogFeature());

var sourcePath = context.Request.Path.Value;
if (sourcePath.Equals(_elmahRoot, StringComparison.InvariantCultureIgnoreCase)
|| sourcePath.StartsWith(_elmahRoot + "/", StringComparison.InvariantCultureIgnoreCase))
var sourcePath = context.Request.PathBase + context.Request.Path.Value;
if (sourcePath.Equals(elmahRoot, StringComparison.InvariantCultureIgnoreCase)
|| sourcePath.StartsWith(elmahRoot + "/", StringComparison.InvariantCultureIgnoreCase))
{
if (!_checkPermissionAction(context))
{
await context.ChallengeAsync();
return;
}

var path = sourcePath.Substring(_elmahRoot.Length, sourcePath.Length - _elmahRoot.Length);
var path = sourcePath.Substring(elmahRoot.Length, sourcePath.Length - elmahRoot.Length);
if (path.StartsWith("/")) path = path.Substring(1);
if (path.Contains('?')) path = path.Substring(0, path.IndexOf('?'));
await ProcessElmahRequest(context, path);
Expand All @@ -181,7 +185,7 @@ public async Task InvokeAsync(HttpContext context)
catch (Exception exception)
{
var id = await LogException(exception, context, _onError, body);
var location = $"{_elmahRoot}/detail/{id}";
var location = $"{elmahRoot}/detail/{id}";

context.Features.Set<IElmahFeature>(new ElmahFeature(id, location));

Expand Down Expand Up @@ -210,6 +214,10 @@ private async Task ProcessElmahRequest(HttpContext context, string resource)
{
try
{
var elmahRoot = (_elmahRoot.StartsWith("~/"))
? context.Request.PathBase + _elmahRoot.Substring(1)
: _elmahRoot;

if (resource.StartsWith("api/"))
{
await ErrorApiHandler.ProcessRequest(context, _errorLog, resource);
Expand Down Expand Up @@ -237,18 +245,18 @@ private async Task ProcessElmahRequest(HttpContext context, string resource)
await ErrorJsonHandler.ProcessRequest(context, _errorLog);
break;
case "rss":
await ErrorRssHandler.ProcessRequest(context, _errorLog, _elmahRoot);
await ErrorRssHandler.ProcessRequest(context, _errorLog, elmahRoot);
break;
case "digestrss":
await ErrorDigestRssHandler.ProcessRequest(context, _errorLog, _elmahRoot);
await ErrorDigestRssHandler.ProcessRequest(context, _errorLog, elmahRoot);
return;
case "download":
await ErrorLogDownloadHandler.ProcessRequestAsync(_errorLog, context);
return;
case "test":
throw new TestException();
default:
await ErrorResourceHandler.ProcessRequest(context, resource, _elmahRoot);
await ErrorResourceHandler.ProcessRequest(context, resource, elmahRoot);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ElmahCore.Mvc/ErrorWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public string Method
[XmlElement("Url")]
public string Url
{
get => _error.ServerVariables["Path"];
get => _error.ServerVariables["PathBase"] + _error.ServerVariables["Path"];
// ReSharper disable once ValueParameterNotUsed
set { }
}
Expand Down
2 changes: 1 addition & 1 deletion ElmahCore.Mvc/Handlers/ErrorDigestRssHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static async Task ProcessRequest(HttpContext context, ErrorLog errorLog,

var title = $@"Daily digest of errors in {log.ApplicationName} on {Environment.MachineName}";

var link = $"{context.Request.Scheme}://{context.Request.Host}{elmahRoot}";
var link = $"{context.Request.Scheme}://{context.Request.PathBase.ToString().TrimEnd('/')}{elmahRoot}";
var baseUrl = new Uri(link.TrimEnd('/') + "/");

var items = GetItems(log, baseUrl, 30, 30).Take(30);
Expand Down
2 changes: 1 addition & 1 deletion ElmahCore.Mvc/Handlers/ErrorResourceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static async Task ProcessRequest(HttpContext context, string path, string
using (var reader = new StreamReader(stream2 ?? throw new InvalidOperationException()))
{
var html = await reader.ReadToEndAsync();
html = html.Replace("ELMAH_ROOT", context.Request.PathBase + elmahRoot);
html = html.Replace("ELMAH_ROOT", elmahRoot);
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(html);
return;
Expand Down
2 changes: 1 addition & 1 deletion ElmahCore.Mvc/Handlers/ErrorRssHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static async Task ProcessRequest(HttpContext context, ErrorLog errorLog,
var title = $@"Error log of {log.ApplicationName} on {Environment.MachineName}";


var link = $"{context.Request.Scheme}://{context.Request.Host}{elmahRoot}";
var link = $"{context.Request.Scheme}://{context.Request.PathBase.ToString().TrimEnd('/')}{elmahRoot}";
var baseUrl = new Uri(link.TrimEnd('/') + "/");

var items =
Expand Down

0 comments on commit 0350dd2

Please sign in to comment.