(u string)
| 100 | } |
| 101 | |
| 102 | func isLoopbackURL(u string) bool { |
| 103 | // 支持 http://host:port、http://host、https://[::1]:port 等格式 |
| 104 | for _, scheme := range []string{"https://", "http://"} { |
| 105 | u = trimPrefixFold(u, scheme) |
| 106 | } |
| 107 | host, _, err := net.SplitHostPort(u) |
| 108 | if err != nil { |
| 109 | // 没有端口号,整个字符串是 host |
| 110 | host = u |
| 111 | } |
| 112 | return host == "localhost" || host == "127.0.0.1" || host == "::1" |
| 113 | } |
| 114 | |
| 115 | func trimPrefixFold(s, prefix string) string { |
| 116 | if len(s) >= len(prefix) && s[:len(prefix)] == prefix { |
no test coverage detected