-
Notifications
You must be signed in to change notification settings - Fork 8
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
Conversation
Styles
Tests
Chore
Bug Fixes
Code Refactoring
ContributorsCommit-Lint commandsYou can trigger Commit-Lint actions by commenting on this PR:
|
src/Imgix/UrlBuilder.cs
Outdated
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"; |
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.
We have to leave pre-encoded path support in since the library has, in the past, supported it. As evidenced by UrlBuilderProperlySignsFullyQualifiedUrls
UrlBuilderProperlySignsFullyQualifiedUrlsWithParameters
tests.
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.
Overall looks really good, most of my concerns were around style and readability which we can discuss 👍
src/Imgix/UrlBuilder.cs
Outdated
String scheme = UseHttps ? "https" : "http"; | ||
path = SanitizePath(path); | ||
|
||
var qs = BuildParams(parameters); |
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.
style nit: I think this would be more clear fully written out as queryString
src/Imgix/UrlBuilder.cs
Outdated
{ | ||
path = WebUtility.UrlEncode(path); | ||
var hashString = String.Format("{0}/{1}{2}", _signKey, path, localParams.Any() ? "?" + qs : String.Empty); |
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.
style: can we break out the localParams.Any() ? "?" + qs : String.Empty
into its own line?
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); |
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.
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.
src/Imgix/UrlBuilder.cs
Outdated
} | ||
return String.Format("{0}://{1}/{2}{3}", scheme, Domain, path, localParams.Any() ? "?" + BuildParams(localParams) : String.Empty); |
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.
Same comment as above.
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) |
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.
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.
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.
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
.
src/Imgix/UrlBuilder.cs
Outdated
{ | ||
String path = p; | ||
Dictionary<String, Boolean> status = new Dictionary<String, Boolean>(); | ||
path.Replace("^/", ""); |
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.
out of curiosity, you already trim the leading and trailing slashes in SanitizePath so do we still need to this line?
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.
💯 good catch, not needed here.
src/Imgix/UrlBuilder.cs
Outdated
if (!String.IsNullOrEmpty(_signKey)) | ||
{ | ||
var hashString = String.Format("{0}/{1}{2}", _signKey, path, localParams.Any() ? "?" + qs : String.Empty); | ||
localParams.Add("s", HashString(hashString)); |
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.
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
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.
💯 , for now going to call it inside a SignParams
helper to make it clearer what this is doing.
Addressed all of this concerns with latest 2 commits.
|
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.
Aside from one question this LGTM! Everything looks a lot cleaner now 👌
Description
This PR includes the following changes:
SanitizePath
,UrlEncode
, andBase64Encode
methods to more closely resemble ur-building logic inimgix-js
and address some path encoding issues.