Cocoa Source Code

A Simple HTTP Server

Download Xcode Project

What it is

The Xcode project of a simple web server. It demonstrates how to use NSFileHandle, NSSocketPort, and Cocoa notifications to write a simple server application. Core Foundation functions are used to create and parse HTTP messages. It also shows how to replace NSSocketPort with native Unix calls.

What it does

SimpleHTTPServer listens to port 50000, uses the path of the request-URL to construct a new URL by inserting "http://" at the beginning, loads this new URL and renders its contents to a PDF file. The PDF file is returned to the client.

How to use it

With SimpleHTTPServer runnning, open Safari 2.0 and type, for example, "http://localhost:50000/www.apple.com". Both the IP address of the connecting client as well as the path of the request-URL are picked up by SimpleHTTPServer, and the corresponding page is loaded into a web view. Once the web page is converted to PDF, the PDF data is sent back to Safari where it is displayed inside the browser window.

License

The source code of SimpleHTTPServer is licensed under the Creative Commons Attribution 2.5 License.

Contribute

If you have extended the source code and would like to publish your changes here as well, please contact us at info[at]culturedcode[dot]com.