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 關注
- 91 瀏覽
添加回答
舉報