66// Copyright (c) 2014年 Qiniu. All rights reserved.
77//
88
9-
109#import " HappyDNS.h"
1110#import " QNAsyncRun.h"
1211#import " QNConfiguration.h"
@@ -51,24 +50,23 @@ - (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock {
5150}
5251
5352- (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+
5857 _progressBlock (totalBytesSent, totalBytesExpectedToSend);
5958 if (_cancelBlock && _cancelBlock ()) {
6059 [_task cancel ];
6160 }
62-
61+
6362 float progress = (float )totalBytesSent / totalBytesExpectedToSend;
64- if (progress== 1 .0f ) {
65- self.task =nil ;
63+ if (progress == 1 .0f ) {
64+ self.task = nil ;
6665 self.cancelBlock = nil ;
6766 self.progressBlock = nil ;
6867 }
6968}
7069
71-
7270@end
7371
7472@interface QNSessionManager ()
@@ -109,7 +107,6 @@ - (void)dealloc {
109107 }
110108}
111109
112-
113110- (instancetype )init {
114111 return [self initWithProxy: nil timeout: 60 urlConverter: nil dns: nil ];
115112}
@@ -206,10 +203,10 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
206203 QNInternalProgressBlock progressBlock2 = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
207204 progressBlock (totalBytesWritten, totalBytesExpectedToWrite);
208205 };
209- QNProgessDelegate *delegate =(QNProgessDelegate *)_httpManager.delegate ;
206+ QNProgessDelegate *delegate = (QNProgessDelegate *)_httpManager.delegate ;
210207 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+
213210 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
214211 double duration = [[NSDate date ] timeIntervalSinceDate: startTime];
215212 QNResponseInfo *info;
@@ -234,7 +231,6 @@ - (void)sendRequest2:(NSMutableURLRequest *)request
234231 [uploadTask resume ];
235232}
236233
237-
238234- (void )multipartPost : (NSString *)url
239235 withData : (NSData *)data
240236 withParams : (NSDictionary *)params
@@ -244,35 +240,35 @@ - (void)multipartPost:(NSString *)url
244240 withProgressBlock : (QNInternalProgressBlock)progressBlock
245241 withCancelBlock : (QNCancelBlock)cancelBlock
246242 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 ];
249245 request.HTTPMethod = @" POST" ;
250246 NSString *boundary = @" werghnvt54wef654rjuhgb56trtg34tweuyrgf" ;
251247 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 ];
255251 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];
257253 [postData appendData: [pair dataUsingEncoding: NSUTF8StringEncoding]];
258-
254+
259255 id value = [params objectForKey: paramsKey];
260256 if ([value isKindOfClass: [NSString class ]]) {
261257 [postData appendData: [value dataUsingEncoding: NSUTF8StringEncoding]];
262- }else if ([value isKindOfClass: [NSData class ]]){
258+ } else if ([value isKindOfClass: [NSData class ]]) {
263259 [postData appendData: value];
264260 }
265261 [postData appendData: [@" \r\n " dataUsingEncoding: NSUTF8StringEncoding]];
266262 }
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];
268264 [postData appendData: [filePair dataUsingEncoding: NSUTF8StringEncoding]];
269265 [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]];
271267 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+
274270 [self sendRequest: request withCompleteBlock: completeBlock withProgressBlock: progressBlock withCancelBlock: cancelBlock
275- withAccess: access];
271+ withAccess: access];
276272}
277273
278274- (void )post : (NSString *)url
@@ -307,8 +303,8 @@ - (void)get:(NSString *)url
307303 QNAsyncRun (^{
308304 NSURL *URL = [NSURL URLWithString: url];
309305 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) {
312308 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
313309 NSData *s = [@" {}" dataUsingEncoding: NSUTF8StringEncoding];
314310 NSDictionary *resp = nil ;
@@ -327,12 +323,11 @@ - (void)get:(NSString *)url
327323 } else {
328324 info = [QNSessionManager buildResponseInfo: httpResponse withError: error withDuration: 0 withResponse: s withHost: @" " withIp: @" " ];
329325 }
330-
326+
331327 completeBlock (info, resp);
332328 }];
333329 [dataTask resume ];
334-
335-
330+
336331 });
337332}
338333
0 commit comments