//
//  NSURLResponse.m
//  mySTEP
//
//  Created by Dr. H. Nikolaus Schaller on Mon Jan 05 2004.
//  Copyright (c) 2004 DSITRI. All rights reserved.
//

// CODE NOT TESTED

#import <Foundation/Foundation.h>
#import <Foundation/NSURLResponse.h>


@implementation NSURLResponse

- (long long) expectedContentLength; { return _expectedContentLength; }
- (NSString *) MIMEType; { return _MIMEType; }
- (NSString *) suggestedFilename; { return @"filename"; }	// FIXME - make it based on MIMEType
- (NSString *) textEncodingName; { return _textEncodingName; }
- (NSURL *) URL; { return _URL; }

- (id) initWithURL:(NSURL *) URL
		  MIMEType:(NSString *) MIMEType
	expectedContentLength:(int) length 
  textEncodingName:(NSString *) name;
{
#if 0
	NSLog(@"initWithURL=%@ mime=%@ encoding=%@ len=%d", URL, MIMEType, name, length);
#endif
	if((self=[super init]))
		{
		_URL=[URL retain];
		_MIMEType=[MIMEType retain];
		_textEncodingName=[name retain];
		_expectedContentLength=length;
		}
	return self;
}

- (void) dealloc;
{
	[_URL release];
	[_MIMEType release];
	[_textEncodingName release];
	[super dealloc];
}

- (id) copyWithZone:(NSZone *) z;
{
	NSURLResponse *c=[isa allocWithZone:z];
	if(c)
		{
		c->_URL=[_URL retain];
		c->_MIMEType=[_MIMEType retain];
		c->_textEncodingName=[_textEncodingName retain];
		c->_expectedContentLength=_expectedContentLength;
		}
	return c;
}

- (void) encodeWithCoder:(NSCoder *) coder;
{
	NIMP;
}

- (id) initWithCoder:(NSCoder *) coder;
{
	return self;
}

@end

@implementation NSHTTPURLResponse

+ (NSString *) localizedStringForStatusCode:(int) code;
{
	return [NSString stringWithFormat:@"Status code %d", code];
}

- (id) _initWithURL:(NSURL *) url headerFields:(NSDictionary *) headers andStatusCode:(int) code;
{
	// FIXME: should have case-insensitive keys!!!
	NSString *len=[headers objectForKey:@"Content-length"];
	NSString *content=[headers objectForKey:@"Content-type"];
	// FIXME:
	NSArray *a=[content componentsSeparatedByString:@"; charset="];
	if((self=[super initWithURL:url
					   MIMEType:[a objectAtIndex:0]
		  expectedContentLength:[len length] > 0?[len intValue]:-1
			   textEncodingName:[a objectAtIndex:1]]))
		{
		// _expectedContentLength=[len length] > 0?[len longLongValue]:-1;
		_headerFields=[headers retain];
		_statusCode=code;
		}
	return self;
}

- (void) dealloc;
{
	[_headerFields release];
	[super dealloc];
}

- (NSString *) suggestedFilename; { return @"filename"; }	// FIXME - make it based on MIMEType

- (NSDictionary *) allHeaderFields;
{
	return _headerFields;
}

- (int) statusCode;
{
	return _statusCode;
}

@end
