azuretls-client icon indicating copy to clipboard operation
azuretls-client copied to clipboard

Questions: Redirects

Open john103 opened this issue 5 months ago • 3 comments

Hello.

There is a hook: Session.CheckRedirect. I have tried assigning a function but it never triggered. The only way I managed to catch a redirect was with the help of CallbackWithContext function. There was a response body of { redirectURL: someUrl }. The redirect happens but Session.Dump doesn't register that.

1.) Could you provide an example of using Session.CheckRedirect function? Maybe I was using it wrong.

2.) Session.Dump doesn't register redirects, so no logs of those?

3.) Content-Length header is omitted in Session.Dump?

john103 avatar Jul 22 '25 13:07 john103

Hello,

For CheckRedirect function, you need to use it as follow

session.CheckRedirect = func(req *azuretls.Request, via []*azuretls.Request) error {
	fmt.Println(req.Url)
        return nil
}

~~There is indeed a bug on the Dump function that needs to be fixed.~~

Could you please send me the part of the code that doesn't work ?

Noooste avatar Jul 27 '25 09:07 Noooste

I have tested with the following. It works.

	session := azuretls.NewSession()
	defer session.Close()

	session.CheckRedirect = func(req *azuretls.Request, via []*azuretls.Request) error {
		fmt.Printf("\n[Url]: %+v [IsRedirected]: %+v [StatusCode]: %+v\n", req.Url, req.IsRedirected, req.Response.StatusCode)
		return nil
	}

	session.CallbackWithContext = func(ctx *azuretls.Context) {
		fmt.Printf("\n[Url]: %+v [IsRedirected]: %+v [StatusCode]: %+v\n", ctx.Request.Url, ctx.Request.IsRedirected, ctx.Response.StatusCode)
		return
	}
	
	response, err := session.Get("https://twitter.com")
	
	if err != nil {
		panic(err)
	}
	
	fmt.Printf("\n[x.com status code]: %+v [IsRedirected]: %+v\n", response.StatusCode, response.Request.IsRedirected)

I believe that page I am working with has no redirect. Page's javascript does the thing after receiving the response from fetch. CallBackWithContext reports StatusCode 200 also and nothing else happens.

But while testing I have noticed two moments:

CallBackWithContext: [Url]: https://twitter.com [IsRedirected]: false [StatusCode]: 302

CheckRedirect:       [Url]: https://x.com/ [IsRedirected]: false [StatusCode]: 302

CallBackWithContext: [Url]: https://x.com/ [IsRedirected]: false [StatusCode]: 200

No hooks:            [x.com status code]: 200 [IsRedirected]: false

1.) IsRedirected property always false. 2.) CheckRedirect reports url it is about to redirect? While CallBackWithContext reveals the original. Not sure which part is intentional though.

john103 avatar Jul 27 '25 20:07 john103

  1. IsRedirected is indeed an unused property of azuretls.Session, don't rely on it.
  2. Yes, CheckRedirect is indeed used to modify redirected requests only. To abort and return directly the response, you can return the error azuretls.ErrUseLastResponse in CheckRedirect function:
session := azuretls.NewSession()
defer session.Close()

var called bool

session.CheckRedirect = func(req *azuretls.Request, via []*azuretls.Request) error {
	if req.Response == nil {
		t.Error("expected non-nil Request.Response")
	}

	called = true
	return azuretls.ErrUseLastResponse
}

response, err := session.Get("https://httpbin.org/redirect/1")

// the error will be nil: azuretls.ErrUseLastResponse is considered as a non real error
if err != nil {
	panic(err)
}

if response.StatusCode != 302 {
	panic("Request failed, expected: 302, got: ", response.StatusCode)
}

if !called {
	panic("Request failed, CheckRedirect was not called")
}

CallBackWithContext method is called after every response being received by the client.

Noooste avatar Jul 27 '25 22:07 Noooste