1 回答

TA貢獻1765條經驗 獲得超5個贊
你不能。標準庫的 HTTP 服務器不為這種情況提供任何攔截點或回調。
在調用處理程序之前,無效請求將被“終止”。server.go您可以在,方法中看到這conn.serve()一點:
? ? w, err := c.readRequest(ctx)
? ? // ...
? ? if err != nil {
? ? ? ? switch {
? ? ? ? // ...
? ? ? ? default:
? ? ? ? ? ? publicErr := "400 Bad Request"
? ? ? ? ? ? if v, ok := err.(badRequestError); ok {
? ? ? ? ? ? ? ? publicErr = publicErr + ": " + string(v)
? ? ? ? ? ? }
? ? ? ? ? ? fmt.Fprintf(c.rwc, "HTTP/1.1 "+publicErr+errorHeaders+publicErr)
? ? ? ? ? ? return
? ? ? ? }
? ? }
? ? // ...
? ? serverHandler{c.server}.ServeHTTP(w, w.req)
你不能。標準庫的 HTTP 服務器不為這種情況提供任何攔截點或回調。
在調用處理程序之前,無效請求將被“終止”。server.go您可以在,方法中看到這conn.serve()一點:
? ? w, err := c.readRequest(ctx)
? ? // ...
? ? if err != nil {
? ? ? ? switch {
? ? ? ? // ...
? ? ? ? default:
? ? ? ? ? ? publicErr := "400 Bad Request"
? ? ? ? ? ? if v, ok := err.(badRequestError); ok {
? ? ? ? ? ? ? ? publicErr = publicErr + ": " + string(v)
? ? ? ? ? ? }
? ? ? ? ? ? fmt.Fprintf(c.rwc, "HTTP/1.1 "+publicErr+errorHeaders+publicErr)
? ? ? ? ? ? return
? ? ? ? }
? ? }
? ? // ...
? ? serverHandler{c.server}.ServeHTTP(w, w.req)
Go 的 HTTP 服務器為您提供了一個實現來處理來自使用/遵守HTTP 協議的客戶端的傳入請求。所有瀏覽器和著名的客戶端都遵循 HTTP 協議。提供完全可定制的服務器并不是實現的目標。
- 1 回答
- 0 關注
- 181 瀏覽
添加回答
舉報