1 回答

TA贡献1839条经验 获得超15个赞
有几种选择。第一种是返回错误响应:
func handle(w http.ResponseWriter, r *http.Request) {
if r.UserAgent() == "test/1.0" {
//Allow
} else {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
}
第二种是从服务器劫持连接并粗鲁地关闭连接:
func handle(w http.ResponseWriter, r *http.Request) {
if r.UserAgent() == "test/1.0" {
//Allow
} else {
if h, ok := w.(http.Hijacker); ok {
c, _, err := h.Hijack()
if err != nil {
c.Close()
return
}
}
// fallback to error response
http.Error(w, "forbidden", http.StatusForbidden)
return
}
}
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报