Skip to content

Add method to parse host, port, and protocol from URL - #100

Open
vartanbeno wants to merge 3 commits into
developfrom
port-secure-flags
Open

Add method to parse host, port, and protocol from URL#100
vartanbeno wants to merge 3 commits into
developfrom
port-secure-flags

Conversation

@vartanbeno

Copy link
Copy Markdown
Contributor

Overview

Normally, when we specify the host of an API, we do so like this:

{
    "host": "example.com",
    "port": 1234,
    "secure": false
}

If we specify both the protocol and the port in the host variable, it makes the sdk behave in unexpected ways. For example:

{
    "host": "http://example.com:1234"
    // secure is true by default
}

All the requests would go to https://http://example.com:1234:443.

Using Node's url module, we parse the url, and if a protocol/port are found, we override the default ones.

// input:
{
    "host": "http://example.com:1234"
}

// output:
{
    "host": "example.com",
    "port": 1234,
    "secure": false
}

This change is made with backward compatibility in mind. If the host we provide is just the hostname, the sdk behaves like normal.

Flaws

If we specify a host with a port but not a protocol:

{
    "host": "example.com:1234"
}

Node's url parser thinks the protocol is example.com: and that the hostname is '', which would default to localhost.

Todo

  • If a host is provided with a port but not a protocol, should we prepend the protocol to the host based on the secure flag?

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
@coveralls

coveralls commented Mar 16, 2020

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.3%) to 91.433% when pulling f85ccc0 on port-secure-flags into de25bde on develop.

@fvnilo

fvnilo commented Mar 16, 2020

Copy link
Copy Markdown

Based on what I see in your unit tests, I would add the flawed case you described above. My naive guess would be that it defaults to https.

Comment thread lib/url.js
* @param {Object} output An object containing host, port, and secure properties.
* @returns {Object} The object containing valid host, port, and secure keys based on the input.
*/
const parseURL = (input) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As mentionned in the global comments, I think here I would handle the case of not specifying a protocol.

Comment thread test/lib/url.js Outdated
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
Comment thread lib/url.js
}

if (!input.startsWith(PROTOCOL_HTTP) && !input.startsWith(PROTOCOL_HTTPS)) {
input = PROTOCOL_HTTPS + input;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't you add a // in the middle here?

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.

It doesn't seem to be required by the url module to parse the input. On second thought though, I'll add it since most people expect https:// instead of just https:.

Comment thread lib/url.js
let url;
try {
url = new URL(input);
} catch (e) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would it be useful to log a warning, you think?

@benjamin-morin
benjamin-morin removed their request for review July 30, 2024 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants