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

Update to use WHATWG URL API instead of url.parse() #793

Merged
merged 2 commits into from
Jul 20, 2021
Merged

Conversation

xiao-lix
Copy link
Contributor

@xiao-lix xiao-lix commented Jul 16, 2021

This PR includes:

Details:

Note:
new URL(input[, base]) : details

  • When creating a new URL object, if the first parameter input is relative, then base is required. If input is absolute, the base is ignored.
  • A TypeError will be thrown if the input or base are not valid URLs.

@@ -46,7 +43,7 @@ class HttpDependencyParser extends RequestParser {
* Gets a dependency data contract object for a completed ClientRequest.
*/
public getDependencyTelemetry(baseTelemetry?: Contracts.Telemetry, dependencyId?: string): Contracts.DependencyTelemetry {
let urlObject = url.parse(this.url);
let urlObject = new url.URL(this.url);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When this.url is used, it is the full url (including protocol) so ignore base.

const parsed = url.parse(options);
if (parsed.host === "443") {
options = url.parse("https://" + options);
const parsed = new url.URL("http://" + options);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The protocol "http://" on this line is in order to make the new URL work - it requires protocol to parse url:
new URL(input[, base])

  • input The absolute or relative input URL to parse. If input is relative, then base is required. If input is absolute, the base is ignored.

@@ -167,13 +165,15 @@ class HttpRequestParser extends RequestParser {
}

var encrypted = (<any>request).connection ? ((<any>request).connection as any).encrypted : null;
var requestUrl = url.parse(request.url);

var protocol = (encrypted || request.headers["x-forwarded-proto"] == "https") ? "https" : "http";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update the sequence to get the protocol first, then URL API can be used - it requires protocol in url.

telemetry.target = url.parse(telemetry.data).host;
// url.parse() is deprecated, update to use WHATWG URL API instead
try {
telemetry.target = new url.URL(telemetry.data).host;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A TypeError will be thrown if the input or base are not valid URLs.
To be compliant with previous behavior, when url is invalid, set telemetry.target as null.

Comment on lines +59 to +60
assert.equal(dependencyTelemetry.data, "https://a.bing.com/search");
assert.equal(dependencyTelemetry.target, "a.bing.com");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove port because :
When using URL API to create a new object, when port matches with the protocol, the port is dropped.

For example:

  • new URL("https://a.bing.com:443/search").port returns "" - port dropped since https matches with 443
  • new URL("http://a.bing.com:443/search").port returns "443" - port not dropped
  • new URL("http://a.bing.com:80/search").port returns "" - port dropped since http matches with 80
  • new URL("https://a.bing.com:80/search").port returns "80" - port not dropped
  • new URL("https://a.bing.com:1234/search").port returns "1234" - port not dropped
  • new URL("http://a.bing.com:1234/search").port returns "1234" - port not dropped

image

Confirmed with Nev that : the default port for a protocol is often never sent @hectorhdzg can you confirm this? Thanks.

@xiao-lix xiao-lix requested a review from hectorhdzg July 16, 2021 20:41
Copy link
Member

@hectorhdzg hectorhdzg left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

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.

None yet

2 participants