This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MCURLConnection.j
70 lines (58 loc) · 1.84 KB
/
MCURLConnection.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
/*
* MCURLConnection.j
* MCResource
*
* Created by Joachim Garth
* Copyright 2011, Monkey & Company UG (haftungsbeschränkt)
*
* This class is a subclass of CPURLConnection, in order to customize its behavior
* and gain access to facilities such as progress events
*
*/
@implementation MCURLConnection : CPURLConnection
{
}
// We override this method in order to send FormData objects, because of
// their smaller memory footprint when sending files,
// as well as gaining more access to event listeners on XMLHTTPRequest
- (void)start
{
_isCanceled = NO;
try
{
if([_delegate respondsToSelector:@selector(connection:progressDidChange:)])
{
var progressFunction = function(event)
{
if (event.lengthComputable)
{
var percentage = event.loaded / event.total;
[_delegate connection:self progressDidChange:percentage];
}
};
_HTTPRequest._nativeRequest.addEventListener("progress", progressFunction, false);
_HTTPRequest._nativeRequest.upload.addEventListener("progress", progressFunction, false);
}
_HTTPRequest.open([_request HTTPMethod], [[_request URL] absoluteString], YES);
_HTTPRequest.onreadystatechange = function() { [self _readyStateDidChange]; }
var fields = [_request allHTTPHeaderFields],
key = nil,
keys = [fields keyEnumerator];
while (key = [keys nextObject])
_HTTPRequest.setRequestHeader(key, [fields objectForKey:key]);
if([_request respondsToSelector:@selector(formData)] && [_request formData])
{
_HTTPRequest.send([_request formData]);
}
else
{
_HTTPRequest.send([_request HTTPBody]);
}
}
catch (anException)
{
if ([_delegate respondsToSelector:@selector(connection:didFailWithError:)])
[_delegate connection:self didFailWithError:anException];
}
}
@end