Skip to content

lfsapi: teach (*Client) Do() how to handle 307 redirections#1791

Merged
technoweenie merged 3 commits intoapi-masterfrom
api/redirections
Dec 21, 2016
Merged

lfsapi: teach (*Client) Do() how to handle 307 redirections#1791
technoweenie merged 3 commits intoapi-masterfrom
api/redirections

Conversation

@technoweenie
Copy link
Contributor

No description provided.

@ttaylorr ttaylorr mentioned this pull request Dec 20, 2016
17 tasks
Copy link
Contributor

@ttaylorr ttaylorr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a few comments to consider 😄

}

func (c *Client) doWithRedirects(cli *http.Client, req *http.Request, via []*http.Request) (*http.Response, error) {
if seeker, ok := req.Body.(io.Seeker); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an error if len(via) > 0 && !ok?

Copy link
Contributor Author

@technoweenie technoweenie Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, but not in this PR. My experiments with re-introducing the http trace code gave me a better way to deal with it.

return res, err
}

if res.StatusCode != 307 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of our roadmap items is to respond to 301s as well. Do you think we could add an || res.StatusCode == 301, and maintain a cache of permanent redirections? I'm thinking something like a map[*url.URL]*url.URL map of from -> to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but not as part of this refactoring effort.

lfsapi/client.go Outdated
redirectedReq.ContentLength = req.ContentLength

if err = checkRedirect(redirectedReq, via); err != nil {
return res, errors.Wrapf(err, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does errors.Wrapf(err, err.Error()) do? I think this would just turn an error "foo" into "foo: foo".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is a relic of the older error wrapping, which was totally different.

lfsapi/client.go Outdated
}

func checkRedirect(req *http.Request, via []*http.Request) error {
if len(via) >= 3 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should make this a configurable value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, but not in this refactoring PR.

lfsapi/client.go Outdated
return errors.New("stopped after 3 redirects")
}

oldest := via[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line will cause a panic if len(via) == 0.

lfsapi/client.go Outdated
continue
}
}
req.Header.Set(key, oldest.Header.Get(key))
Copy link
Contributor

@ttaylorr ttaylorr Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this behavior fits in a check<T>() function. This seems side-effect-y, so I think we should either document it's behavior, or extract it out to another function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. Need more tests first.

Test string
}

func TestClientRedirect(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very comprehensive 👍

Copy link
Contributor

@ttaylorr ttaylorr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I think I misunderstood the intent of this pull-request. From your comments, I think this is just moving some code over from the httputil package, so I think this is fine to go ahead with for now.

@technoweenie technoweenie merged commit 4f9af9e into api-master Dec 21, 2016
@technoweenie technoweenie deleted the api/redirections branch December 21, 2016 17:32
@technoweenie
Copy link
Contributor Author

I did clean up the redundant error wrapping, and removed checkRedirect().

@ttaylorr
Copy link
Contributor

I did clean up the redundant error wrapping, and removed checkRedirect().

Thanks so much! ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants