mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-20 19:44:04 +02:00
feat(middleware): optimize HTML modification with byte pool
This commit is contained in:
parent
f39513483b
commit
46c7ee4d84
1 changed files with 6 additions and 3 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/rs/zerolog/log"
|
||||
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
||||
"github.com/yusing/go-proxy/internal/utils/synk"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
|
@ -20,7 +21,9 @@ type modifyHTML struct {
|
|||
|
||||
var ModifyHTML = NewMiddleware[modifyHTML]()
|
||||
|
||||
func (m *modifyHTML) before(w http.ResponseWriter, req *http.Request) bool {
|
||||
var bytePool = synk.NewBytesPool()
|
||||
|
||||
func (m *modifyHTML) before(_ http.ResponseWriter, req *http.Request) bool {
|
||||
req.Header.Set("Accept-Encoding", "")
|
||||
return true
|
||||
}
|
||||
|
@ -75,7 +78,7 @@ func (m *modifyHTML) modifyResponse(resp *http.Response) error {
|
|||
|
||||
// copied and modified from (*goquery.Selection).Html()
|
||||
func buildHTML(s *goquery.Document) (ret []byte, err error) {
|
||||
var buf bytes.Buffer
|
||||
buf := bytes.NewBuffer(bytePool.Get())
|
||||
|
||||
// Merge all head nodes into one
|
||||
headNodes := s.Find("head")
|
||||
|
@ -97,7 +100,7 @@ func buildHTML(s *goquery.Document) (ret []byte, err error) {
|
|||
|
||||
if len(s.Nodes) > 0 {
|
||||
for c := s.Nodes[0].FirstChild; c != nil; c = c.NextSibling {
|
||||
err = html.Render(&buf, c)
|
||||
err = html.Render(buf, c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue