-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathFBSDKGraphRequestConnection.m
More file actions
1004 lines (870 loc) · 40 KB
/
Copy pathFBSDKGraphRequestConnection.m
File metadata and controls
1004 lines (870 loc) · 40 KB
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
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKGraphRequestConnection+Internal.h"
#import "FBSDKAppEvents+Internal.h"
#import "FBSDKConstants.h"
#import "FBSDKCoreKit+Internal.h"
#import "FBSDKError.h"
#import "FBSDKErrorConfiguration.h"
#import "FBSDKGraphErrorRecoveryProcessor.h"
#import "FBSDKGraphRequest+Internal.h"
#import "FBSDKGraphRequestBody.h"
#import "FBSDKGraphRequestDataAttachment.h"
#import "FBSDKGraphRequestMetadata.h"
#import "FBSDKGraphRequestPiggybackManager.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKSettings+Internal.h"
#import "FBSDKURLConnection.h"
NSString *const FBSDKNonJSONResponseProperty = @"FACEBOOK_NON_JSON_RESULT";
// URL construction constants
static NSString *const kGraphURLPrefix = @"graph.";
static NSString *const kGraphVideoURLPrefix = @"graph-video.";
static NSString *const kBatchKey = @"batch";
static NSString *const kBatchMethodKey = @"method";
static NSString *const kBatchRelativeURLKey = @"relative_url";
static NSString *const kBatchAttachmentKey = @"attached_files";
static NSString *const kBatchFileNamePrefix = @"file";
static NSString *const kBatchEntryName = @"name";
static NSString *const kAccessTokenKey = @"access_token";
static NSString *const kSDK = @"ios";
static NSString *const kUserAgentBase = @"FBiOSSDK";
static NSString *const kBatchRestMethodBaseURL = @"method/";
static const NSTimeInterval kDefaultTimeout = 180.0;
static FBSDKErrorConfiguration *g_errorConfiguration;
// ----------------------------------------------------------------------------
// FBSDKGraphRequestConnectionState
typedef NS_ENUM(NSUInteger, FBSDKGraphRequestConnectionState)
{
kStateCreated,
kStateSerialized,
kStateStarted,
kStateCompleted,
kStateCancelled,
};
// ----------------------------------------------------------------------------
// Private properties and methods
@interface FBSDKGraphRequestConnection () <FBSDKURLConnectionDelegate, FBSDKGraphErrorRecoveryProcessorDelegate>
@property (nonatomic, retain) FBSDKURLConnection *connection;
@property (nonatomic, retain) NSMutableArray *requests;
@property (nonatomic) FBSDKGraphRequestConnectionState state;
@property (nonatomic, retain) FBSDKLogger *logger;
@property (nonatomic) unsigned long requestStartTime;
@end
// ----------------------------------------------------------------------------
// FBSDKGraphRequestConnection
@implementation FBSDKGraphRequestConnection
{
NSString *_overrideVersionPart;
FBSDKGraphRequestMetadata *_recoveringRequestMetadata;
FBSDKGraphErrorRecoveryProcessor *_errorRecoveryProcessor;
NSUInteger _expectingResults;
NSOperationQueue *_delegateQueue;
}
- (instancetype)init
{
if ((self = [super init])) {
_requests = [[NSMutableArray alloc] init];
_timeout = kDefaultTimeout;
_state = kStateCreated;
_logger = [[FBSDKLogger alloc] initWithLoggingBehavior:FBSDKLoggingBehaviorNetworkRequests];
}
return self;
}
- (void)dealloc
{
_connection.delegate = nil;
[_connection cancel];
}
#pragma mark - Public
- (void)addRequest:(FBSDKGraphRequest *)request
completionHandler:(FBSDKGraphRequestHandler)handler
{
[self addRequest:request completionHandler:handler batchEntryName:nil];
}
- (void)addRequest:(FBSDKGraphRequest *)request
completionHandler:(FBSDKGraphRequestHandler)handler
batchEntryName:(NSString *)name
{
NSDictionary *batchParams = (name)? @{kBatchEntryName : name } : nil;
[self addRequest:request completionHandler:handler batchParameters:batchParams];
}
- (void)addRequest:(FBSDKGraphRequest *)request
completionHandler:(FBSDKGraphRequestHandler)handler
batchParameters:(NSDictionary *)batchParameters
{
if (self.state != kStateCreated) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Cannot add requests once started or if a URLRequest is set"
userInfo:nil];
}
FBSDKGraphRequestMetadata *metadata = [[FBSDKGraphRequestMetadata alloc] initWithRequest:request
completionHandler:handler
batchParameters:batchParameters];
[self.requests addObject:metadata];
}
- (void)cancel
{
self.state = kStateCancelled;
[self.connection cancel];
self.connection = nil;
}
- (void)overrideVersionPartWith:(NSString *)version
{
if (![_overrideVersionPart isEqualToString:version]) {
_overrideVersionPart = [version copy];
}
}
- (void)start
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
g_errorConfiguration = [[FBSDKErrorConfiguration alloc] initWithDictionary:nil];
});
//optimistically check for updated server configuration;
g_errorConfiguration = [FBSDKServerConfigurationManager cachedServerConfiguration].errorConfiguration ?: g_errorConfiguration;
if (self.state != kStateCreated && self.state != kStateSerialized) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors
formatString:@"FBSDKGraphRequestConnection cannot be started again."];
return;
}
[FBSDKGraphRequestPiggybackManager addPiggybackRequests:self];
NSMutableURLRequest *request = [self requestWithBatch:self.requests timeout:_timeout];
self.state = kStateStarted;
[self logRequest:request bodyLength:0 bodyLogger:nil attachmentLogger:nil];
_requestStartTime = [FBSDKInternalUtility currentTimeInMilliseconds];
FBSDKURLConnectionHandler handler =
^(FBSDKURLConnection *connection,
NSError *error,
NSURLResponse *response,
NSData *responseData) {
[self completeFBSDKURLConnectionWithResponse:response
data:responseData
networkError:error];
};
FBSDKURLConnection *connection = [[FBSDKURLConnection alloc] initWithRequest:request
completionHandler:handler];
if (_delegateQueue) {
[connection setDelegateQueue:_delegateQueue];
}
connection.delegate = self;
self.connection = connection;
[connection start];
id<FBSDKGraphRequestConnectionDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(requestConnectionWillBeginLoading:)]) {
if (_delegateQueue) {
[_delegateQueue addOperationWithBlock:^{
[delegate requestConnectionWillBeginLoading:self];
}];
} else {
[delegate requestConnectionWillBeginLoading:self];
}
}
}
- (void)setDelegateQueue:(NSOperationQueue *)queue
{
_delegateQueue = queue;
}
#pragma mark - Private methods (request generation)
//
// Adds request data to a batch in a format expected by the JsonWriter.
// Binary attachments are referenced by name in JSON and added to the
// attachments dictionary.
//
- (void)addRequest:(FBSDKGraphRequestMetadata *)metadata
toBatch:(NSMutableArray *)batch
attachments:(NSMutableDictionary *)attachments
batchToken:(NSString *)batchToken
{
NSMutableDictionary *requestElement = [[NSMutableDictionary alloc] init];
if (metadata.batchParameters) {
[requestElement addEntriesFromDictionary:metadata.batchParameters];
}
if (batchToken) {
metadata.request.parameters[kAccessTokenKey] = batchToken;
[self registerTokenToOmitFromLog:batchToken];
}
NSString *urlString = [self urlStringForSingleRequest:metadata.request forBatch:YES];
requestElement[kBatchRelativeURLKey] = urlString;
requestElement[kBatchMethodKey] = metadata.request.HTTPMethod;
NSMutableArray *attachmentNames = [NSMutableArray array];
[metadata.request.parameters enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([FBSDKGraphRequest isAttachment:value]) {
NSString *name = [NSString stringWithFormat:@"%@%lu",
kBatchFileNamePrefix,
(unsigned long)[attachments count]];
[attachmentNames addObject:name];
attachments[name] = value;
}
}];
if ([attachmentNames count]) {
requestElement[kBatchAttachmentKey] = [attachmentNames componentsJoinedByString:@","];
}
[batch addObject:requestElement];
}
- (void)appendAttachments:(NSDictionary *)attachments
toBody:(FBSDKGraphRequestBody *)body
addFormData:(BOOL)addFormData
logger:(FBSDKLogger *)logger
{
[attachments enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
value = [FBSDKInternalUtility convertRequestValue:value];
if ([value isKindOfClass:[NSString class]]) {
if (addFormData) {
[body appendWithKey:key formValue:(NSString *)value logger:logger];
}
} else if ([value isKindOfClass:[UIImage class]]) {
[body appendWithKey:key imageValue:(UIImage *)value logger:logger];
} else if ([value isKindOfClass:[NSData class]]) {
[body appendWithKey:key dataValue:(NSData *)value logger:logger];
} else if ([value isKindOfClass:[FBSDKGraphRequestDataAttachment class]]) {
[body appendWithKey:key dataAttachmentValue:(FBSDKGraphRequestDataAttachment *)value logger:logger];
} else {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors formatString:@"Unsupported FBSDKGraphRequest attachment:%@, skipping.", value];
}
}];
}
//
// Serializes all requests in the batch to JSON and appends the result to
// body. Also names all attachments that need to go as separate blocks in
// the body of the request.
//
// All the requests are serialized into JSON, with any binary attachments
// named and referenced by name in the JSON.
//
- (void)appendJSONRequests:(NSArray *)requests
toBody:(FBSDKGraphRequestBody *)body
andNameAttachments:(NSMutableDictionary *)attachments
logger:(FBSDKLogger *)logger
{
NSMutableArray *batch = [[NSMutableArray alloc] init];
NSString *batchToken = nil;
for (FBSDKGraphRequestMetadata *metadata in requests) {
NSString *individualToken = [self accessTokenWithRequest:metadata.request];
BOOL isClientToken = [FBSDKSettings clientToken] && [individualToken hasSuffix:[FBSDKSettings clientToken]];
if (!batchToken &&
!isClientToken) {
batchToken = individualToken;
}
[self addRequest:metadata
toBatch:batch
attachments:attachments
batchToken:[batchToken isEqualToString:individualToken] ? nil : individualToken];
}
NSString *jsonBatch = [FBSDKInternalUtility JSONStringForObject:batch error:NULL invalidObjectHandler:NULL];
[body appendWithKey:kBatchKey formValue:jsonBatch logger:logger];
if (batchToken) {
[body appendWithKey:kAccessTokenKey formValue:batchToken logger:logger];
}
}
- (BOOL)_shouldWarnOnMissingFieldsParam:(FBSDKGraphRequest *)request
{
NSString *minVersion = @"2.4";
NSString *version = request.version;
if (!version) {
return YES;
}
if ([version hasPrefix:@"v"]) {
version = [version substringFromIndex:1];
}
NSComparisonResult result = [version compare:minVersion options:NSNumericSearch];
// if current version is the same as minVersion, or if the current version is > minVersion
return (result == NSOrderedSame) || (result == NSOrderedDescending);
}
// Validate that all GET requests after v2.4 have a "fields" param
- (void)_validateFieldsParamForGetRequests:(NSArray *)requests
{
for (FBSDKGraphRequestMetadata *metadata in requests) {
FBSDKGraphRequest *request = metadata.request;
if ([request.HTTPMethod.uppercaseString isEqualToString:@"GET"] &&
[self _shouldWarnOnMissingFieldsParam:request] &&
!request.parameters[@"fields"] &&
[request.graphPath rangeOfString:@"fields="].location == NSNotFound) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors
formatString:@"starting with Graph API v2.4, GET requests for /%@ should contain an explicit \"fields\" parameter", request.graphPath];
}
}
}
//
// Generates a NSURLRequest based on the contents of self.requests, and sets
// options on the request. Chooses between URL-based request for a single
// request and JSON-based request for batches.
//
- (NSMutableURLRequest *)requestWithBatch:(NSArray *)requests
timeout:(NSTimeInterval)timeout
{
FBSDKGraphRequestBody *body = [[FBSDKGraphRequestBody alloc] init];
FBSDKLogger *bodyLogger = [[FBSDKLogger alloc] initWithLoggingBehavior:_logger.loggingBehavior];
FBSDKLogger *attachmentLogger = [[FBSDKLogger alloc] initWithLoggingBehavior:_logger.loggingBehavior];
NSMutableURLRequest *request;
if (requests.count == 0) {
[[NSException exceptionWithName:NSInvalidArgumentException
reason:@"FBSDKGraphRequestConnection: Must have at least one request or urlRequest not specified."
userInfo:nil]
raise];
}
[self _validateFieldsParamForGetRequests:requests];
if ([requests count] == 1) {
FBSDKGraphRequestMetadata *metadata = [requests objectAtIndex:0];
NSURL *url = [NSURL URLWithString:[self urlStringForSingleRequest:metadata.request forBatch:NO]];
request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:timeout];
// HTTP methods are case-sensitive; be helpful in case someone provided a mixed case one.
NSString *httpMethod = [metadata.request.HTTPMethod uppercaseString];
[request setHTTPMethod:httpMethod];
[self appendAttachments:metadata.request.parameters
toBody:body
addFormData:[httpMethod isEqualToString:@"POST"]
logger:attachmentLogger];
} else {
// Find the session with an app ID and use that as the batch_app_id. If we can't
// find one, try to load it from the plist. As a last resort, pass 0.
NSString *batchAppID = [FBSDKSettings appID];
if (!batchAppID || batchAppID.length == 0) {
// The Graph API batch method requires either an access token or batch_app_id.
// If we can't determine an App ID to use for the batch, we can't issue it.
[[NSException exceptionWithName:NSInternalInconsistencyException
reason:@"FBSDKGraphRequestConnection: [FBSDKSettings appID] must be specified for batch requests"
userInfo:nil]
raise];
}
[body appendWithKey:@"batch_app_id" formValue:batchAppID logger:bodyLogger];
NSMutableDictionary *attachments = [[NSMutableDictionary alloc] init];
[self appendJSONRequests:requests
toBody:body
andNameAttachments:attachments
logger:bodyLogger];
[self appendAttachments:attachments
toBody:body
addFormData:NO
logger:attachmentLogger];
NSURL *url = [FBSDKInternalUtility facebookURLWithHostPrefix:kGraphURLPrefix path:nil queryParameters:nil defaultVersion:_overrideVersionPart error:NULL];
request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:timeout];
[request setHTTPMethod:@"POST"];
}
[request setHTTPBody:[body data]];
NSUInteger bodyLength = [[body data] length] / 1024;
[request setValue:[FBSDKGraphRequestConnection userAgent] forHTTPHeaderField:@"User-Agent"];
[request setValue:[FBSDKGraphRequestBody mimeContentType] forHTTPHeaderField:@"Content-Type"];
[request setHTTPShouldHandleCookies:NO];
[self logRequest:request bodyLength:bodyLength bodyLogger:bodyLogger attachmentLogger:attachmentLogger];
return request;
}
//
// Generates a URL for a batch containing only a single request,
// and names all attachments that need to go in the body of the
// request.
//
// The URL contains all parameters that are not body attachments,
// including the session key if present.
//
// Attachments are named and referenced by name in the URL.
//
- (NSString *)urlStringForSingleRequest:(FBSDKGraphRequest *)request forBatch:(BOOL)forBatch
{
request.parameters[@"format"] = @"json";
request.parameters[@"sdk"] = kSDK;
request.parameters[@"include_headers"] = @"false";
NSString *baseURL;
if (forBatch) {
baseURL = request.graphPath;
} else {
NSString *token = [self accessTokenWithRequest:request];
if (token) {
[request.parameters setValue:token forKey:kAccessTokenKey];
[self registerTokenToOmitFromLog:token];
}
NSString *prefix = kGraphURLPrefix;
// We special case a graph post to <id>/videos and send it to graph-video.facebook.com
// We only do this for non batch post requests
NSString *graphPath = [request.graphPath lowercaseString];
if ([[request.HTTPMethod uppercaseString] isEqualToString:@"POST"] &&
[graphPath hasSuffix:@"/videos"]) {
graphPath = [graphPath stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
NSArray *components = [graphPath componentsSeparatedByString:@"/"];
if ([components count] == 2) {
prefix = kGraphVideoURLPrefix;
}
}
baseURL = [[FBSDKInternalUtility facebookURLWithHostPrefix:prefix path:request.graphPath queryParameters:nil defaultVersion:request.version error:NULL] absoluteString];
}
NSString *url = [FBSDKGraphRequest serializeURL:baseURL
params:request.parameters
httpMethod:request.HTTPMethod];
return url;
}
#pragma mark - Private methods (response parsing)
- (void)completeFBSDKURLConnectionWithResponse:(NSURLResponse *)response
data:(NSData *)data
networkError:(NSError *)error
{
if (self.state != kStateCancelled) {
NSAssert(self.state == kStateStarted,
@"Unexpected state %lu in completeWithResponse",
(unsigned long)self.state);
self.state = kStateCompleted;
}
NSArray *results = nil;
_URLResponse = (NSHTTPURLResponse *)response;
if (response) {
NSAssert([response isKindOfClass:[NSHTTPURLResponse class]],
@"Expected NSHTTPURLResponse, got %@",
response);
NSInteger statusCode = _URLResponse.statusCode;
if (!error && [response.MIMEType hasPrefix:@"image"]) {
error = [FBSDKError errorWithCode:FBSDKGraphRequestNonTextMimeTypeReturnedErrorCode
message:@"Response is a non-text MIME type; endpoints that return images and other "
@"binary data should be fetched using NSURLRequest and NSURLConnection"];
} else {
results = [self parseJSONResponse:data
error:&error
statusCode:statusCode];
}
} else if (!error) {
error = [FBSDKError errorWithCode:FBSDKUnknownErrorCode
message:@"Missing NSURLResponse"];
}
if (!error) {
if ([self.requests count] != [results count]) {
error = [FBSDKError errorWithCode:FBSDKGraphRequestProtocolMismatchErrorCode
message:@"Unexpected number of results returned from server."];
} else {
[_logger appendFormat:@"Response <#%lu>\nDuration: %lu msec\nSize: %lu kB\nResponse Body:\n%@\n\n",
(unsigned long)[_logger loggerSerialNumber],
[FBSDKInternalUtility currentTimeInMilliseconds] - _requestStartTime,
(unsigned long)[data length],
results];
}
}
if (error) {
[_logger appendFormat:@"Response <#%lu> <Error>:\n%@\n%@\n",
(unsigned long)[_logger loggerSerialNumber],
[error localizedDescription],
[error userInfo]];
}
[_logger emitToNSLog];
[self completeWithResults:results networkError:error];
self.connection = nil;
}
//
// If there is one request, the JSON is the response.
// If there are multiple requests, the JSON has an array of dictionaries whose
// body property is the response.
// [{ "code":200,
// "body":"JSON-response-as-a-string" },
// { "code":200,
// "body":"JSON-response-as-a-string" }]
//
// In both cases, this function returns an NSArray containing the results.
// The NSArray looks just like the multiple request case except the body
// value is converted from a string to parsed JSON.
//
- (NSArray *)parseJSONResponse:(NSData *)data
error:(NSError **)error
statusCode:(NSInteger)statusCode;
{
// Graph API can return "true" or "false", which is not valid JSON.
// Translate that before asking JSON parser to look at it.
NSString *responseUTF8 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableArray *results = [[NSMutableArray alloc] init];;
id response = [self parseJSONOrOtherwise:responseUTF8 error:error];
if (responseUTF8 == nil) {
NSString *base64Data = [data length] != 0 ? [data base64EncodedStringWithOptions:0] : @"";
if (base64Data != nil) {
[FBSDKAppEvents logImplicitEvent:@"fb_response_invalid_utf8"
valueToSum:nil
parameters:nil
accessToken:nil];
}
}
NSDictionary *responseError = nil;
if (!response) {
if ((error != NULL) && (*error == nil)) {
*error = [self errorWithCode:FBSDKUnknownErrorCode
statusCode:statusCode
parsedJSONResponse:nil
innerError:nil
message:@"The server returned an unexpected response."];
}
} else if ([self.requests count] == 1) {
// response is the entry, so put it in a dictionary under "body" and add
// that to array of responses.
[results addObject:@{
@"code":@(statusCode),
@"body":response
}];
} else if ([response isKindOfClass:[NSArray class]]) {
// response is the array of responses, but the body element of each needs
// to be decoded from JSON.
for (id item in response) {
// Don't let errors parsing one response stop us from parsing another.
NSError *batchResultError = nil;
if (![item isKindOfClass:[NSDictionary class]]) {
[results addObject:item];
} else {
NSMutableDictionary *result = [((NSDictionary *)item) mutableCopy];
if (result[@"body"]) {
result[@"body"] = [self parseJSONOrOtherwise:result[@"body"] error:&batchResultError];
}
[results addObject:result];
}
if (batchResultError) {
// We'll report back the last error we saw.
*error = batchResultError;
}
}
} else if ([response isKindOfClass:[NSDictionary class]] &&
(responseError = [FBSDKTypeUtility dictionaryValue:response[@"error"]]) != nil &&
[responseError[@"type"] isEqualToString:@"OAuthException"]) {
// if there was one request then return the only result. if there were multiple requests
// but only one error then the server rejected the batch access token
NSDictionary *result = @{
@"code":@(statusCode),
@"body":response
};
for (NSUInteger resultIndex = 0, resultCount = self.requests.count; resultIndex < resultCount; ++resultIndex) {
[results addObject:result];
}
} else if (error != NULL) {
*error = [self errorWithCode:FBSDKGraphRequestProtocolMismatchErrorCode
statusCode:statusCode
parsedJSONResponse:results
innerError:nil
message:nil];
}
return results;
}
- (id)parseJSONOrOtherwise:(NSString *)utf8
error:(NSError **)error
{
id parsed = nil;
if (!(*error)) {
parsed = [FBSDKInternalUtility objectForJSONString:utf8 error:error];
// if we fail parse we attemp a reparse of a modified input to support results in the form "foo=bar", "true", etc.
// which is shouldn't be necessary since Graph API v2.1.
if (*error) {
// we round-trip our hand-wired response through the parser in order to remain
// consistent with the rest of the output of this function (note, if perf turns out
// to be a problem -- unlikely -- we can return the following dictionary outright)
NSDictionary *original = @{ FBSDKNonJSONResponseProperty : utf8 };
NSString *jsonrep = [FBSDKInternalUtility JSONStringForObject:original error:NULL invalidObjectHandler:NULL];
NSError *reparseError = nil;
parsed = [FBSDKInternalUtility objectForJSONString:jsonrep error:&reparseError];
if (!reparseError) {
*error = nil;
}
}
}
return parsed;
}
- (void)completeWithResults:(NSArray *)results
networkError:(NSError *)networkError
{
NSUInteger count = [self.requests count];
_expectingResults = count;
NSUInteger disabledRecoveryCount = 0;
for (FBSDKGraphRequestMetadata *metadata in self.requests) {
if ([metadata.request isGraphErrorRecoveryDisabled]) {
disabledRecoveryCount++;
}
}
BOOL isSingleRequestToRecover = (count - disabledRecoveryCount == 1);
[self.requests enumerateObjectsUsingBlock:^(FBSDKGraphRequestMetadata *metadata, NSUInteger i, BOOL *stop) {
id result = networkError ? nil : [results objectAtIndex:i];
NSError *resultError = networkError ?: [self errorFromResult:result request:metadata.request];
id body = nil;
if (!resultError && [result isKindOfClass:[NSDictionary class]]) {
NSDictionary *resultDictionary = (NSDictionary *)result;
body = [resultDictionary objectForKey:@"body"];
}
if (resultError && ![metadata.request isGraphErrorRecoveryDisabled] && isSingleRequestToRecover) {
_recoveringRequestMetadata = metadata;
_errorRecoveryProcessor = [[FBSDKGraphErrorRecoveryProcessor alloc] init];
if ([_errorRecoveryProcessor processError:resultError request:metadata.request delegate:self]) {
return;
}
}
[self processResultBody:body error:resultError metadata:metadata canNotifyDelegate:(networkError ? NO : YES)];
}];
if (networkError) {
if ([_delegate respondsToSelector:@selector(requestConnection:didFailWithError:)]) {
[_delegate requestConnection:self didFailWithError:networkError];
}
}
}
- (void)processResultBody:(NSDictionary *)body error:(NSError *)error metadata:(FBSDKGraphRequestMetadata *)metadata canNotifyDelegate:(BOOL)canNotifyDelegate
{
void (^clearToken)() = ^{
if (!(metadata.request.flags & FBSDKGraphRequestFlagDoNotInvalidateTokenOnError)) {
[FBSDKAccessToken setCurrentAccessToken:nil];
}
};
void (^finishAndInvokeCompletionHandler)() = ^{
NSDictionary *graphDebugDict = [body objectForKey:@"__debug__"];
if ([graphDebugDict isKindOfClass:[NSDictionary class]]) {
[self processResultDebugDictionary: graphDebugDict];
}
[metadata invokeCompletionHandlerForConnection:self withResults:body error:error];
if (--_expectingResults == 0) {
if (canNotifyDelegate && [_delegate respondsToSelector:@selector(requestConnectionDidFinishLoading:)]) {
[_delegate requestConnectionDidFinishLoading:self];
}
}
};
FBSDKSystemAccountStoreAdapter *adapter = [FBSDKSystemAccountStoreAdapter sharedInstance];
NSString *metadataTokenString = metadata.request.tokenString;
NSString *currentTokenString = [FBSDKAccessToken currentAccessToken].tokenString;
NSString *accountStoreTokenString = adapter.accessTokenString;
BOOL isAccountStoreLogin = [metadataTokenString isEqualToString:accountStoreTokenString];
if ([metadataTokenString isEqualToString:currentTokenString] || isAccountStoreLogin) {
NSInteger errorCode = [error.userInfo[FBSDKGraphRequestErrorGraphErrorCode] integerValue];
NSInteger errorSubcode = [error.userInfo[FBSDKGraphRequestErrorGraphErrorSubcode] integerValue];
if (errorCode == 190 || errorCode == 102) {
if (isAccountStoreLogin) {
if (errorSubcode == 460) {
// For iOS6, when the password is changed on the server, the system account store
// will continue to issue the old token until the user has changed the
// password AND _THEN_ a renew call is made. To prevent opening
// with an old token which would immediately be closed, we tell our adapter
// that we want to force a blocking renew until success.
adapter.forceBlockingRenew = YES;
} else {
[adapter renewSystemAuthorization:^(ACAccountCredentialRenewResult result, NSError *renewError) {
NSOperationQueue *queue = _delegateQueue ?: [NSOperationQueue mainQueue];
[queue addOperationWithBlock:^{
clearToken();
finishAndInvokeCompletionHandler();
}];
}];
return;
}
}
clearToken();
} else if (errorCode >= 200 && errorCode < 300) {
// permission error
[adapter renewSystemAuthorization:^(ACAccountCredentialRenewResult result, NSError *renewError) {
NSOperationQueue *queue = _delegateQueue ?: [NSOperationQueue mainQueue];
[queue addOperationWithBlock:finishAndInvokeCompletionHandler];
}];
return;
}
}
// this is already on the queue since we are currently in the NSURLConnection callback.
finishAndInvokeCompletionHandler();
}
- (void)processResultDebugDictionary:(NSDictionary *)dict
{
NSArray *messages = [FBSDKTypeUtility arrayValue:dict[@"messages"]];
if (![messages count]) {
return;
}
[messages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *messageDict = [FBSDKTypeUtility dictionaryValue:obj];
NSString *message = [FBSDKTypeUtility stringValue:messageDict[@"message"]];
NSString *type = [FBSDKTypeUtility stringValue:messageDict[@"type"]];
NSString *link = [FBSDKTypeUtility stringValue:messageDict[@"link"]];
if (!message || !type) {
return;
}
NSString *loggingBehavior = FBSDKLoggingBehaviorGraphAPIDebugInfo;
if ([type isEqualToString:@"warning"]) {
loggingBehavior = FBSDKLoggingBehaviorGraphAPIDebugWarning;
}
if (link) {
message = [message stringByAppendingFormat:@" Link: %@", link];
}
[FBSDKLogger singleShotLogEntry:loggingBehavior logEntry:message];
}];
}
- (NSError *)errorFromResult:(id)result request:(FBSDKGraphRequest *)request
{
if ([result isKindOfClass:[NSDictionary class]]) {
NSDictionary *errorDictionary = result[@"body"][@"error"];
if (errorDictionary) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"code"] forKey:FBSDKGraphRequestErrorGraphErrorCode];
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"error_subcode"] forKey:FBSDKGraphRequestErrorGraphErrorSubcode];
//"message" is preferred over error_msg or error_reason.
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"error_msg"] forKey:FBSDKErrorDeveloperMessageKey];
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"error_reason"] forKey:FBSDKErrorDeveloperMessageKey];
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"message"] forKey:FBSDKErrorDeveloperMessageKey];
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"error_user_title"] forKey:FBSDKErrorLocalizedTitleKey];
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"error_user_msg"] forKey:FBSDKErrorLocalizedDescriptionKey];
[FBSDKInternalUtility dictionary:userInfo setObject:errorDictionary[@"error_user_msg"] forKey:NSLocalizedDescriptionKey];
[FBSDKInternalUtility dictionary:userInfo setObject:result[@"code"] forKey:FBSDKGraphRequestErrorHTTPStatusCodeKey];
[FBSDKInternalUtility dictionary:userInfo setObject:result forKey:FBSDKGraphRequestErrorParsedJSONResponseKey];
FBSDKErrorRecoveryConfiguration *recoveryConfiguration = [g_errorConfiguration
recoveryConfigurationForCode:[userInfo[FBSDKGraphRequestErrorGraphErrorCode] stringValue]
subcode:[userInfo[FBSDKGraphRequestErrorGraphErrorSubcode] stringValue]
request:request];
if ([errorDictionary[@"is_transient"] boolValue]) {
userInfo[FBSDKGraphRequestErrorCategoryKey] = @(FBSDKGraphRequestErrorCategoryTransient);
} else {
[FBSDKInternalUtility dictionary:userInfo setObject:@(recoveryConfiguration.errorCategory) forKey:FBSDKGraphRequestErrorCategoryKey];
}
[FBSDKInternalUtility dictionary:userInfo setObject:recoveryConfiguration.localizedRecoveryDescription forKey:NSLocalizedRecoverySuggestionErrorKey];
[FBSDKInternalUtility dictionary:userInfo setObject:recoveryConfiguration.localizedRecoveryOptionDescriptions forKey:NSLocalizedRecoveryOptionsErrorKey];
FBSDKErrorRecoveryAttempter *attempter = [FBSDKErrorRecoveryAttempter recoveryAttempterFromConfiguration:recoveryConfiguration];
[FBSDKInternalUtility dictionary:userInfo setObject:attempter forKey:NSRecoveryAttempterErrorKey];
return [FBSDKError errorWithCode:FBSDKGraphRequestGraphAPIErrorCode
userInfo:userInfo
message:nil
underlyingError:nil];
}
}
return nil;
}
- (NSError *)errorWithCode:(FBSDKErrorCode)code
statusCode:(NSInteger)statusCode
parsedJSONResponse:(id)response
innerError:(NSError *)innerError
message:(NSString *)message {
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
userInfo[FBSDKGraphRequestErrorHTTPStatusCodeKey] = @(statusCode);
if (response) {
userInfo[FBSDKGraphRequestErrorParsedJSONResponseKey] = response;
}
if (innerError) {
userInfo[FBSDKGraphRequestErrorParsedJSONResponseKey] = innerError;
}
if (message) {
userInfo[FBSDKErrorDeveloperMessageKey] = message;
}
NSError *error = [[NSError alloc]
initWithDomain:FBSDKErrorDomain
code:code
userInfo:userInfo];
return error;
}
#pragma mark - Private methods (miscellaneous)
- (void)logRequest:(NSMutableURLRequest *)request
bodyLength:(NSUInteger)bodyLength
bodyLogger:(FBSDKLogger *)bodyLogger
attachmentLogger:(FBSDKLogger *)attachmentLogger
{
if (_logger.isActive) {
[_logger appendFormat:@"Request <#%lu>:\n", (unsigned long)_logger.loggerSerialNumber];
[_logger appendKey:@"URL" value:[[request URL] absoluteString]];
[_logger appendKey:@"Method" value:[request HTTPMethod]];
[_logger appendKey:@"UserAgent" value:[request valueForHTTPHeaderField:@"User-Agent"]];
[_logger appendKey:@"MIME" value:[request valueForHTTPHeaderField:@"Content-Type"]];
if (bodyLength != 0) {
[_logger appendKey:@"Body Size" value:[NSString stringWithFormat:@"%lu kB", (unsigned long)bodyLength / 1024]];
}
if (bodyLogger != nil) {
[_logger appendKey:@"Body (w/o attachments)" value:bodyLogger.contents];
}
if (attachmentLogger != nil) {
[_logger appendKey:@"Attachments" value:attachmentLogger.contents];
}
[_logger appendString:@"\n"];
[_logger emitToNSLog];
}
}
- (NSString *)accessTokenWithRequest:(FBSDKGraphRequest *)request
{
NSString *token = request.tokenString ?: request.parameters[kAccessTokenKey];
if (!token && !(request.flags & FBSDKGraphRequestFlagSkipClientToken) && [FBSDKSettings clientToken].length > 0) {
return [NSString stringWithFormat:@"%@|%@", [FBSDKSettings appID], [FBSDKSettings clientToken]];
}
return token;
}
- (void)registerTokenToOmitFromLog:(NSString *)token
{
if (![[FBSDKSettings loggingBehavior] containsObject:FBSDKLoggingBehaviorAccessTokens]) {
[FBSDKLogger registerStringToReplace:token replaceWith:@"ACCESS_TOKEN_REMOVED"];
}
}
+ (NSString *)userAgent
{
static NSString *agent = nil;
if (!agent) {
agent = [NSString stringWithFormat:@"%@.%@", kUserAgentBase, FBSDK_VERSION_STRING];
}
if ([FBSDKSettings userAgentSuffix]) {
return [NSString stringWithFormat:@"%@/%@", agent, [FBSDKSettings userAgentSuffix]];
}
return agent;
}
- (void)setConnection:(FBSDKURLConnection *)connection
{
if (_connection != connection) {
_connection.delegate = nil;
_connection = connection;
}
}
#pragma mark - FBSDKURLConnectionDelegate
- (void)facebookURLConnection:(FBSDKURLConnection *)connection
didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
id<FBSDKGraphRequestConnectionDelegate> delegate = [self delegate];
if ([delegate respondsToSelector:@selector(requestConnection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:)]) {
[delegate requestConnection:self
didSendBodyData:bytesWritten
totalBytesWritten:totalBytesWritten
totalBytesExpectedToWrite:totalBytesExpectedToWrite];
}
}
#pragma mark - FBSDKGraphErrorRecoveryProcessorDelegate
- (void)processorDidAttemptRecovery:(FBSDKGraphErrorRecoveryProcessor *)processor didRecover:(BOOL)didRecover error:(NSError *)error
{
if (didRecover) {
FBSDKGraphRequest *originalRequest = _recoveringRequestMetadata.request;
FBSDKGraphRequest *retryRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:originalRequest.graphPath
parameters:originalRequest.parameters
tokenString:[FBSDKAccessToken currentAccessToken].tokenString
version:originalRequest.version
HTTPMethod:originalRequest.HTTPMethod];
// prevent further attempts at recovery (i.e., additional retries).
[retryRequest setGraphErrorRecoveryDisabled:YES];
FBSDKGraphRequestMetadata *retryMetadata = [[FBSDKGraphRequestMetadata alloc] initWithRequest:retryRequest completionHandler:_recoveringRequestMetadata.completionHandler batchParameters:_recoveringRequestMetadata.batchParameters];
[retryRequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *retriedError) {
[self processResultBody:result error:retriedError metadata:retryMetadata canNotifyDelegate:YES];
_errorRecoveryProcessor = nil;
_recoveringRequestMetadata = nil;
}];
} else {
[self processResultBody:nil error:error metadata:_recoveringRequestMetadata canNotifyDelegate:YES];
_errorRecoveryProcessor = nil;
_recoveringRequestMetadata = nil;
}
}
#pragma mark - Debugging helpers
- (NSString *)description
{
NSMutableString *result = [NSMutableString stringWithFormat:@"<%@: %p, %lu request(s): (\n",
NSStringFromClass([self class]),
self,
(unsigned long)self.requests.count];
BOOL comma = NO;
for (FBSDKGraphRequestMetadata *metadata in self.requests) {
FBSDKGraphRequest *request = metadata.request;
if (comma) {
[result appendString:@",\n"];
}
[result appendString:[request description]];
comma = YES;
}
[result appendString:@"\n)>"];
return result;