Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

redirect not working? #8

Closed
Zambiorix opened this issue Aug 25, 2017 · 6 comments
Closed

redirect not working? #8

Zambiorix opened this issue Aug 25, 2017 · 6 comments

Comments

@Zambiorix
Copy link

Hi,

Am I correct that aws-lambda-go-net is not passing on a redirect properly to the client?

I think it gets stuck in this component and doing a redirect locally ...

Cheers
Gerd

lion3ls added a commit that referenced this issue Aug 25, 2017
The default Go http client used in the lib follows redirects by default.
The consequences being any redirect returned by the end user Lambda is
first fully resolved by Go before returned to API Gateway.
This patch resolves the issue by disabling this default behaviour.

CAT: #fix
REF: #8
THX: @Zambiorix
@lion3ls
Copy link
Member

lion3ls commented Aug 25, 2017

@Zambiorix update your package, everything should work as expected now 😉

@lion3ls lion3ls closed this as completed Aug 25, 2017
@Zambiorix
Copy link
Author

:-) great, I'll test it immediately!

Another thing, I believe there is a still a bug in the code:

You should always close a response body if a response is present.
Now you have 2 returns leaving the body open (and the socket unavailable for reuse)

  1. I am aware that even if you have an err on newClient().Do, there are cases you still get a reponse body.
  2. you should immediately close the body after a ioutil.ReadAll(res.Body)

res, err := newClient().Do(req)
if err != nil {
	log.Println(err)
	return
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
	log.Println(err)
	return
}
res.Body.Close()


@Zambiorix
Copy link
Author

It's working :-)

I use redirects to test if cookies are allowed by client, and it now works as expected

Thanks!

@Zambiorix
Copy link
Author

I would solve the response body closure like this


res, err := newClient().Do(req)
if res != nil {
	defer res.Body.Close()
}
if err != nil {
	log.Println(err)
	return
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
	log.Println(err)
	return
}


lion3ls added a commit that referenced this issue Aug 25, 2017
Value returned by http client Do function is nil or already closed if
there is any error. See
https://golang.org/src/net/http/client.go?s=16518:16570#L480
Therefore in order to avoid memory leaks in a potential ioutil.ReadAll
error and to be Go idioms compliant, the response body is deferred to be
closed immediately after checking there is no error with the Do function.

CAT: #fix
REF: #8
THX: @Zambiorix
@lion3ls
Copy link
Member

lion3ls commented Aug 25, 2017

@Zambiorix you're right in the fact that the body should be deferred closed before ReadAll but for the Do function, the general idiom is to defer after checking for errors. See also the commit message. And thank you very much for having reported this problem.

@Zambiorix
Copy link
Author

great, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants