2009-03-05 08:01:36 +00:00
|
|
|
//
|
|
|
|
// HTTPSource.m
|
|
|
|
// HTTPSource
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 3/1/07.
|
|
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "HTTPSource.h"
|
2009-03-07 09:25:13 +00:00
|
|
|
#import "HTTPConnection.h"
|
2009-03-05 08:01:36 +00:00
|
|
|
|
|
|
|
@implementation HTTPSource
|
|
|
|
|
|
|
|
- (BOOL)open:(NSURL *)url
|
|
|
|
{
|
2009-03-07 09:25:13 +00:00
|
|
|
_connection = [[HTTPConnection alloc] initWithURL:url];
|
2009-03-05 08:01:36 +00:00
|
|
|
|
2009-03-07 10:11:33 +00:00
|
|
|
// Note: The User-Agent CANNOT contain the string "Mozilla" or Shoutcast/Icecast will serve up HTML
|
2009-03-07 09:25:13 +00:00
|
|
|
NSString *userAgent = [NSString stringWithFormat:@"Cog %@", [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]];
|
|
|
|
[_connection setValue:userAgent forRequestHeader:@"User-Agent"];
|
|
|
|
[_connection setValue:@"close" forRequestHeader:@"Connection"];
|
|
|
|
[_connection setValue:@"*/*" forRequestHeader:@"Accept"];
|
2009-03-05 08:01:36 +00:00
|
|
|
|
2009-03-07 09:25:13 +00:00
|
|
|
BOOL success = [_connection connect];
|
|
|
|
if (NO == success) {
|
|
|
|
return NO;
|
2009-03-05 08:01:36 +00:00
|
|
|
}
|
2009-03-07 09:25:13 +00:00
|
|
|
|
|
|
|
_mimeType = [[_connection valueForResponseHeader:@"Content-type"] copy];
|
2009-03-05 08:01:36 +00:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)mimeType
|
|
|
|
{
|
|
|
|
NSLog(@"Returning mimetype! %@", _mimeType);
|
|
|
|
return _mimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)seekable
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)seek:(long)position whence:(int)whence
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (long)tell
|
|
|
|
{
|
|
|
|
return _byteCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)read:(void *)buffer amount:(int)amount
|
|
|
|
{
|
|
|
|
int totalRead = 0;
|
|
|
|
|
|
|
|
while (totalRead < amount) {
|
2009-03-07 09:25:13 +00:00
|
|
|
NSInteger amountReceived = [_connection receiveData:((uint8_t *)buffer) + totalRead amount:amount - totalRead];
|
|
|
|
if (amountReceived <= 0) {
|
|
|
|
break;
|
2009-03-07 05:58:50 +00:00
|
|
|
}
|
2009-03-05 08:01:36 +00:00
|
|
|
|
2009-03-07 09:25:13 +00:00
|
|
|
totalRead += amountReceived;
|
2009-03-05 08:01:36 +00:00
|
|
|
}
|
2009-03-07 05:58:50 +00:00
|
|
|
|
2009-03-05 08:01:36 +00:00
|
|
|
_byteCount += totalRead;
|
|
|
|
|
|
|
|
return totalRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
2009-03-07 09:25:13 +00:00
|
|
|
[_connection close];
|
|
|
|
[_connection release];
|
|
|
|
_connection = nil;
|
2009-03-05 08:01:36 +00:00
|
|
|
|
|
|
|
[_mimeType release];
|
|
|
|
_mimeType = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSURL *)url
|
|
|
|
{
|
2009-03-07 09:25:13 +00:00
|
|
|
return [_connection URL];
|
2009-03-05 08:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSArray *)schemes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject:@"http"];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|