Skip to content

Conversation

@vartanbeno
Copy link
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
Copy link

coveralls commented Mar 16, 2020

Coverage Status

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

@fvnilo
Copy link

fvnilo commented Mar 16, 2020

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.

* @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

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.

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

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

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
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:.

let url;
try {
url = new URL(input);
} catch (e) {
Copy link

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.

4 participants