Simple https web server as command line application for Mac in objective C

366 views Asked by At

I want to write a simple web server that handle https request. The ssl identity will be store in the keychain. I can read and write from there.

But how can I make it running forever application that wait for a http request and respond?

I am very new to Mac programming. Experience with iPhone and Java.

I tried CocoaHTTPServer, the samples are GUI applications. When I use the source in a command line project it run and exits. If I add a runloop, it stays. Is it the right way to use it?

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [DDLog addLogger:[DDTTYLogger sharedInstance]];
        HTTPServer *httpServer = [[HTTPServer alloc] init];

        [httpServer setPort:TCP_PORT_NUMBER];
        [httpServer setConnectionClass:[MyHTTPConnection class]];

        // Serve files from the standard Sites folder
        NSString *docRoot = [@"~/Sites" stringByExpandingTildeInPath];
        DDLogCInfo(@"Setting document root: %@", docRoot);

        [httpServer setDocumentRoot:docRoot];

        NSError *error = nil;
        if(![httpServer start:&error])
        {
            DDLogCError(@"Error starting HTTP Server: %@", error);
        }

         [[NSRunLoop currentRunLoop] run];

    }
    return 0;
}
0

There are 0 answers