Rhy*_*man 5 audio shoutcast go
Shoutcast的服务器基本上讲HTTP,有一个重要的区别:他们响应GET请求以ICY 200 OK代替HTTP/1.1 200 OK。
Go 不会有它,并且正确失败并显示错误malformed HTTP version "ICY"。
但是,我想让事情发挥作用,并且想知道最好的方法是什么。到目前为止我的想法:
ICY为HTTP/1.1飞行中http.ParseHTTPVersion(但 golang 没有函数重载)ParseHTTPVersion数字 1. 似乎最有吸引力,但我不知道如何尊重 http“范围”并实际修改给定 http 版本上的所有响应。这种事情http.Transport.Proxy能搞定吗?
任何人都可以给我任何指示吗?
我通过创建一个返回包装连接的自定义 Dial 函数来实现此目的。我的包装器拦截连接上的第一次读取,并用 HTTP/1.1 替换 ICY。不是超级强大,但证明了这个概念:
package main
import (
"fmt"
"net"
"net/http"
)
type IcyConnWrapper struct {
net.Conn
haveReadAny bool
}
func (i *IcyConnWrapper) Read(b []byte) (int, error) {
if i.haveReadAny {
return i.Conn.Read(b)
}
i.haveReadAny = true
//bounds checking ommitted. There are a few ways this can go wrong.
//always check array sizes and returned n.
n, err := i.Conn.Read(b[:3])
if err != nil {
return n, err
}
if string(b[:3]) == "ICY" {
//write Correct http response into buffer
copy(b, []byte("HTTP/1.1"))
return 8, nil
}
return n, nil
}
func main() {
tr := &http.Transport{
Dial: func(network, a string) (net.Conn, error) {
realConn, err := net.Dial(network, a)
if err != nil {
return nil, err
}
return &IcyConnWrapper{Conn: realConn}, nil
},
}
client := &http.Client{Transport: tr}
http.DefaultClient = client
resp, err := http.Get("http://178.33.230.189:8100") //random url I found on the internet
fmt.Println(err)
fmt.Println(resp.StatusCode)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
854 次 |
| 最近记录: |