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

Calling port() with a default port returns None unexpectedly #957

Open
1 task done
TomPridham opened this issue Aug 9, 2024 · 4 comments
Open
1 task done

Calling port() with a default port returns None unexpectedly #957

TomPridham opened this issue Aug 9, 2024 · 4 comments

Comments

@TomPridham
Copy link

TomPridham commented Aug 9, 2024

  • Note that this crate implements the URL Standard not RFC 1738 or RFC 3986

Describe the bug
related to #28
This was an issue way back in 2014, but doesn't seem like it was ever really addressed.
I would expect calling port() on http://example.com to return 80. It is really confusing that it would return a None. It's even worse when you set the port explicitly http://example.com:80 and it still seems like it isn't working correctly because it still returns None.

I could update the docs to have an example of the port being ignored, but it seems like it would just make more sense to have port() actually return the port and do what is suggested in this comment

@valenting
Copy link
Collaborator

I would think the docs are pretty clear on this issue.

https://docs.rs/url/2.5.2/url/struct.Url.html#method.port

You want port_or_known_default. Do you feel like the docs are missing something?

Note that changing the behaviour of port is a breaking change, and not something we'd consider doing at the moment.

@link2xt
Copy link

link2xt commented Sep 20, 2024

Can you add another API that returns Some(443) for "https://example.com:443" and None for https://example.com?

I need to distinguish between these cases because when user scans a QR code, in the first case this is likely an HTTPS proxy URL and in the second case it is likely a normal URL that I want to offer to open in a browser:
deltachat/deltachat-core-rust#5980

As a workaround I will now try to look at .authority() and see if it ends in :443 when scheme is https (and :80 for http), but this seems to be really hacky.
EDIT: does not work as default port is also removed from .authority().

@link2xt
Copy link

link2xt commented Sep 20, 2024

Another way to fix this would be to add an option to ParseOption to disable default ports.

@link2xt
Copy link

link2xt commented Sep 20, 2024

As a workaround I now replace http:// or https:// with foobarbaz:// before parsing. This disables all sort of normalization for ports, also does not add / path when the path originally does not exist.

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

No branches or pull requests

3 participants