add no auto redirect config

master v0.1.5
兔子 2 years ago
parent d71eacdc91
commit 93b756d9fb
Signed by: b612
GPG Key ID: 481225A74DEB62A1

@ -39,6 +39,7 @@ type Request struct {
RespHeader http.Header RespHeader http.Header
RespCookies []*http.Cookie RespCookies []*http.Cookie
RespHttpCode int RespHttpCode int
Location *url.URL
CircleBuffer *stario.StarBuffer CircleBuffer *stario.StarBuffer
respReader io.ReadCloser respReader io.ReadCloser
RequestOpts RequestOpts
@ -57,6 +58,7 @@ type RequestOpts struct {
SkipTLSVerify bool SkipTLSVerify bool
CustomTransport *http.Transport CustomTransport *http.Transport
Queries map[string]string Queries map[string]string
DisableRedirect bool
} }
type RequestOpt func(opt *RequestOpts) type RequestOpt func(opt *RequestOpts)
@ -167,6 +169,12 @@ func WithSkipTLSVerify(skip bool) RequestOpt {
} }
} }
func WithDisableRedirect(disable bool) RequestOpt {
return func(opt *RequestOpts) {
opt.DisableRedirect = disable
}
}
func NewRequests(url string, rawdata []byte, method string, opts ...RequestOpt) Request { func NewRequests(url string, rawdata []byte, method string, opts ...RequestOpt) Request {
req := Request{ req := Request{
RequestOpts: RequestOpts{ RequestOpts: RequestOpts{
@ -292,6 +300,7 @@ func Curl(curl Request) (resps Request, err error) {
if err != nil { if err != nil {
return Request{}, err return Request{}, err
} }
curl.Location, _ = resp.Location()
curl.RespHttpCode = resp.StatusCode curl.RespHttpCode = resp.StatusCode
curl.RespHeader = resp.Header curl.RespHeader = resp.Header
curl.RespCookies = resp.Cookies() curl.RespCookies = resp.Cookies()
@ -387,6 +396,11 @@ func netcurl(curl Request) (*http.Response, error) {
client := &http.Client{ client := &http.Client{
Transport: curl.CustomTransport, Transport: curl.CustomTransport,
} }
if curl.DisableRedirect {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
resp, err := client.Do(req) resp, err := client.Do(req)
return resp, err return resp, err
} }

Loading…
Cancel
Save