2021-07-22 18:26:29 +08:00
|
|
|
|
package http_proxy
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2021-07-29 10:45:00 +08:00
|
|
|
|
"compress/gzip"
|
2021-07-22 18:26:29 +08:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
"net/http"
|
2021-07-29 10:45:00 +08:00
|
|
|
|
"strings"
|
2021-07-22 18:26:29 +08:00
|
|
|
|
|
2021-08-14 00:17:22 +08:00
|
|
|
|
"github.com/eolinker/goku/node/http-proxy/backend"
|
2021-07-22 18:26:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
|
|
body []byte
|
|
|
|
|
|
resp *http.Response
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Body 响应体
|
|
|
|
|
|
func (r *response) Body() []byte {
|
|
|
|
|
|
return r.body
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//StatusCode 状态码
|
|
|
|
|
|
func (r *response) StatusCode() int {
|
|
|
|
|
|
return r.resp.StatusCode
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Header 响应头部
|
|
|
|
|
|
func (r *response) Header() http.Header {
|
|
|
|
|
|
return r.resp.Header
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Proto 协议
|
|
|
|
|
|
func (r *response) Proto() string {
|
|
|
|
|
|
return r.resp.Proto
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-29 10:45:00 +08:00
|
|
|
|
//Status 状态
|
|
|
|
|
|
func (r *response) Status() string {
|
|
|
|
|
|
return r.resp.Status
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-22 18:26:29 +08:00
|
|
|
|
//NewResponse 新建响应,返回IResponse节点
|
|
|
|
|
|
func NewResponse(resp *http.Response) (backend.IResponse, error) {
|
2021-07-29 10:45:00 +08:00
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
bd := resp.Body
|
|
|
|
|
|
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
|
|
|
|
|
|
bd, _ = gzip.NewReader(resp.Body)
|
|
|
|
|
|
}
|
|
|
|
|
|
body, err := ioutil.ReadAll(bd)
|
2021-07-22 18:26:29 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2021-07-29 10:45:00 +08:00
|
|
|
|
|
2021-07-22 18:26:29 +08:00
|
|
|
|
r := &response{
|
|
|
|
|
|
body: body,
|
|
|
|
|
|
resp: resp,
|
|
|
|
|
|
}
|
|
|
|
|
|
return r, nil
|
|
|
|
|
|
}
|