访问net / http响应的底层套接字 [英] Accessing the underlying socket of a net/http response
问题描述
我正在尝试编写一个自定义处理程序来为文件提供净/ HTTP
。
我不能使用默认的 http.FileServer()
处理程序,因为我需要访问底层套接字(内部 net> .Conn
),所以我可以对它执行一些特定于平台的syscall调用(主要是 TCP_INFO
)。
更精确:我需要访问处理函数中的 http.ResponseWriter
的底层套接字:
func myHandler(w http.ResponseWriter,r * http.Request){
...
//我需要net.Conn of w
...
}
用于
http.HandleFunc(/,myHandler)
有没有办法解决这个问题。我查看了 websocket.Upgrade
是如何实现的,但它使用了 Hijack()
,这是'太多',因为我必须通过我获得的原始tcp套接字来编写'说出http'。我只想引用套接字而不是完全接管。注意,尽管在当前的实现中 http.ResponseWriter
* http.response
(注意小写字母!字段是未导出的,您无法访问它。
请转到 Server.ConnState
hook:你可以注册一个函数,当连接状态改变时将被调用,参见 http.ConnState
了解详情。例如,您甚至可以在 net.Conn
之前请求进入处理程序( http.StateNew
和 http.StateActive
states)。
您可以通过像这样创建自定义的 Server
来安装连接状态侦听器:
<$ p $
func main(){
http.HandleFunc(/,myHandler)
s:=& http.Server {
Addr: :8081,
ReadTimeout:10 * time.Second,
WriteTimeout:10 * time.Second,
MaxHeaderBytes:1<< 20,
ConnState:ConnStateListener,
}
panic(s.ListenAndServe())
}
func ConnStateListener(c net.Conn,cs http .ConnState){
fmt.Printf(CONN STATE:%v,%v \ n,cs,c)
}
即使在调用处理程序之前(以及在调用处理程序之前和之后),这种方式也会具有所需的 net.Conn
。缺点是它与 ResponseWriter
没有配对,如果你需要,你必须手动完成。
I'm new to Go and evaluating it for a project.
I'm trying to write a custom handler to serve files with net/http
.
I can't use the default http.FileServer()
handler because I need to have access to the underlying socket (the internal net.Conn
) so I can perform some informational platform specific "syscall" calls on it (mainly TCP_INFO
).
More precisly: I need to access the underlying socket of the http.ResponseWriter
in the handler function:
func myHandler(w http.ResponseWriter, r *http.Request) {
...
// I need the net.Conn of w
...
}
used in
http.HandleFunc("/", myHandler)
Is there a way to this. I looked at how websocket.Upgrade
does this but it uses Hijack()
which is 'too much' because then I have to code 'speaking http' over the raw tcp socket I get. I just want a reference to the socket and not taking over completely.
Note that although in current implementation http.ResponseWriter
is a *http.response
(note the lowercase!) which holds the connection, the field is unexported and you can't access it.
Instead take a look at the Server.ConnState
hook: you can "register" a function which will be called when the connection state changes, see http.ConnState
for details. For example you will get the net.Conn
even before the request enters the handler (http.StateNew
and http.StateActive
states).
You can install a connection state listener by creating a custom Server
like this:
func main() {
http.HandleFunc("/", myHandler)
s := &http.Server{
Addr: ":8081",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
ConnState: ConnStateListener,
}
panic(s.ListenAndServe())
}
func ConnStateListener(c net.Conn, cs http.ConnState) {
fmt.Printf("CONN STATE: %v, %v\n", cs, c)
}
This way you will have exactly the desired net.Conn
even before (and also during and after) invoking the handler. The downside is that it is not "paired" with the ResponseWriter
, you have to do that manually if you need that.
这篇关于访问net / http响应的底层套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!