Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Url.parse without protocol #7547

Closed
petrjanda opened this issue May 2, 2014 · 4 comments
Closed

Url.parse without protocol #7547

petrjanda opened this issue May 2, 2014 · 4 comments

Comments

@petrjanda
Copy link

Recently stumbled upon the unexpected behaviour on url.parse. Url without protocol:

require('url').parse('localhost:3000')

will result in the following:

{ protocol: 'localhost:',
  slashes: null,
  auth: null,
  host: '3000',
  port: null,
  hostname: '3000',
  hash: null,
  search: null,
  query: null,
  pathname: null,
  path: null,
  href: 'localhost:3000' }

Instead I would expect protocol being empty and host/hostname and port being initialised.
Running node v0.10.22.

@indutny
Copy link
Member

indutny commented May 2, 2014

I'm afraid that what you are passing to the url.parse() is not a URL.

 URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

      hier-part   = "//" authority path-abempty
                  / path-absolute
                  / path-rootless
                  / path-empty

See http://tools.ietf.org/html/rfc3986 for details.

@indutny indutny closed this as completed May 2, 2014
@petrjanda
Copy link
Author

Yeah, indeed its not valid URL. I felt like it would be good to get a feedback that I am providing invalid input. Would it than make sense to raise an exception?

@indutny
Copy link
Member

indutny commented May 2, 2014

Hm... perhaps... @tjfontaine ?

@tjfontaine
Copy link

We can certainly document slightly better how this works, but for instance the reason this doesn't work this way is because there are urls without slashes that should be accurately parsed in that way:

From url.format http://nodejs.org/api/url.html#url_url_format_urlobj

All other protocols mailto, xmpp, aim, sftp, foo, etc will be postfixed with : (colon)

There's not a good deterministic approach to know that if otherwise not specified we meant "parse me as host and port" and cover any potential new protocol that someone makes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants