This project is read-only.

Custom HTTP Module

Main program

    class Program
    {
        static void Main(string[] args)
        {
            // create a server.
            var server = new Server();

            // same as previous example.
            server.Resources.Add(new FileResources("/", Environment.CurrentDirectory + "\\files\\"));
            server.Add(new FileModule(server.Resources, false));

            // use one http listener.
            server.Add(HttpListener.Create(IPAddress.Any, 8085));

            // add our own module.
            server.Add(new Streamer());


            // start server, can have max 5 pending accepts.
            server.Start(5);

            Console.ReadLine();
        }
    }

The module

    /// <summary>
    /// Example on how to stream a file
    /// </summary>
    public class Streamer : IModule
    {
        /// <summary>
        /// Process a request.
        /// </summary>
        /// <param name="context">Request information</param>
        /// <returns>What to do next.</returns>
        public ProcessingResult Process(RequestContext context)
        {
            // should add information about the file here.

            // Lets send the header.
            ResonseWriter generator = new ResonseWriter();
            generator.SendHeaders(context.HttpContext, context.Response);

            // loop through file contents.
            BinaryWriter writer = new BinaryWriter(context.HttpContext.Stream);
            


            // Abort is needed since we've used the network
            // stream directly. Else the framework will try to use the
            // response object + network stream to send another response.
            return ProcessingResult.Abort; 
        }
    }

Last edited Feb 17, 2011 at 9:09 PM by jgauffin, version 3

Comments

No comments yet.