diff --git a/Burr/Burr.csproj b/Burr/Burr.csproj index b61fa224..7a499d8d 100644 --- a/Burr/Burr.csproj +++ b/Burr/Burr.csproj @@ -41,9 +41,11 @@ + Code + diff --git a/Burr/Http/Builder.cs b/Burr/Http/Builder.cs new file mode 100644 index 00000000..5f476419 --- /dev/null +++ b/Burr/Http/Builder.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Burr.Helpers; + +namespace Burr.Http +{ + /// + /// Standard Implementation of a . + /// A 's job is to build up the middleware for a connection. + /// + public class Builder : IBuilder + { + readonly List> handlers = new List>(); + bool frozen; + + public List> Handlers + { + get { return handlers; } + } + + public void Use(Func handler) + { + Ensure.ArgumentNotNull(handler, "handler"); + if (frozen) throw new NotSupportedException("The middleware stack has already been built. You cannot modify the handlers now"); + + handlers.Add(handler); + } + + public IApplication Run(IApplication adapter = null) + { + if (frozen) throw new NotSupportedException("The middleware stack has already been built. You can only call run once."); + frozen = true; + + // The builder sets up a chain of apps to be called just like Rack + // The inner most app is always an adapter which must produce the actual response + // The call tree for a basic middleware stack will look like this: + // + // request( response ( adapter(env) ) ) + + adapter = adapter ?? new HttpClientAdapter(); + + return Enumerable.Reverse(handlers).Aggregate(adapter, (app, handler) => handler(app)); + } + } +} diff --git a/Burr/Http/Connection.cs b/Burr/Http/Connection.cs index 49e2a38a..8e0cefcd 100644 --- a/Burr/Http/Connection.cs +++ b/Burr/Http/Connection.cs @@ -7,55 +7,6 @@ using Burr.Helpers; namespace Burr.Http { - /// - /// A 's job is to build up the "application" for a connection. - /// - public interface IBuilder - { - List> Handlers { get; } - IApplication Run(IApplication adapter); - void Use(Func handler); - } - - /// - /// Standard Implementation of a . - /// A 's job is to build up the middleware for a connection. - /// - public class Builder : IBuilder - { - readonly List> handlers = new List>(); - bool frozen; - - public List> Handlers - { - get { return handlers; } - } - - public void Use(Func handler) - { - Ensure.ArgumentNotNull(handler, "handler"); - if (frozen) throw new NotSupportedException("The middleware stack has already been built. You cannot modify the handlers now"); - - handlers.Add(handler); - } - - public IApplication Run(IApplication adapter = null) - { - if (frozen) throw new NotSupportedException("The middleware stack has already been built. You can only call run once."); - frozen = true; - - // The builder sets up a chain of apps to be called just like Rack - // The inner most app is always an adapter which must produce the actual response - // The call tree for a basic middleware stack will look like this: - // - // request( response ( adapter(env) ) ) - - adapter = adapter ?? new HttpClientAdapter(); - - return Enumerable.Reverse(handlers).Aggregate(adapter, (app, handler) => handler(app)); - } - } - public interface IResponse { string Body { get; set; } diff --git a/Burr/Http/IBuilder.cs b/Burr/Http/IBuilder.cs new file mode 100644 index 00000000..6e491b88 --- /dev/null +++ b/Burr/Http/IBuilder.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Burr.Http +{ + /// + /// A 's job is to build up the "application" for a connection. + /// + public interface IBuilder + { + List> Handlers { get; } + IApplication Run(IApplication adapter); + void Use(Func handler); + } +}