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

Closes #1976, HttpUrlConnection now allowed to follow redirects #2027

Merged
merged 3 commits into from
Jan 9, 2018

Conversation

grubdragon
Copy link
Contributor

@pocmo pointed out that instead of accepting a redirect as successful, we could follow redirects.

@mcomella
Copy link
Contributor

Hey @grubdragon – we're on vacation for the holidays so at worst, we'll get to this when we come back around 1/5.

btw, from a glance, the code looks good but I'd like to test it, which is why I can't just land it now.

@grubdragon
Copy link
Contributor Author

Yeah cool! I'll try to fix some other bugs till then! 😄

@mcomella mcomella self-requested a review January 5, 2018 17:44
@mcomella mcomella added the 🕵️‍♀️ needs review PRs that need to be reviewed label Jan 5, 2018
@mcomella
Copy link
Contributor

mcomella commented Jan 5, 2018

Yeah cool! I'll try to fix some other bugs till then!

@grubdragon Have you found enough bugs or do you want me to find something for you?

I'll get around to reviewing this soon.

@mcomella mcomella removed the 🕵️‍♀️ needs review PRs that need to be reviewed label Jan 5, 2018
Copy link
Contributor

@mcomella mcomella left a comment

Choose a reason for hiding this comment

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

Turns out this was happening by default (as mentioned in the docs) but it's good to be explicit!

I tested this with redirection on these redirect pages: https://jigsaw.w3.org/HTTP/300/Overview.html

Looks good with the change I suggested!

@@ -219,6 +219,7 @@ private void showServerError(final ManualAddSearchEngineSettingsFragment that) {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) searchURL.openConnection();
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(SEARCH_QUERY_VALIDATION_TIMEOUT_MILLIS);
connection.setReadTimeout(SEARCH_QUERY_VALIDATION_TIMEOUT_MILLIS);
Copy link
Contributor

Choose a reason for hiding this comment

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

A few lines below this, we check against responseCode < 400 – we should check < 300 now because we're following redirects. If we get a 300 even after following redirects, something must be crazy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh right, covering all bases

@grubdragon
Copy link
Contributor Author

@mcomella Hey could you suggest me some bugs?

Copy link
Contributor

@mcomella mcomella 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 to me! Thanks for your help, grubdragon! :)

@@ -223,8 +223,8 @@ private void showServerError(final ManualAddSearchEngineSettingsFragment that) {
connection.setConnectTimeout(SEARCH_QUERY_VALIDATION_TIMEOUT_MILLIS);
connection.setReadTimeout(SEARCH_QUERY_VALIDATION_TIMEOUT_MILLIS);

// A non-error HTTP response is good enough as a sanity check, some search engines redirect to https.
return connection.getResponseCode() < 400;
// Now that redirects are followed, 300 is a better and stronger sanity check, checks for a non error and non redirect response
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: general coding tip: this comment references the old state of the code "now that redirects...", "...is a better..." but someone reading this code has no idea what the previous code looked like. Thus, comments should either:

  • Only reference the current code
  • Explain the previous state of the code and describe why the current code doesn't do that anymore.

In this case, a dev doesn't need to understand the old state to understand the code so I'd only reference the current code (e.g. "We follow redirects, check for success codes only"). Note that your comment could be a good extended commit message though! That's usually the place to write, "I changed this code for these reasons".

That being said, I don't think this is worth creating another commit for - I'm just going to land it.

@mcomella
Copy link
Contributor

mcomella commented Jan 8, 2018

@mcomella Hey could you suggest me some bugs?

@grubdragon I saw you commenting on #1085 and #1433 – those sound like they'd be good to tackle! Thanks for hacking on code with us! :)

// A non-error HTTP response is good enough as a sanity check, some search engines redirect to https.
return connection.getResponseCode() < 400;
// Now that redirects are followed, 300 is a better and stronger sanity check, checks for a non error and non redirect response
return connection.getResponseCode() < 300;
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, this change I request breaks the tests: https://travis-ci.org/mozilla-mobile/focus-android/builds/325730045?utm_source=github_status&utm_medium=notification since the test explicitly checks request codes in 3xx – do you mind updating the tests too? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I've done that right though

Copy link
Contributor

@mcomella mcomella left a comment

Choose a reason for hiding this comment

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

Tests pass and this looks good to me! Thanks grubdragon!

btw, you can see the test results at the bottom of the Conversation page on github. Note that Buddybuild will always be pending because it can't build all PRs.

// Currently we accept redirects as valid. We might want to follow the redirect (Issue #1976)
assertTrue(isValidSearchQueryURL(it.rootUrl()))
fun `URL using HTTP redirect is invalid`() = withMockWebServer(responseWithStatus(301)) {
// We now follow redirects(Issue #1976). This test now asserts false.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: similar to my comment above, this comment references the past state of the code, "We now follow redirects... This test now asserts...". I think a better description might be: "We follow redirects so we should never receive a 3xx. If we do, we want to fail." Or something.

But I don't think it's worth the turn around time to fix now.

Copy link
Contributor Author

@grubdragon grubdragon Jan 10, 2018

Choose a reason for hiding this comment

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

Will keep in mind the next time 😄 Thanks for helping me throughout!

@mcomella mcomella merged commit c04d024 into mozilla-mobile:master Jan 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants