Looking for a Free, Open-Source, .NET Web Server? Introducing CassiniBy Scott Mitchell
If you're a veteran ASP developer you're probably aware of a now defunct Microsoft product called Personal Web Server, or PWS. PWS was a simple Web server that ran on Windows 9x and Windows NT Client, and could be used to testing ASP pages. Of course the "server" OSes - Windows NT Server, Windows 2000, and Windows XP Pro - all contain Microsoft's IIS Web server, a production-grade product. While this is nice, if you're currently using Windows XP Home edition you may be startled to learn that there is no Web server designed for Windows XP Home - PWS won't install on Windows XP. Of course, you can install IIS using a hack described in this FAQ, but it's still just that - a hack.
Fortunately the ASP.NET team at Microsoft remedied this problem rather indirectly when they released the Web Matrix Project, a free ASP.NET editor that includes a mini-Web server called the Microsoft ASP.NET Web Matrix Web Server. This Web server could be used on Windows XP Home (as well as on Windows 2000 and all other versions of Windows XP) to test ASP.NET Web pages.
Very recently, the ASP.NET team at Microsoft released the complete source code (in C#) to the ASP.NET Web Matrix Web Server. With this source code, named Cassini, you can make changes to the Web server, or simply "plug in" the Web server into your own applications.
|As with the Web Matrix Project, Cassini requires as an operatig system either Windows 2000 or Windows XP; furthermore, the .NET Framework must also be installed. If you have problems building Cassini or running the Web server, be sure to ask your questions in the Cassini Forum.|
Some Quick Facts About Cassini
Here are a few germane facts about Cassini:
- Cassini is written 100% managed C# code.
- Cassini is free!
- By default, Cassini will only accept local requests (requests from
localhost). Hence, by installing and running Cassini you are not opening yourself up to Web server worms like Code Red. (Of course by altering the source code and recompiling you can have Cassini accept nonlocal requests.)
- The source code of Cassini is already in use in Microsoft's ASP.NET Web Matrix Project.
- Cassini requires the .NET Framework and Windows 2000 or Windows XP.
Dissecting the Source Code
Cassini's source is broken up into eight C# source files:
CassiniWebServerMain.cs- A WinForms class that displays a dialog box when the Web server is launched. The dialog box allows the user to provide some options (port number, virtual path, etc.) and then launches the actual Web server.
Server.cs- This class abstractly defines the Web server, with methods like
Restart(). It delegates all of "real" the work to the
Host.cs- sets up the socket that is responsible for listening for incoming requests on the specified port. When a request comes in, it is dispatched to the
Connection.cs- abstractly defines a connection; utilized when a socket connection is achieved. Essentially, it instructs the
Requestclass to read in the HTTP request made to the Web server.
Request.cs- handles the incoming HTTP request, parsing out the HTTP headers. The actual work of retrieving the requested file, processing it, and sending it back to the client is handled by the .NET Framework
Messages.cs- A helper class providing formatting for HTTP error messages.
ByteParse.cs- a simple helper class that is used to parse through a byte array.
ByteString.cs- a helper class that provides String-like methods and properties to a byte array.
The last three C# source files contain classes that are essentially just helper classes. The bulk of the "interesting" code is in the first five files.
Unfortunately the Cassini code is fairly undocumented. While each of the classes has a decent amount of header comments, explaining the purpose of the class at a high-level, there is nary a comment found within the actual methods of the classes.
Additionally, in order to understand the source code it is vital that you have at least a fuzzy understanding
of the syntax and semantics of sockets. To learn more about sockets be sure to check out the
documentation, which will give you the technical details of sockets in the .NET Framework. For
a more general, high-level look at what sockets are be sure to read the UNIX Socket
FAQ. The FAQ is quite long, but, if you are new to sockets, it would help to read through a lot
of the FAQs. While the source code and explanations may seem confusing, I think you'll find them helpful
to compare against when picking through the undocumented Cassini source.
Potential Uses for Cassini
Cassini is a great avenue for learning more about .NET, C#, and socket programming. In addition to this educational benefit, Cassini's source can be modified and extended to provide functionality not previously present. For example, imagine that your company has a data-driven Web site developed using ASP.NET. Now, what if you wanted to be able to ship this Web site on a CD? By shipping Cassini on the CD, you could simply serve the ASP.NET Web pages off the CD on a user's computer, without having to worry if they had IIS installed (granted, they would need to have the .NET Framework installed, and be using Windows 2000 or Windows XP...).
With Cassini, Microsoft has released the complete source code for the ASP.NET Web Matrix Project's built-in Web server out into the hands of developers who may wish to employ similar functionality in their own applications. Realize that Cassini is not a substitute for IIS - it lacks many important features of IIS (such as auditing/logging, support for SSL, etc.); rather, Cassini is aimed for developers that want a quick and easy way to test their ASP.NET Web pages, even on Windows XP Home.
The Cassini source code, which is relatively short and easy to follow, requires the .NET Framework and Windows 2000 or the one of the Windows XP family operating systems in order to run. Cassini can be downloaded for free from Microsoft's www.asp.net Web site. There's even a forum setup to discuss Cassini, ask any questions, etc.