using System; using ln.http.exceptions; namespace ln.http.router { public class RouterTarget :IHttpRouter { public Func Target { get; } public RouterTarget(Func target) { Target = (HttpRoutingContext routingContext, HttpRequest httpRequest) => target(routingContext.Path, httpRequest); } public RouterTarget(Func target) { Target = target; } public RouterTarget(Func target) { Target = (context,request) => target(request); } protected RouterTarget() { Target = (path,request) => Dispatch(request); } public virtual HttpResponse Dispatch(HttpRequest request) { switch (request.Method.ToUpper()) { case "HEAD": return HEAD(request); case "GET": return GET(request); case "PROPFIND": return PROPFIND(request); case "POST": return POST(request); case "PUT": return PUT(request); case "DELETE": return DELETE(request); default: throw new MethodNotAllowedException(); } } public HttpResponse Route(HttpRoutingContext routingContext, HttpRequest httpRequest) { return Target(routingContext, httpRequest); } public virtual HttpResponse HEAD(HttpRequest request) => throw new MethodNotAllowedException(); public virtual HttpResponse GET(HttpRequest request) => throw new MethodNotAllowedException(); public virtual HttpResponse PROPFIND(HttpRequest request) => throw new MethodNotAllowedException(); public virtual HttpResponse POST(HttpRequest request) => throw new MethodNotAllowedException(); public virtual HttpResponse PUT(HttpRequest request) => throw new MethodNotAllowedException(); public virtual HttpResponse DELETE(HttpRequest request) => throw new MethodNotAllowedException(); } }