C# Embedded Web Server

VqEmbWebSharp Embedded Web Server is a C# embedded web server. It is very lightweight. It was ported from VQEmbWeb java web server by Gareth Cronin. Support local static files,local images, embedded web pages and embedded images. Handles cookies. It is licensed under LGPL v3.

Now tested under Mono!

Download

After downloading, you can extract the content to the location of your choice. The project should edit with Visual Studio 2008 or SharpDevelop 3.x

Usage:

1) Modify the Program.cs file to set the port and IP Address.

public Program()
        {
            SERVER_VERSION = Resource1.SERVER_VERSION;

            // this needs set to your needs
            this.port = 8080;

            try
            {
                // set the ip address here
                string ipAddress = "127.0.0.1";
                IPAddress address = IPAddress.Parse(ipAddress);

                this.server = EmbeddedServer.createInstance(this.port, address, new MyRequestHandler());
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Could not create Server " + e);
            }
        }


2) Modify the MyRequestHandler.cs to suit your needs

With no modifications you can begin serving static content of local files by putting them in the same directory or below and providing the proper path in the url.

Embedded Resources

You can serve embedded resources. To do this, add resources to your project. This can be done in Visual Studio or SharpDevelop by double clicking the file Resource1.resx. Once a resource is added, it can be accessed in the request handler.

For text resources change the as indicated to match the resource name and the url:

    if (url.Equals("/abc.xml"))                // change the url to match the desired request
    {
       response.addContent(Resource1.ABC_dmp); // this is the resource to change
       return response.responseOk();
    }

For binary change the text as indicated:

   if (url.Equals("/favicon.ico"))                 // change url
   {
        MemoryStream ms = new MemoryStream();
        Resource1.favicon.Save(ms);                 // change resource
        response.setBinaryContent(ms.ToArray());
        return response.responseOk();
   }

Cookies

To set a cookie

    response.setCookie(strKey, strVal);

To read a cookie

    if(cookies.ContainsKey("key"))
        String val = cookies["key"];

3) Additional Mime types can be set in ServerConfig.cs