2.2.2goweb内置的 HTTP 处理程序2
http.StripPrefix
 
http.StripPrefix 是 Go 语言 net/http 包中的一个函数,它的主要作用是创建一个新的 HTTP 处理程序。这个新处理程序会在处理请求之前,从请求的 URL 路径中移除指定的前缀,然后将处理工作委托给另一个提供的处理程序。
使用示例
package mainimport ("fmt""net/http"
)// 定义一个简单的处理函数
func simpleHandler(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "Path after stripping prefix: %s", r.URL.Path)
}func main() {// 创建一个处理程序,移除 "/static/" 前缀后将请求交给 simpleHandler 处理http.Handle("/static/", http.StripPrefix("/static/", http.HandlerFunc(simpleHandler)))// 启动 HTTP 服务器,监听 8080 端口fmt.Println("Starting server on :8080")http.ListenAndServe(":8080", nil)
}
在这个例子中,如果客户端请求 /static/css/style.css,http.StripPrefix 会移除 /static/ 前缀,将 /css/style.css 作为路径传递给 simpleHandler 处理。
源码分析
http.StripPrefix 函数的定义如下:
// StripPrefix returns a handler that serves HTTP requests
// by removing the given prefix from the request URL's Path
// and invoking the handler h. StripPrefix handles a
// request for a path that doesn't begin with prefix by
// replying with an HTTP 404 not found error.
func StripPrefix(prefix string, h Handler) Handler {if prefix == "" {return h}return HandlerFunc(func(w ResponseWriter, r *Request) {if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {r2 := new(Request)*r2 = *rr2.URL = new(url.URL)*r2.URL = *r.URLr2.URL.Path = ph.ServeHTTP(w, r2)} else {http.NotFound(w, r)}})
}
源码详细解释
-  参数检查: if prefix == "" {return h }如果传入的前缀 prefix为空字符串,那么直接返回原处理程序h,因为不需要移除任何前缀。
-  创建新的处理程序: return HandlerFunc(func(w ResponseWriter, r *Request) {// ... })使用 HandlerFunc类型创建一个新的处理程序。HandlerFunc是一个函数类型,它实现了http.Handler接口的ServeHTTP方法。
-  移除前缀并处理请求: if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {r2 := new(Request)*r2 = *rr2.URL = new(url.URL)*r2.URL = *r.URLr2.URL.Path = ph.ServeHTTP(w, r2) }- 使用 strings.TrimPrefix函数尝试从请求的 URL 路径中移除指定的前缀。
- 如果移除成功(即移除后的路径长度小于原路径长度),则创建一个新的 http.Request对象r2,并复制原请求的所有信息。
- 修改新请求对象 r2的 URL 路径为移除前缀后的路径。
- 调用原处理程序 h的ServeHTTP方法,将新请求对象r2传递给它进行处理。
 
- 使用 
-  处理未匹配的请求: } else {http.NotFound(w, r) }如果请求的 URL 路径不包含指定的前缀,那么调用 http.NotFound函数返回一个 404 错误响应。
http.StripPrefix 是一个非常实用的工具,它允许你在处理请求之前对 URL 路径进行预处理,移除不必要的前缀。这在处理静态文件服务、API 路由等场景中非常有用。通过分析源码,我们可以看到它是如何创建新的请求对象、修改路径并将处理工作委托给原处理程序的,同时也处理了未匹配前缀的情况。
http.TimeoutHandler
http.TimeoutHandler 是 Go 语言 net/http 包中的一个函数,它用于为 HTTP 请求处理设置超时时间。当一个请求的处理时间超过预设的超时时间时,会返回一个超时响应给客户端,避免客户端长时间等待无响应的请求。
使用示例
package mainimport ("fmt""net/http""time"
)// 模拟一个耗时的处理函数
func slowHandler(w http.ResponseWriter, r *http.Request) {time.Sleep(5 * time.Second)fmt.Fprint(w, "Slow handler completed")
}func main() {// 设置超时时间为 2 秒timeoutHandler := http.TimeoutHandler(http.HandlerFunc(slowHandler), 2*time.Second, "Request timed out")// 注册处理程序http.Handle("/slow", timeoutHandler)// 启动 HTTP 服务器fmt.Println("Starting server on :8080")if err := http.ListenAndServe(":8080", nil); err != nil {fmt.Println("Error starting server:", err)}
}
在这个示例中,slowHandler 函数模拟了一个耗时 5 秒的处理过程,而 http.TimeoutHandler 设置的超时时间为 2 秒。当客户端请求 /slow 路径时,如果处理时间超过 2 秒,客户端将收到 "Request timed out" 的响应。
源码分析
// TimeoutHandler returns a Handler that runs h with the given time limit.
//
// The new Handler calls h.ServeHTTP to handle each request, but if a
// call runs for longer than its time limit, the handler responds with
// a 503 Service Unavailable error and the given message in its body.
// (If msg is empty, a suitable default message will be sent.)
// After such a timeout, writes by h to its ResponseWriter will return
// ErrHandlerTimeout.
//
// TimeoutHandler buffers all Handler writes to memory and does not
// support the Hijacker or Flusher interfaces.
func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler {return &timeoutHandler{handler: h,timeout: dt,msg:     msg,}
}
TimeoutHandler 函数接收三个参数:
- h:一个- http.Handler类型的处理程序,代表实际要执行的请求处理逻辑。
- dt:一个- time.Duration类型的超时时间,指定了处理请求的最大允许时间。
- msg:一个字符串类型的超时消息,当请求处理超时时,会将该消息作为响应体返回给客户端。
timeoutHandler 结构体
 
type timeoutHandler struct {handler Handlertimeout time.Durationmsg     string
}
timeoutHandler 结构体包含三个字段,分别存储传入的处理程序、超时时间和超时消息。
ServeHTTP 方法实现
 
func (th *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {done := make(chan struct{})tw := &timeoutWriter{w:   w,h:   make(http.Header),msg: th.msg,}go func() {th.handler.ServeHTTP(tw, r)close(done)}()select {case <-done:tw.mu.Lock()defer tw.mu.Unlock()copyHeader(w.Header(), tw.h)w.WriteHeader(tw.code)w.Write(tw.wbuf.Bytes())case <-time.After(th.timeout):tw.mu.Lock()defer tw.mu.Unlock()if !tw.wroteHeader {http.Error(w, tw.errorMessage(), http.StatusServiceUnavailable)tw.timedOut = true}}
}
ServeHTTP 方法是 timeoutHandler 结构体实现 http.Handler 接口的方法,其执行流程如下:
-  创建通道和包装响应写入器: - 创建一个 done通道,用于通知请求处理是否完成。
- 创建一个 timeoutWriter结构体实例tw,用于包装原始的http.ResponseWriter,并记录响应信息。
 
- 创建一个 
-  启动 goroutine 处理请求: - 启动一个新的 goroutine 来执行实际的请求处理逻辑 th.handler.ServeHTTP(tw, r)。
- 当处理完成后,关闭 done通道。
 
- 启动一个新的 goroutine 来执行实际的请求处理逻辑 
-  使用 select语句等待结果:- 如果 done通道接收到信号,说明请求处理在超时时间内完成。此时,将tw中记录的响应头、状态码和响应体复制到原始的http.ResponseWriter中并发送给客户端。
- 如果 time.After(th.timeout)通道接收到信号,说明请求处理超时。此时,检查是否已经写入响应头,如果没有,则使用http.Error函数返回一个 503 状态码和超时消息给客户端,并标记tw.timedOut为true。
 
- 如果 
timeoutWriter 结构体
 
type timeoutWriter struct {w    http.ResponseWriterh    http.Headerwbuf bytes.Buffercode intwroteHeader booltimedOut    boolmu          sync.Mutexmsg         string
}
timeoutWriter 结构体用于包装原始的 http.ResponseWriter,并记录响应头、状态码、响应体等信息。同时,它使用互斥锁 mu 来保证并发安全。
http.TimeoutHandler 是一个非常实用的工具,它可以帮助我们避免长时间无响应的请求阻塞服务器资源。通过使用 goroutine 和通道,结合 select 语句进行超时控制,实现了对请求处理时间的有效管理。需要注意的是,TimeoutHandler 会将处理程序的所有写入操作缓冲到内存中,并且不支持 Hijacker 和 Flusher 接口。
