|
1 | 1 | package com.microsoft.graph.httpcore; |
2 | 2 |
|
3 | | -import java.net.URI; |
4 | | -import java.net.URISyntaxException; |
| 3 | +import static java.net.HttpURLConnection.HTTP_MOVED_PERM; |
| 4 | +import static java.net.HttpURLConnection.HTTP_MOVED_TEMP; |
| 5 | +import static java.net.HttpURLConnection.HTTP_SEE_OTHER; |
| 6 | +import static okhttp3.internal.http.StatusLine.HTTP_PERM_REDIRECT; |
| 7 | +import static okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT; |
5 | 8 |
|
6 | | -import org.apache.http.Header; |
7 | | -import org.apache.http.HttpRequest; |
8 | | -import org.apache.http.HttpResponse; |
9 | | -import org.apache.http.HttpStatus; |
10 | | -import org.apache.http.ProtocolException; |
11 | | -import org.apache.http.client.methods.HttpGet; |
12 | | -import org.apache.http.client.methods.HttpHead; |
13 | | -import org.apache.http.client.methods.HttpUriRequest; |
14 | | -import org.apache.http.client.methods.RequestBuilder; |
15 | | -import org.apache.http.impl.client.DefaultRedirectStrategy; |
16 | | -import org.apache.http.protocol.HttpContext; |
17 | | -import org.apache.http.util.Args; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.ProtocolException; |
18 | 11 |
|
19 | | -public class RedirectHandler extends DefaultRedirectStrategy{ |
| 12 | +import okhttp3.HttpUrl; |
| 13 | +import okhttp3.Interceptor; |
| 14 | +import okhttp3.Request; |
| 15 | +import okhttp3.RequestBody; |
| 16 | +import okhttp3.Response; |
| 17 | +import okhttp3.internal.http.HttpMethod; |
| 18 | + |
| 19 | +public class RedirectHandler implements Interceptor{ |
20 | 20 |
|
21 | 21 | public static final RedirectHandler INSTANCE = new RedirectHandler(); |
| 22 | + final int maxRedirect = 5; |
22 | 23 |
|
23 | | - @Override |
24 | | - public boolean isRedirected( |
25 | | - final HttpRequest request, |
26 | | - final HttpResponse response, |
27 | | - final HttpContext context) throws ProtocolException { |
28 | | - Args.notNull(request, "HTTP request"); |
29 | | - Args.notNull(response, "HTTP response"); |
30 | | - |
31 | | - final int statusCode = response.getStatusLine().getStatusCode(); |
32 | | - final Header locationHeader = response.getFirstHeader("location"); |
| 24 | + public boolean isRedirected(Request request, Response response, int redirectCount) throws IOException { |
| 25 | + |
| 26 | + if(redirectCount > maxRedirect) return false; |
| 27 | + |
| 28 | + final String locationHeader = response.header("location"); |
33 | 29 | if(locationHeader == null) |
34 | 30 | return false; |
35 | 31 |
|
36 | | - if(statusCode == HttpStatus.SC_MOVED_TEMPORARILY || |
37 | | - statusCode == HttpStatus.SC_MOVED_PERMANENTLY || |
38 | | - statusCode == HttpStatus.SC_TEMPORARY_REDIRECT || |
39 | | - statusCode == HttpStatus.SC_SEE_OTHER || |
40 | | - statusCode == 308) |
| 32 | + final int statusCode = response.code(); |
| 33 | + if(statusCode == HTTP_PERM_REDIRECT || |
| 34 | + statusCode == HTTP_MOVED_PERM || |
| 35 | + statusCode == HTTP_TEMP_REDIRECT || |
| 36 | + statusCode == HTTP_SEE_OTHER || |
| 37 | + statusCode == HTTP_MOVED_TEMP) |
41 | 38 | return true; |
42 | 39 |
|
43 | 40 | return false; |
44 | 41 | } |
45 | 42 |
|
46 | | - @Override |
47 | | - public HttpUriRequest getRedirect( |
48 | | - final HttpRequest request, |
49 | | - final HttpResponse response, |
50 | | - final HttpContext context) throws ProtocolException { |
51 | | - final URI uri = getLocationURI(request, response, context); |
52 | | - try { |
53 | | - final URI requestURI = new URI(request.getRequestLine().getUri()); |
54 | | - if(!uri.getHost().equalsIgnoreCase(requestURI.getHost()) || |
55 | | - !uri.getScheme().equalsIgnoreCase(requestURI.getScheme())) |
56 | | - request.removeHeaders("Authorization"); |
57 | | - } |
58 | | - catch (final URISyntaxException ex) { |
59 | | - throw new ProtocolException(ex.getMessage(), ex); |
60 | | - } |
61 | | - |
62 | | - final int status = response.getStatusLine().getStatusCode(); |
63 | | - if(status == HttpStatus.SC_SEE_OTHER) |
64 | | - return new HttpGet(uri); |
65 | | - return RequestBuilder.copy(request).setUri(uri).build(); |
| 43 | + public Request getRedirect( |
| 44 | + final Request request, |
| 45 | + final Response userResponse) throws ProtocolException { |
| 46 | + String location = userResponse.header("Location"); |
| 47 | + if (location == null) return null; |
| 48 | + HttpUrl url = userResponse.request().url().resolve(location); |
| 49 | + // Don't follow redirects to unsupported protocols. |
| 50 | + if (url == null) return null; |
| 51 | + |
| 52 | + // Most redirects don't include a request body. |
| 53 | + Request.Builder requestBuilder = userResponse.request().newBuilder(); |
| 54 | + final String method = userResponse.request().method(); |
| 55 | + |
| 56 | + if (HttpMethod.permitsRequestBody(method)) { |
| 57 | + final boolean maintainBody = HttpMethod.redirectsWithBody(method); |
| 58 | + if (HttpMethod.redirectsToGet(method)) { |
| 59 | + requestBuilder.method("GET", null); |
| 60 | + } else { |
| 61 | + RequestBody requestBody = maintainBody ? userResponse.request().body() : null; |
| 62 | + requestBuilder.method(method, requestBody); |
| 63 | + } |
| 64 | + if (!maintainBody) { |
| 65 | + requestBuilder.removeHeader("Transfer-Encoding"); |
| 66 | + requestBuilder.removeHeader("Content-Length"); |
| 67 | + requestBuilder.removeHeader("Content-Type"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // When redirecting across hosts, drop all authentication headers. This |
| 72 | + // is potentially annoying to the application layer since they have no |
| 73 | + // way to retain them. |
| 74 | + boolean sameScheme = url.scheme().equals(userResponse.request().url().scheme()); |
| 75 | + boolean sameHost = url.host().equals(userResponse.request().url().host()); |
| 76 | + if (!sameScheme || !sameHost) { |
| 77 | + requestBuilder.removeHeader("Authorization"); |
| 78 | + } |
| 79 | + |
| 80 | + return requestBuilder.url(url).build(); |
66 | 81 | } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Response intercept(Chain chain) throws IOException { |
| 85 | + Request request = chain.request(); |
| 86 | + Response response = null; |
| 87 | + int redirectCount = 1; |
| 88 | + while(true) { |
| 89 | + response = chain.proceed(request); |
| 90 | + boolean shouldRedirect = isRedirected(request, response, redirectCount); |
| 91 | + if(!shouldRedirect) break; |
| 92 | + |
| 93 | + Request followup = getRedirect(request, response); |
| 94 | + if(followup == null) break; |
| 95 | + request = followup; |
| 96 | + |
| 97 | + redirectCount++; |
| 98 | + } |
| 99 | + return response; |
| 100 | + } |
67 | 101 | } |
0 commit comments