Skip to content

Commit b28cb89

Browse files
committed
Merge pull request #119 from longbai/avoid_dns_hijacking
cancel check
2 parents 602ce10 + 18e6467 commit b28cb89

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.Map;
1818
import java.util.Random;
1919
import java.util.concurrent.ExecutorService;
20-
import java.util.concurrent.ThreadPoolExecutor;
2120

2221
import static java.lang.String.format;
2322

library/src/main/java/com/qiniu/android/storage/FormUploader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public void onProgress(int bytesWritten, int totalSize) {
103103
CompletionHandler completion = new CompletionHandler() {
104104
@Override
105105
public void complete(ResponseInfo info, JSONObject response) {
106+
if(options.cancellationSignal != null && options.cancellationSignal.isCancelled()){
107+
ResponseInfo i = ResponseInfo.cancelled();
108+
completionHandler.complete(key, i, null);
109+
return;
110+
}
106111
if (info.isOK()) {
107112
options.progressHandler.progress(key, 1.0);
108113
completionHandler.complete(key, info, response);

library/src/main/java/com/qiniu/android/storage/ResumeUploader.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void putChunk(String host, int offset, int chunkSize, String context, Pr
125125
post(url, chunkBuffer, 0, chunkSize, progress, _completionHandler, c);
126126
}
127127

128-
private void makeFile(String host, CompletionHandler _completionHandler) {
128+
private void makeFile(String host, CompletionHandler _completionHandler, UpCancellationSignal c) {
129129
String mime = format(Locale.ENGLISH, "/mimeType/%s", UrlSafeBase64.encodeToString(options.mimeType));
130130

131131
String keyStr = "";
@@ -145,8 +145,8 @@ private void makeFile(String host, CompletionHandler _completionHandler) {
145145
String url = format(Locale.ENGLISH, "http://%s/mkfile/%d%s%s%s", host, size, mime, keyStr, paramStr);
146146
String bodyStr = StringUtils.join(contexts, ",");
147147
byte[] data = bodyStr.getBytes();
148-
// 不取消 makeFile 操作
149-
post(url, data, 0, data.length, null, _completionHandler, null);
148+
149+
post(url, data, 0, data.length, null, _completionHandler, c);
150150
}
151151

152152
private void post(String url, byte[] data, int offset, int size, ProgressHandler progress,
@@ -165,14 +165,19 @@ private int calcBlockSize(int offset) {
165165
}
166166

167167
private boolean isCancelled() {
168-
return options.cancellationSignal.isCancelled();
168+
return options.cancellationSignal != null && options.cancellationSignal.isCancelled();
169169
}
170170

171171
private void nextTask(final int offset, final int retried, final String host) {
172172
if (offset == size) {
173173
CompletionHandler complete = new CompletionHandler() {
174174
@Override
175175
public void complete(ResponseInfo info, JSONObject response) {
176+
if(isCancelled()){
177+
ResponseInfo i = ResponseInfo.cancelled();
178+
completionHandler.complete(key, i, null);
179+
return;
180+
}
176181
if (info.isOK()) {
177182
removeRecord();
178183
options.progressHandler.progress(key, 1.0);
@@ -187,7 +192,7 @@ public void complete(ResponseInfo info, JSONObject response) {
187192
completionHandler.complete(key, info, response);
188193
}
189194
};
190-
makeFile(host, complete);
195+
makeFile(host, complete, options.cancellationSignal);
191196
return;
192197
}
193198

@@ -207,6 +212,11 @@ public void onProgress(int bytesWritten, int totalSize) {
207212
@Override
208213
public void complete(ResponseInfo info, JSONObject response) {
209214
if (!info.isOK()) {
215+
if(isCancelled()){
216+
ResponseInfo i = ResponseInfo.cancelled();
217+
completionHandler.complete(key, i, null);
218+
return;
219+
}
210220
if (info.statusCode == 701) {
211221
nextTask((offset / Configuration.BLOCK_SIZE) * Configuration.BLOCK_SIZE, retried, host);
212222
return;

0 commit comments

Comments
 (0)