|
| 1 | +package com.qiniu.android.http; |
| 2 | + |
| 3 | +import org.apache.http.Header; |
| 4 | +import org.apache.http.HttpHost; |
| 5 | +import org.apache.http.HttpRequest; |
| 6 | +import org.apache.http.HttpResponse; |
| 7 | +import org.apache.http.HttpStatus; |
| 8 | +import org.apache.http.ProtocolException; |
| 9 | +import org.apache.http.client.CircularRedirectException; |
| 10 | +import org.apache.http.client.RedirectHandler; |
| 11 | +import org.apache.http.client.params.ClientPNames; |
| 12 | +import org.apache.http.client.utils.URIUtils; |
| 13 | +import org.apache.http.impl.client.RedirectLocations; |
| 14 | +import org.apache.http.params.HttpParams; |
| 15 | +import org.apache.http.protocol.ExecutionContext; |
| 16 | +import org.apache.http.protocol.HttpContext; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | +import java.net.URISyntaxException; |
| 20 | + |
| 21 | +/** |
| 22 | + * Created by bailong on 15/5/25. |
| 23 | + */ |
| 24 | +public class UpRedirectHandler implements RedirectHandler { |
| 25 | + |
| 26 | + private static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations"; |
| 27 | + |
| 28 | + private static boolean isQiniu(final HttpResponse response){ |
| 29 | + return response.getFirstHeader("X-Reqid") != null; |
| 30 | + } |
| 31 | + |
| 32 | + public boolean isRedirectRequested( |
| 33 | + final HttpResponse response, |
| 34 | + final HttpContext context) { |
| 35 | + if (response == null) { |
| 36 | + throw new IllegalArgumentException("HTTP response may not be null"); |
| 37 | + } |
| 38 | + |
| 39 | + final int statusCode = response.getStatusLine().getStatusCode(); |
| 40 | + switch (statusCode) { |
| 41 | + //upload only allow 303 |
| 42 | + case HttpStatus.SC_SEE_OTHER: |
| 43 | + return isQiniu(response); |
| 44 | + default: |
| 45 | + return false; |
| 46 | + } //end of switch |
| 47 | + } |
| 48 | + |
| 49 | + public URI getLocationURI( |
| 50 | + final HttpResponse response, |
| 51 | + final HttpContext context) throws ProtocolException { |
| 52 | + if (response == null) { |
| 53 | + throw new IllegalArgumentException("HTTP response may not be null"); |
| 54 | + } |
| 55 | + //get the location header to find out where to redirect to |
| 56 | + Header locationHeader = response.getFirstHeader("location"); |
| 57 | + if (locationHeader == null) { |
| 58 | + // got a redirect response, but no location header |
| 59 | + throw new ProtocolException( |
| 60 | + "Received redirect response " + response.getStatusLine() |
| 61 | + + " but no location header" |
| 62 | + ); |
| 63 | + } |
| 64 | + //HERE IS THE MODIFIED LINE OF CODE |
| 65 | + String location = locationHeader.getValue().replaceAll(" ", "%20"); |
| 66 | + |
| 67 | + URI uri; |
| 68 | + try { |
| 69 | + uri = new URI(location); |
| 70 | + } catch (URISyntaxException ex) { |
| 71 | + throw new ProtocolException("Invalid redirect URI: " + location, ex); |
| 72 | + } |
| 73 | + |
| 74 | + HttpParams params = response.getParams(); |
| 75 | + // rfc2616 demands the location value be a complete URI |
| 76 | + // Location = "Location" ":" absoluteURI |
| 77 | + if (!uri.isAbsolute()) { |
| 78 | + if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) { |
| 79 | + throw new ProtocolException("Relative redirect location '" |
| 80 | + + uri + "' not allowed"); |
| 81 | + } |
| 82 | + // Adjust location URI |
| 83 | + HttpHost target = (HttpHost) context.getAttribute( |
| 84 | + ExecutionContext.HTTP_TARGET_HOST); |
| 85 | + if (target == null) { |
| 86 | + throw new IllegalStateException("Target host not available " + |
| 87 | + "in the HTTP context"); |
| 88 | + } |
| 89 | + |
| 90 | + HttpRequest request = (HttpRequest) context.getAttribute( |
| 91 | + ExecutionContext.HTTP_REQUEST); |
| 92 | + |
| 93 | + try { |
| 94 | + URI requestURI = new URI(request.getRequestLine().getUri()); |
| 95 | + URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true); |
| 96 | + uri = URIUtils.resolve(absoluteRequestURI, uri); |
| 97 | + } catch (URISyntaxException ex) { |
| 98 | + throw new ProtocolException(ex.getMessage(), ex); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) { |
| 103 | + |
| 104 | + RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute( |
| 105 | + REDIRECT_LOCATIONS); |
| 106 | + |
| 107 | + if (redirectLocations == null) { |
| 108 | + redirectLocations = new RedirectLocations(); |
| 109 | + context.setAttribute(REDIRECT_LOCATIONS, redirectLocations); |
| 110 | + } |
| 111 | + |
| 112 | + URI redirectURI; |
| 113 | + if (uri.getFragment() != null) { |
| 114 | + try { |
| 115 | + HttpHost target = new HttpHost( |
| 116 | + uri.getHost(), |
| 117 | + uri.getPort(), |
| 118 | + uri.getScheme()); |
| 119 | + redirectURI = URIUtils.rewriteURI(uri, target, true); |
| 120 | + } catch (URISyntaxException ex) { |
| 121 | + throw new ProtocolException(ex.getMessage(), ex); |
| 122 | + } |
| 123 | + } else { |
| 124 | + redirectURI = uri; |
| 125 | + } |
| 126 | + |
| 127 | + if (redirectLocations.contains(redirectURI)) { |
| 128 | + throw new CircularRedirectException("Circular redirect to '" + |
| 129 | + redirectURI + "'"); |
| 130 | + } else { |
| 131 | + redirectLocations.add(redirectURI); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return uri; |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments