#import #include #ifndef USECOCOA // build private NSURLConnection and NSURLProtocols based on mySTEP sources on top of Apple Foundation #if __APPLE__ #define NIMP #define SUBCLASS #define _mySTEP_H_NSPrivate #define NSURLConnection myNSURLConnection #define NSURLProtocol myNSURLProtocol #define NSURLProtocolClient myNSURLProtocolClient #define NSURLResponse myNSURLResponse #define NSHTTPURLResponse myHTTPNSURLResponse #define objc_realloc(buf, len) realloc(buf, len) #define objc_free(buf) free(buf) @class myNSURLConnection, myNSURLProtocol, myNSURLResponse, myHTTPNSURLResponse; @protocol myNSURLProtocolClient; #import "../../Foundation/Sources/NSURLConnection.h" #import "../../Foundation/Sources/NSURLConnection.m" #import "../../Foundation/Sources/NSURLProtocol.h" #import "../../Foundation/Sources/NSURLProtocol.m" #import "../../Foundation/Sources/NSURLResponse.h" #import "../../Foundation/Sources/NSURLResponse.m" #endif #endif @interface URLConnector : NSObject { BOOL done; } @end @implementation URLConnector - (void) start:(NSString *) url; { NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url]]; NSURLConnection *c=[NSURLConnection connectionWithRequest:request delegate:self]; NSLog(@"--- start ---"); while(!done) { // NSLog(@"loop for event"); if(![[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) { NSLog(@"*** no input sources for mode %@", NSDefaultRunLoopMode); return; } } NSLog(@"--- done ---"); } - (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error; { NSLog(@"connection: %@ didFailWithError: %@", connection, error); done=YES; } - (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response; { NSLog(@"connection: %@ didReceiveResponse: %@", connection, response); NSLog(@" status: %d", [(NSHTTPURLResponse *) response statusCode]); NSLog(@" all headers: %@", [(NSHTTPURLResponse *) response allHeaderFields]); NSLog(@" URL: %@", [response URL]); NSLog(@" textencoding: %@", [response textEncodingName]); NSLog(@" filename: %@", [response suggestedFilename]); NSLog(@" MIMEType: %@", [response MIMEType]); NSLog(@" length: %lld", [response expectedContentLength]); } - (void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data; { NSLog(@"connection: %@ didReceiveData: %p (%lu bytes)", connection, data, [data length]); } - (void) connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"connectionDidFinishLoading: %@", connection); done=YES; } - (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"connection: %@ didReceiveAuthenticationChallenge: %@", connection, challenge); } - (void) connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"connection: %@ didCancelAuthenticationChallenge: %@", connection, challenge); } - (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { NSLog(@"connection: %@ willCacheResponse: %@", connection, cachedResponse); NSLog(@" data: %p (%lu bytes)", [cachedResponse data], [[cachedResponse data] length]); NSLog(@" policy: %u", [cachedResponse storagePolicy]); NSLog(@" userinfo: %u", [cachedResponse userInfo]); return cachedResponse; } - (NSURLRequest *) connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse { NSLog(@"connection: %@ willSendRequest: %@ for redirect %@", connection, request, redirectResponse); return request; } @end void printfds() { int fd; for(fd=0; fd<20; fd++) { struct stat stat; if(fstat(fd, &stat) >= 0) NSLog(@"fd in use: %d", fd); } } int main(int argc, const char *argv[]) { NSAutoreleasePool *arp=[NSAutoreleasePool new]; NSLog(@"started"); NSLog(@"bundle identifier %@", [[NSBundle mainBundle] bundleIdentifier]); NSLog(@"process name %@", [[NSProcessInfo processInfo] processName]); NSLog(@"shared URL cache: %@", [NSURLCache sharedURLCache]); printfds(); #if 1 // NSURLConnection (synchronize in URLConnector) { NSString *random=[NSString stringWithFormat:@"http://localhost:8888/rand%lu.html", time(NULL)]; NSLog(@"*** NSURLConnection demo ***"); [[[URLConnector alloc] init] start:random]; [[[URLConnector alloc] init] start:random]; // again (should now be cached if it exists!) [[[URLConnector alloc] init] start:@"http://www.handheld-linux.com/index.html"]; [[[URLConnector alloc] init] start:@"http://www.handheld-linux.com/index.html"]; // again (from cache?) [[[URLConnector alloc] init] start:@"http://www.goldelico.de/intranet"]; [[[URLConnector alloc] init] start:@"http://www.quantum-step.com/swi/"]; [[[URLConnector alloc] init] start:@"http://www.quantum-stepppppp.com/swi/"]; [[[URLConnector alloc] init] start:@"http://www.handheld-linux.com/index.html"]; // and again (from cache?) } #endif #if 0 // NSURLConnection sendSynchronousRequest { NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888"]]; NSURLResponse *response; NSError *error; NSLog(@"*** NSURLConnection sendSynchronousRequest demo ***"); NSLog(@"request %@", request); NSLog(@"request allHTTPHeaderFields %@", [request allHTTPHeaderFields]); NSLog(@"data length %d", [[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] length]); NSLog(@"response %@", response); NSLog(@"error %@", error); request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/www.dsitri.de/index.html"]]; // reuse NSLog(@"request %@", request); NSLog(@"request allHTTPHeaderFields %@", [request allHTTPHeaderFields]); NSLog(@"data length %d", [[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] length]); NSLog(@"response %@", response); NSLog(@"error %@", error); request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.goldelico.com"]]; // external page NSLog(@"request %@", request); NSLog(@"request allHTTPHeaderFields %@", [request allHTTPHeaderFields]); NSLog(@"data length %d", [[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] length]); NSLog(@"response %@", response); NSLog(@"error %@", error); } #endif [arp release]; printfds(); exit(0); }