6
6
// Copyright (c) 2014年 Qiniu. All rights reserved.
7
7
//
8
8
9
-
10
9
#import " HappyDNS.h"
11
10
#import " QNAsyncRun.h"
12
11
#import " QNConfiguration.h"
@@ -51,24 +50,23 @@ - (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock {
51
50
}
52
51
53
52
- (void )URLSession : (NSURLSession *)session task : (NSURLSessionTask *)task
54
- didSendBodyData : (int64_t )bytesSent
55
- totalBytesSent : (int64_t )totalBytesSent
56
- totalBytesExpectedToSend : (int64_t )totalBytesExpectedToSend {
57
-
53
+ didSendBodyData : (int64_t )bytesSent
54
+ totalBytesSent : (int64_t )totalBytesSent
55
+ totalBytesExpectedToSend : (int64_t )totalBytesExpectedToSend {
56
+
58
57
_progressBlock (totalBytesSent, totalBytesExpectedToSend);
59
58
if (_cancelBlock && _cancelBlock ()) {
60
59
[_task cancel ];
61
60
}
62
-
61
+
63
62
float progress = (float )totalBytesSent / totalBytesExpectedToSend;
64
- if (progress== 1 .0f ) {
65
- self.task =nil ;
63
+ if (progress == 1 .0f ) {
64
+ self.task = nil ;
66
65
self.cancelBlock = nil ;
67
66
self.progressBlock = nil ;
68
67
}
69
68
}
70
69
71
-
72
70
@end
73
71
74
72
@interface QNSessionManager ()
@@ -109,7 +107,6 @@ - (void)dealloc {
109
107
}
110
108
}
111
109
112
-
113
110
- (instancetype )init {
114
111
return [self initWithProxy: nil timeout: 60 urlConverter: nil dns: nil ];
115
112
}
@@ -206,10 +203,10 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
206
203
QNInternalProgressBlock progressBlock2 = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
207
204
progressBlock (totalBytesWritten, totalBytesExpectedToWrite);
208
205
};
209
- QNProgessDelegate *delegate =(QNProgessDelegate *)_httpManager.delegate ;
206
+ QNProgessDelegate *delegate = (QNProgessDelegate *)_httpManager.delegate ;
210
207
delegate.progressBlock = progressBlock2;
211
- NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithRequest: request fromData: nil completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
212
-
208
+ NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithRequest: request fromData: nil completionHandler: ^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
209
+
213
210
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
214
211
double duration = [[NSDate date ] timeIntervalSinceDate: startTime];
215
212
QNResponseInfo *info;
@@ -234,7 +231,6 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
234
231
[uploadTask resume ];
235
232
}
236
233
237
-
238
234
- (void )multipartPost : (NSString *)url
239
235
withData : (NSData *)data
240
236
withParams : (NSDictionary *)params
@@ -244,35 +240,35 @@ - (void)multipartPost:(NSString *)url
244
240
withProgressBlock : (QNInternalProgressBlock)progressBlock
245
241
withCancelBlock : (QNCancelBlock)cancelBlock
246
242
withAccess : (NSString *)access {
247
- NSURL *URL = [[NSURL alloc ]initWithString:url];
248
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]initWithURL:URL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 30 ];
243
+ NSURL *URL = [[NSURL alloc ] initWithString: url];
244
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] initWithURL: URL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 30 ];
249
245
request.HTTPMethod = @" POST" ;
250
246
NSString *boundary = @" werghnvt54wef654rjuhgb56trtg34tweuyrgf" ;
251
247
request.allHTTPHeaderFields = @{
252
- @" Content-Type" : [NSString stringWithFormat: @" multipart/form-data; boundary=%@ " ,boundary]
253
- };
254
- NSMutableData *postData = [[NSMutableData alloc ]init];
248
+ @" Content-Type" : [NSString stringWithFormat: @" multipart/form-data; boundary=%@ " , boundary]
249
+ };
250
+ NSMutableData *postData = [[NSMutableData alloc ] init ];
255
251
for (NSString *paramsKey in params) {
256
- NSString *pair = [NSString stringWithFormat: @" --%@ \r\n Content-Disposition: form-data; name=\" %@ \"\r\n\r\n " ,boundary,paramsKey];
252
+ NSString *pair = [NSString stringWithFormat: @" --%@ \r\n Content-Disposition: form-data; name=\" %@ \"\r\n\r\n " , boundary, paramsKey];
257
253
[postData appendData: [pair dataUsingEncoding: NSUTF8StringEncoding]];
258
-
254
+
259
255
id value = [params objectForKey: paramsKey];
260
256
if ([value isKindOfClass: [NSString class ]]) {
261
257
[postData appendData: [value dataUsingEncoding: NSUTF8StringEncoding]];
262
- }else if ([value isKindOfClass: [NSData class ]]){
258
+ } else if ([value isKindOfClass: [NSData class ]]) {
263
259
[postData appendData: value];
264
260
}
265
261
[postData appendData: [@" \r\n " dataUsingEncoding: NSUTF8StringEncoding]];
266
262
}
267
- NSString *filePair = [NSString stringWithFormat: @" --%@ \r\n Content-Disposition: form-data; name=\" %@ \" ; filename=\" %@ \"\n Content-Type:%@ \r\n\r\n " ,boundary,@" file" ,key,mime];
263
+ NSString *filePair = [NSString stringWithFormat: @" --%@ \r\n Content-Disposition: form-data; name=\" %@ \" ; filename=\" %@ \"\n Content-Type:%@ \r\n\r\n " , boundary, @" file" , key, mime];
268
264
[postData appendData: [filePair dataUsingEncoding: NSUTF8StringEncoding]];
269
265
[postData appendData: data];
270
- [postData appendData: [[NSString stringWithFormat: @" \r\n --%@ --\r\n " ,boundary] dataUsingEncoding: NSUTF8StringEncoding]];
266
+ [postData appendData: [[NSString stringWithFormat: @" \r\n --%@ --\r\n " , boundary] dataUsingEncoding: NSUTF8StringEncoding]];
271
267
request.HTTPBody = postData;
272
- [request setValue: [NSString stringWithFormat: @" %lu " ,(unsigned long )postData.length] forHTTPHeaderField: @" Content-Length" ];
273
-
268
+ [request setValue: [NSString stringWithFormat: @" %lu " , (unsigned long )postData.length] forHTTPHeaderField: @" Content-Length" ];
269
+
274
270
[self sendRequest: request withCompleteBlock: completeBlock withProgressBlock: progressBlock withCancelBlock: cancelBlock
275
- withAccess: access];
271
+ withAccess: access];
276
272
}
277
273
278
274
- (void )post : (NSString *)url
@@ -307,8 +303,8 @@ - (void)get:(NSString *)url
307
303
QNAsyncRun (^{
308
304
NSURL *URL = [NSURL URLWithString: url];
309
305
NSURLRequest *request = [NSURLRequest requestWithURL: URL];
310
-
311
- NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession ] dataTaskWithRequest: request completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
306
+
307
+ NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession ] dataTaskWithRequest: request completionHandler: ^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
312
308
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
313
309
NSData *s = [@" {}" dataUsingEncoding: NSUTF8StringEncoding];
314
310
NSDictionary *resp = nil ;
@@ -327,12 +323,11 @@ - (void)get:(NSString *)url
327
323
} else {
328
324
info = [QNSessionManager buildResponseInfo: httpResponse withError: error withDuration: 0 withResponse: s withHost: @" " withIp: @" " ];
329
325
}
330
-
326
+
331
327
completeBlock (info, resp);
332
328
}];
333
329
[dataTask resume ];
334
-
335
-
330
+
336
331
});
337
332
}
338
333
0 commit comments