-
Notifications
You must be signed in to change notification settings - Fork 14
/
WLRemoteLink.j
854 lines (711 loc) · 23.1 KB
/
WLRemoteLink.j
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
/*
* WLRemoteLink.j
* Ratatosk
*
* Created by Alexander Ljungberg on November 25, 2009.
* Copyright 2009-11, WireLoad Inc. All rights reserved.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution. Neither the name of WireLoad Inc. nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
@import <Foundation/Foundation.j>
@typedef WLRemoteActionType
var SharedWLRemoteLink = nil,
DefaultBaseUrl = '/api/',
WLRemoteLinkRetryDelay = 10; // in seconds
WLRemoteLinkStateNormal = 0;
WLRemoteLinkStateAuthenticationError = 1;
WLRemoteLinkStateRequestFailureError = 2;
/*!
A link to the server side API where the RemoteObjects live. WLRemoteLink does the following:
* enqueues actions and performs them sequentially
* receives responses
* detects errors and retries actions
* detects expired authentication
* works together with RemoteObject to collect multiple `PATCH` / `PUT` operations into single ones
* allows special error state actions to run with priority
Queued actions are executed strictly in order to allow for actions to be
scheduled for objects which don't have PKs yet. E.g. you could enqueue an
action to `POST` an object A and then another to `PUT` an update to A. When the
`POST` finishes you receive the server side `PK` / `URI` for the new object and in the
`PUT` action's `remoteActionWillBegin` you can set the correct URI to put to.
The sequential nature of the queue means the POST has gone before the `PUT`.
Special "in error state" actions can move to the top of the queue during error
states. This allows 'session timed out' errors to be rectified with an action
which restores the session, after which any other scheduled actions will proceed
like normal.
*/
@implementation WLRemoteLink : CPObject
{
CPArray actionQueue;
CPString baseUrl @accessors;
int updateDelay;
CPTimer updateDelayTimer;
CPTimer retryTimer;
BOOL isDelayingAction;
CPDate lastSuccessfulSave @accessors;
BOOL shouldFlushActions @accessors;
BOOL hasSaveActions @accessors;
WLRemoteActionType saveActionType @accessors;
BOOL isAuthenticated @accessors;
BOOL _retryOneAction;
int state @accessors;
CPString authorizationHeader @accessors;
id delegate @accessors;
}
+ (void)setDefaultBaseURL:(CPString)anApiUrl
{
DefaultBaseUrl = anApiUrl;
}
+ (CPString)DefaultBaseURL
{
return DefaultBaseUrl;
}
/*!
Returns the singleton instance of the WLRemoteLink.
*/
+ (WLRemoteLink)sharedRemoteLink
{
if (!SharedWLRemoteLink)
SharedWLRemoteLink = [[WLRemoteLink alloc] init];
return SharedWLRemoteLink;
}
+ (CPSet)keyPathsForValuesAffectingIsInErrorState
{
return [CPSet setWithObjects:"state"];
}
- (id)init
{
if (self = [super init])
{
state = 0;
// Optimistically assume we're authenticated initially.
isAuthenticated = YES;
_retryOneAction = NO;
lastSuccessfulSave = [CPDate date];
shouldFlushActions = NO;
actionQueue = [];
baseUrl = DefaultBaseUrl;
saveActionType = WLRemoteActionPatchType;
}
return self;
}
- (BOOL)isSecure
{
var url = [CPURL URLWithString:baseUrl];
return [url scheme] == 'https';
}
/*!
Return true if the remote link is broken for any reason other than
not being authenticated.
*/
- (BOOL)isInErrorState
{
return state == WLRemoteLinkStateRequestFailureError;
}
- (CPString)urlWithSslIffNeeded:(CPString)aUrl
{
if (!aUrl)
return aUrl;
if ([self isSecure])
return aUrl.replace(new RegExp('^http:', 'i'), 'https:');
else
return aUrl.replace(new RegExp('^https:', 'i'), 'http:');
}
/*!
If the only action in the queue is of saveActionType, it will be delayed up
to aDelay seconds before being executed.
setUpdateDelay in combination with WLRemoteObject's policy to only keep one
save in the queue at any time enables multiple save operations, e.g. for
each character the user types, to be collected into single operations at
aDelay second intervals.
*/
- (void)setUpdateDelay:(int)aDelay
{
updateDelay = aDelay;
}
- (void)scheduleAction:(WLRemoteAction)action
{
// CPLog.info("Remote op scheduled: " + [action description]);
var i = [actionQueue count],
indexes = [CPIndexSet indexSetWithIndex:i];
[self willChange:CPKeyValueChangeInsertion valuesAtIndexes:indexes forKey:"actionQueue"];
[actionQueue addObject:action];
[self didChange:CPKeyValueChangeInsertion valuesAtIndexes:indexes forKey:"actionQueue"];
if ([action isSaveAction])
[self setHasSaveActions:YES];
//CPLog.info("Action queue: " + actionQueue);
// Limit the rate of actions to make sure the browser gets a chance to refresh
// the UI. This probably only really matters when running against localhost but
// it doesn't harm the general case that much.
window.setTimeout(function() {
[self maybeExecute];
}, 0.3);
}
- (void)unscheduleAction:(WLRemoteAction)anAction
{
var i = [actionQueue indexOfObject:anAction];
if (i == CPNotFound)
{
CPLog.warn("Unschedule unscheduled action "+anAction);
return;
}
var indexes = [CPIndexSet indexSetWithIndex:i];
[self willChange:CPKeyValueChangeRemoval valuesAtIndexes:indexes forKey:"actionQueue"];
[actionQueue removeObject:anAction];
[self didChange:CPKeyValueChangeRemoval valuesAtIndexes:indexes forKey:"actionQueue"];
if ([anAction isSaveAction])
[self _updateHasSaveActions];
}
- (void)setState:(int)aState
{
if (state === aState)
return;
state = aState;
// Maybe excute regardless of flag: there could be an 'error' action if YES, and if NO
// a previously blocked action may be startable.
[self maybeExecute];
}
- (void)setHasSaveActions:(BOOL)aFlag
{
if (hasSaveActions === aFlag)
return;
// If we go from having actions to not, then a save just completed.
// We'll bump it in the opposite case too: if there were no save actions
// before, we were obviously saved up. We bump so that the 'time since last
// save' doesn't show 'five hours' even that we are in fact saved up.
lastSuccessfulSave = [CPDate date];
hasSaveActions = aFlag;
}
/*!
When a remote action fails, leave it in queue for a retry.
*/
- (void)remoteActionDidFail:(WLRemoteAction)anAction dueToAuthentication:(BOOL)dueToAuthentication
{
CPLog.error("Action failed " + anAction);
// Ready the action for a later retry.
[anAction reset];
if (dueToAuthentication)
{
[self setState:WLRemoteLinkStateAuthenticationError];
if (isAuthenticated)
[self setIsAuthenticated:NO];
}
else
{
[self setState:WLRemoteLinkStateRequestFailureError];
// Schedule a retry.
if ([retryTimer isValid])
return; // ...but just one scheduled retry at a time is enough.
retryTimer = [CPTimer scheduledTimerWithTimeInterval:WLRemoteLinkRetryDelay target:self selector:@selector(retry:) userInfo:nil repeats:NO];
}
}
/*!
If in an error state, retry the first action in the queue.
*/
- (void)retry:(id)sender
{
if (state === WLRemoteLinkStateRequestFailureError)
{
_retryOneAction = YES;
[self maybeExecute];
}
}
- (void)remoteActionDidFinish:(WLRemoteAction)anAction
{
//CPLog.info("Remote op finished: "+[anAction description]);
var actionIndex = [actionQueue indexOfObject:anAction];
if (actionIndex == CPNotFound && ![anAction isLoginAction])
{
CPLog.error("Unscheduled action finished");
}
if (![anAction shouldRunInErrorState] && ![anAction isLogoutAction])
{
// The success of a normal action indicates we're logged in.
if (!isAuthenticated)
[self setIsAuthenticated:YES];
}
// Any successful action means the link is nominal.
[self setState:WLRemoteLinkStateNormal];
if (actionIndex !== CPNotFound && actionQueue[actionIndex] === anAction)
{
var indexes = [CPIndexSet indexSetWithIndex:actionIndex];
[self willChange:CPKeyValueChangeRemoval valuesAtIndexes:indexes forKey:"actionQueue"];
[actionQueue removeObjectAtIndex:actionIndex];
[self didChange:CPKeyValueChangeRemoval valuesAtIndexes:indexes forKey:"actionQueue"];
}
[self _updateHasSaveActions];
// CPLog.info("Action queue: "+actionQueue);
[self maybeExecute];
}
- (void)_updateHasSaveActions
{
var r = NO;
for (var i = 0, count = [actionQueue count]; i < count; i++)
{
var anotherAction = actionQueue[i];
if ([anotherAction isSaveAction])
{
r = YES;
break;
}
}
[self setHasSaveActions:r];
}
- (BOOL)willDelayAction
{
if (!updateDelay || shouldFlushActions)
return NO;
var nextAction = [self nextAction];
if ([nextAction isExecuting] || [nextAction isDone])
return NO;
// Are we actually done delaying?
if (updateDelayTimer && ![updateDelayTimer isValid])
return NO;
return actionQueue.length === 1 && [nextAction type] === saveActionType;
}
- (WLRemoteAction)nextAction
{
if (actionQueue.length == 0)
return nil;
var nextActionIndex = 0;
if (!_retryOneAction && [self isInErrorState])
{
while (nextActionIndex < actionQueue.length && ![actionQueue[nextActionIndex] shouldRunInErrorState])
nextActionIndex++;
if (nextActionIndex >= actionQueue.length)
{
/*
There is no action appropriate for the current error state to execute at this time.
*/
return nil;
}
}
return actionQueue[nextActionIndex];
}
- (void)maybeExecute
{
var nextAction = [self nextAction];
if (!nextAction)
{
[self setShouldFlushActions:NO];
return;
}
// This flag is 'used up'.
_retryOneAction = NO;
if ([nextAction isExecuting] || [nextAction isDone])
return;
if ([self willDelayAction])
{
if (updateDelayTimer === nil)
{
[self setIsDelayingAction:YES];
updateDelayTimer = [CPTimer scheduledTimerWithTimeInterval:updateDelay target:self selector:"_updateWasDelayed" userInfo:nil repeats:NO];
return;
}
if ([updateDelayTimer isValid])
return;
}
if (isDelayingAction)
[self setIsDelayingAction:NO];
/*
We could get here from a) the timer firing or b) entering a
circumstance where the delay timer is no longer suitable. In both
cases the timer should be removed.
*/
[updateDelayTimer invalidate];
updateDelayTimer = nil;
[nextAction execute];
}
- (void)_updateWasDelayed
{
// If a timer already exists, a new one will not be created. If the timer is invalid
// the action will be taken.
[updateDelayTimer invalidate];
[self maybeExecute];
}
- (CPArray)actionQueue
{
return actionQueue;
}
- (void)cancelAllActions
{
[[actionQueue copy] enumerateObjectsUsingBlock:function(anAction)
{
if (![anAction isStarted])
[anAction cancel];
}];
}
/*!
Return YES if the next action is being delayed.
*/
- (BOOL)isDelayingAction
{
return isDelayingAction;
}
- (void)setIsDelayingAction:(BOOL)aFlag
{
isDelayingAction = aFlag;
}
- (void)setShouldFlushActions:(BOOL)aFlag
{
if (aFlag === shouldFlushActions)
return;
shouldFlushActions = aFlag;
if (aFlag)
[self maybeExecute];
}
/*!
Connect to the remote server using the given request. Any link delegate will be given
a chance to make header modifications to set any authorisation details.
@param aContext an optional context object
*/
- (CPURLConnection)sendRequest:(CPURLRequest)aRequest withDelegate:(id)aDelegate context:(id)aContext
{
if (authorizationHeader)
[aRequest setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
if ([delegate respondsToSelector:@selector(remoteLink:willSendRequest:withDelegate:context:)])
[delegate remoteLink:self willSendRequest:aRequest withDelegate:aDelegate context:aContext];
return [CPURLConnection connectionWithRequest:aRequest delegate:aDelegate];
}
@end
/*
@global
@group WLRemoteActionType
*/
WLRemoteActionGetType = @"GET";
/*
@global
@group WLRemoteActionType
*/
WLRemoteActionPostType = @"POST";
/*
@global
@group WLRemoteActionType
*/
WLRemoteActionPutType = @"PUT";
/*
@global
@group WLRemoteActionType
*/
WLRemoteActionPatchType = @"PATCH";
/*
@global
@group WLRemoteActionType
*/
WLRemoteActionDeleteType = @"DELETE";
/*
For a later potential bitmask optimization.
var WLRemoteActionDelegate_remoteAction_willBegin = 1 << 0,
WLRemoteActionDelegate_remoteAction_didFinish = 1 << 1;
*/
var WLRemoteActionSerial = 1;
@implementation WLRemoteAction : CPObject
{
id _delegate;
long serial;
BOOL done;
WLRemoteLink link @accessors;
CPURLConnection connection;
WLRemoteActionType type @accessors;
CPString path @accessors;
CPDictionary payload @accessors;
BOOL shouldRunInErrorState @accessors;
BOOL _didCallWillBegin;
int statusCode @accessors;
JSObject result @accessors;
CPString message @accessors;
CPString error @accessors(readonly);
}
+ (WLRemoteAction)schedule:(WLRemoteActionType)aType path:(CPString)aPath delegate:(id)aDelegate message:(CPString)aMessage
{
var action = [[WLRemoteAction alloc] initWithType:aType path:aPath delegate:aDelegate message:aMessage];
[action schedule];
return action;
}
+ (CPString)urlEncode:(CPDictionary)data
{
var keys = [data keyEnumerator],
r = [[CPArray alloc] init],
key;
while (key = [keys nextObject])
{
var value = [data objectForKey:key];
value = value.replace(new RegExp('%', 'g'), '%25');
value = value.replace(new RegExp('&', 'g'), '%26');
value = value.replace(new RegExp('=', 'g'), '%3D');
[r addObject:[CPString stringWithFormat:@"%s=%s", key, value]];
}
return r.join("&");
}
- (id)initWithType:(WLRemoteActionType)aType path:(CPString)aPath delegate:(id)aDelegate message:(CPString)aMessage
{
if (self = [super init])
{
link = [WLRemoteLink sharedRemoteLink];
serial = WLRemoteActionSerial++;
shouldRunInErrorState = NO;
done = NO;
type = aType;
path = aPath;
message = aMessage;
payload = nil;
[self setDelegate:aDelegate];
}
return self;
}
- (void)schedule
{
[[self link] scheduleAction:self];
}
- (void)cancel
{
done = YES;
[[self link] unscheduleAction:self];
}
- (void)setDelegate:(id)aDelegate
{
if (_delegate === aDelegate)
return;
_delegate = aDelegate;
}
- (void)execute
{
if (!_didCallWillBegin && [_delegate respondsToSelector:@selector(remoteActionWillBegin:)])
{
[_delegate remoteActionWillBegin:self];
}
_didCallWillBegin = YES;
[self makeRequest];
}
- (BOOL)isStarted
{
return [self isExecuting] || [self isDone];
}
- (BOOL)isExecuting
{
return connection != nil;
}
- (BOOL)isDone
{
return done;
}
- (BOOL)isSaveAction
{
return type !== WLRemoteActionGetType;
}
/*!
Return true if this action causes the link to become authenticated.
*/
- (BOOL)isLoginAction
{
return NO;
}
/*!
Return true if success for this action indicates the user is no longer authenticated.
*/
- (BOOL)isLogoutAction
{
return NO;
}
/*!
Reset the action so that it can be retried. This message is sent after an action fails due to
a network or a server error and will need to be performed a second time.
*/
- (void)reset
{
connection = nil;
done = NO;
}
- (void)contentType
{
if ([_delegate respondsToSelector:@selector(remoteActionContentType:)])
return [_delegate remoteActionContentType:self];
else
return @"application/json; charset=utf-8";
}
- (CPString)encodeRequestBody:(Object)aPayload
{
if ([_delegate respondsToSelector:@selector(remoteAction:encodeRequestBody:)])
return [_delegate remoteAction:self encodeRequestBody:aPayload];
return [CPString JSONFromObject:aPayload];
}
- (Object)decodeResponseBody:(CPString)responseData
{
if ([_delegate respondsToSelector:@selector(remoteAction:decodeResponseBody:)])
return [_delegate remoteAction:self decodeResponseBody:responseData];
return [responseData objectFromJSON];
}
- (void)makeRequest
{
CPLog.info("makeRequest: " + self);
if (connection || done)
{
CPLog.error("Action fired twice without reset.");
return;
}
var request = [CPURLRequest requestWithURL:[self fullPath]],
contentType = [self contentType];
[request setHTTPMethod:type];
[request setValue:contentType forHTTPHeaderField:@"Accept"];
if (type == WLRemoteActionPostType || type == WLRemoteActionPutType || type == WLRemoteActionPatchType)
{
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
if (payload)
{
var convertedPayload = payload;
if (payload.isa && [payload isKindOfClass:CPDictionary])
{
convertedPayload = {};
var keyEnumerator = [payload keyEnumerator],
key;
while (key = [keyEnumerator nextObject])
{
var value = [payload objectForKey:key];
convertedPayload[key] = value;
}
}
try
{
[request setHTTPBody:[self encodeRequestBody:convertedPayload]];
}
catch(err)
{
// This indicates JSON.stringify or the delegate encoding method failed on the given object.
CPLog.error("Failed to convert payload: " + convertedPayload);
CPLog.error(err);
if (typeof(console) !== 'undefined')
console.log(convertedPayload);
[CPException raise:CPInvalidArgumentException reason:"Invalid entry payload."];
}
}
}
[self makeConnectionWithRequest:request];
}
/*!
Make the actual connection with a request created by makeRequest. This method
could be overriden to make advanced request modifications.
*/
- (CPURLConnection)makeConnectionWithRequest:(CPURLRequest)aRequest
{
connection = [[self link] sendRequest:aRequest withDelegate:self context:self];
}
- (void)connection:(CPURLConnection)aConnection didReceiveResponse:(CPURLResponse)aResponse
{
[self setStatusCode:200];
if ([aResponse class] == CPHTTPURLResponse)
{
var code = [aResponse statusCode];
[self setStatusCode:code];
if (code < 200 || code > 299)
CPLog.error("Received error code %d.", code);
}
}
- (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
{
if (done)
{
CPLog.error("Action received data twice");
return;
}
result = nil;
error = [self statusCode];
if (error === 0)
{
// Sometimes we get code 0 back. Often this means there was no response, e.g.
// no connection could be made. Simulate a 503 error in that case.
error = [aConnection _HTTPRequest].success() ? 200 : 503;
}
error = error >= 200 && error <= 299 ? nil : error; // 2XX codes are not errors.
// If the status code says there should be no data, expect no data.
if ([self statusCode] == 204)
{
if (data && data !== "OK")
CPLog.warn("Unexpected data in response: %@", data);
}
else
{
// Ok there's data.
try
{
result = [self decodeResponseBody:data];
}
catch(err)
{
CPLog.error("Unable to decode response (status code %d).", [self statusCode]);
error = 500;
}
}
[self finish];
}
- (void)connection:(CPURLConnection)aConnection didFailWithError:(CPString)anError
{
error = anError;
[self finish];
}
- (void)finish
{
connection = nil;
if (error)
{
if ([_delegate respondsToSelector:@selector(remoteActionDidFail:)])
[_delegate remoteActionDidFail:self];
// The delegate might have cancelled the action as a response to the failure.
if (done)
{
// Give the next action a chance to go.
[[WLRemoteLink sharedRemoteLink] maybeExecute];
return;
}
if (error == 401)
{
[[WLRemoteLink sharedRemoteLink] remoteActionDidFail:self dueToAuthentication:YES];
}
else
{
if (error !== 500 && error !== 503)
CPLog.error("Connection did fail with unknown error: " + error);
[[WLRemoteLink sharedRemoteLink] remoteActionDidFail:self dueToAuthentication:NO];
}
return;
}
done = YES;
if ([_delegate respondsToSelector:@selector(remoteActionDidFinish:)])
{
[_delegate remoteActionDidFinish:self];
}
[[WLRemoteLink sharedRemoteLink] remoteActionDidFinish:self];
}
- (CPString)fullPath
{
var baseUrlString = [[WLRemoteLink sharedRemoteLink] baseUrl] || @"/",
baseUrl = [CPURL URLWithString:baseUrlString];
return [[CPURL URLWithString:String(path || "") relativeToURL:baseUrl] absoluteString];
}
- (CPString)description
{
return "<WLRemoteAction " + serial + " " + type + " " + [self fullPath] + " " + payload + ">";
}
@end