62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
using System;
|
|
using ln.http.exceptions;
|
|
namespace ln.http.router
|
|
{
|
|
public class RouterTarget :IHttpRouter
|
|
{
|
|
public Func<HttpRoutingContext,HttpRequest, HttpResponse> Target { get; }
|
|
|
|
public RouterTarget(Func<string, HttpRequest, HttpResponse> target)
|
|
{
|
|
Target = (HttpRoutingContext routingContext, HttpRequest httpRequest) => target(routingContext.Path, httpRequest);
|
|
}
|
|
public RouterTarget(Func<HttpRoutingContext, HttpRequest, HttpResponse> target)
|
|
{
|
|
Target = target;
|
|
}
|
|
public RouterTarget(Func<HttpRequest, HttpResponse> 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();
|
|
|
|
|
|
}
|
|
}
|