Skip to content
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using this literal 'redirecturl' 4 times.

See more on https://sonarcloud.io/project/issues?id=happy-geeks_geeks-core-library&issues=AZsOADIV1oIfvfUA_4nA&open=AZsOADIV1oIfvfUA_4nA&pullRequest=955
{
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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using this literal 'templateid' 6 times.

See more on https://sonarcloud.io/project/issues?id=happy-geeks_geeks-core-library&issues=AZsOADIV1oIfvfUA_4nB&open=AZsOADIV1oIfvfUA_4nB&pullRequest=955
{
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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make 'RunRedirectQuery' a static method.

See more on https://sonarcloud.io/project/issues?id=happy-geeks_geeks-core-library&issues=AZsOADIV1oIfvfUA_4nC&open=AZsOADIV1oIfvfUA_4nC&pullRequest=955
{
var queryStringCollection = System.Web.HttpUtility.ParseQueryString(queryString.ToString());
var query = template.Content;
Expand All @@ -338,28 +360,14 @@
throw new Exception($"Redirect query (id {template.Id}) returned {dataTable.Rows.Count} results, expected one!");

Copy link
Copy Markdown
Contributor

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 InvalidOperationException

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


var dataRow = dataTable.Rows[0];
string result = null;
var dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give this dictionary a more descriptive name than dict. Something like queryResults or redirectResults is my suggestion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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>
Expand Down