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

#define ADDRESS @"localhost"
#define PORT    @"4281"

@interface MyDelegate : NSObject
- (BOOL) processRequest: (WebServerRequest*)request
               response: (WebServerResponse*)response
                    for: (WebServer*)server;
@end

@implementation MyDelegate

- (BOOL) processRequest: (WebServerRequest*)request
               response: (WebServerResponse*)response
                    for: (WebServer*)server
{
  NSString *httpMethod = [[request headerNamed: @"x-http-method"] value];
  if ([httpMethod caseInsensitiveCompare: @"GET"] != NSOrderedSame)
    {
      [response setHeader: @"HTTP"
                    value: @"405 Method Not Allowed"
               parameters: nil];
    }
  return YES;
}

@end

int
main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new];
  WebServer *server = [WebServer new];
  if ([server setAddress: ADDRESS port: PORT secure: nil])
    {
      MyDelegate *delegate = [MyDelegate new];
      [server setDelegate: delegate];
      [[NSRunLoop currentRunLoop] run];
      [server setDelegate: nil];
      [delegate release];
    }
  else
    {
      NSLog(@"failed to create web server for address: %@ port: %@",
            ADDRESS, PORT);
    }
  [server release];
  [pool release];
  return 0;
}

