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

fix(BuildUrl): address path encoding lapses #40

Merged
merged 10 commits into from
Apr 12, 2021
Merged

fix(BuildUrl): address path encoding lapses #40

merged 10 commits into from
Apr 12, 2021

Conversation

luqven
Copy link
Contributor

@luqven luqven commented Apr 2, 2021

Description

This PR includes the following changes:

  • fixed ms-dotnettools lint issues
  • adds NUnitTestAdapter
  • adds vscode dotnet-test test settings
  • adds more Base64 encoding tests
  • creates the SanitizePath, UrlEncode, and Base64Encode methods to more closely resemble ur-building logic in imgix-js and address some path encoding issues.

@luqven luqven self-assigned this Apr 2, 2021
@commit-lint
Copy link

commit-lint bot commented Apr 2, 2021

Styles

  • UrlBuilder: fix ms-dotnettools lint issues (707dc70)

Tests

  • NUnit: add NUnitTestAdapter (228e2fb)
  • UrlBuilder: add more Base64 encoding tests (83826e4)
  • UrlBuilder: change UrlBuilder test name (b83f672)

Chore

  • VSCODE: add dotnet-test test settings (8528fc6)
  • remove string const (7675772)
  • CheckProxy: fix typo (f3d2fab)
  • remvoe vscode ext settings file (d35e965)

Bug Fixes

  • BuildURl: address path encoding lapses (8068ee0)

Code Refactoring

  • BuildParams: add SignParams and comments (ff4bc98)

Contributors

luqven

Commit-Lint commands

You can trigger Commit-Lint actions by commenting on this PR:

  • @Commit-Lint merge patch will merge dependabot PR on "patch" versions (X.X.Y - Y change)
  • @Commit-Lint merge minor will merge dependabot PR on "minor" versions (X.Y.Y - Y change)
  • @Commit-Lint merge major will merge dependabot PR on "major" versions (Y.Y.Y - Y change)
  • @Commit-Lint merge disable will desactivate merge dependabot PR
  • @Commit-Lint review will approve dependabot PR
  • @Commit-Lint stop review will stop approve dependabot PR

Comment on lines 163 to 167
String encodedHTTP = "http%3A%2F%2F";
String encodedHTTPS = "https%3A%2F%2F";

if (path.StartsWith("http"))
String encodedHTTPLower = "http%3a%2f%2f";
String encodedHTTPSLower = "https%3a%ff%2f";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to leave pre-encoded path support in since the library has, in the past, supported it. As evidenced by UrlBuilderProperlySignsFullyQualifiedUrls UrlBuilderProperlySignsFullyQualifiedUrlsWithParameters tests.

@luqven luqven marked this pull request as ready for review April 5, 2021 13:57
@luqven luqven requested a review from a team as a code owner April 5, 2021 13:57
Copy link
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

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

Overall looks really good, most of my concerns were around style and readability which we can discuss 👍

String scheme = UseHttps ? "https" : "http";
path = SanitizePath(path);

var qs = BuildParams(parameters);
Copy link
Contributor

Choose a reason for hiding this comment

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

style nit: I think this would be more clear fully written out as queryString

{
path = WebUtility.UrlEncode(path);
var hashString = String.Format("{0}/{1}{2}", _signKey, path, localParams.Any() ? "?" + qs : String.Empty);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: can we break out the localParams.Any() ? "?" + qs : String.Empty into its own line?

Suggested change
var hashString = String.Format("{0}/{1}{2}", _signKey, path, localParams.Any() ? "?" + qs : String.Empty);
var queryString = localParams.Any() ? "?" + qs : String.Empty;
var hashString = String.Format("{0}/{1}{2}", _signKey, path, queryString);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree with this. We can do away with a lot of this ternary logic by making BuildParams better and using a SignParams helper, as I mention above.

}
return String.Format("{0}://{1}/{2}{3}", scheme, Domain, path, localParams.Any() ? "?" + BuildParams(localParams) : String.Empty);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above.

Suggested change
return String.Format("{0}://{1}/{2}{3}", scheme, Domain, path, localParams.Any() ? "?" + BuildParams(localParams) : String.Empty);
var queryString = localParams.Any() ? "?" + BuildParams(localParams) : String.Empty;
return String.Format("{0}://{1}/{2}{3}", scheme, Domain, path, queryString);

* Encodes the `path` and `paramater` arguments to the UTF8
* scheme and returns a formatted string that conforms to the
* "{scheme}://{domain}/{path}{params}" format.
*/
public String BuildUrl(String path, Dictionary<String, String> parameters)
Copy link
Contributor

Choose a reason for hiding this comment

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

I had a bit of difficulty parsing out the steps of this method despite knowing what it's supposed to do, specifically why qs was used over localParams at times (and vice versa). I think stylistically we can make things clearer for other contributors by either 1) reworking the variable names to be a bit more descriptive (I'll mull this one over) and/or 2) adding a comment describing what the purpose of the if block on L82 is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sense. I think the best thing to do here is to break this up similarly to js-core, where sanitizePath, buildParams, and signParams are all extracted out into methods. Makes reading this a lot easier and also allows us remove references to qs or queryString and just say finalParams.

{
String path = p;
Dictionary<String, Boolean> status = new Dictionary<String, Boolean>();
path.Replace("^/", "");
Copy link
Contributor

Choose a reason for hiding this comment

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

out of curiosity, you already trim the leading and trailing slashes in SanitizePath so do we still need to this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

💯 good catch, not needed here.

if (!String.IsNullOrEmpty(_signKey))
{
var hashString = String.Format("{0}/{1}{2}", _signKey, path, localParams.Any() ? "?" + qs : String.Empty);
localParams.Add("s", HashString(hashString));
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it's out of scope for this PR, but I also think we should rename the HashString method to more closely match js-core

Copy link
Contributor Author

Choose a reason for hiding this comment

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

💯 , for now going to call it inside a SignParams helper to make it clearer what this is doing.

@luqven
Copy link
Contributor Author

luqven commented Apr 8, 2021

Overall looks really good, most of my concerns were around style and readability which we can discuss 👍

Addressed all of this concerns with latest 2 commits.

  • Improved syntax in BuildUrl by refactoring some of the logic into BuildParams and a new SignParams helper.
  • Removed a lot of ternary operators thanks to the above
  • Added comments for hover-help and generally improved readability.
  • Fixed a mismatched test name

@luqven luqven requested a review from sherwinski April 9, 2021 02:53
Copy link
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

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

Aside from one question this LGTM! Everything looks a lot cleaner now 👌

.vscode/settings.json Outdated Show resolved Hide resolved
@luqven luqven merged commit e12c54b into main Apr 12, 2021
@luqven luqven deleted the luis/pathEncoding branch April 12, 2021 20:43
@luqven luqven mentioned this pull request Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants