|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionMiniHttpd is a standalone HTTP and HTTP web server library. It can be used either as a web server, or as an HTTP listener in situations where deploying a full-blown web server such as IIS or Apache would not be feasible. One of the original motives for making MiniHttpd was to make a web interface for the Windows Media Player that I could use to control Media Player from my phone or Pocket PC and download tracks onto. One such program, called PlayerPal, as of version 3.0 uses the Cassini library which is similar in functionality but is no longer under development. Another motive was to make an alternative file server to use between my friends. It doesn't seem to matter how many ways there are to send files - MSN, IRC, FTP, email... sometimes none of them work! MiniHttpd is not complete, and additional features and improvements are sure to come. If you have any suggestions or problems, I'd be glad to hear them. If you're using MiniHttpd in the wild, I recommend isolating it in a separate OS account with limited privileges. As always, I appreciate bug finds - this is a one-man project for now, and there's only so much that I can do alone. Many thanks to those who've looked at the code. DownloadTo demonstrate the server, I've set up a MiniHttpd server on my friend's computer hosting the demo application binary. You can download the MiniHttpdApp here. Compared to other web serversHere is a feature comparison of MiniHttpd and other common web servers. This list is exhaustive only to the limit of my knowledge, so let me know if I'm missing anything important.
Using the codeBasic setupAt minimum, setting up a web server requires only a few lines of code. This example initializes a web server that uses your C drive as the root directory: // Create a server object on the
// default HTTP web port (80)
HttpWebServer server = new HttpWebServer();
// Set the root directory to C (don't try this at home)
server.Root = new DriveDirectory(@"c:\");
// Start the server
server.Start();
Console.ReadLine();
// Stop the server (note that you must stop the listener
// explicitly either by calling Stop or Dispose, or by
// wrapping the server in a 'using' block)
server.Stop();
And that's it! You can browse to your local address in your favorite browser and it should show you a directory listing or an index.htm/index.html page if there is one. You can specify a different listener port in the constructor or by setting the Virtual directoriesVirtual directories are directories that you can populate programmatically rather than by reflecting a physical directory. By default, an // Create a new virtual directory
// and set the root to it
VirtualDirectory root = new VirtualDirectory();
server.Root = root;
// Add a few files
root.AddFile("notepad.exe");
root.AddFile("autoexec.bat");
// Add a directory
root.AddDirectory(@"c:\windows");
// Remove a file
root.Remove(@"autoexec.bat");
// Add a virtual subdirectory
root.AddDirectory(new VirtualDirectory("virtual", root));
ASP.NET supportASPX is implemented as extensions of Unfortunately, there is no virtual ASPX directory because ASP.NET requires files to have absolute paths on-disk. I will keep an eye out for workarounds -- it'd be really nice to be able to store ASPX applications in assemblies. The ASPX code is contained in the MiniHttpd assembly to avoid cluttering distributions. If for any reason you don't want the ASPX at all, you can just exclude the Aspx folder from the project and recompile. Certain features are still not implemented in version 1.1; the two that I know are not yet implemented are HTTP handlers and HTTPS. Note that MiniHttpd leaves Bin folders with MiniHttpd.dll in whichever folder you use as an ASPX directory. MiniHttpd 1.2.0 onwards will remove the file upon disposal as well as the folder if it is empty. PHP supportPHP is implemented similar to ASPX, with There are three assemblies you need to use PHP with MiniHttpd:
Thanks to Daaron Dwyer for demonstrating PHP on MiniHttpd to me and doing the bulk of the work on MiniHttpd.Php! Index PageThe index page currently built into the library is a very boring list of files available in the folder. You can override this by replacing the Additionally, as of version 1.2.0, there's a slightly fancier index page called HttpServer
LoggingThe server writes log messages to the AuthenticationAs of version 1.2.0, MiniHttpd supports a very simple authentication (logins and passwords) through the
Authentication is primitive for the time being; access is server-wide. Also note that this is not HTTPS, so passwords and transactions are not encrypted. Thanks again to Daaron Dwyer for demonstrating LDAP and authentication on MiniHttpd. IDirectory and IFileAll objects in the
There isn't much ASPX couldn't do that Implementing IDirectoryImplementing
Be sure to call Implementing IFileImplementing Like Then you implement Last but not least, implement the An public class HelloWorldFile : IFile
{
public HelloWorldFile(string name, IDirectory parent)
{
this.name = name;
this.parent = parent;
}
string name;
IDirectory parent;
public void OnFileRequested(HttpRequest request,
IDirectory directory)
{
// Assign a MemoryStream to hold the response content.
request.Response.ResponseContent = new MemoryStream();
// Create a StreamWriter to which we
// can write some text, and write to it.
StreamWriter writer =
new StreamWriter(request.Response.ResponseContent);
writer.WriteLine("Hello, world!");
// Don't forget to flush!
writer.Flush();
}
public string ContentType
{
get { return ContentTypes.GetExtensionType(".txt"); }
}
public string Name
{
get { return name; }
}
public IDirectory Parent
{
get { return parent; }
}
public void Dispose()
{
}
}
Chunked ResponsesBecause HTTP normally requires that you provide the size of a resource in the header, MiniHttpd uses the MiniHttpdApp demo projectsMiniHttpdAppMiniHttpdApp is a web server sample application that demonstrates the MiniHttpd library. Although the GUI looks nearly the same, I've rewritten it from scratch for version 1.2.0 to address several crashing issues that were part of its crude design and lack of thread-safety consideration. It should now be stable enough to be a permanent resident of your Windows tray. Specify a port and optionally the host name if you are behind a router or other NAT device, and then click Server -> Start. There are more options in the property grid on the left, most of them are self-explanatory. With MiniHttpdConsoleMiniHttpdConsole is a command-line demo project I made primarily to try MiniHttpd on Linux. The interface is primitive, with a somewhat nostalgic text-based menu system (maybe not so to Unix users) and command-line argument input. Entering "?" brings up a list of available menu items, and entering "h" brings up a list of available command-line arguments. Aside the mono MiniHttpdConsole.exe -s -l &
Note that MiniHttpdConsole requires .NET 2.0 or Mono 1.1.10. MSH is awesomeMicrosoft is working on a new command-line shell for the Longhorn Server (and possibly Vista) called Microsoft Shell (Monad), a .NET-powered shell with type-safe pipes and a syntax borrowed from functional languages, SQL and Unix shells. I couldn't resist sharing the fact that I tried MiniHttpd through Monad. Note that I did this right in the shell, but this could almost as easily be made into a function (called a cmdlet) and saved to a cmdlet file: > [Reflection.Assembly]::LoadFile($(combine-path
$(get-location) "minihttpd.dll"))
GAC Version Location
--- ------- --------
False v1.1.4322 C:\Documents and Settings\Rei Miyasaka\
My Documents\Visual Studio Proj...
>$server = new-object MiniHttpd.HttpWebServer
>$server.Root = new-object MiniHttpd.DriveDirectory($(get-location))
>$server.Start()
Server: MiniHttpd/1.2.0.92
CLR: 2.0.50727.42
Server running at http://rei/
Isn't that cool?! Pseudo-legal stuffI will bind MiniHttpd by the Creative Commons Attribution 2.5 license, which in a word says that you may use MiniHttpd for anything you want, however you want (including commercially, but with no warranty), so long as you credit me for the original work. On the other hand, I'm a college student, and programming work here in Vancouver is not very well-paying despite being scarce. In my position, it's hard to justify working on anything that won't earn me at least something. So if you ever do find commercial use for MiniHttpd, a small donation would be appreciated to give me encouragement. Also, please let me know if you use MiniHttpd for a project. It'd look good on my resume :) Future updatesThese are ideas for future versions, in no particular order, and with no particular deadline. I will increment a minor version number (1.x) each time I make a breaking change.
Links
History
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||