-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
#658 Change upstream Regex to account for blank placeholders #1333
base: develop
Are you sure you want to change the base?
Changes from all commits
94f9abe
285e07a
58c5d5f
295f7b9
687c869
4c23837
2c19c3d
8877c90
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ namespace Ocelot.Configuration.Creator | |
public class UpstreamTemplatePatternCreator : IUpstreamTemplatePatternCreator | ||
{ | ||
private const string RegExMatchOneOrMoreOfEverything = ".+"; | ||
private const string RegExMatchLastPlaceHolderZeroOrMoreOfEverything = "(|/.+|/[\\?&#].+)"; | ||
private const string RegExMatchOneOrMoreOfEverythingUntilNextForwardSlash = "[^/]+"; | ||
private const string RegExMatchEndString = "$"; | ||
private const string RegExIgnoreCase = "(?i)"; | ||
|
@@ -49,7 +50,15 @@ public UpstreamPathTemplate Create(IRoute route) | |
var indexOfNextForwardSlash = upstreamTemplate.IndexOf("/", indexOfPlaceholder, StringComparison.Ordinal); | ||
if (indexOfNextForwardSlash < indexOfPlaceholder || (containsQueryString && upstreamTemplate.IndexOf('?', StringComparison.Ordinal) < upstreamTemplate.IndexOf(placeholders[i], StringComparison.Ordinal))) | ||
{ | ||
upstreamTemplate = upstreamTemplate.Replace(placeholders[i], RegExMatchOneOrMoreOfEverything); | ||
if (upstreamTemplate[indexOfPlaceholder - 1] == '/') | ||
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. Could you look at line 77 please? |
||
{ | ||
upstreamTemplate = upstreamTemplate.Remove(indexOfPlaceholder - 1, 1); | ||
upstreamTemplate = upstreamTemplate.Replace(placeholders[i], RegExMatchLastPlaceHolderZeroOrMoreOfEverything); | ||
} | ||
else | ||
{ | ||
upstreamTemplate = upstreamTemplate.Replace(placeholders[i], RegExMatchOneOrMoreOfEverything); | ||
} | ||
} | ||
else | ||
{ | ||
|
@@ -76,7 +85,7 @@ public UpstreamPathTemplate Create(IRoute route) | |
|
||
private static bool ForwardSlashAndOnePlaceHolder(string upstreamTemplate, List<string> placeholders, int postitionOfPlaceHolderClosingBracket) | ||
{ | ||
if (upstreamTemplate.Substring(0, 2) == "/{" && placeholders.Count == 1 && upstreamTemplate.Length == postitionOfPlaceHolderClosingBracket + 1) | ||
if (upstreamTemplate[..2] == "/{" && placeholders.Count == 1 && upstreamTemplate.Length == postitionOfPlaceHolderClosingBracket + 1) | ||
{ | ||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,6 @@ | ||||||||
using Ocelot.Responses; | ||||||||
using System; | ||||||||
using System.Collections.Generic; | ||||||||
|
||||||||
namespace Ocelot.DownstreamRouteFinder.UrlMatcher | ||||||||
{ | ||||||||
|
@@ -68,6 +70,29 @@ public Response<List<PlaceholderNameAndValue>> Find(string path, string query, s | |||||||
|
||||||||
counterForTemplate = endOfPlaceholder; | ||||||||
} | ||||||||
else if (TemplateDoesNotEndInForwardSlash(pathTemplate) && IsLastPlaceholder(path, counterForPath, pathTemplate)) | ||||||||
{ | ||||||||
Comment on lines
+73
to
+74
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. The code for this if-block was copied from lines 26-46. |
||||||||
//should_find_multiple_query_string make test pass | ||||||||
if (PassedQueryString(pathTemplate, counterForTemplate)) | ||||||||
{ | ||||||||
delimiter = '&'; | ||||||||
nextDelimiter = '&'; | ||||||||
Comment on lines
+78
to
+79
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. Can be one line assignment
Suggested change
|
||||||||
} | ||||||||
|
||||||||
//should_find_multiple_query_string_and_path makes test pass | ||||||||
if (NotPassedQueryString(pathTemplate, counterForTemplate) && NoMoreForwardSlash(pathTemplate, counterForTemplate)) | ||||||||
{ | ||||||||
delimiter = '?'; | ||||||||
nextDelimiter = '?'; | ||||||||
Comment on lines
+85
to
+86
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.
Suggested change
|
||||||||
} | ||||||||
|
||||||||
var placeholderName = GetPlaceholderName(pathTemplate, counterForTemplate); | ||||||||
var placeholderValue = GetPlaceholderValue(pathTemplate, query, placeholderName, path, counterForPath, delimiter); | ||||||||
placeHolderNameAndValues.Add(new PlaceholderNameAndValue(placeholderName, placeholderValue)); | ||||||||
|
||||||||
var endOfPlaceholder = GetNextCounterPosition(pathTemplate, counterForTemplate, '}'); | ||||||||
counterForTemplate = endOfPlaceholder; | ||||||||
} | ||||||||
|
||||||||
counterForPath++; | ||||||||
} | ||||||||
|
@@ -142,5 +167,12 @@ private static bool ContinueScanningUrl(int counterForUrl, int urlLength) | |||||||
} | ||||||||
|
||||||||
private static bool IsPlaceholder(char character) => character == '{'; | ||||||||
|
||||||||
private static bool IsLastPlaceholder(string path, int counterForPath, string pathTemplate) | ||||||||
=> path.Length <= counterForPath && pathTemplate.Length > 1 | ||||||||
&& pathTemplate.Substring(counterForPath, 2) == "/{" | ||||||||
&& pathTemplate.IndexOf('}') == pathTemplate.Length - 1; | ||||||||
|
||||||||
private static bool TemplateDoesNotEndInForwardSlash(string pathTemplate) => !pathTemplate.EndsWith('/'); | ||||||||
} | ||||||||
} |
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.
What about URL encoded queries including
everything
case?Should we add
%
to the set of chars[\\?&#]
?The 2nd case of having something after last slash char (everything-case) is defined by expression which matches by
.+
quantifier. Can we replace all reg-expression like that:(/[\\?&#].*)
?