Skip to content

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

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

technoweenie merged 3 commits into
api-masterfrom
api/redirections

Conversation

@technoweenie

Copy link
Copy Markdown
Contributor

No description provided.

@ttaylorr ttaylorr mentioned this pull request Dec 20, 2016
17 tasks done

@ttaylorr ttaylorr left a comment

Copy link
Copy Markdown
Contributor

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 😄

Comment thread lfsapi/client.go
}

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
Copy Markdown
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?

@technoweenie technoweenie Dec 21, 2016 •

Copy link
Copy Markdown
Contributor Author

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.

Comment thread lfsapi/client.go
return res, err
}

if res.StatusCode != 307 {

Copy link
Copy Markdown
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
Copy Markdown
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.

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

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

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread lfsapi/client.go Outdated
}

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

Copy link
Copy Markdown
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
Copy Markdown
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.

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

oldest := via[0]

Copy link
Copy Markdown
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.

Comment thread lfsapi/client.go Outdated
continue
}
}
req.Header.Set(key, oldest.Header.Get(key))

@ttaylorr ttaylorr Dec 21, 2016 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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.

Comment thread lfsapi/client_test.go
Test string
}

func TestClientRedirect(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very comprehensive 👍

@ttaylorr ttaylorr left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

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

@ttaylorr

Copy link
Copy Markdown
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