This project is read-only.

HttpServer 2.0

Topics: blog
Dec 23, 2009 at 3:27 PM

It's become quite impossible to implement advanced features like streaming, adding support for new view engines or implement custom features. So I've rewritten the http server from scratch. :)

The new version has no external dependencies at all and is even more modular. 

Some of the new features include:

  • HttpFactory - Used to create all objects in the server. You can now easily create your own types and inject them in the web server.
  • Better logging abstraction.
  • HttpContext - Wrapper around the socket. You can now access the stream directly to build streaming support.
  • HttpFactory.Current, Server.Current, HttpContext.Current - Access the factory, webserver, httpcontext from any class.
  • ResourceProvider - Better resource handling, easier to include own resource handlers.
  • New request parser. Uses strategy pattern instead of a bunch of if statements. More robust and should be a lot faster.
  • Header classes - All recognized headers have their own class (and parser).

The MVC project 

  • Nothing is the same ;)
  • Controller methods now return IViewData or IActionResult (inspired by ASP.Net MVC)
  • Still have support for a bunch of attributes to customize controllers (more than v1.0).
  • Suffix a method with Get, Post, Put, Delete to make the method valid for a certain verb. (to be able to use for instance "/user/" for all verbs, but invoke different methods)
  • Limit a method to a certain verb.
  • A lot easier to code a new view engine.

 

Code example:

 

Server server = new Server();

// Load file resources from c:\\temp
server.Add(new FileResources("/files/", "C:\\temp\\"));

// Only decode forms of type application/multi-part
server.Add(new MultipartDecoder());

// Serve files from the built in resource provider
server.Add(new FileModule(server.ResourceProvider, false));

// Only using one listener (could use many different ones)
HttpListener listener = HttpListener.Create(IPAddress.Any, 8085);
server.Add(listener);

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

 

Mvc example:

 

// create a MVC server.
MvcServer server = new MvcServer();

// same as previous example.
server.Add(new FileResources("/", "C:\\myapp\\public\\"));
server.Add(new MultipartDecoder());
server.Add(new UrlDecoder());
server.Add(new FileModule(server.ResourceProvider, false));

// load views from hard disk only.
server.ViewProvider.Add(new FileResources("/", "C:\\myapp\\views\\"));

// use NHaml view engine (look in google code)
server.ViewEngines.Add(new NHamlViewEngine());

// only using an user controller.
server.Add(new UserController());

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

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

 

Keep in mind that the code will be alpha when I check it in (after new year). A lot of functionality are still missing compared to version 1.0.

I have read a whole lot about Spark view engine and will talk with them as soon as the MVC library is a bit stable.

 

Dec 23, 2009 at 7:02 PM
On Wed, Dec 23, 2009 at 7:27 AM, [email removed] wrote:
> From: jgauffin
>
> It's become quite impossible to implement advanced features like streaming,
> adding support for new view engines or implement custom features. So I've
> rewritten the http server from scratch. :)
...
> Keep in mind that the code will be alpha when I check it in (after new
> year). A lot of functionality are still missing compared to version 1.0.

The points you posted sound great. I look forward to giving it a try
when you check it in.

-Chuck
Dec 24, 2009 at 3:03 PM

Wow, jgauffin, that's great news! I'm particularly excited by Spark view engine support. Let me know if I can help in any way.

 

Dec 26, 2009 at 9:36 AM

Great! Have you any beta ready?

Dec 26, 2009 at 4:34 PM

I will apply all patches and try to fix any existing issues in the current version next week.

After that I'll tag the current version commit the new one.