private void _ListenerCallback(IAsyncResult result) { var context = new ConsoleContext(this.listener.EndGetContext(result)); _HandleRequest(context); this.listener.BeginGetContext(new AsyncCallback(_ListenerCallback), null); }
private void _HandleRequest(ConsoleContext context) { try { var handled = false; for (; context.CurrentRoute < this.registeredRoutes.Count; ++context.CurrentRoute) { var route = this.registeredRoutes[context.CurrentRoute]; var match = route.route.Match(context.Path); if (!match.Success) { continue; } if (!route.methods.IsMatch(context.Request.HttpMethod)) { continue; } // TODO: Main thread context.Match = match; route.callback(context); handled = !context.Pass; if (handled) { break; } } if (!handled) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; context.Response.StatusDescription = "Not Found"; } } catch (Exception exception) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.StatusDescription = string.Format("Fatal error:\n{0}", exception); Debug.LogException(exception); } context.Response.OutputStream.Close(); }