增加基础响应数据重写

This commit is contained in:
白茶清欢 2021-08-06 20:49:42 +08:00
parent 1f3456a7e1
commit d9e202001b

View File

@ -8,6 +8,7 @@
package proxy package proxy
import ( import (
"bytes"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
@ -15,8 +16,6 @@ import (
"net" "net"
"net/http" "net/http"
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/gopkg/safe"
) )
// Forward 正向代理的实现 // Forward 正向代理的实现
@ -61,6 +60,9 @@ func Forward(rw http.ResponseWriter, req *http.Request, serverConfig *Server) {
// step 3 // step 3
for key, value := range res.Header { for key, value := range res.Header {
for _, v := range value { for _, v := range value {
if strings.ToLower(key) == "content-encoding" {
continue
}
rw.Header().Add(key, v) rw.Header().Add(key, v)
} }
} }
@ -81,9 +83,9 @@ func Forward(rw http.ResponseWriter, req *http.Request, serverConfig *Server) {
gzipData, err = gzip.NewReader(res.Body) gzipData, err = gzip.NewReader(res.Body)
responseData, err = ioutil.ReadAll(gzipData) responseData, err = ioutil.ReadAll(gzipData)
fmt.Println(string(responseData), err) fmt.Println(string(responseData), err)
formatData, _ := safe.Filter(responseData, serverConfig.RewriteResponseData)
rw.Write(formatData)
}
bytesBuffer := bytes.NewReader([]byte(`{"data":{"permission":true}}`))
io.Copy(rw, bytesBuffer)
}
res.Body.Close() res.Body.Close()
} }