38

我正在尝试使用它URLComponents来组成一个URL,因为这似乎是它的用途。

但是,当我然后访问url组件的属性时,它是零。

例子...

var urlComponents = URLComponents(string: "http://google.com")!
urlComponents.path = "auth/login"

那我做...

print(urlComponents)

输出...

scheme: http host: google.com path: auth/login
  - scheme : "http"
  - host : "google.com"
  - path : "auth/login"

但是之后...

print(urlComponents.url)

输出nil

我做错了什么吗?我如何从这一切中获得完整的 URL?谢谢

4

2 回答 2

117

看起来path参数的字符串必须以/.

所以改成"auth/login"就行了"/auth/login"

于 2016-10-04T12:59:13.533 回答
25

TL;博士

如果您正在创建一个 Web URL(例如http://example.com/path),请包含 ,/因为它使用以下模板:

{scheme}://{user}:{password}@{host}:{port}{path}?{query}#{fragment}

如果您正在创建非 Web URL(例如mailto:user@example.com),请不要包含 ,/因为它使用以下模板:

{scheme}:{path}?{query}%23{fragment}

为了澄清为什么会发生这种情况,URLComponents的实际模板如下:

{scheme}://{user}:{password}@{host}:{port}{path}?{query}#{fragment}

请注意,除了and之外,每两个组件之间都有标点符号 ( ://, :, @, :, ?, ) 。#{port}{path}

因此,{path}必须以标点符号 ( /) 为前缀,否则会产生无效的 URL。

在您的情况下,您必须使用:

urlComponents.path = "/auth/login"

请注意,即使您省略了 URLComponents 的不常见字段,您仍然会遇到同样的问题:

{scheme}://{host}{path}?{query}#{fragment}

注意在这种情况下{host}{path}是如何相邻的,没有标点符号将两者分开。

您可能想知道为什么 Apple 之前没有包含标点符号{port}。我的猜测是因为 URLComponents 可用于生成RFC 3986规范中的任何类型的 URL,而不仅仅是 Web URL。例如,它可以很容易地生成http://www.example.com/foo,但它应该同样容易地生成表单的 URL mailto:user@example.com

URLComponents 通过更改省略之间{scheme}{path}之间的分隔符来实现这一点:{host}

{scheme}:{path}?{query}%23{fragment}

因此,当我们想要生成一个 mailto 链接时,我们实际上不会包含/in 路径:

urlComponents.scheme = "mailto"
urlComponents.path = "user@example.com"
// urlComponents.url!.absoluteString == "mailto:user@example.com"

将 URLComponents 的属性与 URL 的属性进行比较提供了更多的洞察力(并揭示了一些不一致之处):

let string = "scheme://user:password@host:123/path/path/path/?query#fragment"
let urlComponents = URLComponents(string: string)!
let url = URL(string: string)!

// these are expected
url.absoluteString == urlComponents.url!.absoluteString
url.scheme == urlComponents.scheme
url.user == urlComponents.user
url.password == urlComponents.password
url.host == urlComponents.host
url.port == urlComponents.port
url.query == urlComponents.query
url.fragment == urlComponents.fragment

// this is unexpected. They should have been equal to each other
url.path != urlComponents.path

url.path == "/path/path/path"
urlComponents.path == "/path/path/path/"

url.pathComponents == ["/", "path", "path", "path"]
url.lastPathComponent == "path"

有两点需要注意:

  • url.path后缀中没有/
  • url.pathComponents[0]/URL 中的第一个。

对于模板的详尽列表,您可以使用以下代码:

let componentsCount = Double(8)
let iterations = Int(pow(2, componentsCount))

for i in 0..<iterations {
  var urlComponents = URLComponents()
  var key = ""
  if i & 0b10000000 > 0 { urlComponents.scheme = "scheme"; key += "s" } else { key += " " }
  if i & 0b01000000 > 0 { urlComponents.user = "user"; key += "u" } else { key += " " }
  if i & 0b00100000 > 0 { urlComponents.password = "password"; key += "w" } else { key += " " }
  if i & 0b00010000 > 0 { urlComponents.host = "host"; key += "h" } else { key += " " }
  if i & 0b00001000 > 0 { urlComponents.port = 123; key += "r" } else { key += " " }
  if i & 0b00000100 > 0 { urlComponents.path = "/path"; key += "p" } else { key += " " }
  if i & 0b00000010 > 0 { urlComponents.query = "query"; key += "q" } else { key += " " }
  if i & 0b00000001 > 0 { urlComponents.fragment = "fragment"; key += "f" } else { key += " " }

  if let url = urlComponents.url?.absoluteString {
    print("[\(key)] \(url)")
  } else {
    print("[\(key)] ------- nil --------")
  }
}

产生以下输出:

[        ] 
[       f] #fragment
[      q ] ?query
[      qf] ?query#fragment
[     p  ] /path
[     p f] /path#fragment
[     pq ] /path?query
[     pqf] /path?query#fragment
[    r   ] //:123
[    r  f] //:123#fragment
[    r q ] //:123?query
[    r qf] //:123?query#fragment
[    rp  ] //:123/path
[    rp f] //:123/path#fragment
[    rpq ] //:123/path?query
[    rpqf] //:123/path?query#fragment
[   h    ] //host
[   h   f] //host#fragment
[   h  q ] //host?query
[   h  qf] //host?query#fragment
[   h p  ] //host/path
[   h p f] //host/path#fragment
[   h pq ] //host/path?query
[   h pqf] //host/path?query#fragment
[   hr   ] //host:123
[   hr  f] //host:123#fragment
[   hr q ] //host:123?query
[   hr qf] //host:123?query#fragment
[   hrp  ] //host:123/path
[   hrp f] //host:123/path#fragment
[   hrpq ] //host:123/path?query
[   hrpqf] //host:123/path?query#fragment
[  w     ] //:password@
[  w    f] //:password@#fragment
[  w   q ] //:password@?query
[  w   qf] //:password@?query#fragment
[  w  p  ] //:password@/path
[  w  p f] //:password@/path#fragment
[  w  pq ] //:password@/path?query
[  w  pqf] //:password@/path?query#fragment
[  w r   ] //:password@:123
[  w r  f] //:password@:123#fragment
[  w r q ] //:password@:123?query
[  w r qf] //:password@:123?query#fragment
[  w rp  ] //:password@:123/path
[  w rp f] //:password@:123/path#fragment
[  w rpq ] //:password@:123/path?query
[  w rpqf] //:password@:123/path?query#fragment
[  wh    ] //:password@host
[  wh   f] //:password@host#fragment
[  wh  q ] //:password@host?query
[  wh  qf] //:password@host?query#fragment
[  wh p  ] //:password@host/path
[  wh p f] //:password@host/path#fragment
[  wh pq ] //:password@host/path?query
[  wh pqf] //:password@host/path?query#fragment
[  whr   ] //:password@host:123
[  whr  f] //:password@host:123#fragment
[  whr q ] //:password@host:123?query
[  whr qf] //:password@host:123?query#fragment
[  whrp  ] //:password@host:123/path
[  whrp f] //:password@host:123/path#fragment
[  whrpq ] //:password@host:123/path?query
[  whrpqf] //:password@host:123/path?query#fragment
[ u      ] //user@
[ u     f] //user@#fragment
[ u    q ] //user@?query
[ u    qf] //user@?query#fragment
[ u   p  ] //user@/path
[ u   p f] //user@/path#fragment
[ u   pq ] //user@/path?query
[ u   pqf] //user@/path?query#fragment
[ u  r   ] //user@:123
[ u  r  f] //user@:123#fragment
[ u  r q ] //user@:123?query
[ u  r qf] //user@:123?query#fragment
[ u  rp  ] //user@:123/path
[ u  rp f] //user@:123/path#fragment
[ u  rpq ] //user@:123/path?query
[ u  rpqf] //user@:123/path?query#fragment
[ u h    ] //user@host
[ u h   f] //user@host#fragment
[ u h  q ] //user@host?query
[ u h  qf] //user@host?query#fragment
[ u h p  ] //user@host/path
[ u h p f] //user@host/path#fragment
[ u h pq ] //user@host/path?query
[ u h pqf] //user@host/path?query#fragment
[ u hr   ] //user@host:123
[ u hr  f] //user@host:123#fragment
[ u hr q ] //user@host:123?query
[ u hr qf] //user@host:123?query#fragment
[ u hrp  ] //user@host:123/path
[ u hrp f] //user@host:123/path#fragment
[ u hrpq ] //user@host:123/path?query
[ u hrpqf] //user@host:123/path?query#fragment
[ uw     ] //user:password@
[ uw    f] //user:password@#fragment
[ uw   q ] //user:password@?query
[ uw   qf] //user:password@?query#fragment
[ uw  p  ] //user:password@/path
[ uw  p f] //user:password@/path#fragment
[ uw  pq ] //user:password@/path?query
[ uw  pqf] //user:password@/path?query#fragment
[ uw r   ] //user:password@:123
[ uw r  f] //user:password@:123#fragment
[ uw r q ] //user:password@:123?query
[ uw r qf] //user:password@:123?query#fragment
[ uw rp  ] //user:password@:123/path
[ uw rp f] //user:password@:123/path#fragment
[ uw rpq ] //user:password@:123/path?query
[ uw rpqf] //user:password@:123/path?query#fragment
[ uwh    ] //user:password@host
[ uwh   f] //user:password@host#fragment
[ uwh  q ] //user:password@host?query
[ uwh  qf] //user:password@host?query#fragment
[ uwh p  ] //user:password@host/path
[ uwh p f] //user:password@host/path#fragment
[ uwh pq ] //user:password@host/path?query
[ uwh pqf] //user:password@host/path?query#fragment
[ uwhr   ] //user:password@host:123
[ uwhr  f] //user:password@host:123#fragment
[ uwhr q ] //user:password@host:123?query
[ uwhr qf] //user:password@host:123?query#fragment
[ uwhrp  ] //user:password@host:123/path
[ uwhrp f] //user:password@host:123/path#fragment
[ uwhrpq ] //user:password@host:123/path?query
[ uwhrpqf] //user:password@host:123/path?query#fragment
[s       ] scheme:
[s      f] scheme:%23fragment
[s     q ] scheme:?query
[s     qf] scheme:?query%23fragment
[s    p  ] scheme:/path
[s    p f] scheme:/path#fragment
[s    pq ] scheme:/path?query
[s    pqf] scheme:/path?query#fragment
[s   r   ] scheme://:123
[s   r  f] scheme://:123#fragment
[s   r q ] scheme://:123?query
[s   r qf] scheme://:123?query#fragment
[s   rp  ] scheme://:123/path
[s   rp f] scheme://:123/path#fragment
[s   rpq ] scheme://:123/path?query
[s   rpqf] scheme://:123/path?query#fragment
[s  h    ] scheme://host
[s  h   f] scheme://host#fragment
[s  h  q ] scheme://host?query
[s  h  qf] scheme://host?query#fragment
[s  h p  ] scheme://host/path
[s  h p f] scheme://host/path#fragment
[s  h pq ] scheme://host/path?query
[s  h pqf] scheme://host/path?query#fragment
[s  hr   ] scheme://host:123
[s  hr  f] scheme://host:123#fragment
[s  hr q ] scheme://host:123?query
[s  hr qf] scheme://host:123?query#fragment
[s  hrp  ] scheme://host:123/path
[s  hrp f] scheme://host:123/path#fragment
[s  hrpq ] scheme://host:123/path?query
[s  hrpqf] scheme://host:123/path?query#fragment
[s w     ] scheme://:password@
[s w    f] scheme://:password@#fragment
[s w   q ] scheme://:password@?query
[s w   qf] scheme://:password@?query#fragment
[s w  p  ] scheme://:password@/path
[s w  p f] scheme://:password@/path#fragment
[s w  pq ] scheme://:password@/path?query
[s w  pqf] scheme://:password@/path?query#fragment
[s w r   ] scheme://:password@:123
[s w r  f] scheme://:password@:123#fragment
[s w r q ] scheme://:password@:123?query
[s w r qf] scheme://:password@:123?query#fragment
[s w rp  ] scheme://:password@:123/path
[s w rp f] scheme://:password@:123/path#fragment
[s w rpq ] scheme://:password@:123/path?query
[s w rpqf] scheme://:password@:123/path?query#fragment
[s wh    ] scheme://:password@host
[s wh   f] scheme://:password@host#fragment
[s wh  q ] scheme://:password@host?query
[s wh  qf] scheme://:password@host?query#fragment
[s wh p  ] scheme://:password@host/path
[s wh p f] scheme://:password@host/path#fragment
[s wh pq ] scheme://:password@host/path?query
[s wh pqf] scheme://:password@host/path?query#fragment
[s whr   ] scheme://:password@host:123
[s whr  f] scheme://:password@host:123#fragment
[s whr q ] scheme://:password@host:123?query
[s whr qf] scheme://:password@host:123?query#fragment
[s whrp  ] scheme://:password@host:123/path
[s whrp f] scheme://:password@host:123/path#fragment
[s whrpq ] scheme://:password@host:123/path?query
[s whrpqf] scheme://:password@host:123/path?query#fragment
[su      ] scheme://user@
[su     f] scheme://user@#fragment
[su    q ] scheme://user@?query
[su    qf] scheme://user@?query#fragment
[su   p  ] scheme://user@/path
[su   p f] scheme://user@/path#fragment
[su   pq ] scheme://user@/path?query
[su   pqf] scheme://user@/path?query#fragment
[su  r   ] scheme://user@:123
[su  r  f] scheme://user@:123#fragment
[su  r q ] scheme://user@:123?query
[su  r qf] scheme://user@:123?query#fragment
[su  rp  ] scheme://user@:123/path
[su  rp f] scheme://user@:123/path#fragment
[su  rpq ] scheme://user@:123/path?query
[su  rpqf] scheme://user@:123/path?query#fragment
[su h    ] scheme://user@host
[su h   f] scheme://user@host#fragment
[su h  q ] scheme://user@host?query
[su h  qf] scheme://user@host?query#fragment
[su h p  ] scheme://user@host/path
[su h p f] scheme://user@host/path#fragment
[su h pq ] scheme://user@host/path?query
[su h pqf] scheme://user@host/path?query#fragment
[su hr   ] scheme://user@host:123
[su hr  f] scheme://user@host:123#fragment
[su hr q ] scheme://user@host:123?query
[su hr qf] scheme://user@host:123?query#fragment
[su hrp  ] scheme://user@host:123/path
[su hrp f] scheme://user@host:123/path#fragment
[su hrpq ] scheme://user@host:123/path?query
[su hrpqf] scheme://user@host:123/path?query#fragment
[suw     ] scheme://user:password@
[suw    f] scheme://user:password@#fragment
[suw   q ] scheme://user:password@?query
[suw   qf] scheme://user:password@?query#fragment
[suw  p  ] scheme://user:password@/path
[suw  p f] scheme://user:password@/path#fragment
[suw  pq ] scheme://user:password@/path?query
[suw  pqf] scheme://user:password@/path?query#fragment
[suw r   ] scheme://user:password@:123
[suw r  f] scheme://user:password@:123#fragment
[suw r q ] scheme://user:password@:123?query
[suw r qf] scheme://user:password@:123?query#fragment
[suw rp  ] scheme://user:password@:123/path
[suw rp f] scheme://user:password@:123/path#fragment
[suw rpq ] scheme://user:password@:123/path?query
[suw rpqf] scheme://user:password@:123/path?query#fragment
[suwh    ] scheme://user:password@host
[suwh   f] scheme://user:password@host#fragment
[suwh  q ] scheme://user:password@host?query
[suwh  qf] scheme://user:password@host?query#fragment
[suwh p  ] scheme://user:password@host/path
[suwh p f] scheme://user:password@host/path#fragment
[suwh pq ] scheme://user:password@host/path?query
[suwh pqf] scheme://user:password@host/path?query#fragment
[suwhr   ] scheme://user:password@host:123
[suwhr  f] scheme://user:password@host:123#fragment
[suwhr q ] scheme://user:password@host:123?query
[suwhr qf] scheme://user:password@host:123?query#fragment
[suwhrp  ] scheme://user:password@host:123/path
[suwhrp f] scheme://user:password@host:123/path#fragment
[suwhrpq ] scheme://user:password@host:123/path?query
[suwhrpqf] scheme://user:password@host:123/path?query#fragment

注意这些都不是nil。但是,如果您更改urlComponents.path = "/path"urlComponents.path = "path",您将获得一组完全不同的模板:

[        ] 
[       f] #fragment
[      q ] ?query
[      qf] ?query#fragment
[     p  ] path
[     p f] path#fragment
[     pq ] path?query
[     pqf] path?query#fragment
[    r   ] //:123
[    r  f] //:123#fragment
[    r q ] //:123?query
[    r qf] //:123?query#fragment
[    rp  ] ------- nil --------
[    rp f] ------- nil --------
[    rpq ] ------- nil --------
[    rpqf] ------- nil --------
[   h    ] //host
[   h   f] //host#fragment
[   h  q ] //host?query
[   h  qf] //host?query#fragment
[   h p  ] ------- nil --------
[   h p f] ------- nil --------
[   h pq ] ------- nil --------
[   h pqf] ------- nil --------
[   hr   ] //host:123
[   hr  f] //host:123#fragment
[   hr q ] //host:123?query
[   hr qf] //host:123?query#fragment
[   hrp  ] ------- nil --------
[   hrp f] ------- nil --------
[   hrpq ] ------- nil --------
[   hrpqf] ------- nil --------
[  w     ] //:password@
[  w    f] //:password@#fragment
[  w   q ] //:password@?query
[  w   qf] //:password@?query#fragment
[  w  p  ] ------- nil --------
[  w  p f] ------- nil --------
[  w  pq ] ------- nil --------
[  w  pqf] ------- nil --------
[  w r   ] //:password@:123
[  w r  f] //:password@:123#fragment
[  w r q ] //:password@:123?query
[  w r qf] //:password@:123?query#fragment
[  w rp  ] ------- nil --------
[  w rp f] ------- nil --------
[  w rpq ] ------- nil --------
[  w rpqf] ------- nil --------
[  wh    ] //:password@host
[  wh   f] //:password@host#fragment
[  wh  q ] //:password@host?query
[  wh  qf] //:password@host?query#fragment
[  wh p  ] ------- nil --------
[  wh p f] ------- nil --------
[  wh pq ] ------- nil --------
[  wh pqf] ------- nil --------
[  whr   ] //:password@host:123
[  whr  f] //:password@host:123#fragment
[  whr q ] //:password@host:123?query
[  whr qf] //:password@host:123?query#fragment
[  whrp  ] ------- nil --------
[  whrp f] ------- nil --------
[  whrpq ] ------- nil --------
[  whrpqf] ------- nil --------
[ u      ] //user@
[ u     f] //user@#fragment
[ u    q ] //user@?query
[ u    qf] //user@?query#fragment
[ u   p  ] ------- nil --------
[ u   p f] ------- nil --------
[ u   pq ] ------- nil --------
[ u   pqf] ------- nil --------
[ u  r   ] //user@:123
[ u  r  f] //user@:123#fragment
[ u  r q ] //user@:123?query
[ u  r qf] //user@:123?query#fragment
[ u  rp  ] ------- nil --------
[ u  rp f] ------- nil --------
[ u  rpq ] ------- nil --------
[ u  rpqf] ------- nil --------
[ u h    ] //user@host
[ u h   f] //user@host#fragment
[ u h  q ] //user@host?query
[ u h  qf] //user@host?query#fragment
[ u h p  ] ------- nil --------
[ u h p f] ------- nil --------
[ u h pq ] ------- nil --------
[ u h pqf] ------- nil --------
[ u hr   ] //user@host:123
[ u hr  f] //user@host:123#fragment
[ u hr q ] //user@host:123?query
[ u hr qf] //user@host:123?query#fragment
[ u hrp  ] ------- nil --------
[ u hrp f] ------- nil --------
[ u hrpq ] ------- nil --------
[ u hrpqf] ------- nil --------
[ uw     ] //user:password@
[ uw    f] //user:password@#fragment
[ uw   q ] //user:password@?query
[ uw   qf] //user:password@?query#fragment
[ uw  p  ] ------- nil --------
[ uw  p f] ------- nil --------
[ uw  pq ] ------- nil --------
[ uw  pqf] ------- nil --------
[ uw r   ] //user:password@:123
[ uw r  f] //user:password@:123#fragment
[ uw r q ] //user:password@:123?query
[ uw r qf] //user:password@:123?query#fragment
[ uw rp  ] ------- nil --------
[ uw rp f] ------- nil --------
[ uw rpq ] ------- nil --------
[ uw rpqf] ------- nil --------
[ uwh    ] //user:password@host
[ uwh   f] //user:password@host#fragment
[ uwh  q ] //user:password@host?query
[ uwh  qf] //user:password@host?query#fragment
[ uwh p  ] ------- nil --------
[ uwh p f] ------- nil --------
[ uwh pq ] ------- nil --------
[ uwh pqf] ------- nil --------
[ uwhr   ] //user:password@host:123
[ uwhr  f] //user:password@host:123#fragment
[ uwhr q ] //user:password@host:123?query
[ uwhr qf] //user:password@host:123?query#fragment
[ uwhrp  ] ------- nil --------
[ uwhrp f] ------- nil --------
[ uwhrpq ] ------- nil --------
[ uwhrpqf] ------- nil --------
[s       ] scheme:
[s      f] scheme:%23fragment
[s     q ] scheme:?query
[s     qf] scheme:?query%23fragment
[s    p  ] scheme:path
[s    p f] scheme:path%23fragment
[s    pq ] scheme:path?query
[s    pqf] scheme:path?query%23fragment
[s   r   ] scheme://:123
[s   r  f] scheme://:123#fragment
[s   r q ] scheme://:123?query
[s   r qf] scheme://:123?query#fragment
[s   rp  ] ------- nil --------
[s   rp f] ------- nil --------
[s   rpq ] ------- nil --------
[s   rpqf] ------- nil --------
[s  h    ] scheme://host
[s  h   f] scheme://host#fragment
[s  h  q ] scheme://host?query
[s  h  qf] scheme://host?query#fragment
[s  h p  ] ------- nil --------
[s  h p f] ------- nil --------
[s  h pq ] ------- nil --------
[s  h pqf] ------- nil --------
[s  hr   ] scheme://host:123
[s  hr  f] scheme://host:123#fragment
[s  hr q ] scheme://host:123?query
[s  hr qf] scheme://host:123?query#fragment
[s  hrp  ] ------- nil --------
[s  hrp f] ------- nil --------
[s  hrpq ] ------- nil --------
[s  hrpqf] ------- nil --------
[s w     ] scheme://:password@
[s w    f] scheme://:password@#fragment
[s w   q ] scheme://:password@?query
[s w   qf] scheme://:password@?query#fragment
[s w  p  ] ------- nil --------
[s w  p f] ------- nil --------
[s w  pq ] ------- nil --------
[s w  pqf] ------- nil --------
[s w r   ] scheme://:password@:123
[s w r  f] scheme://:password@:123#fragment
[s w r q ] scheme://:password@:123?query
[s w r qf] scheme://:password@:123?query#fragment
[s w rp  ] ------- nil --------
[s w rp f] ------- nil --------
[s w rpq ] ------- nil --------
[s w rpqf] ------- nil --------
[s wh    ] scheme://:password@host
[s wh   f] scheme://:password@host#fragment
[s wh  q ] scheme://:password@host?query
[s wh  qf] scheme://:password@host?query#fragment
[s wh p  ] ------- nil --------
[s wh p f] ------- nil --------
[s wh pq ] ------- nil --------
[s wh pqf] ------- nil --------
[s whr   ] scheme://:password@host:123
[s whr  f] scheme://:password@host:123#fragment
[s whr q ] scheme://:password@host:123?query
[s whr qf] scheme://:password@host:123?query#fragment
[s whrp  ] ------- nil --------
[s whrp f] ------- nil --------
[s whrpq ] ------- nil --------
[s whrpqf] ------- nil --------
[su      ] scheme://user@
[su     f] scheme://user@#fragment
[su    q ] scheme://user@?query
[su    qf] scheme://user@?query#fragment
[su   p  ] ------- nil --------
[su   p f] ------- nil --------
[su   pq ] ------- nil --------
[su   pqf] ------- nil --------
[su  r   ] scheme://user@:123
[su  r  f] scheme://user@:123#fragment
[su  r q ] scheme://user@:123?query
[su  r qf] scheme://user@:123?query#fragment
[su  rp  ] ------- nil --------
[su  rp f] ------- nil --------
[su  rpq ] ------- nil --------
[su  rpqf] ------- nil --------
[su h    ] scheme://user@host
[su h   f] scheme://user@host#fragment
[su h  q ] scheme://user@host?query
[su h  qf] scheme://user@host?query#fragment
[su h p  ] ------- nil --------
[su h p f] ------- nil --------
[su h pq ] ------- nil --------
[su h pqf] ------- nil --------
[su hr   ] scheme://user@host:123
[su hr  f] scheme://user@host:123#fragment
[su hr q ] scheme://user@host:123?query
[su hr qf] scheme://user@host:123?query#fragment
[su hrp  ] ------- nil --------
[su hrp f] ------- nil --------
[su hrpq ] ------- nil --------
[su hrpqf] ------- nil --------
[suw     ] scheme://user:password@
[suw    f] scheme://user:password@#fragment
[suw   q ] scheme://user:password@?query
[suw   qf] scheme://user:password@?query#fragment
[suw  p  ] ------- nil --------
[suw  p f] ------- nil --------
[suw  pq ] ------- nil --------
[suw  pqf] ------- nil --------
[suw r   ] scheme://user:password@:123
[suw r  f] scheme://user:password@:123#fragment
[suw r q ] scheme://user:password@:123?query
[suw r qf] scheme://user:password@:123?query#fragment
[suw rp  ] ------- nil --------
[suw rp f] ------- nil --------
[suw rpq ] ------- nil --------
[suw rpqf] ------- nil --------
[suwh    ] scheme://user:password@host
[suwh   f] scheme://user:password@host#fragment
[suwh  q ] scheme://user:password@host?query
[suwh  qf] scheme://user:password@host?query#fragment
[suwh p  ] ------- nil --------
[suwh p f] ------- nil --------
[suwh pq ] ------- nil --------
[suwh pqf] ------- nil --------
[suwhr   ] scheme://user:password@host:123
[suwhr  f] scheme://user:password@host:123#fragment
[suwhr q ] scheme://user:password@host:123?query
[suwhr qf] scheme://user:password@host:123?query#fragment
[suwhrp  ] ------- nil --------
[suwhrp f] ------- nil --------
[suwhrpq ] ------- nil --------
[suwhrpqf] ------- nil --------
于 2018-08-01T18:41:02.360 回答