defunctzombie / node-url

node.js core url module as a module
MIT License
376 stars 96 forks source link

Parsing host without protocol #40

Open ccorcos opened 6 years ago

ccorcos commented 6 years ago

This library doesnt seem to parse the host properly when there isnt a protocol.

> u.parse("www.google.com/yay?q=10&q=12")
Url {
  protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '?q=10&q=12',
  query: 'q=10&q=12',
  pathname: 'www.google.com/yay',
  path: 'www.google.com/yay?q=10&q=12',
  href: 'www.google.com/yay?q=10&q=12' }
sc0Vu commented 6 years ago

I met this too.

vv13 commented 6 years ago

i judge protocol exists before parse:

  if (/http/gi.test(url)) {
    url.parse(url)
  }

hope it can help you .

ljharb commented 3 years ago

What does node's core url module do here?

(specifically, without a protocol, it's not actually a URL, so this might simply be user error)

ameshkin commented 1 year ago

I have seen many cases of protocol less links such as //google.com which are annoying. Everyone should always just use SSL and https.

This library does not correctly handle these links well but you can use regex to look for //. I may fork and update this package.

ameshkin commented 1 year ago

This library doesnt seem to parse the host properly when there isnt a protocol.

> u.parse("www.google.com/yay?q=10&q=12")
Url {
  protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '?q=10&q=12',
  query: 'q=10&q=12',
  pathname: 'www.google.com/yay',
  path: 'www.google.com/yay?q=10&q=12',
  href: 'www.google.com/yay?q=10&q=12' }

Without // , http or https, it's not a domain.

www.google.com/yay?q=10&q=12 needs to have // instead of www.

ljharb commented 1 year ago

@ameshkin this library matches what node’s core url module does, so if that one doesn’t do it, this one shouldn’t either.