30 - (void)connectionDidReceiveAuthenticationChallenge:(
CPURLConnection)anURLConnection;
85 id <CPURLConnectionDelegate> _delegate;
87 BOOL _isLocalFileConnection;
89 HTTPRequest _HTTPRequest;
111 var aCFHTTPRequest =
new CFHTTPRequest();
112 aCFHTTPRequest.setWithCredentials([aRequest withCredentials]);
114 aCFHTTPRequest.open([aRequest HTTPMethod], [[aRequest URL] absoluteString], NO);
118 keys = [fields keyEnumerator];
120 while ((key = [keys nextObject]) !== nil)
121 aCFHTTPRequest.setRequestHeader(key, [fields objectForKey:key]);
123 aCFHTTPRequest.send([aRequest HTTPBody]);
125 if (!aCFHTTPRequest.success())
146 return [[
self alloc] _initWithRequest:aRequest queue:aQueue completionHandler:aHandler];
173 _delegate = aDelegate;
174 _operationQueue = nil;
175 _connectionOperation = nil;
177 [
self _initWithRequest:aRequest];
180 if (shouldStartImmediately)
189 _originalRequest = [aRequest
copy];
192 var URL = [_request URL],
193 scheme = [URL scheme];
196 _isLocalFileConnection = scheme ===
"file" ||
197 ((scheme ===
"http" || scheme ===
"https") &&
199 (window.location.protocol ===
"file:" || window.location.protocol ===
"app:"));
201 _HTTPRequest =
new CFHTTPRequest();
202 _HTTPRequest.setTimeout([aRequest timeoutInterval] * 1000);
203 _HTTPRequest.setWithCredentials([aRequest withCredentials]);
213 _operationQueue = aQueue;
214 _connectionOperation = [[_AsynchronousConnectionOperation alloc] initWithFunction:aHandler];
216 [
self _initWithRequest:aRequest];
245 _HTTPRequest.open([_request HTTPMethod], [[_request URL] absoluteString], YES);
247 _HTTPRequest.onreadystatechange =
function() { [
self _readyStateDidChange]; };
248 _HTTPRequest.ontimeout =
function() { [
self _didTimeout]; };
250 var fields = [_request allHTTPHeaderFields],
252 keys = [fields keyEnumerator];
254 while ((key = [keys nextObject]) !== nil)
255 _HTTPRequest.setRequestHeader(key, [fields objectForKey:key]);
257 _HTTPRequest.send([_request HTTPBody]);
261 [
self _sendDelegateDidFailWithError:anException];
265 - (void)_sendDelegateDidFailWithError:(
CPException)anException
267 if ([_delegate respondsToSelector:
@selector(connection:didFailWithError:)])
268 [_delegate connection:self didFailWithError:anException];
269 else if (_connectionOperation !== nil)
270 [
self _connectionOperationDidReceiveResponse:nil data:nil error:anException];
282 _HTTPRequest.abort();
284 if (_connectionOperation)
285 [_connectionOperation cancel];
293 - (BOOL)isLocalFileConnection
295 return _isLocalFileConnection;
304 reason:"The request timed out."
307 [
self _sendDelegateDidFailWithError:exception];
310 - (void)_readyStateDidChange
312 if (_HTTPRequest.readyState() === CFHTTPRequest.CompleteState && !_HTTPRequest.isTimeoutRequest())
314 var statusCode = _HTTPRequest.status(),
315 URL = [_request URL];
317 if (statusCode === 401 && [
CPURLConnectionDelegate respondsToSelector:
@selector(connectionDidReceiveAuthenticationChallenge:)])
323 if (_isLocalFileConnection)
328 [response _setStatusCode:statusCode];
329 [response _setAllResponseHeaders:_HTTPRequest.getAllResponseHeaders()];
332 if ([_delegate respondsToSelector:
@selector(connection:didReceiveResponse:)])
333 [_delegate connection:self didReceiveResponse:response];
337 if ([_delegate respondsToSelector:
@selector(connection:didReceiveData:)])
338 [_delegate connection:self didReceiveData:_HTTPRequest.responseText()];
339 else if (_connectionOperation !== nil)
340 [
self _connectionOperationDidReceiveResponse:response data:_HTTPRequest.responseText() error:nil];
342 if ([_delegate respondsToSelector:
@selector(connectionDidFinishLoading:)])
343 [_delegate connectionDidFinishLoading:self];
352 - (HTTPRequest)_HTTPRequest
359 [_connectionOperation _setResponse:aResponse data:aData error:anError];
362 [_operationQueue addOperation:_connectionOperation];
366 [_connectionOperation main];
373 @implementation _AsynchronousConnectionOperation :
CPOperation 375 BOOL _didReceiveResponse;
380 Function _operationFunction;
384 - (id)initWithFunction:(Function)aFunction
390 _didReceiveResponse = NO;
394 _operationFunction = aFunction;
402 _didReceiveResponse = YES;
403 _response = aResponse;
411 _operationFunction(_response, _data, _error);
417 return (_didReceiveResponse && [super isReady]);
426 _CPReportLenientDeprecation(
self, _cmd,
@selector(sendSynchronousRequest:returningResponse:));
431 - (HTTPRequest)_XMLHTTPRequest
433 _CPReportLenientDeprecation(
self, _cmd,
@selector(_HTTPRequest));
435 return [
self _HTTPRequest];
447 return _originalRequest;
463 return _connectionOperation;
Used to implement exception handling (creating & raising).
id initWithRequest:delegate:(CPURLRequest aRequest, [delegate] id< CPURLConnectionDelegate > aDelegate)
id initWithRequest:delegate:startImmediately:(CPURLRequest aRequest, [delegate] id< CPURLConnectionDelegate > aDelegate, [startImmediately] BOOL shouldStartImmediately)
The main run loop for the application.
A Cappuccino wrapper for any data type.
Provides loading of a URL request.
CPData sendSynchronousRequest:returningResponse:(CPURLRequest aRequest, [returningResponse]/*{ */CPURLResponse/*} */aURLResponse)
CPRunLoop currentRunLoop()
An immutable string (collection of characters).
Protocol agnostic information about a request to a specific URL.
CPData dataWithRawString:(CPString aString)
CPDate limitDateForMode:(CPString aMode)
Used for encapsulating, presenting, and recovery from errors.
HTTPRequest var CPURLConnectionDelegate
CPDictionary allHTTPHeaderFields()
Represents an operation that can be run in an CPOperationQueue.
Contains data obtained during a request made with CPURLConnection.
FrameUpdater prototype start
CPException exceptionWithName:reason:userInfo:(CPString aName, [reason] CPString aReason, [userInfo] CPDictionary aUserInfo)
Represents an operation queue that can run CPOperations.