2021-08-16 18:30:13 +08:00
|
|
|
/**
|
2022-10-31 17:22:49 +08:00
|
|
|
* @Author: Echo
|
|
|
|
* @Email: 1711788888@qq.com
|
2021-08-16 18:30:13 +08:00
|
|
|
* @Date: 2021/8/16 9:47 上午
|
|
|
|
* @Desc: request's middleware
|
|
|
|
*/
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
type MiddlewareFunc = func(r *Request) (*Response, error)
|
|
|
|
|
|
|
|
const middlewareKey = "__httpClientMiddlewareKey"
|
|
|
|
|
|
|
|
type middleware struct {
|
|
|
|
err error
|
|
|
|
req *Request
|
|
|
|
resp *Response
|
|
|
|
index int
|
|
|
|
handlers []MiddlewareFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next exec the next middleware.
|
|
|
|
func (m *middleware) Next() (*Response, error) {
|
|
|
|
if m.index < len(m.handlers) {
|
|
|
|
m.index++
|
|
|
|
if m.resp, m.err = m.handlers[m.index](m.req); m.err != nil {
|
|
|
|
return m.resp, m.err
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-16 18:30:13 +08:00
|
|
|
return m.resp, m.err
|
|
|
|
}
|