英文:
In golang How to split a URL and encode back to URL from decoded components. Any packages?
问题
在Golang中,如何拆分URL并将解码后的组件重新编码为URL?有没有可以完成这个任务的包?
net/url 只能帮助解码URL。我想修改 HOST
和 PORT
并重新创建URL。我的问题源于接收到没有方括号的 IPV6:port
的情况。假设我以以下格式接收到 IPV6:port
:
aaa:abbb:cccc:dddd:0000:0000:00aa:bbbb:8080/static/silly.html
我想用方括号重新构建URL中的IPV6地址。
英文:
In golang How to split a URL and encode back to URL from decoded components. Any packages that do the job?
net/url helps only in decoding the URL. I want to modify the HOST
and PORT
and recreate the URL. My problem originates from the case where I receive IPV6:port
without square brackets. Let us say I get IPV6:port
in the format as:
aaa:abbb:cccc:dddd:0000:0000:00aa:bbbb:8080/static/silly.html
I want to reconstruct the URL with brackets arround IPV6 address.
答案1
得分: 4
我认为这是不可能的。例如,如果你得到了:
2001:db8::1:80
你怎么能判断这个IP地址是
2001:db8::1
还是:
2001:db8::1:80
这就是为什么RFC 5952建议使用方括号(或其他字符)来区分IP地址和端口号的原因。
因此,如果可能的话,我建议你忽略这种模糊的格式。
更新
实际上,如果你能确保能够区分这两部分,你可以做你想做的事情,也就是当你能够准确计算出字符:
的出现次数为8时。
if strings.Count(url, ":") == 8 {
split := strings.Split(url, ":")
url = "[" + strings.Join(split[:8], ":") + "]:" + split[8]
}
但是,这可能不是处理这种类型的URL格式的最佳方法...
英文:
I think that would not be possible. For instance, if you get:
2001:db8::1:80
How could you tell if the IP address is
2001:db8::1
Or:
2001:db8::1:80
That is the reason why RFC 5952 recommands to use brackets (or some other characters) to distinguish the IP address from the port number.
Consequently, if possible, I recommand you to ignore this ambiguous formatting.
UPDATE
Actually, you can do what you want if you are sure to be able to distinguish the two parts, which is when you can count exatcly 8 occurences of the character :
.
if strings.Count(url, ":") == 8 {
split := strings.Split(url, ":")
url = "[" + strings.Join(split[:8], ":") + "]:" + split[8]
}
But this is probably not the best idea to process this kind of url formatting anyway...
答案2
得分: 0
你可以使用标准的net
包来帮助处理。我使用了net.ParseIP
和net.JoinHostPort
函数。
package main
import (
"fmt"
"net"
"strings"
)
func splitLastColumn(host string) (string, string, string) {
idx := strings.Index(host, "/")
col_idx := strings.LastIndex(host[:idx], ":")
return host[:col_idx], host[col_idx+1 : idx], host[idx:]
}
func main() {
ipstr := "aaa:abbb:cccc:dddd:0000:0000:00aa:bbbb:8080/static/silly.html"
host, port, path := splitLastColumn(ipstr)
ip := net.ParseIP(host)
if ip == nil {
fmt.Println("invalid addr ")
}
fmt.Println(ip, host, port, path)
fmt.Println(net.JoinHostPort(host, port))
}
你可以在Playground上运行这段代码。
英文:
You can use the standard net
package to help in the process [Playground][http://play.golang.org/p/wz0g7rgdU4]
I use the net.ParseIP
and net.JoinHostPort
package main
import (
"fmt"
"net"
"strings"
)
func splitLastColumn(host string) (string, string, string) {
idx := strings.Index(host, "/")
col_idx := strings.LastIndex(host[:idx], ":")
return host[:col_idx], host[col_idx+1 : idx], host[idx:]
}
func main() {
ipstr := "aaa:abbb:cccc:dddd:0000:0000:00aa:bbbb:8080/static/silly.html"
host, port, path := splitLastColumn(ipstr)
ip := net.ParseIP(host)
if ip == nil {
fmt.Println("invalid addr ")
}
fmt.Println(ip, host, port, path)
fmt.Println(net.JoinHostPort(host, port))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论