Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 2078: 404 for archive post bad slug #2083

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/Piranha.AspNetCore/Http/RoutingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ public override async Task Invoke(HttpContext context, IApi api, IApplicationSer
bool foundTag = false;
bool foundPage = false;

// only consider a 404 if there are segments to parse, otherwise it's the base archive page
bool throw404 = segments.Length > pos;

for (var n = pos; n < segments.Length; n++)
{
if (segments[n] == "category" && !foundPage)
Expand Down Expand Up @@ -361,6 +364,7 @@ public override async Task Invoke(HttpContext context, IApi api, IApplicationSer
{
query.Append("&category=");
query.Append(categoryId);
throw404 = false;
}
}
finally
Expand All @@ -379,6 +383,7 @@ public override async Task Invoke(HttpContext context, IApi api, IApplicationSer
{
query.Append("&tag=");
query.Append(tagId);
throw404 = false;
}
}
finally
Expand All @@ -396,6 +401,7 @@ public override async Task Invoke(HttpContext context, IApi api, IApplicationSer
query.Append(pageNum);
query.Append("&pagenum=");
query.Append(pageNum);
throw404 = false;
}
catch
{
Expand All @@ -418,6 +424,7 @@ public override async Task Invoke(HttpContext context, IApi api, IApplicationSer
}
query.Append("&year=");
query.Append(year);
throw404 = false;
}
catch
{
Expand All @@ -432,6 +439,7 @@ public override async Task Invoke(HttpContext context, IApi api, IApplicationSer
var month = Math.Max(Math.Min(Convert.ToInt32(segments[n]), 12), 1);
query.Append("&month=");
query.Append(month);
throw404 = false;
}
catch
{
Expand All @@ -440,6 +448,14 @@ public override async Task Invoke(HttpContext context, IApi api, IApplicationSer
}
}
}

// if we got this far and nothing was found, there are segments that were never matched and therefore should be not found
if (throw404)
{
context.Response.StatusCode = 404;
await _next.Invoke(context);
return;
}
}
}
else
Expand Down