static void Main(string[] args) { using var svr = new HttpServer() .LogToConsole(LogLevel.Debug) .ListenToLoopback(); // close regex pattern at the and when not diving svr .Use(new ExceptionHandler()) .Use(new TimingMiddleware(LogLevel.Information)) .Use(new CompressionMiddelware(new GZipCompressor(), new DeflateCompressor())) .Dive("/files").Get(new FileHandler(@"F:\")).Ascent(new NotFoundHandler()) .UseWhen("redirect", (ctx, _) => ctx.RedirectTemporarily("about")) .Get("about", ctx => "About") .Dive("api") .Get("info", (ctx, _) => ctx.Respond(StringHttpResponse.Text("Info"))) .Ascent(ctx => "API") .Get("other", ctx => "Other") .Get("/", ctx => "Index") .Use(new NotFoundHandler()); svr.Start(); Console.WriteLine("Press any key to stop..."); Console.ReadKey(); }
protected IHttpResponse Found(string url) { var headers = new ListHttpHeaders(new[] { new KeyValuePair <string, string>("Location", url) }); return(StringHttpResponse.Create(string.Empty, HttpResponseCode.Found, null, false, headers)); }
public Task Handle(IHttpContext context, Func <Task> next) { IDictionary <string, dynamic> session = context.State.Session; dynamic ipAddress; if (!session.TryGetValue(_authenticationKey, out ipAddress) || ipAddress != context.RemoteEndPoint) { if (TryAuthenticate(context, session)) { return(next()); } context.Response = StringHttpResponse.Create("Not Authenticated", HttpResponseCode.Unauthorized, headers: _headers); return(Task.Factory.GetCompleted()); } return(next()); }
public async Task <IHttpResponse> Respond(IHttpContext context, IView view) { var output = await view.Render(context, _state).ConfigureAwait(false); return(StringHttpResponse.Create(output.Body, _code, output.ContentType)); }