2007-03-02 01:36:52 +00:00
|
|
|
//
|
|
|
|
// HTTPSource.m
|
|
|
|
// HTTPSource
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 3/1/07.
|
|
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "HTTPSource.h"
|
|
|
|
|
|
|
|
|
|
|
|
@implementation HTTPSource
|
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
- (BOOL)open:(NSURL *)url
|
2007-03-02 01:36:52 +00:00
|
|
|
{
|
2007-10-14 16:29:49 +00:00
|
|
|
_url = [url copy];
|
|
|
|
|
2007-10-14 18:12:15 +00:00
|
|
|
_responseReceived = NO;
|
2007-10-14 16:29:49 +00:00
|
|
|
_connectionFinished = NO;
|
|
|
|
_byteCount = 0;
|
|
|
|
_data = [[NSMutableData alloc] init];
|
|
|
|
_sem = [[Semaphore alloc] init];
|
|
|
|
|
2007-10-14 16:57:32 +00:00
|
|
|
[NSThread detachNewThreadSelector:@selector(doConnection) toTarget:self withObject:nil];
|
2007-10-14 17:31:20 +00:00
|
|
|
|
|
|
|
//Wait for a response.
|
|
|
|
while (!_responseReceived && !_connectionFinished) {
|
|
|
|
[_sem wait];
|
|
|
|
}
|
2007-10-14 16:29:49 +00:00
|
|
|
|
|
|
|
NSLog(@"Connection opened!");
|
|
|
|
|
|
|
|
return YES;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 16:57:32 +00:00
|
|
|
- (void)doConnection
|
2007-03-02 01:36:52 +00:00
|
|
|
{
|
2007-10-14 16:29:49 +00:00
|
|
|
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:_url];
|
|
|
|
|
|
|
|
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
|
|
|
|
|
|
|
|
[request release];
|
2007-10-14 16:37:06 +00:00
|
|
|
|
2007-10-14 16:57:32 +00:00
|
|
|
while (!_connectionFinished)
|
2007-10-14 16:37:06 +00:00
|
|
|
{
|
2007-10-14 16:57:32 +00:00
|
|
|
NSDate *date = [[NSDate alloc] init];
|
|
|
|
|
|
|
|
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:date];
|
|
|
|
|
|
|
|
[date release];
|
|
|
|
}
|
2007-10-14 16:29:49 +00:00
|
|
|
|
2007-10-14 16:57:32 +00:00
|
|
|
NSLog(@"Thread exit");
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 17:31:20 +00:00
|
|
|
- (NSString *)mimeType
|
2007-03-02 01:36:52 +00:00
|
|
|
{
|
2007-10-14 18:12:15 +00:00
|
|
|
NSLog(@"Returning mimetype! %@", _mimeType);
|
2007-10-14 17:31:20 +00:00
|
|
|
return _mimeType;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)seekable
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)seek:(long)position whence:(int)whence
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (long)tell
|
|
|
|
{
|
2007-10-14 16:29:49 +00:00
|
|
|
return _byteCount;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int)read:(void *)buffer amount:(int)amount
|
|
|
|
{
|
2007-10-14 16:29:49 +00:00
|
|
|
while (amount > [_data length] && !_connectionFinished) {
|
|
|
|
[_sem timedWait: 2];
|
2007-03-04 00:17:05 +00:00
|
|
|
}
|
2007-10-14 16:29:49 +00:00
|
|
|
|
|
|
|
NSLog(@"Read called!");
|
|
|
|
|
|
|
|
if (amount > [_data length])
|
|
|
|
amount = [_data length];
|
|
|
|
|
2007-10-14 17:31:20 +00:00
|
|
|
@synchronized (_data) {
|
|
|
|
[_data getBytes:buffer length:amount];
|
2007-10-14 16:29:49 +00:00
|
|
|
|
2007-10-14 17:31:20 +00:00
|
|
|
//Remove the bytes
|
|
|
|
[_data replaceBytesInRange:NSMakeRange(0, amount) withBytes:NULL length:0];
|
|
|
|
}
|
2007-10-14 16:29:49 +00:00
|
|
|
|
|
|
|
_byteCount += amount;
|
|
|
|
|
|
|
|
return amount;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
//Only called from thread.
|
|
|
|
- (void)cancel
|
2007-03-02 01:36:52 +00:00
|
|
|
{
|
2007-10-14 16:29:49 +00:00
|
|
|
NSLog(@"CANCEL!");
|
|
|
|
|
|
|
|
[_connection cancel];
|
|
|
|
_connectionFinished = YES;
|
|
|
|
|
|
|
|
[_sem signal];
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
- (void)close
|
2007-03-04 04:36:10 +00:00
|
|
|
{
|
2007-10-14 16:57:32 +00:00
|
|
|
NSLog(@"CLOSING HTTPSource!");
|
2007-10-14 16:37:06 +00:00
|
|
|
_connectionFinished = YES;
|
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
[_connection cancel];
|
|
|
|
[_connection release];
|
|
|
|
_connection = nil;
|
2007-10-13 07:09:46 +00:00
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
[_data release];
|
|
|
|
_data = nil;
|
|
|
|
|
2007-03-04 04:36:10 +00:00
|
|
|
[_url release];
|
2007-10-14 16:29:49 +00:00
|
|
|
_url = nil;
|
2007-10-14 18:12:15 +00:00
|
|
|
|
|
|
|
[_mimeType release];
|
|
|
|
_mimeType = nil;
|
2007-10-14 16:29:49 +00:00
|
|
|
|
|
|
|
[_sem release];
|
|
|
|
_sem = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
|
|
|
|
{
|
|
|
|
NSLog(@"Authentication cancelled");
|
|
|
|
[self cancel];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
NSLog(@"Connection failed: %@", error);
|
|
|
|
[self cancel];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
|
|
|
|
{
|
|
|
|
NSLog(@"Received authentication challenge. Canceling.");
|
|
|
|
[self cancel];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
|
|
|
|
{
|
2007-10-14 18:12:15 +00:00
|
|
|
_mimeType = [[response MIMEType] copy];
|
2007-10-14 17:31:20 +00:00
|
|
|
_responseReceived = YES;
|
2007-10-14 18:12:15 +00:00
|
|
|
|
|
|
|
NSLog(@"Received response: %@", _mimeType);
|
2007-03-04 04:36:10 +00:00
|
|
|
|
2007-10-14 17:31:20 +00:00
|
|
|
[_sem signal];
|
2007-03-04 04:36:10 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
|
2007-03-04 04:36:10 +00:00
|
|
|
{
|
2007-10-14 16:29:49 +00:00
|
|
|
NSLog(@"Received cache request");
|
|
|
|
|
|
|
|
//No caching an HTTP stream
|
|
|
|
return nil;
|
2007-03-04 04:36:10 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
|
2007-03-04 04:36:10 +00:00
|
|
|
{
|
2007-10-14 16:29:49 +00:00
|
|
|
NSLog(@"Received redirect");
|
|
|
|
|
|
|
|
//Redirect away
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
|
|
|
|
{
|
|
|
|
NSLog(@"Connection finished loading.");
|
|
|
|
|
|
|
|
_connectionFinished = YES;
|
|
|
|
|
|
|
|
[_sem signal];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
|
|
|
|
{
|
2007-10-14 17:31:20 +00:00
|
|
|
@synchronized (_data) {
|
|
|
|
[_data appendData:data];
|
|
|
|
}
|
|
|
|
|
2007-10-14 16:29:49 +00:00
|
|
|
[_sem signal];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSURL *)url
|
|
|
|
{
|
|
|
|
return _url;
|
2007-03-04 04:36:10 +00:00
|
|
|
}
|
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
+ (NSArray *)schemes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject:@"http"];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|