#import <Foundation/Foundation.h>
#import <HttpdKit/HttpParser.h>
#import <HttpdKit/HttpServer.h>

@implementation HttpServer
-(id) initOnPort: (int)myport
{
	port = myport;
	return self;
}

-(void) processClient: (id)client
{
	id parser = [[HttpParser alloc] init];
	char data[1500];
	int nparsed = 0;
	int *datalength;

	datalength = [client read: data maxLength: 1500];

	while(nparsed < datalength) {
		nparsed = [parser execute: data start: nparsed];
		if([parser is_finished]) {
			if(![[[parser request] header] valueForKey: @"REQUEST_PATH"]) {
				/* FIXME: parse REQUEST_URI for REQUEST_PATH */
			}

			if(![[[parser request] header] valueForKey: @"REQUEST_PATH"]) {
				/* Raise error */
			}

			// Resolve script name, path info and handlers from registered, based
			// on REQUEST_PATH

			// if there are handlerss {
				// set PATH_INFO and SCRIPT_NAME
				// set REMOTE_ADDR and REMOTE_HOST based on client

				if(![[parser request] httpBody]) {
					// If the connection broke (nil body), abort processing since the
					// user did.
					break;
				}

				// response = [[HttpResponse alloc] init];

				// for each handler, [handler processRequest: request withResponse:
				// response]; break if [request done] or [client closed]

				// if(![response done] and ![client closed]) {
					// [response finish];
				// }
			// } else {
				// Send 404 to client;
			// }
			break;
		} else {
			// append next chunk to data, continue parse
		}
	}
}
@end
