HTTPSVR: Demonstrates World Wide Web HTTP Server

Click to open or copy the HTTPSVR project files.

HTTPSVR is an MFC sample that demonstrates the use of MFC and its Windows Sockets (WinSock) classes in implementing a simple World Wide Web HTTP server. HTTPSVR turns any computer connected to a network into a publishing platform viewable from any of a number of web browsers available on the market today, such as Microsoft's Internet Explorer.

HTTPSVR also minimally supports the Common Gateway Interface (CGI). You can create forms and execute CGI-compatible server-side applications using the standard hypertext markup language (HTML) tags.

To increase performance, HTTPSVR checks a file's last modified date and compares it to the date sent by the browser in the HTTP If-Modified-Since field. Most browsers will send this field if they already have a copy of the file in their cache. If the file has not been modified since the date of the cached copy, it will send the browser a Not Modified status code instead of downloading the entire file again.

Running the Sample

When the sample starts, it presents an empty common control list view and begins listening for HTTP requests from the network. The root folder of the server defaults to WebPages on the same drive as the executable the first time you run it. If the root folder does not exist, HTTPSVR asks if you want to create it. If you don't like the default name, you can change it using the edit control provided. HTTPSVR gets and saves its settings from a special configuration file with an HSC extension. It is recommended that you save changes you make to the default configuration by clicking Save on the Server menu, then use the resulting HSC file to start HTTPSVR after that.

Once HTTPSVR is started, all files and folders under the root folder will be visible to anyone on the network with a web browser, unless the file's Hidden attribute is set. If someone requests to view a folder under your root, HTTPSVR will first check to see if it contains a default file. Default filenames are Default.html, Default.htm, index.html, and index.htm. You can make other names default filenames by modifying the string resource, IDS_DEFAULTDOC. If a file with one of the default names is in the folder requested by the browser, HTTPSVR will redirect the browser to that file. Otherwise, HTTPSVR will create an FTP-like directory listing of all the files in the folder. Unless you want everyone to be able to view the contents of your folders in this manner, it is recommended that every folder contain an HTML file with one of the default names.

You should place your HTML documents in logical subfolders for easy retrieval and maintenance. One subfolder you should have under your root is SvrAdmin. The HTTPSVR sample contains a SvrAdmin subfolder. Copy all the .gif files from HTTPSVR's SvrAdmin to the SvrAdmin subfolder under your root. When the server needs to create a directory listing (see previous paragraph), it will try to use the icons from the SvrAdmin subfolder if the Display Icons option is set. The SvrAdmin subfolder is not necessary. If it does not exist, HTTPSVR will not attempt to put the icons in any listings.

The main window will list all documents, folders, and returned status codes that are accessed by someone browsing the web. When the view is in Details mode, it will display the total number of times each file has been downloaded and some additional information related to the last time the file was accessed: the time, the server that referred it, and by what Uniform Resource Locator (URL) it was accessed.

HTPSVR is an SDI application, and multiple instances of it can be running at one time provided they are all operating on different ports. Only one server can be running on a single port at one time. You set the port of your server on the Server Name page of the server's property sheet. The default is 80, and it can be any number between 1 and 63535. To access a web server with any port other than 80, use the colon syntax for the server's name: http://www.cochin.com:1008 would refer to a server listening on port 1008. HTTPSVR's title bar will reflect the correct syntax needed to access the server.