Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

url.parse parses hostname as protocol when protocol/scheme is absent in hostname:port formatted uRL #2465

Closed
dap opened this issue Aug 20, 2015 · 4 comments
Labels
url Issues and PRs related to the legacy built-in url module.

Comments

@dap
Copy link

dap commented Aug 20, 2015

I've observed that if a URL with port specified, and scheme unspecified, is supplied to url.parse, the hostname is set as protocol, and port as hostname in the result:

$ node 
> var url = require('url');
undefined
> url.parse('localhost:8080/foo/bar');
{ protocol: 'localhost:',
  slashes: null,
  auth: null,
  host: '8080',
  port: null,
  hostname: '8080',
  hash: null,
  search: null,
  query: null,
  pathname: '/foo/bar',
  path: '/foo/bar',
  href: 'localhost:8080/foo/bar' }
> 

I'm unsure of whether this is a bug, but I figured I'd report the behavior. I'm using version v0.12.7, on OS X.

@mscdex mscdex added the url Issues and PRs related to the legacy built-in url module. label Aug 20, 2015
@mscdex
Copy link
Contributor

mscdex commented Aug 20, 2015

I believe this is expected behavior as the url module cannot know that there is a missing protocol (because there is no hardcoded protocol list to verify against) and what that missing protocol is supposed to be.

Also, the url module supports urls of the form <protocol>:... like mailto:foo@bar.com and magnet:?xt=urn:sha1:40AE8A90DE40CA3AFA763C8EDB43FC1FC47D75F1, so that is why it's parsed the way you currently see it.

@sam-github
Copy link
Contributor

Expected behaviour, your protocol is localhost:, though you probably didn't intend it that way. Or maybe you do have a protocol called "localhost", who's to tell?

@dap
Copy link
Author

dap commented Aug 21, 2015

Got it. Both of your comments make sense. Thanks.

@ioncreature
Copy link

ioncreature commented Dec 15, 2016

I have caught the same behaviour, quite annoying.

So now I have to check manually - is there protocol in string or not.

Believe it should do url.parse by itself

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
url Issues and PRs related to the legacy built-in url module.
Projects
None yet
Development

No branches or pull requests

4 participants