Is that an HTTP server in your Cocoa application or are you just pleased to see me?

This is a little 30 second/1.4MB screencast showing a little idea I had to embed a simple HTTP server inside a Cocoa application. The screencast just shows an application hosting a basic HTML form that allows someone browsing to the application’s address to modify the applications defaults. Changes to the HTML form are reflected live in the Cocoa Application’s GUI.

This is pretty neat in its own right but isn’t too useful to the average Cocoa application. Still I can see a few types of applications making good use of this, for instance combined with Bonjour broadcasting this could become a great interface for controlling background daemons from any machine. Alternatively web editors like Sandvox could host their own mini web servers to provide a quick way of realistically previewing websites in Safari. Applications that edit hyper-linked data (mentioning no names) could benefit from hosting the data in a web server too.

Some interesting uses for the code would be to provide a “poor man’s RemoteDesktop” style application and use the Universal Access api to populate an HTML based alternative GUI for an application. I don’t think I have the time to code this hack but it would be interesting to see how useful it could be made to be. And for extra points write it as an InputManager and provide a HTML interface to every application (the author in no way encourages the use of dirty evil hacks written as Input Managers, Input Managers are of course the loadable bundle of the beast and should of course be avoided at all times because of course Apple (Apple be praised) knows best – even if you can do some really freaking cool things with hacks).

Over the next week or so I’ll try to release the code to the HTTP server. The code is actually rather old (written for 10.0 IIRC) having originally been written as a HTTP client library for a XML SOAP implementation I wrote that never saw the light of day (but may bundle with this code when I release it). The code was modular enough that it was pretty easy to turn the HTTP client code into HTTP server code. It consists of a bunch of ObjC socket wrappers (like SmallSocket) and HTTP classes themselves. I have since created a framework (ToxicWarped) that works in a similar way to Python’s Twisted library and will try to modify my code to use that and also CFSocket instead of my socket code.

And a quick warning, obviously this code could constitute a major security risk if the embedded in an application that exposes the wrong kind of information via HTTP. Also, the HTTP server is obviously no where near the quality/robustness of Apache or Lighttpd, if you actually need to serve real content from your Cocoa application then I am pretty sure my code isn’t enough.

This entry was posted in Default. Bookmark the permalink.
  • Carlo
    Hi. So can you please provide the the code for that application? Would be really appreciated!
  • Very early betas of NetNewsWire actually embedded an http server. This was before Lite 1.0 shipped.

    It handled just one type of request, a request to subscribe to a feed. The idea was that people could put a button on their site that linked to a url like http://127.0.0.1:5335/subscribe?url=http://some... -- and the request would then go to NetNewsWire on your desktop, which would subscribe to the feed. Easy one-click subscribing.

    It wasn't my idea -- Radio UserLand was already doing this, and we figured we had to do the same thing.

    (But then there was the issue of how to manage two aggregators that want to listen to the same port -- and then more apps that would come along and want to listen on that port too. I decided to remove the http server before it became something to fight over.)

    But anyway -- I always thought the idea was interesting, that connection between web page and the desktop. (It doesn't have to be feed-subscribing, of course.)
  • Maybe a job for a Sandvox plugin?
  • Great idea Jon -- I had actually considered something similar for Sandvox (though I had been thinking about custom URL schemes) but punted on it (for now at least) since webkit is so similar to viewing with Safari. Still, it would be cool just so people could see what their site would look like with another browser.

    Where I see the promise for your idea is for writing some interesting AJAX kinds of web applications in Cocoa.....
  • Yeah Python (and by extension PyObjC) is great for things like this. But I had the code anyway and just needed to polish it up a tad and whip the demo up.
  • I have done this quite a few times with PyObjC and Twisted.

    It is quite easy to embed Twisted into a Cocoa app via PyObjC. It is even possible to inject said solution into a Cocoa app that is already running.

    Because of the design of Twisted, the I/O happens on a thread, but the actual request handling happens in the main event loop. As a result, there is no need to deal with threading issues.

    Combined with Nevow and MochiKit, one can relatively easily develop rich web based clients to your Cocoa app's internals.

    I really should put a demo together.
  • Very cool idea - currently for Cocoa Apps which I want to have an HTML front-end for I've worked with a Python Server with an XML socket connection to the app. But being able to directly hook into Cocoa code would be very nice.
blog comments powered by Disqus