Пример #1
0
        /// <summary>
        /// 输出文件
        /// </summary>
        /// <param name="response">回复对象</param>
        public void ExecuteResult(HttpResponse response)
        {
            if (string.IsNullOrEmpty(this.ContentType))
            {
                this.ContentType = "application/ocelet-stream";
            }
            response.Charset = null;
            response.ContentType = this.ContentType;
            response.ContentDisposition = this.ContentDisposition;

            using (var stream = new FileStream(this.FileName, FileMode.Open, FileAccess.Read))
            {
                const int size = 8 * 1024;               
                var state = 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 = response.WriteContent(content);
                }
            }
        }
Пример #2
0
 /// <summary>
 /// 收到http请求
 /// </summary>
 /// <param name="request"></param>
 /// <param name="response"></param>
 protected override void OnHttpRequest(HttpRequest request, HttpResponse response)
 {
     var parameters = string.Join("&", request.Form, request.Query).Trim('&');
     if (string.Equals(request.Path, AppConfig.HttpURL.AbsolutePath, StringComparison.OrdinalIgnoreCase))
     {
         this.TryPushParameters(parameters);
     }
     response.Write(parameters);
 }
Пример #3
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)));
            }
        }
Пример #4
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);
            }
        }
Пример #5
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);
            }
        }
Пример #6
0
 /// <summary>
 /// 请求上下文
 /// </summary>
 /// <param name="request">请求上下文对象</param>
 /// <param name="response">回复上下文对象</param>        
 internal RequestContext(HttpRequest request, HttpResponse response)
 {
     this.Request = request;
     this.Response = response;
 }
Пример #7
0
 /// <summary>
 /// 异常时
 /// </summary>
 /// <param name="session">产生异常的会话</param>
 /// <param name="exception">异常</param>
 protected virtual void OnException(ISession session, Exception exception)
 {
     var httpException = exception as HttpException;
     if (httpException == null)
     {
         httpException = new HttpException(500, exception.Message);
     }
     var result = new ErrorResult(httpException);
     var response = new HttpResponse(session);
     result.ExecuteResult(response);
 }
Пример #8
0
 /// <summary>
 /// 收到Http请求
 /// </summary>       
 /// <param name="request">请求对象</param>
 /// <param name="response">回复对象</param>
 protected override void OnHttpRequest(HttpRequest request, HttpResponse response)
 {
     Task.Factory.StartNew(() => this.ProcessRequest(new RequestContext(request, response)));
 }
Пример #9
0
 /// <summary>
 /// 执行结果
 /// </summary>
 /// <param name="response">回复对象</param>
 public void ExecuteResult(HttpResponse response)
 {
     response.Status = this.Status;
     response.Write(this.Errors);
 }