ln.http/ln.http/HttpContext.cs

53 lines
1.7 KiB
C#

using System;
using ln.http.exceptions;
using ln.http.router;
namespace ln.http
{
public class HttpContext
{
public HttpServer HttpServer { get; }
public HttpContext SourceContext { get; }
public HttpException HttpException { get; }
public HttpRequest Request { get; set; }
public HttpResponse Response { get; set; }
public HttpPrincipal AuthenticatedPrincipal { get; private set; }
public HttpContext(HttpServer httpServer)
{
HttpServer = httpServer;
}
public HttpContext(HttpServer httpServer, HttpRequest httpRequest) : this(httpServer)
{
Request = httpRequest;
RoutableUri = httpRequest.RequestUri.AbsolutePath;
}
public HttpContext(HttpContext sourceContext, HttpException httpException)
{
SourceContext = sourceContext;
HttpException = httpException;
HttpServer = sourceContext.HttpServer;
RoutableUri = String.Format("/_err/{0:3}.html", (int)httpException.HttpStatusCode);
AuthenticatedPrincipal = SourceContext.AuthenticatedPrincipal;
}
public string RoutableUri { get; set; }
public bool Authenticate()
{
foreach (var authenticationSource in HttpServer.AuthenticationSources)
{
if (authenticationSource.AuthenticatePrincipal(this, out HttpPrincipal httpPrincipal))
{
AuthenticatedPrincipal = httpPrincipal;
return true;
}
}
return false;
}
public void DeAuthenticate() => AuthenticatedPrincipal = null;
}
}