-
Notifications
You must be signed in to change notification settings - Fork 6
Fixed redirect being overwritten #955
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,7 +93,9 @@ | |
|
|
||
| await HandleRewritesAsync(context, path, queryString, templatesService, objectsService, databaseConnection, replacementsMediator); | ||
|
|
||
| await next.Invoke(context); | ||
| // if a Redirect() has been called, don't invoke the next context | ||
| if (context.Response.StatusCode != 302) | ||
| await next.Invoke(context); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -135,21 +137,31 @@ | |
| context.Request.Path = "/template.gcl"; | ||
| break; | ||
| case TemplateTypes.Query when template is QueryTemplate{ UsedForRedirect: true }: | ||
| var redirectTo = await RunRedirectQuery(template, queryString, databaseConnection, templatesService, replacementsMediator); | ||
| if (redirectTo is null) | ||
| var redirectResults = await RunRedirectQuery(template, queryString, databaseConnection, templatesService, replacementsMediator); | ||
|
|
||
| // if we need to redirect, do that (this sets StatusCode to 302) | ||
| if (redirectResults.ContainsKey("redirecturl")) | ||
|
Check warning on line 143 in GeeksCoreLibrary/Modules/Templates/Middlewares/RewriteUrlToTemplateMiddleware.cs
|
||
| { | ||
| context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
| context.Response.Redirect(redirectResults["redirecturl"]); | ||
| return; | ||
| } | ||
|
|
||
| if (Int32.TryParse(redirectTo, out Int32 templateId)) | ||
| // if the redirect result is a number, it's a template id, so use that | ||
| if (redirectResults.ContainsKey("templateid") && Int32.TryParse(redirectResults["templateid"], out Int32 templateId)) | ||
|
Check warning on line 149 in GeeksCoreLibrary/Modules/Templates/Middlewares/RewriteUrlToTemplateMiddleware.cs
|
||
| { | ||
| template.Id = templateId; | ||
| context.Request.Path = "/template.gcl"; | ||
| template.Id = templateId; | ||
| foreach (var kvp in redirectResults) | ||
| { | ||
| if (kvp.Key.Equals("templateid", StringComparison.InvariantCultureIgnoreCase)) | ||
| continue; | ||
|
|
||
| queryString.Add(kvp.Key, kvp.Value); | ||
| } | ||
| } | ||
| // If the query doesn't give a (correct) result, we go 404. | ||
| else | ||
| { | ||
| context.Request.Path = redirectTo; | ||
| context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
| return; | ||
| } | ||
| break; | ||
|
|
@@ -291,20 +303,30 @@ | |
| var template = await templatesService.GetTemplateAsync(number); | ||
| if (template is QueryTemplate { UsedForRedirect: true }) | ||
| { | ||
| var redirectTo = await RunRedirectQuery(template, queryString, databaseConnection, templatesService, replacementsMediator); | ||
| if (redirectTo is null) | ||
| var redirectResults = await RunRedirectQuery(template, queryString, databaseConnection, templatesService, replacementsMediator); | ||
|
|
||
| // if we need to redirect, do that (this sets StatusCode to 302) | ||
| if (redirectResults.ContainsKey("redirecturl")) | ||
| { | ||
| context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
| context.Response.Redirect(redirectResults["redirecturl"]); | ||
| return; | ||
| } | ||
|
|
||
| if (Int32.TryParse(redirectTo, out Int32 templateId)) | ||
| // if the redirect result is a number, it's a template id, so use that | ||
| if (redirectResults.ContainsKey("templateid") && Int32.TryParse(redirectResults["templateid"], out Int32 templateId)) | ||
| { | ||
| queryString = CombineQueryString(queryString, $"?templateid={templateId}"); | ||
| queryString = CombineQueryString(queryString, $"?templateid={urlMatchLastPart.Replace($"{number}", $"{templateId}").Replace("?", "&")}"); | ||
| foreach (var kvp in redirectResults) | ||
| { | ||
| if (kvp.Key.Equals("templateid", StringComparison.InvariantCultureIgnoreCase)) | ||
| continue; | ||
|
|
||
| queryString.Add(kvp.Key, kvp.Value); | ||
| } | ||
| } | ||
| // If the query doesn't give a (correct) result, we go 404. | ||
| else | ||
| { | ||
| context.Request.Path = redirectTo; | ||
| context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
| return; | ||
| } | ||
| } | ||
|
|
@@ -325,7 +347,7 @@ | |
| } | ||
| } | ||
|
|
||
| private async Task<string> RunRedirectQuery(Template template, QueryString queryString, IDatabaseConnection databaseConnection, ITemplatesService templatesService, IReplacementsMediator replacementsMediator) | ||
| private async Task<Dictionary<string, string>> RunRedirectQuery(Template template, QueryString queryString, IDatabaseConnection databaseConnection, ITemplatesService templatesService, IReplacementsMediator replacementsMediator) | ||
|
Check warning on line 350 in GeeksCoreLibrary/Modules/Templates/Middlewares/RewriteUrlToTemplateMiddleware.cs
|
||
| { | ||
| var queryStringCollection = System.Web.HttpUtility.ParseQueryString(queryString.ToString()); | ||
| var query = template.Content; | ||
|
|
@@ -338,28 +360,14 @@ | |
| throw new Exception($"Redirect query (id {template.Id}) returned {dataTable.Rows.Count} results, expected one!"); | ||
|
|
||
| var dataRow = dataTable.Rows[0]; | ||
| string result = null; | ||
| var dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please give this dictionary a more descriptive name than
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| foreach (DataColumn column in dataTable.Columns) | ||
| { | ||
| switch (column.ColumnName.ToLower()) | ||
| { | ||
| case "templateid": | ||
| var id = dataRow.IsNull(column) ? 0 : Convert.ToInt64(dataRow[column]); | ||
| if (id > 0) | ||
| result = id.ToString(); | ||
| break; | ||
| case "redirecturl": | ||
| var url = dataRow.Field<string>(column); | ||
| if (!String.IsNullOrWhiteSpace(url)) | ||
| result = url; | ||
| break; | ||
| default: | ||
| queryString = queryString.Add(column.ColumnName, dataRow.Field<string>(column)); | ||
| break; | ||
| } | ||
| string value = dataRow[column] == DBNull.Value ? "" : dataRow[column].ToString(); | ||
| dict.Add(column.ColumnName, value); | ||
| } | ||
|
|
||
| return result; | ||
| return dict; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this method is being updated anyway please update this exception to something more appropriate.
My suggestion is using an
InvalidOperationExceptionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done