Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

behaviour: fix downloading assets with redirects #98

Merged
merged 3 commits into from
May 28, 2020

Conversation

aoldershaw
Copy link
Contributor

This PR addresses two issues around downloading assets that provide redirect URLs:

  1. If an access token was provided, it should be used to download from a redirect URL (previously, these requests were made unauthenticated, which has a much lower rate limit)
  2. Fixes Release asset download fails when under redirect case #92 - set the Accept header to application/octet-stream as documented here

After doing more thorough acceptance on #97, I realized that the HTTP
request was not using the OAuth HTTP client if the access token was
provided (i.e. if an access token is provided, the initial request will
use it, but if the initial response is a redirect, the subsequent
request will be unauthenticated).

Signed-off-by: Aidan Oldershaw <aoldershaw@pivotal.io>
fixes #92

Previously, we were just downloading the API response to a file since we
did not specify the `Accept` header (as documented here:
https://developer.github.com/v3/repos/releases/#get-a-single-release-asset).

Signed-off-by: Aidan Oldershaw <aoldershaw@pivotal.io>
When trying to download release assets that redirected to e.g. AWS S3,
my prior commits would cause an error. Since we were using the OAuth
client Transport as an `http.RoundTripper`, we were always adding the
`Authorization` header (overwriting Go's default behaviour of stripping
the Authorization header on redirects).

To avoid this, just use a vanilla HTTP client and pass the access token
manually. If the initial redirect URL is external to github, check the
Host explicitly to determine whether to add the access token. On
subsequent redirects, this is taken care of by Go's http client
stripping the Authorization header.

I did consider creating a custom `http.RoundTripper` to strip the
Authorization header for any request outside of github.com, but that
felt like overkill here.

Signed-off-by: Aidan Oldershaw <aoldershaw@pivotal.io>
@jomsie
Copy link

jomsie commented May 28, 2020

Screen Shot 2020-05-28 at 3 12 24 PM

it appears as though it works! :)

@aoldershaw
Copy link
Contributor Author

@jomsie no rush, but do you have further reviewing to do, or is this good to be merged?

Copy link

@jomsie jomsie left a comment

Choose a reason for hiding this comment

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

this all looks good!

@jomsie
Copy link

jomsie commented May 28, 2020

@jomsie no rush, but do you have further reviewing to do, or is this good to be merged?

I was just finishing up reviewing the code :D

@aoldershaw
Copy link
Contributor Author

@jomsie whoops, forgive my impatience!

@aoldershaw aoldershaw merged commit af189e0 into master May 28, 2020
@aoldershaw aoldershaw deleted the fix-download-asset branch May 28, 2020 20:19
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.

Release asset download fails when under redirect case
2 participants