HTTP Example Server

by Keith Vogel

The HTTPServer library abstracts the complexities of creating HTTP Web server hosting pages that can be written in almost any HTML editor. Once created, just copy your pages onto an SD card and plug it into the SD card reader on your chipKIT™ board. Restart the server. All links specified in the pages should be relative to the current page, or relative to the root of the SD file system. The default page is called HomePage.htm, and this page must exist at the root of the SD file system. All files on the SD file system must be limited to the 8.3 file naming convention; appropriate extensions should be used on your files. The SD file system can contain HTML pages, JPEGs, GIFs, ICOs, TXT, MPEGs, WMVs, and XMLs for download to the browser; however, only the three-letter extensions may be used for each file type. The content type specified to the requesting browser is determined by the three-letter file extension.

In addition to pages stored on the SD card, it is possible to add active pages that are dynamically created by writing a compose function and specifying the accessing URL to the server. These dynamic pages can respond to browser HTTP GETs, PUTs, or POSTs and can dynamically interact with the resources on the chipKIT board. One example is to create a dynamic page that talks to a camera that is connected to the board, and then have it take a picture and compose a JPEG picture. In designing your HTML pages you can use URLs that reference local SD files, dynamically created pages, or absolute URLs to other sites embedded in your HTML pages. For example, if you want to see some locally hosted pages on the SD card, checkout the page about The chipKIT MPIDE System. Or, you can go to another site like the Digilent products page at Digilent Inc.

The HTTP Server uses a highly cooperative embedded programming model where multiple connections and pages can be processed concurrently. To ensure this works reliably, any code written for the server’s loop() function or composing pages should be written as a state machine, with each state only doing a small portion of work. The HTTP server loops repeatedly, calling your code as well as the HTTP server code. As part of the HTTP server code, it listens for connections. When a connection is made, the appropriate compose() function is called based on the URL. If no URL matches a compose page, the default compose() function is called. Typically, the SD file system compose function is used as the default compose() function, as this will look on the SD file system for a matching file. If no file exists, the SD compose() function will return an “HTTP File Not Found (404)” error to the browser.

For specific information on how to set up this example server, goto SrvSetup.htm.