Making headless httpclient more similar to real browsers

dev
mzack 2021-10-20 13:26:47 +02:00
parent 8e8249e6a5
commit e6728e8ff9
2 changed files with 17 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package engine
import ( import (
"crypto/tls" "crypto/tls"
"net/http" "net/http"
"net/http/cookiejar"
"net/url" "net/url"
"time" "time"
@ -30,5 +31,17 @@ func newhttpClient(options *types.Options) *http.Client {
} }
} }
return &http.Client{Transport: transport, Timeout: time.Duration(options.Timeout*3) * time.Second} jar, _ := cookiejar.New(nil)
httpclient := &http.Client{
Transport: transport,
Timeout: time.Duration(options.Timeout*3) * time.Second,
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// the browser should follow redirects not us
return http.ErrUseLastResponse
},
}
return httpclient
} }

View File

@ -8,6 +8,9 @@ import (
// routingRuleHandler handles proxy rule for actions related to request/response modification // routingRuleHandler handles proxy rule for actions related to request/response modification
func (p *Page) routingRuleHandler(ctx *rod.Hijack) { func (p *Page) routingRuleHandler(ctx *rod.Hijack) {
// usually browsers don't use chunked transfer encoding so we set the content-length nevertheless
ctx.Request.Req().ContentLength = int64(len(ctx.Request.Body()))
for _, rule := range p.rules { for _, rule := range p.rules {
if rule.Part != "request" { if rule.Part != "request" {
continue continue