/// <summary> /// 处理Http请求 /// </summary> /// <param name="e"></param> /// <returns></returns> private ProcessingResult HandleRequest(RequestEventArgs e) { var context = new RequestContext { HttpContext = e.Context, Request = e.Request, Response = e.Response }; // 鉴权 AuthenticateRequest(context); // 前期处理 OnBeforeProcessRequest(context); // 通知外部请求到达 BeforeProcessRequest(this, e); // 解析消息体 if (e.Request.ContentLength.Value > 0) { DecodeBody(e.Request); } ProcessingResult result = ProcessingResult.Continue; // 由路由器处理消息 result = ProcessRouters(context); if (ProcessResult(result, e)) { Logger.Debug("Routers processed the request."); } // 有模块处理消息 result = ProcessModules(context); if (ProcessResult(result, e)) { return(result); } // 通知请求已收到,由外部模块处理请求 RequestReceived(this, e); return(ProcessingResult.Continue); }
/// <summary> /// 检查处理结果,是否需要发送响应消息 /// Process result (check if it should be sent back or not) /// </summary> /// <param name="result"></param> /// <param name="e"></param> /// <returns><c>true</c> if request was processed properly.; otherwise <c>false</c>.</returns> protected virtual bool ProcessResult(ProcessingResult result, RequestEventArgs e) { if (result == ProcessingResult.Abort) { e.IsHandled = true; return(true); } if (result == ProcessingResult.SendResponse) { // 发送响应消息 SendResponse(e.Context, e.Request, e.Response); e.IsHandled = true; return(true); } return(false); }
/// <summary> /// 响应上下文中接收请求消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnContextRequest(object sender, RequestEventArgs e) { var context = (HttpContext)sender; HttpFactory.Current = Factory; HttpContext.Current = context; try { var args = new RequestEventArgs(context, e.Request, e.Response); // 触发请求收到事件通知 RequestReceived(this, args); // 判断外部程序是否已处理该请求 if (!args.IsHandled) { // 如果外部程序未处理该请求,则发送请求的响应消息 var generator = new ResponseWriter(); generator.Send(context, args.Response); } // 请求被处理后断开连接 if (e.Response.HttpVersion == "HTTP/1.0" || e.Response.Connection.Type == ConnectionType.Close) { context.Disconnect(); } } catch (Exception err) { if (err is HttpException) { var exception = (HttpException)err; SendErrorPage(exception); } else { Logger.Debug("Request failed."); ExceptionHandler.Handle(err); SendErrorPage(err); } e.IsHandled = true; } }
private void OnListenerRequestReceived(object sender, RequestEventArgs e) { _server = this; Exception exception; try { ProcessingResult result = HandleRequest(e); if (result != ProcessingResult.Continue) { return; } exception = null; } catch (HttpException err) { Logger.Error("Got an HTTP exception."); ExceptionHandler.Handle(err); e.Response.Status = err.Code; e.Response.Reason = err.Message; exception = err; } catch (Exception err) { Logger.Error("Got an unhandled exception."); ExceptionHandler.Handle(err); exception = err; e.Response.Status = HttpStatusCode.InternalServerError; e.Response.Reason = "Failed to process request."; } if (exception == null) { e.Response.Status = HttpStatusCode.NotFound; e.Response.Reason = "Requested resource is not found."; exception = new HttpException(HttpStatusCode.NotFound, "Failed to find uri " + e.Request.Uri); } DisplayErrorPage(e.Context, exception); e.IsHandled = true; }
/// <summary> /// 响应上下文中接收请求消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnContextRequest(object sender, RequestEventArgs e) { var context = (HttpContext)sender; HttpFactory.Current = Factory; HttpContext.Current = context; try { var args = new RequestEventArgs(context, e.Request, e.Response); // 触发请求收到事件通知 RequestReceived(this, args); // 判断外部程序是否已处理该请求 if (!args.IsHandled) { // 如果外部程序未处理该请求,则发送请求的响应消息 var generator = new ResponseWriter(); generator.Send(context, args.Response); } // 请求被处理后断开连接 if (e.Response.HttpVersion == "HTTP/1.0" || e.Response.Connection.Type == ConnectionType.Close) context.Disconnect(); } catch (Exception err) { if (err is HttpException) { var exception = (HttpException)err; SendErrorPage(exception); } else { Logger.Debug("Request failed."); ExceptionHandler.Handle(err); SendErrorPage(err); } e.IsHandled = true; } }
/// <summary> /// 检查处理结果,是否需要发送响应消息 /// Process result (check if it should be sent back or not) /// </summary> /// <param name="result"></param> /// <param name="e"></param> /// <returns><c>true</c> if request was processed properly.; otherwise <c>false</c>.</returns> protected virtual bool ProcessResult(ProcessingResult result, RequestEventArgs e) { if (result == ProcessingResult.Abort) { e.IsHandled = true; return true; } if (result == ProcessingResult.SendResponse) { // 发送响应消息 SendResponse(e.Context, e.Request, e.Response); e.IsHandled = true; return true; } return false; }
/// <summary> /// 处理Http请求 /// </summary> /// <param name="e"></param> /// <returns></returns> private ProcessingResult HandleRequest(RequestEventArgs e) { var context = new RequestContext { HttpContext = e.Context, Request = e.Request, Response = e.Response }; // 鉴权 AuthenticateRequest(context); // 前期处理 OnBeforeProcessRequest(context); // 通知外部请求到达 BeforeProcessRequest(this, e); // 解析消息体 if (e.Request.ContentLength.Value > 0) { DecodeBody(e.Request); } ProcessingResult result = ProcessingResult.Continue; // 由路由器处理消息 result = ProcessRouters(context); if (ProcessResult(result, e)) { Logger.Debug("Routers processed the request."); } // 有模块处理消息 result = ProcessModules(context); if (ProcessResult(result, e)) { return result; } // 通知请求已收到,由外部模块处理请求 RequestReceived(this, e); return ProcessingResult.Continue; }
private void OnListenerRequestReceived(object sender, RequestEventArgs e) { _server = this; Exception exception; try { ProcessingResult result = HandleRequest(e); if (result != ProcessingResult.Continue) return; exception = null; } catch (HttpException err) { Logger.Error("Got an HTTP exception."); ExceptionHandler.Handle(err); e.Response.Status = err.Code; e.Response.Reason = err.Message; exception = err; } catch (Exception err) { Logger.Error("Got an unhandled exception."); ExceptionHandler.Handle(err); exception = err; e.Response.Status = HttpStatusCode.InternalServerError; e.Response.Reason = "Failed to process request."; } if (exception == null) { e.Response.Status = HttpStatusCode.NotFound; e.Response.Reason = "Requested resource is not found."; exception = new HttpException(HttpStatusCode.NotFound, "Failed to find uri " + e.Request.Uri); } DisplayErrorPage(e.Context, exception); e.IsHandled = true; }