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

Complies with rfc7231 about statuses 307 and 308 #5990

Merged
merged 3 commits into from Apr 28, 2020

Conversation

yschimke
Copy link
Collaborator

@yschimke yschimke commented Apr 25, 2020

Fix for #2874 #3111 and #4229

https://tools.ietf.org/html/rfc7231#page-58

6.4.7. 307 Temporary Redirect

The 307 (Temporary Redirect) status code indicates that the target
resource resides temporarily under a different URI and the user agent
MUST NOT change the request method if it performs an automatic
redirection to that URI. Since the redirection can change over time,
the client ought to continue using the original effective request URI
for future requests.

The user agent MAY
use the Location field value for automatic redirection.

https://tools.ietf.org/html/rfc7238

The user agent MAY use the Location field
value for automatic redirection.

Note: This status code is similar to 301 (Moved Permanently)
([RFC7231], Section 6.4.2), except that it does not allow changing
the request method from POST to GET.

Author:    Vladimir Kravets <vova.kravets@gmail.com>
@yschimke
Copy link
Collaborator Author

@swankjesse Any thoughts on how to implement the interceptor? The logic sits between interceptors, and networkInterceptors. So seems like we would need to tag requests in interceptors such that the RetryAndFollowupInterceptor doesn't retry for these?

Or put this as a field like client.retryOnConnectionFailure.

@swankjesse
Copy link
Member

I was thinking something much more basic. Maybe the interceptor would rewrite 308s to a bogus code like 399 to get the previous behavior.

@yschimke
Copy link
Collaborator Author

That feels tough to explain if you see that code elsewhere like another interceptor.

@yschimke
Copy link
Collaborator Author

The more I think about this, the more I think we should just follow the correct logic and leave reverting to the old behaviour to users affected by this. But with heavy notification and a suggestion for how users could revert to the previous behaviour, e.g. implement your own interceptor that rewrites the response code.

Under what scenarios would mobile clients want the old behaviour? And shouldn't we encourage clients and servers move towards the correct defined behaviour? An API server is likely to only have okhttp as the only mobile client transport.

So definitely not a client flag. A new public API as a workaround interceptor feels like a bad public API also.

@yschimke yschimke marked this pull request as ready for review April 26, 2020 08:53
@swankjesse
Copy link
Member

Oh I was thinking the interceptor would be a code sample you'd gave to copy-paste.

@yschimke
Copy link
Collaborator Author

Oh I was thinking the interceptor would be a code sample you'd gave to copy-paste.

Perfect

@yschimke
Copy link
Collaborator Author

I hope noone ever uses this

  class RevertInterceptor implements Interceptor {
    @NotNull @Override public Response intercept(@NotNull Chain chain) throws IOException {
      Response response = chain.proceed(chain.request());

      return remapResponse(response);
    }

    @NotNull private Response remapResponse(Response response) {
      if (response.request().method().equals("POST") && (response.code() == HTTP_TEMP_REDIRECT || response.code() == HTTP_PERM_REDIRECT)) {
        // special response code to indicate custom rules
        return response.newBuilder().code(response.code() + 10).build();
      }

      return response;
    }
  }

@yschimke
Copy link
Collaborator Author

@swankjesse Is this for 4.6?

@swankjesse swankjesse added this to the 4.6 milestone Apr 28, 2020
@@ -2367,6 +2370,71 @@ private void testResponseRedirectedWithPost(int redirectCode, TransferKind trans
testRedirect(false, "POST");
}

class RevertInterceptor implements Interceptor {
@NotNull @Override public Response intercept(@NotNull Chain chain) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

nit: we don’t typically use @NotNull because everything is not null unless otherwise specified

}

private Response remapResponse(Response response) {
if (response.request().method().equals("POST") && (response.code() == HTTP_TEMP_REDIRECT || response.code() == HTTP_PERM_REDIRECT)) {
Copy link
Member

Choose a reason for hiding this comment

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

oh this should probably be checking for not GET and not HEAD, otherwise PUT isn‘t consistent!

@swankjesse swankjesse merged commit 0cc3aef into square:master Apr 28, 2020
@yschimke yschimke deleted the rfc7231 branch May 21, 2020 20:22
markush81 added a commit to markush81/jira-steps-plugin that referenced this pull request Sep 24, 2021
…, okhttp only follows them starting version 4.6.0 (square/okhttp#5990)

Ignored findbugs issues, since null was always a valid return value anyhow.
markush81 added a commit to markush81/jira-steps-plugin that referenced this pull request Apr 7, 2023
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.

None yet

3 participants