コード例 #1
0
        public static IHttpRequest ToRequest(this HttpContext httpContext, string operationName = null)
        {
            var req = new NetCoreRequest(httpContext, operationName, RequestAttributes.None);

            req.RequestAttributes = req.GetAttributes();
            return(req);
        }
コード例 #2
0
        //For pass-through requests not handled by ServiceStack
        public async Task SignInAuthenticatedSessions(NetCoreRequest req)
        {
            if (!AutoSignInSessionsMatching(req))
            {
                return;
            }

            var session = req.GetSession();

            if (session.IsAuthenticated)
            {
                var claims = session.ConvertSessionToClaims(
                    issuer: Issuer,
                    roleClaimType: RoleClaimType,
                    permissionClaimType: PermissionClaimType);

                if (HostContext.HasValidAuthSecret(req))
                {
                    claims.Add(new Claim(RoleClaimType, RoleNames.Admin, Issuer));
                }

                var principal = CreateClaimsPrincipal != null
                    ? CreateClaimsPrincipal(claims, session, req)
                    : new ClaimsPrincipal(new ClaimsIdentity(claims, AuthenticationType));

                req.HttpContext.User = principal;
            }
        }
コード例 #3
0
        public virtual async Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
                {
                    await next();
                }

                pathInfo = pathInfo.Substring(mode.Length + 1);
            }

            RequestContext.Instance.StartRequestContext();

            var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);

            httpReq.RequestAttributes = httpReq.GetAttributes();

            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;

            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    await next();
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                await HostContext.Async.ContinueWith(httpReq, task, x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);

                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return;
            }

            await next();
        }
コード例 #4
0
 public NetCoreResponse(NetCoreRequest request, HttpResponse response)
 {
     this.request = request;
     this.response = response;
     this.Items = new Dictionary<string, object>();
     this.Cookies = new NetCoreCookies(response);
     response.StatusCode = 200;
 }
コード例 #5
0
ファイル: NetCoreResponse.cs プロジェクト: AVee/ServiceStack
        public NetCoreResponse(NetCoreRequest request, HttpResponse response)
        {
            this.request = request;
            this.response = response;
            this.Items = new Dictionary<string, object>();
            this.Cookies = HostContext.AppHost.GetCookies(this);

            //Don't set StatusCode here as it disables Redirects from working in MVC 
            //response.StatusCode = 200;
        }
コード例 #6
0
        public virtual Task ProcessRequest(HttpContext context, Func<Task> next)
        {
            //Keep in sync with AppHostBase.NetCore.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";

            var pathInfo = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;
            if (!string.IsNullOrEmpty(mode))
            {
                if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
                    return next();

                pathInfo = pathInfo.Substring(mode.Length + 1);
            }

            var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);
            httpReq.RequestAttributes = httpReq.GetAttributes();

            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;
            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                    return next();

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                    operationName = serviceStackHandler.RequestName;

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return task;
            }

            return next();
        }
コード例 #7
0
        public virtual async Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            if (NetCoreHandler != null)
            {
                var handled = await NetCoreHandler(context);

                if (handled)
                {
                    return;
                }
            }

            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                var includedInPathInfo = pathInfo.IndexOf(mode, StringComparison.Ordinal) == 1;
                var includedInPathBase = context.Request.PathBase.HasValue &&
                                         context.Request.PathBase.Value.IndexOf(mode, StringComparison.Ordinal) == 1;
                if (!includedInPathInfo && !includedInPathBase)
                {
                    await next();

                    return;
                }

                if (includedInPathInfo)
                {
                    pathInfo = pathInfo.Substring(mode.Length + 1);
                }
            }

            RequestContext.Instance.StartRequestContext();

            NetCoreRequest httpReq;
            IResponse      httpRes;

            System.Web.IHttpHandler handler;

            try
            {
                httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);
                httpReq.RequestAttributes = httpReq.GetAttributes() | RequestAttributes.Http;

                httpRes = httpReq.Response;
                handler = HttpHandlerFactory.GetHandler(httpReq);

                if (BeforeNextMiddleware != null)
                {
                    var holdNext = next;
                    next = async() => {
                        await BeforeNextMiddleware(httpReq);
                        await holdNext();
                    };
                }
            }
            catch (Exception ex) //Request Initialization error
            {
                var logFactory = context.Features.Get <ILoggerFactory>();
                if (logFactory != null)
                {
                    var log = logFactory.CreateLogger(GetType());
                    log.LogError(default(EventId), ex, ex.Message);
                }

                context.Response.ContentType = MimeTypes.PlainText;
                await context.Response.WriteAsync($"{ex.GetType().Name}: {ex.Message}");

                if (Config.DebugMode)
                {
                    await context.Response.WriteAsync($"\nStackTrace:\n{ex.StackTrace}");
                }
                return;
            }

            if (handler is IServiceStackHandler serviceStackHandler)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    await next();

                    return;
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                if (serviceStackHandler is RestHandler restHandler)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                try
                {
                    await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                }
                catch (Exception ex)
                {
                    var logFactory = context.Features.Get <ILoggerFactory>();
                    if (logFactory != null)
                    {
                        var log = logFactory.CreateLogger(GetType());
                        log.LogError(default(EventId), ex, ex.Message);
                    }
                }
                finally
                {
                    httpRes.Close();
                }
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return;
            }

            await next();
        }
コード例 #8
0
 public static IHttpRequest ToRequest(this HttpContext httpContext, string operationName = null)
 {
     var req = new NetCoreRequest(httpContext, operationName, RequestAttributes.None);
     req.RequestAttributes = req.GetAttributes();
     return req;
 }
コード例 #9
0
        public virtual async Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
                {
                    await next();
                }

                pathInfo = pathInfo.Substring(mode.Length + 1);
            }

            RequestContext.Instance.StartRequestContext();

            var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);

            httpReq.RequestAttributes = httpReq.GetAttributes();

            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;

            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    await next();

                    return;
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                try
                {
                    await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                }
                catch (Exception ex)
                {
                    var logFactory = context.Features.Get <ILoggerFactory>();
                    var log        = logFactory.CreateLogger(GetType());
                    log.LogError(default(EventId), ex, ex.Message);
                }
                finally
                {
                    httpRes.Close();
                }
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return;
            }

            await next();
        }
コード例 #10
0
        public virtual Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
                {
                    return(next());
                }

                pathInfo = pathInfo.Substring(mode.Length + 1);
            }

#if NETSTANDARD1_6
            // This fixes problems if the RequestContext.Instance.Items was touched on startup or outside of request context.
            // It would turn it into a static dictionary instead flooding request with each-others values.
            // This can already happen if I register a Funq.Container Request Scope type and Resolve it on startup.
            RequestContext.Instance.StartRequestContext();
#endif

            var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);
            httpReq.RequestAttributes = httpReq.GetAttributes();

            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;
            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    return(next());
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                HostContext.Async.ContinueWith(httpReq, task, x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return(task);
            }

            return(next());
        }