Exemplo n.º 1
0
 /// <summary>
 /// Api行为上下文
 /// </summary>
 /// <param name="requestContext">请求上下文</param>
 /// <param name="action">Api行为</param>
 /// <param name="context">上下文</param>
 public ActionContext(RequestContext requestContext, HttpAction action, IContenxt context)
     : base(requestContext.Request, requestContext.Response)
 {
     this.Action = action;
     this.Session = context.Session;
     this.AllSessions = context.AllSessions;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 输出文件
        /// </summary>
        /// <param name="context">上下文</param>
        private void ExecuteFileResult(RequestContext context)
        {
            if (string.IsNullOrEmpty(this.ContentType))
            {
                this.ContentType = "application/ocelet-stream";
            }
            context.Response.Charset = null;
            context.Response.ContentType = this.ContentType;
            context.Response.ContentDisposition = this.ContentDisposition;

            using (var stream = new FileStream(this.FileName, FileMode.Open, FileAccess.Read))
            {
                const int size = 8 * 1024;
                var state = context.Response.WriteHeader((int)stream.Length);

                while (state == true)
                {
                    var bytes = new byte[size];
                    var length = stream.Read(bytes, 0, size);
                    if (length == 0)
                    {
                        break;
                    }

                    var content = new ByteRange(bytes, 0, length);
                    state = context.Response.WriteContent(content);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="context">上下文</param>
 public override void ExecuteResult(RequestContext context)
 {
     if (File.Exists(this.FileName) == true)
     {
         this.ExecuteFileResult(context);
     }
     else
     {
         var result = new ErrorResult { Status = 404, Errors = "找不到文件:" + this.FileName };
         result.ExecuteResult(context);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 执行结果
        /// </summary>
        /// <param name="context">上下文</param>
        public override void ExecuteResult(RequestContext context)
        {
            context.Response.ContentType = "text/event-stream";
            context.Response.WriteHeader();

            var session =((IWrapper) context.Response).UnWrap();
            var wapper = new HttpEventSession(session);
            session.SetProtocolWrapper("sse", wapper);

            if (this.httpEvent != null)
            {
                wapper.SendEvent(this.httpEvent);
            }
        }
Exemplo n.º 5
0
        /// <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)));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 收到http请求
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="requestContext">请求上下文</param>
        protected override void OnHttpRequest(IContenxt context, NetworkSocket.Http.RequestContext requestContext)
        {
            var request  = requestContext.Request;
            var response = requestContext.Response;

            if (AppConfig.PathPattern.IsMatch(request.Path) == false)
            {
                var message = "路径不匹配,数据未转发 ..";
                response.Write(message);
                Console.WriteLine(message);
            }
            else
            {
                var fastSessions = context.AllSessions.FilterWrappers <FastSession>().ToArray();
                foreach (var session in fastSessions)
                {
                    this.PushRequest(session, request);
                }

                var message = string.Format("数据已转发到{0}到客户端 ..", fastSessions.Length);
                response.Write(message);
                Console.WriteLine(message);
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="context">上下文</param>
 public override void ExecuteResult(RequestContext context)
 {
     var json = new DefaultDynamicJsonSerializer().Serialize(this.data);
     context.Response.ContentType = "application/json";
     context.Response.Write(json);
 }
Exemplo n.º 8
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="context">上下文</param>
 public override void ExecuteResult(RequestContext context)
 {
     var json = new JavaScriptSerializer().Serialize(data);
     context.Response.ContentType = "application/json";
     context.Response.Write(json);
 }
Exemplo n.º 9
0
 /// <summary>
 /// 异常上下文
 /// </summary>
 /// <param name="requestContext">请求上下文</param>
 /// <param name="exception">异常</param>
 public ExceptionContext(RequestContext requestContext, Exception exception)
     : base(requestContext.Request, requestContext.Response)
 {
     this.Exception = exception;
 }
Exemplo n.º 10
0
 /// <summary>
 /// 处理一般的请求
 /// </summary>
 /// <param name="route">路由</param>
 /// <param name="requestContext">上下文</param>
 private void ProcessNormalRequest(string route, RequestContext requestContext)
 {
     var action = this.httpActionList.TryGet(requestContext.Request);
     if (action == null)
     {
         this.ProcessActionNotFound(route, requestContext);
     }
     else
     {
         this.ExecuteHttpAction(action, requestContext);
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// 异常时
        /// </summary>
        /// <param name="session">产生异常的会话</param>
        /// <param name="exception">异常</param>
        protected sealed override void OnException(ISession session, Exception exception)
        {
            var response = new HttpResponse(session);
            var requestContext = new RequestContext(null, response);
            var exceptionConext = new ExceptionContext(requestContext, exception);
            this.ExecGlobalExceptionFilters(exceptionConext);

            if (exceptionConext.Result != null)
            {
                exceptionConext.Result.ExecuteResult(requestContext);
            }
            else
            {
                base.OnException(session, exception);
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="context">上下文</param>
 public abstract void ExecuteResult(RequestContext context);
Exemplo n.º 13
0
        /// <summary>
        /// 执行结果
        /// </summary>
        /// <param name="context">上下文</param>
        public override void ExecuteResult(RequestContext context)
        {
            var gzip = context.Request.IsAcceptGZip();

            context.Response.WriteResponse(Content, gzip);
        }
Exemplo n.º 14
0
 /// <summary>
 /// 处理一般的请求
 /// </summary>
 /// <param name="route">路由</param>
 /// <param name="context">上下文</param>
 /// <param name="requestContext">请求上下文</param>
 private void ProcessActionRequest(string route, IContenxt context, RequestContext requestContext)
 {
     var action = this.httpActionList.TryGet(requestContext.Request);
     if (action == null)
     {
         var ex = new HttpException(404, "找不到路径" + route);
         this.ProcessHttpException(ex, requestContext);
     }
     else
     {
         this.ExecuteHttpAction(action, context, requestContext);
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// 处理http请求
        /// </summary>
        /// <param name="requestContext">请求上下文</param>
        private void ProcessRequest(RequestContext requestContext)
        {
            var route = requestContext.Request.Url.AbsolutePath;
            var action = this.httpActionList.TryGet(requestContext.Request);

            if (action == null)
            {
                var exception = new ApiNotExistException(route);
                var exceptionContext = new ExceptionContext(requestContext, exception);
                foreach (var filter in this.GlobalFilter.ExceptionFilters)
                {
                    filter.OnException(exceptionContext);
                    if (exceptionContext.ExceptionHandled == true) break;
                }

                var result = exceptionContext.Result != null ? exceptionContext.Result : new ErrorResult
                {
                    Status = 404,
                    Errors = exception.Message
                };
                result.ExecuteResult(requestContext);
            }
            else
            {
                var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController;
                var actionContext = new ActionContext(requestContext, action);

                controller.Server = this;
                ((IHttpController)controller).Execute(actionContext);

                // 释放资源
                this.DependencyResolver.TerminateService(controller);
            }
        }
Exemplo n.º 16
0
 /// <summary>
 /// Api行为上下文
 /// </summary>
 /// <param name="context">请求上下文</param>
 /// <param name="action">Api行为</param>
 public ActionContext(RequestContext context, HttpAction action)
     : base(context.Request, context.Response)
 {
     this.Action = action;
 }
Exemplo n.º 17
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="context">上下文</param>
 public override void ExecuteResult(RequestContext context)
 {
     this.ExecuteResult(context.Response);
 }
Exemplo n.º 18
0
        /// <summary>
        /// 处理静态资源请求
        /// </summary>
        /// <param name="extension">扩展名</param>
        /// <param name="requestContext">上下文</param>
        private void ProcessStaticFileRequest(string extension, RequestContext requestContext)
        {
            var contenType = this.MIMECollection.GetContentType(extension);
            var file = requestContext.Request.Url.AbsolutePath.TrimStart('/').Replace(@"/", @"\");

            if (string.IsNullOrWhiteSpace(contenType) == true)
            {
                var result = new ErrorResult { Status = 403 };
                result.ExecuteResult(requestContext);
            }
            else if (File.Exists(file) == false)
            {
                var result = new ErrorResult { Status = 404, Errors = "找不到指定的文件 .." };
                result.ExecuteResult(requestContext);
            }
            else
            {
                var result = new FileResult { FileName = file, ContentType = contenType };
                result.ExecuteResult(requestContext);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// 处理http请求
        /// </summary>
        /// <param name="requestContext">请求上下文</param>
        private void ProcessRequest(RequestContext requestContext)
        {
            var extenstion = Path.GetExtension(requestContext.Request.Path);

            if (string.IsNullOrWhiteSpace(extenstion) == false)
            {
                this.ProcessStaticFileRequest(extenstion, requestContext);
            }
            else
            {
                this.ProcessNormalRequest(requestContext.Request.Path, requestContext);
            }
        }
Exemplo n.º 20
0
 /// <summary>
 /// 收到Http请求时触发
 /// </summary>       
 /// <param name="context">上下文</param>
 /// <param name="requestContext">请求上下文对象</param>
 protected override void OnHttpRequest(IContenxt context, RequestContext requestContext)
 {
     var extenstion = Path.GetExtension(requestContext.Request.Path);
     if (string.IsNullOrWhiteSpace(extenstion) == false)
     {
         this.ProcessStaticFileRequest(extenstion, requestContext);
     }
     else
     {
         this.ProcessActionRequest(requestContext.Request.Path, context, requestContext);
     }
 }
Exemplo n.º 21
0
 /// <summary>
 /// 异常上下文
 /// </summary>
 /// <param name="requestContext">请求上下文</param>
 /// <param name="exception">异常</param>
 public ExceptionContext(RequestContext requestContext, Exception exception)
     : base(requestContext.Request, requestContext.Response)
 {
     this.Exception = exception;
 }
Exemplo n.º 22
0
        /// <summary>
        /// 处理静态资源请求
        /// </summary>
        /// <param name="extension">扩展名</param>
        /// <param name="requestContext">上下文</param>
        private void ProcessStaticFileRequest(string extension, RequestContext requestContext)
        {
            var contenType = this.MIMECollection[extension];
            var file = requestContext.Request.Url.AbsolutePath.TrimStart('/').Replace(@"/", @"\");

            if (string.IsNullOrWhiteSpace(contenType) == true)
            {
                var ex = new HttpException(403, string.Format("未配置{0}格式的MIME ..", extension));
                this.ProcessHttpException(ex, requestContext);
            }
            else if (File.Exists(file) == false)
            {
                var ex = new HttpException(404, string.Format("找不到文件{0} ..", file));
                this.ProcessHttpException(ex, requestContext);
            }
            else
            {
                var result = new FileResult { FileName = file, ContentType = contenType };
                result.ExecuteResult(requestContext);
            }
        }
Exemplo n.º 23
0
 /// <summary>
 /// 收到Http请求时触发
 /// </summary>
 /// <param name="context">上下文</param>
 /// <param name="requestContext">请求上下文对象</param>
 /// <returns ></returns>
 protected abstract Task OnHttpRequestAsync(IContenxt context, RequestContext requestContext);
Exemplo n.º 24
0
        /// <summary>
        /// 执行httpAction
        /// </summary>
        /// <param name="action">httpAction</param>
        /// <param name="context">上下文</param>
        /// <param name="requestContext">请求上下文</param>      
        private void ExecuteHttpAction(HttpAction action, IContenxt context, RequestContext requestContext)
        {
            var actionContext = new ActionContext(requestContext, action, context);
            var controller = GetHttpController(actionContext);

            if (controller != null)
            {
                controller.Execute(actionContext);
                this.DependencyResolver.TerminateService(controller);
            }
        }
Exemplo n.º 25
0
 /// <summary>
 /// 收到Http请求时触发
 /// </summary>       
 /// <param name="context">上下文</param>
 /// <param name="requestContext">请求上下文对象</param>
 protected abstract void OnHttpRequest(IContenxt context, RequestContext requestContext);
Exemplo n.º 26
0
 /// <summary>
 /// 处理Http异常
 /// </summary>
 /// <param name="ex">Http异常</param>
 /// <param name="context">请求上下文</param>
 private void ProcessHttpException(HttpException ex, RequestContext context)
 {
     var exceptionContent = new ExceptionContext(context, ex);
     this.ExecGlobalExceptionFilters(exceptionContent);
     var result = exceptionContent.Result ?? new ErrorResult(ex);
     result.ExecuteResult(context);
 }
Exemplo n.º 27
0
        /// <summary>
        /// 执行httpAction
        /// </summary>
        /// <param name="action">httpAction</param>
        /// <param name="requestContext">上下文</param>      
        private void ExecuteHttpAction(HttpAction action, RequestContext requestContext)
        {
            var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController;
            var actionContext = new ActionContext(requestContext, action);

            controller.Server = this;
            ((IHttpController)controller).Execute(actionContext);

            // 释放资源
            this.DependencyResolver.TerminateService(controller);
        }
Exemplo n.º 28
0
        /// <summary>
        /// 收到Http请求
        /// </summary>       
        /// <param name="request">请求对象</param>
        /// <param name="response">回复对象</param>
        protected override void OnHttpRequest(HttpRequest request, HttpResponse response)
        {
            var route = request.Url.AbsolutePath;
            var action = this.httpActionList.TryGet(route);
            var requestContext = new RequestContext(request, response);

            if (action == null)
            {
                var exception = new ApiNotExistException(route);
                var exceptionContext = new ExceptionContext(requestContext, exception);
                foreach (var filter in this.GlobalFilter.ExceptionFilters)
                {
                    filter.OnException(exceptionContext);
                    if (exceptionContext.ExceptionHandled == true) break;
                }

                var result = exceptionContext.Result == null ? new ErrorResult { Status = 404, Errors = exception.Message } : exceptionContext.Result;
                result.ExecuteResult(requestContext);
            }
            else
            {
                var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController;
                var actionContext = new ActionContext(requestContext, action);

                controller.Server = this;
                ((IHttpController)controller).Execute(actionContext);
            }
        }
Exemplo n.º 29
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="context">上下文</param>
 public override void ExecuteResult(RequestContext context)
 {
     context.Response.Write(content);
 }
Exemplo n.º 30
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="context">上下文</param>
 public override void ExecuteResult(RequestContext context)
 {
     context.Response.Status = this.Status;
     context.Response.Write(this.Errors);
 }
Exemplo n.º 31
0
        /// <summary>
        /// 处理找不到Action
        /// </summary>
        /// <param name="route">路由</param>
        /// <param name="requestContext">上下文</param>
        private void ProcessActionNotFound(string route, RequestContext requestContext)
        {
            var exception = new ApiNotExistException(route);
            var exceptionContext = new ExceptionContext(requestContext, exception);
            foreach (var filter in this.GlobalFilter.ExceptionFilters)
            {
                filter.OnException(exceptionContext);
                if (exceptionContext.ExceptionHandled == true) break;
            }

            var result = exceptionContext.Result != null ? exceptionContext.Result : new ErrorResult
            {
                Status = 404,
                Errors = exception.Message
            };
            result.ExecuteResult(requestContext);
        }