Skip to content

Commit 1ba2cc8

Browse files
committed
token decode
1 parent 37fa91b commit 1ba2cc8

File tree

3 files changed

+180
-1
lines changed

3 files changed

+180
-1
lines changed

library/src/main/java/com/qiniu/android/http/HttpManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public HttpManager(Proxy proxy, IReport reporter, String backUpIp,
4343
client.setConnectTimeout(connectTimeout*1000);
4444
client.setResponseTimeout(responseTimeout*1000);
4545
client.setUserAgent(userAgent);
46-
client.setEnableRedirects(false);
46+
client.setEnableRedirects(true);
4747
AsyncHttpClient.blockRetryExceptionClass(CancellationHandler.CancellationException.class);
4848
if (proxy != null) {
4949
client.setProxy(proxy.hostAddress, proxy.port, proxy.user, proxy.password);
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.qiniu.android.storage;
2+
3+
import com.qiniu.android.utils.UrlSafeBase64;
4+
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
/**
9+
* Created by bailong on 15/5/25.
10+
*/
11+
final class UpPolicy {
12+
private String returnUrl = null;
13+
static UpPolicy parse(String token){
14+
byte[] dtoken = UrlSafeBase64.decode(token);
15+
JSONObject obj;
16+
try {
17+
obj = new JSONObject(new String(dtoken));
18+
} catch (JSONException e) {
19+
return null;
20+
}
21+
String scope = obj.optString("scope");
22+
if (scope.equals("")){
23+
return null;
24+
}
25+
26+
int deadline = obj.optInt("deadline");
27+
if (deadline == 0){
28+
return null;
29+
}
30+
return new UpPolicy(obj);
31+
}
32+
33+
private UpPolicy(JSONObject obj){
34+
returnUrl = obj.optString("returnUrl");
35+
}
36+
37+
private boolean hasReturnUrl(){
38+
return !returnUrl.equals("");
39+
}
40+
41+
}

0 commit comments

Comments
 (0)