
#import <foundation/foundation.h>

#import <OmniFoundation/OmniHost.h>
#import <OmniFoundation/OmniSocketStream.h>
#import <OmniWeb/OWNetLocation.h>
#import <OmniWeb/OWURL.h>

#import "OWHTTPSProcessor.h"
#import "SSLSocket.h"

@implementation OWHTTPSProcessor

static const int DEFAULT_HTTPS_PORT = 443;

+ (int)defaultPort;
{
	return DEFAULT_HTTPS_PORT;
}

- (void)connect;
{
	OWNetLocation	*netLocation;
	NSString		*portString;
	SSLSocket		*sslSocket;
	int				port;
	
	[self setStatusFormat:@"Connecting to %@", [proxyURL netLocation]];

	sslSocket = [SSLSocket sslSocket];

	[sslSocket setReadBufferSize:32*1024];
	socketStream = [[OmniSocketStream alloc] initWithSocket:sslSocket];
	netLocation = [proxyURL parsedNetLocation];

	portString = [netLocation port];
	port = (portString != nil) ? [portString intValue] : [OWHTTPSProcessor defaultPort];

	[self setStatusFormat:@"Use Port '%d' for connection", port];

NS_DURING
	[sslSocket connectToHost:[OmniHost hostForHostname:[netLocation hostname]] port:port];
NS_HANDLER
	[self setStatus:[exception exceptionReason]];
	[exception raise];
NS_ENDHANDLER

	[self setStatus:@"Connected"];
}

@end
