objective-ccocoahttpcocoaasyncsocket

Add run-time information to CocoaHTTPServer using custom Response class


I have the following code to start my own Cocoa HTTP Server. In order to manually handle the responses I have created my custom HTTPConnection class named PictureHTTPConnection

self.httpServer = [[HTTPServer alloc]init];
self.httpServer.type = @"_http._tcp.";
self.httpServer.name = @"MyBonjour Name";
self.httpServer.connectionClass = [PictureHTTPConnection class];

The problem here is that the PictureHTTPConnection class needs some information in order to handle the HTTP connections. However, I only provide the class and therefore I don't have a reference to the instance. I could do something with global data but this is not very good programming practise.

The best way I can think of is setting the PictureHTTPConnection's delegate to the UIApplicationDelegate so that it can answer any callbacks. :-(


Solution

  • I had the same exact problem. To solve this, I changed the code of the HTTPServer class so that you can set it a delegate that will in turn be set on each HTTPConnection created. When the HTTPConnection has to handle the request I call the delegate. This is far from clean as well and I was seeking for a better solution.