Skip to content

Commit 35b5e1e

Browse files
committed
Merge pull request #120 from longbai/upport_backup
upport_backup
2 parents b28cb89 + 531b222 commit 35b5e1e

File tree

11 files changed

+248
-44
lines changed

11 files changed

+248
-44
lines changed

library/src/androidTest/java/com/qiniu/android/FormUploadTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
7070
Assert.assertTrue(info.isOK());
7171
Assert.assertNotNull(info.reqId);
7272
Assert.assertNotNull(resp);
73+
Assert.assertEquals("/", info.path);
7374
}
7475

7576
@SmallTest
@@ -106,6 +107,7 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
106107
Assert.assertEquals(expectKey, key);
107108
Assert.assertTrue(info.isOK());
108109
Assert.assertNotNull(info.reqId);
110+
Assert.assertEquals("/", info.path);
109111
Assert.assertNotNull(resp);
110112
Assert.assertEquals("Fqr0xh3cxeii2r7eDztILNmuqUNN", resp.optString("key", ""));
111113
}
@@ -323,4 +325,90 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
323325
Assert.assertNotNull(resp);
324326
}
325327

328+
@SmallTest
329+
public void testPortBackup() throws Throwable {
330+
Configuration c = new Configuration.Builder()
331+
.zone(new Zone("upload.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
332+
.upPort(9999)
333+
.build();
334+
UploadManager _up = new UploadManager(c);
335+
final String expectKey = "你好;\"\r\n\r\n\r\n";
336+
Map<String, String> params = new HashMap<String, String>();
337+
params.put("x:foo", "fooval");
338+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
339+
340+
uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
341+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
342+
Log.i("qiniutest", k + rinfo);
343+
key = k;
344+
info = rinfo;
345+
resp = response;
346+
signal.countDown();
347+
}
348+
}, opt);
349+
350+
351+
try {
352+
signal.await(120, TimeUnit.SECONDS); // wait for callback
353+
} catch (InterruptedException e) {
354+
e.printStackTrace();
355+
}
356+
// 尝试获取info信息。
357+
// key == null : 没进入 complete ? 什么导致的?
358+
if (!expectKey.equals(key)) {
359+
//此处通不过, travis 会打印信息
360+
Assert.assertEquals("", info);
361+
}
362+
if (info == null || !info.isOK()) {
363+
//此处通不过, travis 会打印信息
364+
Assert.assertEquals("", info);
365+
}
366+
Assert.assertEquals(expectKey, key);
367+
Assert.assertTrue(info.isOK());
368+
Assert.assertNotNull(info.reqId);
369+
Assert.assertNotNull(resp);
370+
}
371+
372+
@SmallTest
373+
public void testDnsHijacking() throws Throwable {
374+
Configuration c = new Configuration.Builder()
375+
.zone(new Zone("uphijacktest.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
376+
.build();
377+
UploadManager _up = new UploadManager(c);
378+
final String expectKey = "你好;\"\r\n\r\n\r\n";
379+
Map<String, String> params = new HashMap<String, String>();
380+
params.put("x:foo", "fooval");
381+
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
382+
383+
uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, new UpCompletionHandler() {
384+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
385+
Log.i("qiniutest", k + rinfo);
386+
key = k;
387+
info = rinfo;
388+
resp = response;
389+
signal.countDown();
390+
}
391+
}, opt);
392+
393+
394+
try {
395+
signal.await(120, TimeUnit.SECONDS); // wait for callback
396+
} catch (InterruptedException e) {
397+
e.printStackTrace();
398+
}
399+
// 尝试获取info信息。
400+
// key == null : 没进入 complete ? 什么导致的?
401+
if (!expectKey.equals(key)) {
402+
//此处通不过, travis 会打印信息
403+
Assert.assertEquals("", info);
404+
}
405+
if (info == null || !info.isOK()) {
406+
//此处通不过, travis 会打印信息
407+
Assert.assertEquals("", info);
408+
}
409+
Assert.assertEquals(expectKey, key);
410+
Assert.assertTrue(info.isOK());
411+
Assert.assertNotNull(info.reqId);
412+
Assert.assertNotNull(resp);
413+
}
326414
}

library/src/androidTest/java/com/qiniu/android/ResumeUploadTest.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import android.util.Log;
77

88
import com.qiniu.android.http.ResponseInfo;
9+
import com.qiniu.android.storage.Configuration;
910
import com.qiniu.android.storage.UpCompletionHandler;
1011
import com.qiniu.android.storage.UploadManager;
12+
import com.qiniu.android.storage.Zone;
1113

1214
import junit.framework.Assert;
1315

