-
Notifications
You must be signed in to change notification settings - Fork 75
/
STHTTPRequest.m
1248 lines (913 loc) · 47.3 KB
/
STHTTPRequest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// STHTTPRequest.m
// STHTTPRequest
//
// Created by Nicolas Seriot on 07.11.11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#if __has_feature(objc_arc)
#else
// see http://www.codeography.com/2011/10/10/making-arc-and-non-arc-play-nice.html
#error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag
#endif
#import "STHTTPRequest.h"
NSUInteger const kSTHTTPRequestCancellationError = 1;
NSUInteger const kSTHTTPRequestDefaultTimeout = 30;
static NSMutableDictionary *localCredentialsStorage = nil;
static NSMutableArray *localCookiesStorage = nil;
static NSMutableDictionary *sessionCompletionHandlersForIdentifier = nil;
static BOOL globalIgnoreCache = NO;
static STHTTPRequestCookiesStorage globalCookiesStoragePolicy = STHTTPRequestCookiesStorageShared;
/**/
@interface STHTTPRequestFileUpload : NSObject
@property (nonatomic, retain) NSString *path;
@property (nonatomic, retain) NSString *parameterName;
@property (nonatomic, retain) NSString *mimeType;
+ (instancetype)fileUploadWithPath:(NSString *)path parameterName:(NSString *)parameterName mimeType:(NSString *)mimeType;
+ (instancetype)fileUploadWithPath:(NSString *)path parameterName:(NSString *)parameterName;
@end
@interface STHTTPRequestDataUpload : NSObject
@property (nonatomic, retain) NSData *data;
@property (nonatomic, retain) NSString *parameterName;
@property (nonatomic, retain) NSString *mimeType; // can be nil
@property (nonatomic, retain) NSString *fileName; // can be nil
+ (instancetype)dataUploadWithData:(NSData *)data parameterName:(NSString *)parameterName mimeType:(NSString *)mimeType fileName:(NSString *)fileName;
@end
/**/
@interface STHTTPRequest ()
@property (nonatomic) NSInteger responseStatus;
@property (nonatomic, strong) NSURLSessionTask *task;
@property (nonatomic, strong) NSMutableData *responseData;
@property (nonatomic, strong) NSString *responseStringEncodingName;
@property (nonatomic, strong) NSDictionary *responseHeaders;
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, strong) NSError *error;
@property (nonatomic, strong) NSMutableArray *filesToUpload; // STHTTPRequestFileUpload instances
@property (nonatomic, strong) NSMutableArray *dataToUpload; // STHTTPRequestDataUpload instances
@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, strong) NSURL *HTTPBodyFileURL; // created for NSURLSessionUploadTask, removed on completion
@property (nonatomic, strong) NSMutableArray *ephemeralRequestCookies;
@end
@interface NSData (Base64)
- (NSString *)base64Encoding; // private API
@end
@implementation STHTTPRequest
#pragma mark Initializers
+ (instancetype)requestWithURL:(NSURL *)url {
if(url == nil) return nil;
return [(STHTTPRequest *)[self alloc] initWithURL:url];
}
+ (instancetype)requestWithURLString:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
return [self requestWithURL:url];
}
+ (void)setGlobalIgnoreCache:(BOOL)ignoreCache {
globalIgnoreCache = ignoreCache;
}
+ (void)setGlobalCookiesStoragePolicy:(STHTTPRequestCookiesStorage)cookieStoragePolicy {
globalCookiesStoragePolicy = cookieStoragePolicy;
}
- (instancetype)initWithURL:(NSURL *)theURL {
if (self = [super init]) {
self.url = theURL;
self.responseData = [[NSMutableData alloc] init];
self.requestHeaders = [NSMutableDictionary dictionary];
self.POSTDataEncoding = NSUTF8StringEncoding;
self.encodePOSTDictionary = YES;
self.encodeGETDictionary = YES;
self.addCredentialsToURL = NO;
self.timeoutSeconds = kSTHTTPRequestDefaultTimeout;
self.filesToUpload = [NSMutableArray array];
self.dataToUpload = [NSMutableArray array];
self.HTTPMethod = @"GET"; // default
self.cookieStoragePolicyForInstance = STHTTPRequestCookiesStorageUndefined; // globalCookiesStoragePolicy will be used
self.ephemeralRequestCookies = [NSMutableArray array];
}
return self;
}
+ (void)clearSession {
[[self class] deleteAllCookiesFromSharedCookieStorage];
[[self class] deleteAllCookiesFromLocalCookieStorage];
[[self class] deleteAllCredentials];
}
#pragma mark Credentials
+ (NSMutableDictionary *)sharedCredentialsStorage {
if(localCredentialsStorage == nil) {
localCredentialsStorage = [NSMutableDictionary dictionary];
}
return localCredentialsStorage;
}
+ (NSURLCredential *)sessionAuthenticationCredentialsForURL:(NSURL *)requestURL {
return [[[self class] sharedCredentialsStorage] valueForKey:[requestURL host]];
}
+ (void)deleteAllCredentials {
localCredentialsStorage = [NSMutableDictionary dictionary];
}
- (void)setCredentialForCurrentHost:(NSURLCredential *)c {
#if DEBUG
NSAssert(_url, @"missing url to set credential");
#endif
[[[self class] sharedCredentialsStorage] setObject:c forKey:[_url host]];
}
- (NSURLCredential *)credentialForCurrentHost {
return [[[self class] sharedCredentialsStorage] valueForKey:[_url host]];
}
- (void)setUsername:(NSString *)username password:(NSString *)password {
NSURLCredential *c = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceNone];
[self setCredentialForCurrentHost:c];
}
- (NSString *)username {
return [[self credentialForCurrentHost] user];
}
- (NSString *)password {
return [[self credentialForCurrentHost] password];
}
#pragma mark Cookies
- (STHTTPRequestCookiesStorage)cookieStoragePolicy {
if(_cookieStoragePolicyForInstance != STHTTPRequestCookiesStorageUndefined) {
return _cookieStoragePolicyForInstance;
}
return globalCookiesStoragePolicy;
}
+ (NSMutableArray *)localCookiesStorage {
if(localCookiesStorage == nil) {
localCookiesStorage = [NSMutableArray array];
}
return localCookiesStorage;
}
+ (NSArray *)sessionCookiesInSharedCookiesStorage {
NSArray *allCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSArray *sessionCookies = [allCookies filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSHTTPCookie *cookie = (NSHTTPCookie *)evaluatedObject;
return [cookie isSessionOnly];
}]];
return sessionCookies;
}
- (NSArray *)sessionCookies {
NSArray *allCookies = nil;
if([self cookieStoragePolicy] == STHTTPRequestCookiesStorageShared) {
allCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
} else if ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageLocal) {
allCookies = [[self class] localCookiesStorage];
}
NSArray *sessionCookies = [allCookies filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSHTTPCookie *cookie = (NSHTTPCookie *)evaluatedObject;
return [cookie isSessionOnly];
}]];
return sessionCookies;
}
- (void)deleteSessionCookies {
for(NSHTTPCookie *cookie in [self sessionCookies]) {
if([self cookieStoragePolicy] == STHTTPRequestCookiesStorageShared) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
} else if ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageLocal) {
[[[self class] localCookiesStorage] removeObject:cookie];
}
}
}
+ (void)deleteAllCookiesFromSharedCookieStorage {
NSHTTPCookieStorage *sharedCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedCookieStorage cookies];
for (NSHTTPCookie *cookie in cookies) {
[sharedCookieStorage deleteCookie:cookie];
}
}
+ (void)deleteAllCookiesFromLocalCookieStorage {
localCookiesStorage = nil;
}
- (void)deleteAllCookies {
if([self cookieStoragePolicy] == STHTTPRequestCookiesStorageShared) {
[[self class] deleteAllCookiesFromSharedCookieStorage];
} else if ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageLocal) {
[[[self class] localCookiesStorage] removeAllObjects];
}
}
+ (void)addCookieToSharedCookiesStorage:(NSHTTPCookie *)cookie {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
#if DEBUG
NSHTTPCookie *readCookie = [[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] lastObject];
NSAssert(readCookie, @"cannot read any cookie after adding one");
#endif
}
- (void)addCookie:(NSHTTPCookie *)cookie {
NSParameterAssert(cookie);
if(cookie == nil) return;
if([self cookieStoragePolicy] == STHTTPRequestCookiesStorageShared) {
[[self class] addCookieToSharedCookiesStorage:cookie];
} else if ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageLocal) {
[[[self class] localCookiesStorage] addObject:cookie];
} else if ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageNoStorage) { // ephemeral cookie, only for this request
[_ephemeralRequestCookies addObject:cookie];
}
}
+ (void)addCookieToSharedCookiesStorageWithName:(NSString *)name value:(NSString *)value url:(NSURL *)url {
NSHTTPCookie *cookie = [[self class] createCookieWithName:name value:value url:url];
[self addCookieToSharedCookiesStorage:cookie];
}
+ (NSHTTPCookie *)createCookieWithName:(NSString *)name value:(NSString *)value url:(NSURL *)url {
NSParameterAssert(url);
if(url == nil) return nil;
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionaryWithObjectsAndKeys:
name, NSHTTPCookieName,
value, NSHTTPCookieValue,
url, NSHTTPCookieOriginURL,
@"FALSE", NSHTTPCookieDiscard,
@"/", NSHTTPCookiePath,
@"0", NSHTTPCookieVersion,
[[NSDate date] dateByAddingTimeInterval:3600 * 24 * 30], NSHTTPCookieExpires,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
return cookie;
}
- (void)addCookieWithName:(NSString *)name value:(NSString *)value url:(NSURL *)url {
NSHTTPCookie *cookie = [[self class] createCookieWithName:name value:value url:url];
[self addCookie:cookie];
}
- (NSArray *)requestCookies {
if([self cookieStoragePolicy] == STHTTPRequestCookiesStorageShared) {
return [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[_url absoluteURL]];
} else if ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageLocal) {
NSArray *filteredCookies = [[[self class] localCookiesStorage] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSHTTPCookie *cookie = (NSHTTPCookie *)evaluatedObject;
return [[cookie domain] isEqualToString:[self.url host]];
}]];
return filteredCookies;
} else if ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageNoStorage) {
return _ephemeralRequestCookies;
}
return nil;
}
- (void)addCookieWithName:(NSString *)name value:(NSString *)value {
[self addCookieWithName:name value:value url:_url];
}
#pragma mark Headers
- (void)setHeaderWithName:(NSString *)name value:(NSString *)value {
if(name == nil || value == nil) return;
[[self requestHeaders] setObject:value forKey:name];
}
- (void)removeHeaderWithName:(NSString *)name {
if(name == nil) return;
[[self requestHeaders] removeObjectForKey:name];
}
+ (NSURL *)urlByAddingCredentials:(NSURLCredential *)credentials toURL:(NSURL *)url {
if(credentials == nil) return nil; // no credentials to add
NSString *scheme = [url scheme];
NSString *host = [url host];
BOOL hostAlreadyContainsCredentials = [host rangeOfString:@"@"].location != NSNotFound;
if(hostAlreadyContainsCredentials) return url;
NSMutableString *resourceSpecifier = [[url resourceSpecifier] mutableCopy];
if([resourceSpecifier hasPrefix:@"//"] == NO) return nil;
NSString *userPassword = [NSString stringWithFormat:@"%@:%@@", credentials.user, credentials.password];
[resourceSpecifier insertString:userPassword atIndex:2];
NSString *urlString = [NSString stringWithFormat:@"%@:%@", scheme, resourceSpecifier];
return [NSURL URLWithString:urlString];
}
// {k2:v2, k1:v1} -> [{k1:v1}, {k2:v2}]
+ (NSArray *)dictionariesSortedByKey:(NSDictionary *)dictionary {
NSArray *keys = [dictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSComparisonResult result = [obj1 compare:obj2];
return result;
}];
NSMutableArray *sortedDictionaries = [NSMutableArray arrayWithCapacity:[dictionary count]];
for(NSString *key in sortedKeys) {
NSDictionary *d = @{ key : dictionary[key] };
[sortedDictionaries addObject:d];
}
return sortedDictionaries;
}
+ (NSData *)multipartContentWithBoundary:(NSString *)boundary data:(NSData *)someData fileName:(NSString *)fileName parameterName:(NSString *)parameterName mimeType:(NSString *)aMimeType {
NSString *mimeType = aMimeType ? aMimeType : @"application/octet-stream";
NSMutableData *data = [[NSMutableData alloc] init];
[data appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *fileNameContentDisposition = fileName ? [NSString stringWithFormat:@"filename=\"%@\"", fileName] : @"";
NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; %@\r\n", parameterName, fileNameContentDisposition];
[data appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimeType] dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:someData];
return data;
}
+ (NSURL *)appendURL:(NSURL *)url withGETParameters:(NSDictionary *)parameters doApplyURLEncoding:(BOOL)doApplyURLEncoding {
NSMutableString *urlString = [[NSMutableString alloc] initWithString:[url absoluteString]];
NSString *s = [urlString st_stringByAppendingGETParameters:parameters doApplyURLEncoding:doApplyURLEncoding];
return [NSURL URLWithString:s];
}
- (NSURLRequest *)prepareURLRequest {
NSURL *theURL = nil;
if(_addCredentialsToURL) {
NSURLCredential *credential = [self credentialForCurrentHost];
if(credential == nil) return nil;
theURL = [[self class] urlByAddingCredentials:credential toURL:_url];
if(theURL == nil) return nil;
} else {
theURL = _url;
}
theURL = [[self class] appendURL:theURL withGETParameters:_GETDictionary doApplyURLEncoding:_encodeGETDictionary];
if([_HTTPMethod isEqualToString:@"GET"]) {
if(_POSTDictionary || _rawPOSTData || [self.filesToUpload count] > 0 || [self.dataToUpload count] > 0) {
self.HTTPMethod = @"POST";
}
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theURL];
[request setHTTPMethod:_HTTPMethod];
if(globalIgnoreCache || _ignoreCache) {
request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
}
if(self.timeoutSeconds != 0.0) {
request.timeoutInterval = self.timeoutSeconds;
}
NSArray *cookies = [self requestCookies];
NSDictionary *d = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:d];
// escape POST dictionary keys and values if needed
if(_encodePOSTDictionary) {
NSMutableDictionary *escapedPOSTDictionary = _POSTDictionary ? [NSMutableDictionary dictionary] : nil;
[_POSTDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *k = [key st_stringByAddingRFC3986PercentEscapesUsingEncoding:self.POSTDataEncoding];
NSString *v = [[obj description] st_stringByAddingRFC3986PercentEscapesUsingEncoding:self.POSTDataEncoding];
[escapedPOSTDictionary setValue:v forKey:k];
}];
self.POSTDictionary = escapedPOSTDictionary;
}
// sort POST parameters in order to get deterministic, unit testable requests
NSArray *sortedPOSTDictionaries = [[self class] dictionariesSortedByKey:_POSTDictionary];
NSData *bodyData = nil;
if([self.filesToUpload count] > 0 || [self.dataToUpload count] > 0) {
NSString *boundary = @"----------kStHtTpReQuEsTbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
/**/
NSMutableData *mutableBodyData = [NSMutableData data];
for(STHTTPRequestFileUpload *fileToUpload in self.filesToUpload) {
NSData *data = [NSData dataWithContentsOfFile:fileToUpload.path];
if(data == nil) continue;
NSString *fileName = [fileToUpload.path lastPathComponent];
NSData *multipartData = [[self class] multipartContentWithBoundary:boundary
data:data
fileName:fileName
parameterName:fileToUpload.parameterName
mimeType:fileToUpload.mimeType];
[mutableBodyData appendData:multipartData];
}
/**/
for(STHTTPRequestDataUpload *dataToUpload in self.dataToUpload) {
NSData *multipartData = [[self class] multipartContentWithBoundary:boundary
data:dataToUpload.data
fileName:dataToUpload.fileName
parameterName:dataToUpload.parameterName
mimeType:dataToUpload.mimeType];
[mutableBodyData appendData:multipartData];
}
/**/
[sortedPOSTDictionaries enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *d = (NSDictionary *)obj;
NSString *key = [[d allKeys] lastObject];
NSObject *value = [[d allValues] lastObject];
[mutableBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[mutableBodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[mutableBodyData appendData:[[value description] dataUsingEncoding:NSUTF8StringEncoding]];
}];
/**/
[mutableBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:[NSString stringWithFormat:@"%u", (unsigned int)[bodyData length]] forHTTPHeaderField:@"Content-Length"];
bodyData = mutableBodyData;
} else if (_rawPOSTData) {
[request setValue:[NSString stringWithFormat:@"%u", (unsigned int)[_rawPOSTData length]] forHTTPHeaderField:@"Content-Length"];
bodyData = _rawPOSTData;
} else if (_POSTDictionary != nil) { // may be empty (POST request without body)
NSMutableString *contentTypeValue = [NSMutableString stringWithString:@"application/x-www-form-urlencoded"];
if(_encodePOSTDictionary) {
CFStringEncoding cfStringEncoding = CFStringConvertNSStringEncodingToEncoding(_POSTDataEncoding);
NSString *encodingName = (NSString *)CFStringConvertEncodingToIANACharSetName(cfStringEncoding);
if(encodingName) {
[contentTypeValue appendFormat:@"; charset=%@", encodingName];
}
}
[self setHeaderWithName:@"Content-Type" value:contentTypeValue];
NSMutableArray *ma = [NSMutableArray arrayWithCapacity:[_POSTDictionary count]];
[sortedPOSTDictionaries enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *d = (NSDictionary *)obj;
NSString *key = [[d allKeys] lastObject];
NSObject *value = [[d allValues] lastObject];
NSString *kv = [NSString stringWithFormat:@"%@=%@", key, value];
[ma addObject:kv];
}];
NSString *s = [ma componentsJoinedByString:@"&"];
bodyData = [s dataUsingEncoding:_POSTDataEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@"%u", (unsigned int)[bodyData length]] forHTTPHeaderField:@"Content-Length"];
}
if(bodyData) {
[request setHTTPBody:bodyData];
}
[_requestHeaders enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[request addValue:obj forHTTPHeaderField:key];
}];
NSURLCredential *credentialForHost = [self credentialForCurrentHost];
if(credentialForHost) {
NSString *authString = [NSString stringWithFormat:@"%@:%@", credentialForHost.user, credentialForHost.password];
NSData *authData = [authString dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64Encoding]];
[request addValue:authValue forHTTPHeaderField:@"Authorization"];
}
request.HTTPShouldHandleCookies = ([self cookieStoragePolicy] == STHTTPRequestCookiesStorageShared);
return request;
}
#pragma mark Upload
- (void)addFileToUpload:(NSString *)path parameterName:(NSString *)parameterName {
STHTTPRequestFileUpload *fu = [STHTTPRequestFileUpload fileUploadWithPath:path parameterName:parameterName];
[self.filesToUpload addObject:fu];
}
- (void)addDataToUpload:(NSData *)data parameterName:(NSString *)param {
STHTTPRequestDataUpload *du = [STHTTPRequestDataUpload dataUploadWithData:data parameterName:param mimeType:nil fileName:nil];
[self.dataToUpload addObject:du];
}
- (void)addDataToUpload:(NSData *)data parameterName:(NSString *)param mimeType:(NSString *)mimeType fileName:(NSString *)fileName {
STHTTPRequestDataUpload *du = [STHTTPRequestDataUpload dataUploadWithData:data parameterName:param mimeType:mimeType fileName:fileName];
[self.dataToUpload addObject:du];
}
#pragma mark Response
- (NSString *)responseString {
if(_responseString == nil) {
self.responseString = [self stringWithData:_responseData encodingName:_responseStringEncodingName];
}
return _responseString;
}
- (NSString *)stringWithData:(NSData *)data encodingName:(NSString *)encodingName {
if(data == nil) return nil;
if(_forcedResponseEncoding > 0) {
return [[NSString alloc] initWithData:data encoding:_forcedResponseEncoding];
}
NSStringEncoding encoding = NSUTF8StringEncoding;
/* try to use encoding declared in HTTP response headers */
if(encodingName != nil) {
encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)encodingName));
if(encoding == kCFStringEncodingInvalidId) {
encoding = NSUTF8StringEncoding; // by default
}
}
return [[NSString alloc] initWithData:data encoding:encoding];
}
#pragma mark HTTP Error Codes
+ (NSString *)descriptionForHTTPStatus:(NSUInteger)status {
NSString *s = [NSString stringWithFormat:@"HTTP Status %@", @(status)];
NSString *description = nil;
// http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
if(status == 400) description = @"Bad Request";
if(status == 401) description = @"Unauthorized";
if(status == 402) description = @"Payment Required";
if(status == 403) description = @"Forbidden";
if(status == 404) description = @"Not Found";
if(status == 405) description = @"Method Not Allowed";
if(status == 406) description = @"Not Acceptable";
if(status == 407) description = @"Proxy Authentication Required";
if(status == 408) description = @"Request Timeout";
if(status == 409) description = @"Conflict";
if(status == 410) description = @"Gone";
if(status == 411) description = @"Length Required";
if(status == 412) description = @"Precondition Failed";
if(status == 413) description = @"Payload Too Large";
if(status == 414) description = @"URI Too Long";
if(status == 415) description = @"Unsupported Media Type";
if(status == 416) description = @"Requested Range Not Satisfiable";
if(status == 417) description = @"Expectation Failed";
if(status == 422) description = @"Unprocessable Entity";
if(status == 423) description = @"Locked";
if(status == 424) description = @"Failed Dependency";
if(status == 425) description = @"Unassigned";
if(status == 426) description = @"Upgrade Required";
if(status == 427) description = @"Unassigned";
if(status == 428) description = @"Precondition Required";
if(status == 429) description = @"Too Many Requests";
if(status == 430) description = @"Unassigned";
if(status == 431) description = @"Request Header Fields Too Large";
if(status == 432) description = @"Unassigned";
if(status == 500) description = @"Internal Server Error";
if(status == 501) description = @"Not Implemented";
if(status == 502) description = @"Bad Gateway";
if(status == 503) description = @"Service Unavailable";
if(status == 504) description = @"Gateway Timeout";
if(status == 505) description = @"HTTP Version Not Supported";
if(status == 506) description = @"Variant Also Negotiates";
if(status == 507) description = @"Insufficient Storage";
if(status == 508) description = @"Loop Detected";
if(status == 509) description = @"Unassigned";
if(status == 510) description = @"Not Extended";
if(status == 511) description = @"Network Authentication Required";
if(description) {
s = [s stringByAppendingFormat:@": %@", description];
}
return s;
}
#pragma mark Descriptions
- (NSString *)curlDescription {
NSMutableArray *ma = [NSMutableArray array];
[ma addObject:@"\U0001F300 curl -i"];
if([_HTTPMethod isEqualToString:@"GET"] == NO) { // GET is optional in curl
NSString *s = [NSString stringWithFormat:@"-X %@", _HTTPMethod];
[ma addObject:s];
}
// -u username:password
NSURLCredential *credential = [[self class] sessionAuthenticationCredentialsForURL:[self url]];
if(credential) {
NSString *s = [NSString stringWithFormat:@"-u \"%@:%@\"", credential.user, credential.password];
[ma addObject:s];
}
// -d "k1=v1&k2=v2" // POST, url encoded params
if(_POSTDictionary) {
NSMutableArray *postParameters = [NSMutableArray array];
[_POSTDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *s = [NSString stringWithFormat:@"%@=%@", key, obj];
[postParameters addObject:s];
}];
NSString *ss = [postParameters componentsJoinedByString:@"&"];
[ma addObject:[NSString stringWithFormat:@"-d \"%@\"", ss]];
}
if(_rawPOSTData) {
// try JSON
id jsonObject = [NSJSONSerialization JSONObjectWithData:_rawPOSTData options:NSJSONReadingMutableContainers error:nil];
if(jsonObject) {
NSString *jsonString = [[NSString alloc] initWithData:_rawPOSTData encoding:NSUTF8StringEncoding];
// [ma addObject:@"-X POST"];
[ma addObject:[NSString stringWithFormat:@"-d \'%@\'", jsonString]];
}
}
// -F "[email protected];type=image/gif,fil2.txt,fil3.html" // file upload
for(STHTTPRequestFileUpload *f in _filesToUpload) {
NSString *s = [NSString stringWithFormat:@"%@=@%@", f.parameterName, f.path];
[ma addObject:[NSString stringWithFormat:@"-F \"%@\"", s]];
}
// -H "X-you-and-me: yes" // extra headers
NSMutableDictionary *headers = [[_request allHTTPHeaderFields] mutableCopy];
// [headers removeObjectForKey:@"Cookie"];
NSMutableArray *headersStrings = [NSMutableArray array];
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *s = [NSString stringWithFormat:@"-H \"%@: %@\"", key, obj];
[headersStrings addObject:s];
}];
if([headersStrings count] > 0) {
[ma addObject:[headersStrings componentsJoinedByString:@" \\\n"]];
}
// url
NSURL *url = [_request URL] ? [_request URL] : _url;
[ma addObject:[NSString stringWithFormat:@"\"%@\"", url]];
return [ma componentsJoinedByString:@" \\\n"];
}
- (NSString *)debugDescription {
NSMutableString *ms = [NSMutableString string];
NSString *method = (self.POSTDictionary || [self.filesToUpload count] || [self.dataToUpload count]) ? @"POST" : @"GET";
[ms appendFormat:@"%@ %@\n", method, [_request URL]];
NSMutableDictionary *headers = [[_request allHTTPHeaderFields] mutableCopy];
if([headers count]) [ms appendString:@"HEADERS\n"];
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[ms appendFormat:@"\t %@ = %@\n", key, obj];
}];
NSArray *kvDictionaries = [[self class] dictionariesSortedByKey:_POSTDictionary];
if([kvDictionaries count]) [ms appendString:@"POST DATA\n"];
for(NSDictionary *kv in kvDictionaries) {
NSString *k = [[kv allKeys] lastObject];
NSString *v = [[kv allValues] lastObject];
[ms appendFormat:@"\t %@ = %@\n", k, v];
}
for(STHTTPRequestFileUpload *f in self.filesToUpload) {
[ms appendString:@"UPLOAD FILE\n"];
[ms appendFormat:@"\t %@ = %@\n", f.parameterName, f.path];
}
for(STHTTPRequestDataUpload *d in self.dataToUpload) {
[ms appendString:@"UPLOAD DATA\n"];
[ms appendFormat:@"\t %@ = [%u bytes]\n", d.parameterName, (unsigned int)[d.data length]];
}
return ms;
}
- (NSError *)errorDescribingRequestNonfulfillment {
if(_responseStatus < 400) return nil;
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey : [[self class] descriptionForHTTPStatus:_responseStatus],
@"headers": self.responseHeaders,
@"body": self.responseString
};
return [NSError errorWithDomain:NSStringFromClass([self class]) code:_responseStatus userInfo:userInfo];
}
#pragma mark Start Request
- (void)startAsynchronous {
NSAssert((self.completionBlock || self.completionDataBlock), @"a completion block is mandatory");
NSAssert(self.errorBlock, @"the error block is mandatory");
NSURLRequest *request = [self prepareURLRequest];
NSURLSessionConfiguration *sessionConfiguration = nil;
if(_useUploadTaskInBackground) {
NSString *backgroundSessionIdentifier = [[NSProcessInfo processInfo] globallyUniqueString];
if ([[NSURLSessionConfiguration class] respondsToSelector:@selector(backgroundSessionConfigurationWithIdentifier:)]) {
// iOS 8+
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:backgroundSessionIdentifier];
} else {
// iOS 7
sessionConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
}
} else {
sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
}
sessionConfiguration.allowsCellularAccess = YES;
NSString *containerIdentifier = _sharedContainerIdentifier ? _sharedContainerIdentifier : [[NSBundle mainBundle] bundleIdentifier];
sessionConfiguration.sharedContainerIdentifier = containerIdentifier;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
if(_useUploadTaskInBackground) {
NSString *fileName = [[NSProcessInfo processInfo] globallyUniqueString];
self.HTTPBodyFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName]];
[request.HTTPBody writeToURL:_HTTPBodyFileURL atomically:YES];
self.task = [session uploadTaskWithRequest:request fromFile:_HTTPBodyFileURL];
} else {
self.task = [session dataTaskWithRequest:request];
}
[_task resume];
self.request = [_task currentRequest];
self.requestHeaders = [[_request allHTTPHeaderFields] mutableCopy];
/**/
BOOL showDebugDescription = [[NSUserDefaults standardUserDefaults] boolForKey:@"STHTTPRequestShowDebugDescription"];
BOOL showCurlDescription = [[NSUserDefaults standardUserDefaults] boolForKey:@"STHTTPRequestShowCurlDescription"];
NSMutableString *logString = nil;
if(showDebugDescription || showCurlDescription) {
logString = [NSMutableString stringWithString:@"\n----------\n"];
}
if(showDebugDescription) {
[logString appendString:[self debugDescription]];
}
if(showDebugDescription && showCurlDescription) {
[logString appendString:@"\n"];
}
if(showCurlDescription) {
[logString appendString:[self curlDescription]];
}
if(showDebugDescription || showCurlDescription) {
[logString appendString:@"\n----------\n"];
}
if(logString) NSLog(@"%@", logString);
/**/
if(_task == nil) {
NSString *s = @"can't create task";
self.error = [NSError errorWithDomain:NSStringFromClass([self class])
code:0
userInfo:@{NSLocalizedDescriptionKey: s}];
self.errorBlock(self.error);
}
}
// TODO: rewrite synch requests without NSURLConnection
- (NSString *)startSynchronousWithError:(NSError **)e {
self.responseHeaders = nil;
self.responseStatus = 0;
NSURLRequest *request = [self prepareURLRequest];
NSURLResponse *urlResponse = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:e];
self.responseData = [NSMutableData dataWithData:data];
if([urlResponse isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)urlResponse;
self.responseHeaders = [httpResponse allHeaderFields];
self.responseStatus = [httpResponse statusCode];
self.responseStringEncodingName = [httpResponse textEncodingName];
}
self.responseString = [self stringWithData:_responseData encodingName:_responseStringEncodingName];
if(_responseStatus >= 400) {
if(e) *e = [self errorDescribingRequestNonfulfillment];
}
return _responseString;
}
- (void)cancel {
[_task cancel];
NSString *s = @"Connection was cancelled.";
self.error = [NSError errorWithDomain:NSStringFromClass([self class])
code:kSTHTTPRequestCancellationError
userInfo:@{NSLocalizedDescriptionKey: s}];
self.errorBlock(self.error);
}
+ (void)setBackgroundCompletionHandler:(void(^)(void))completionHandler forSessionIdentifier:(NSString *)sessionIdentifier {
if(sessionCompletionHandlersForIdentifier == nil) {
sessionCompletionHandlersForIdentifier = [NSMutableDictionary dictionary];
}
sessionCompletionHandlersForIdentifier[sessionIdentifier] = [completionHandler copy];
}
//+ (void(^)())backgroundCompletionHandlerForSessionIdentifier:(NSString *)sessionIdentifier {
// return sessionCompletionHandlersForIdentifier[sessionIdentifier];
//}
#pragma mark NSURLSessionDelegate
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if(strongSelf == nil) return;
if(error == nil) return; // normal session invalidation, no error
if(strongSelf.errorBlock) {
strongSelf.errorBlock(error);
}
});
}
#if DEBUG
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
// accept self-signed SSL certificates
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
} else {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
}
#endif
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session NS_AVAILABLE_IOS(7_0) {
dispatch_async(dispatch_get_main_queue(), ^{
void (^completionHandler)(void) = sessionCompletionHandlersForIdentifier[session.configuration.identifier];
if(completionHandler) {
completionHandler();
[sessionCompletionHandlersForIdentifier removeObjectForKey:session.configuration.identifier];
}
});
}
#pragma mark NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
newRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSURLRequest *))completionHandler {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
NSURLRequest *actualRequest = weakSelf.preventRedirections ? nil : request;
completionHandler(actualRequest);
});
}