cog/Plugins/HTTPSource/HTTPSource.m

104 lines
1.8 KiB
Matlab
Raw Normal View History

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"
#import "HTTPConnection.h"
2009-03-05 08:01:36 +00:00
#import "Logging.h"
2009-03-05 08:01:36 +00:00
@implementation HTTPSource
- (BOOL)open:(NSURL *)url
{
_connection = [[HTTPConnection alloc] initWithURL:url];
2009-03-05 08:01:36 +00:00
// Note: The User-Agent CANNOT contain the string "Mozilla" or Shoutcast/Icecast will serve up HTML
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
BOOL success = [_connection connect];
if (NO == success) {
return NO;
2009-03-05 08:01:36 +00:00
}
_mimeType = [[_connection valueForResponseHeader:@"Content-type"] copy];
2009-03-05 08:01:36 +00:00
return YES;
}
- (NSString *)mimeType
{
DLog(@"Returning mimetype! %@", _mimeType);
2009-03-05 08:01:36 +00:00
return _mimeType;
}
- (BOOL)seekable
{
return NO;
}
- (BOOL)seek:(long)position whence:(int)whence
{
return NO;
}
- (long)tell
{
return _byteCount;
}
- (long)read:(void *)buffer amount:(long)amount
2009-03-05 08:01:36 +00:00
{
long totalRead = 0;
2009-03-05 08:01:36 +00:00
while (totalRead < amount) {
NSInteger amountReceived = [_connection receiveData:((uint8_t *)buffer) + totalRead amount:amount - totalRead];
if (amountReceived <= 0) {
break;
}
2009-03-05 08:01:36 +00:00
totalRead += amountReceived;
2009-03-05 08:01:36 +00:00
}
2009-03-05 08:01:36 +00:00
_byteCount += totalRead;
return totalRead;
}
- (void)close
{
[_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
{
return [_connection URL];
2009-03-05 08:01:36 +00:00
}
+ (NSArray *)schemes
{
return [NSArray arrayWithObject:@"http"];
}
@end