@@ -26,7 +28,8 @@ public class ResumeUploadTest extends InstrumentationTestCase {
2628
private volatile JSONObject resp;
2729

2830
public void setUp() throws Exception {
29-
uploadManager = new UploadManager();
31+
Configuration config = new Configuration.Builder().upPort(9999).build();
32+
uploadManager = new UploadManager(config);
3033
}
3134

3235
private void template(int size) throws Throwable {
@@ -104,6 +107,47 @@ public void complete(String k, ResponseInfo rinfo, JSONObject response) {
104107
TempFile.remove(f);
105108
}
106109

110+
private void templateHijack(int size) throws Throwable {
111+
final String expectKey = "r=" + size + "k";
112+
final File f = TempFile.createFile(size);
113+
114+
Configuration c = new Configuration.Builder()
115+
.zone(new Zone("uphijacktest.qiniu.com", Zone.zone0.upHostBackup, Zone.zone0.upIp))
116+
.build();
117+
UploadManager uploadManager = new UploadManager(c);
118+
119+
uploadManager.put(f, expectKey, TestConfig.token, new UpCompletionHandler() {
120+
public void complete(String k, ResponseInfo rinfo, JSONObject response) {
121+
Log.i("qiniutest", k + rinfo);
122+
key = k;
123+
info = rinfo;
124+
resp = response;
125+
signal.countDown();
126+
}
127+
}, null);
128+
129+
try {
130+
signal.await(500, TimeUnit.SECONDS); // wait for callback
131+
} catch (InterruptedException e) {
132+
e.printStackTrace();
133+
}
134+
// 尝试获取info信息。
135+
// key == null : 没进入 complete ? 什么导致的?
136+
if (!expectKey.equals(key)) {
137+
//此处通不过, travis 会打印信息
138+
Assert.assertEquals("", info);
139+
}
140+
if (info == null || !info.isOK()) {
141+
//此处通不过, travis 会打印信息
142+
Assert.assertEquals("", info);
143+
}
144+
Assert.assertEquals(expectKey, key);
145+
Assert.assertTrue(info.isOK());
146+
Assert.assertNotNull(info.reqId);
147+
Assert.assertNotNull(resp);
148+
TempFile.remove(f);
149+
}
150+
107151
@MediumTest
108152
public void test600k() throws Throwable {
109153
template(600);
@@ -119,6 +163,12 @@ public void test4M() throws Throwable {
119163
template(1024 * 4);
120164
}
121165

166+
@LargeTest
167+
public void test2MHijack() throws Throwable {
168+
templateHijack(1024 * 2);
169+
}
170+
171+
122172
// @LargeTest
123173
// public void test8M1k() throws Throwable{
124174
// template(1024*8+1);

library/src/androidTest/java/com/qiniu/android/TokenTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@ public void testRight() {
1414
UpToken t = UpToken.parse(TestConfig.token);
1515
Assert.assertNotNull(t);
1616
}
17+
18+
public void testEmpty() {
19+
UpToken t = UpToken.parse(null);
20+
Assert.assertNull(t);
21+
22+
t = UpToken.parse("");
23+
Assert.assertNull(t);
24+
}
25+
26+
public void testReturnUrl() {
27+
UpToken t = UpToken.parse("QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:1jLiztn4plVyeB8Hie1ryO5z9uo=:eyJzY29wZSI6InB5c2RrIiwiZGVhZGxpbmUiOjE0MzM0ODM5MzYsInJldHVyblVybCI6Imh0dHA6Ly8xMjcuMC4wLjEvIn0=");
28+
Assert.assertTrue(t.hasReturnUrl());
29+
}
30+
1731
}

library/src/main/java/com/qiniu/android/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "7.0.5.1";
5+
public static final String VERSION = "7.0.6";
66

77
public static final String UTF_8 = "utf-8";
88
}

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ public void postData(String url, byte[] data, Header[] headers, ProgressHandler
111111
}
112112

113113
private void postEntity(String url, final HttpEntity entity, Header[] headers,
114-
ProgressHandler progressHandler, CompletionHandler completionHandler, final boolean forceIp) {
114+
final ProgressHandler progressHandler, final CompletionHandler completionHandler, final boolean forceIp) {
115115
final CompletionHandler wrapper = wrap(completionHandler);
116116
final Header[] h = reporter.appendStatHeaders(headers);
117117

118118
if (converter != null){
119119
url = converter.convert(url);
120120
}
121121

122-
final AsyncHttpResponseHandler originHandler = new ResponseHandler(url, wrapper, progressHandler);
122+
ResponseHandler handler = new ResponseHandler(url, wrapper, progressHandler);
123123
if(backUpIp == null || converter != null){
124-
client.post(null, url, h, entity, null, originHandler);
124+
client.post(null, url, h, entity, null, handler);
125125
return;
126126
}
127127
final String url2 = url;
@@ -130,13 +130,18 @@ private void postEntity(String url, final HttpEntity entity, Header[] headers,
130130
t.execute(new Runnable() {
131131
@Override
132132
public void run() {
133-
URI uri = URI.create(url2);
134-
String ip = Dns.getAddress(uri.getHost());
135-
if (ip == null || ip.equals("") || forceIp) {
133+
final URI uri = URI.create(url2);
134+
String ip = null;
135+
if (forceIp) {
136136
ip = backUpIp;
137+
}else {
138+
ip = Dns.getAddress(uri.getHost());
139+
if (ip == null || ip.equals("")){
140+
ip = backUpIp;
141+
}
137142
}
138143

139-
Header[] h2 = new Header[h.length + 1];
144+
final Header[] h2 = new Header[h.length + 1];
140145
System.arraycopy(h, 0, h2, 0, h.length);
141146

142147
String newUrl = null;
@@ -146,7 +151,25 @@ public void run() {
146151
throw new AssertionError(e);
147152
}
148153
h2[h.length] = new BasicHeader("Host", uri.getHost());
149-
client.post(null, newUrl, h2, entity, null, originHandler);
154+
final String ip2 = ip;
155+
ResponseHandler handler2 = new ResponseHandler(url2, wrap(new CompletionHandler() {
156+
@Override
157+
public void complete(ResponseInfo info, JSONObject response) {
158+
if (uri.getPort() == 80 || info.statusCode != ResponseInfo.CannotConnectToHost){
159+
completionHandler.complete(info, response);
160+
return;
161+
}
162+
String newUrl80 = null;
163+
try {
164+
newUrl80 = new URI(uri.getScheme(), null, ip2, 80, uri.getPath(), uri.getQuery(), null).toString();
165+
} catch (URISyntaxException e) {
166+
throw new AssertionError(e);
167+
}
168+
ResponseHandler handler3 = new ResponseHandler(newUrl80, completionHandler, progressHandler);
169+
client.post(null, newUrl80, h2, entity, null, handler3);
170+
}
171+
}), progressHandler);
172+
client.post(null, newUrl, h2, entity, null, handler2);
150173
}
151174
});
152175
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ public final class ResponseHandler extends AsyncHttpResponseHandler {
5353
*/
5454
private int port = -1;
5555

56+
private String path = null;
57+
5658
public ResponseHandler(String url, CompletionHandler completionHandler, ProgressHandler progressHandler) {
5759
super(Looper.getMainLooper());
5860
URI uri = null;
5961
try {
6062
uri = new URI(url);
6163
this.host = uri.getHost();
6264
this.port = uri.getPort();
65+
this.path = uri.getPath();
6366
} catch (URISyntaxException e) {
6467
this.host = "N/A";
6568
e.printStackTrace();
@@ -69,7 +72,7 @@ public ResponseHandler(String url, CompletionHandler completionHandler, Progress
6972
}
7073

7174
private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers, byte[] responseBody,
72-
String host, String ip, int port, double duration, Throwable error) {
75+
String host, String path, String ip, int port, double duration, Throwable error) {
7376

7477
if (error != null && error instanceof CancellationHandler.CancellationException) {
7578
return ResponseInfo.cancelled();
@@ -138,7 +141,7 @@ private static ResponseInfo buildResponseInfo(int statusCode, Header[] headers,
138141
}
139142
}
140143

141-
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, port, duration, err);
144+
return new ResponseInfo(statusCode, reqId, xlog, xvia, host, path, ip, port, duration, err);
142145
}
143146

144147
private static JSONObject buildJsonResp(byte[] body) throws Exception {
@@ -156,15 +159,15 @@ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
156159
} catch (Exception e) {
157160
exception = e;
158161
}
159-
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, ip, port, duration, exception);
162+
ResponseInfo info = buildResponseInfo(statusCode, headers, null, host, path, ip, port, duration, exception);
160163
Log.i("upload----success", info.toString());
161164
completionHandler.complete(info, obj);
162165
}
163166

164167
@Override
165168
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
166169
double duration = (System.currentTimeMillis() - reqStartTime) / 1000.0;
167-
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, ip, port, duration, error);
170+
ResponseInfo info = buildResponseInfo(statusCode, headers, responseBody, host, path, ip, port, duration, error);
168171
Log.i("upload----failed", info.toString());
169172
completionHandler.complete(info, null);
170173
}

0 commit comments

Comments
 (0)