00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPObject.j"
00024 @import "CPRunLoop.j"
00025 @import "CPURLRequest.j"
00026 @import "CPURLResponse.j"
00027
00028
00029 var XMLHTTPRequestUninitialized = 0,
00030 XMLHTTPRequestLoading = 1,
00031 XMLHTTPRequestLoaded = 2,
00032 XMLHTTPRequestInteractive = 3,
00033 XMLHTTPRequestComplete = 4;
00034
00035 var CPURLConnectionDelegate = nil;
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 @implementation CPURLConnection : CPObject
00081 {
00082 CPURLRequest _request;
00083 id _delegate;
00084 BOOL _isCanceled;
00085 BOOL _isLocalFileConnection;
00086
00087 XMLHTTPRequest _XMLHTTPRequest;
00088 }
00089
00090 + (void)setClassDelegate:(id)delegate
00091 {
00092 CPURLConnectionDelegate = delegate;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102 + (CPData)sendSynchronousRequest:(CPURLRequest)aRequest returningResponse:({CPURLResponse})aURLResponse error:({CPError})anError
00103 {
00104 try
00105 {
00106 var request = objj_request_xmlhttp();
00107
00108 request.open([aRequest HTTPMethod], [aRequest URL], NO);
00109
00110 var fields = [aRequest allHTTPHeaderFields],
00111 key = nil,
00112 keys = [fields keyEnumerator];
00113
00114 while (key = [keys nextObject])
00115 request.setRequestHeader(key, [fields objectForKey:key]);
00116
00117 request.send([aRequest HTTPBody]);
00118
00119 return [CPData dataWithString:request.responseText];
00120 }
00121 catch (anException)
00122 {
00123 }
00124
00125 return nil;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 + (CPURLConnection)connectionWithRequest:(CPURLRequest)aRequest delegate:(id)aDelegate
00135 {
00136 return [[self alloc] initWithRequest:aRequest delegate:aDelegate];
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 - (id)initWithRequest:(CPURLRequest)aRequest delegate:(id)aDelegate startImmediately:(BOOL)shouldStartImmediately
00147 {
00148 self = [super init];
00149
00150 if (self)
00151 {
00152 _request = aRequest;
00153 _delegate = aDelegate;
00154 _isCanceled = NO;
00155
00156 var path = [_request URL];
00157
00158
00159 _isLocalFileConnection = path.indexOf("file:") === 0 ||
00160 ((path.indexOf("http:") !== 0 || path.indexOf("https:") !== 0) &&
00161 window.location &&
00162 (window.location.protocol === "file:" || window.location.protocol === "app:"));
00163
00164 _XMLHTTPRequest = objj_request_xmlhttp();
00165
00166 if (shouldStartImmediately)
00167 [self start];
00168 }
00169
00170 return self;
00171 }
00172
00173 - (id)initWithRequest:(CPURLRequest)aRequest delegate:(id)aDelegate
00174 {
00175 return [self initWithRequest:aRequest delegate:aDelegate startImmediately:YES];
00176 }
00177
00178
00179
00180
00181 - (id)delegate
00182 {
00183 return _delegate;
00184 }
00185
00186
00187
00188
00189 - (void)start
00190 {
00191 _isCanceled = NO;
00192
00193 try
00194 {
00195 _XMLHTTPRequest.open([_request HTTPMethod], [_request URL], YES);
00196
00197 _XMLHTTPRequest.onreadystatechange = function() { [self _readyStateDidChange]; }
00198
00199 var fields = [_request allHTTPHeaderFields],
00200 key = nil,
00201 keys = [fields keyEnumerator];
00202
00203 while (key = [keys nextObject])
00204 _XMLHTTPRequest.setRequestHeader(key, [fields objectForKey:key]);
00205
00206 _XMLHTTPRequest.send([_request HTTPBody]);
00207 }
00208 catch (anException)
00209 {
00210 if ([_delegate respondsToSelector:@selector(connection:didFailWithError:)])
00211 [_delegate connection:self didFailWithError:anException];
00212 }
00213 }
00214
00215
00216
00217
00218 - (void)cancel
00219 {
00220 _isCanceled = YES;
00221
00222 try
00223 {
00224 _XMLHTTPRequest.abort();
00225 }
00226
00227 catch (anException)
00228 {
00229 }
00230 }
00231
00232 - (BOOL)isLocalFileConnection
00233 {
00234 return _isLocalFileConnection;
00235 }
00236
00237
00238 - (void)_readyStateDidChange
00239 {
00240 if (_XMLHTTPRequest.readyState == XMLHTTPRequestComplete)
00241 {
00242 var statusCode = _XMLHTTPRequest.status,
00243 URL = [_request URL];
00244
00245 if ([_delegate respondsToSelector:@selector(connection:didReceiveResponse:)])
00246 {
00247 if (_isLocalFileConnection)
00248 [_delegate connection:self didReceiveResponse:[[CPURLResponse alloc] initWithURL:URL]];
00249 else
00250 {
00251 var response = [[CPHTTPURLResponse alloc] initWithURL:URL];
00252
00253 [response _setStatusCode:statusCode];
00254
00255 [_delegate connection:self didReceiveResponse:response];
00256 }
00257 }
00258 if (!_isCanceled)
00259 {
00260 if (statusCode == 401 && [CPURLConnectionDelegate respondsToSelector:@selector(connectionDidReceiveAuthenticationChallenge:)])
00261 [CPURLConnectionDelegate connectionDidReceiveAuthenticationChallenge:self];
00262 else
00263 {
00264 if ([_delegate respondsToSelector:@selector(connection:didReceiveData:)])
00265 [_delegate connection:self didReceiveData:_XMLHTTPRequest.responseText];
00266 if ([_delegate respondsToSelector:@selector(connectionDidFinishLoading:)])
00267 [_delegate connectionDidFinishLoading:self];
00268 }
00269 }
00270 }
00271
00272 [[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
00273 }
00274
00275
00276 - (void)_XMLHTTPRequest
00277 {
00278 return _XMLHTTPRequest;
00279 }
00280
00281 @end