/// <summary> /// 收到http请求 /// </summary> /// <param name="context">上下文</param> /// <returns></returns> private Task OnHttpRequest(IContenxt context) { try { var request = default(HttpRequest); if (HttpRequest.Parse(context, out request) == false) { return(this.Next.Invoke(context)); } if (context.Session.Protocol == null) { context.Session.SetProtocolWrapper("http", null); } if (request == null) { return(new Task(() => { })); } if (request.IsWebsocketRequest() == true) { const string secKey = "Sec-WebSocket-Key"; context.Session.Tag.Set(secKey, request.Headers[secKey]); return(this.Next.Invoke(context)); } return(new Task(() => { var response = new HttpResponse(context.Session); var requestContext = new RequestContext(request, response); this.OnHttpRequest(context, requestContext); })); } catch (HttpException ex) { return(new Task(() => this.OnException(context.Session, ex))); } catch (Exception ex) { return(new Task(() => this.OnException(context.Session, new HttpException(500, ex.Message)))); } }
/// <summary> /// 收到tcp请求 /// </summary> /// <param name="session">会话</param> /// <param name="buffer">数据</param> protected override void OnReceive(SessionBase session, ReceiveBuffer buffer) { try { var request = HttpRequest.Parse(buffer, base.LocalEndPoint, session.RemoteEndPoint); if (request != null) { this.OnHttpRequest(request, new HttpResponse(session)); } } catch (HttpException ex) { var response = new HttpResponse(session); response.Status = ex.Status; response.Write(ex.Message); } catch (Exception) { session.Close(); } }