Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseOwin(pipeline => pipeline(next => context => {
                foreach (string key in context.Keys)
                {
                    Console.WriteLine($"{key}: {context[key]}");
                }
                return(next(context));
            }));

            // alternative
            app.Use((context, next) => {
                OwinEnvironment environment            = new OwinEnvironment(context);
                IDictionary <string, string[]> headers = (IDictionary <string, string[]>)environment.Single(item => item.Key == "owin.RequestHeaders").Value;

                return(next());
            });

            app.Use((context, next) => {
                OwinEnvironment environment            = new OwinEnvironment(context);
                OwinFeatureCollection features         = new OwinFeatureCollection(environment);
                IDictionary <string, string[]> headers = (IDictionary <string, string[]>)features.Environment["owin.RequestHeaders"];

                return(next());
            });

            app.UseMvc();
        }
        protected IDictionary <string, object> SessionState(OwinFeatureCollection context)
        {
            if (context.Environment.ContainsKey("its.SessionState"))
            {
                return(context.Environment["its.SessionState"] as IDictionary <string, object>);
            }

            return(null);
        }
        protected T GetSessionStateObject <T>(OwinFeatureCollection context, string key)
        {
            var session = SessionState(context);

            if (session.ContainsKey(key))
            {
                return((T)session[key]);
            }
            return(default(T));
        }
        protected void SetSessionStateObject <T>(OwinFeatureCollection context, string key, T val)
        {
            var session = SessionState(context);

            if (session.ContainsKey(key))
            {
                session.Remove(key);
            }

            session[key] = val;
        }
        private JrpcContext ConvertToContext(IDictionary <string, object> environment)
        {
            OwinFeatureCollection collection = new OwinFeatureCollection(environment);

            var context = new JrpcContext {
                JrpcRequestContext  = new KestrelJrpcRequestContext(collection, collection),
                JrpcResponseContext = new KestrelJrpcResponseContext(collection)
            };

            return(context);
        }
Ejemplo n.º 6
0
        public Task Invoke(HttpContext context)
        {
            var owinEnvironment = new OwinEnvironment(context);
            var owinFeatures    = new OwinFeatureCollection(owinEnvironment);

            var requestId     = owinFeatures.Environment.ContainsKey("owin.RequestId") ? owinFeatures.Environment["owin.RequestId"] : string.Empty;
            var requestMethod = owinFeatures.Environment.ContainsKey("owin.RequestMethod") ? owinFeatures.Environment["owin.RequestMethod"] : string.Empty;
            var requestPath   = owinFeatures.Environment.ContainsKey("owin.RequestPath") ? owinFeatures.Environment["owin.RequestPath"] : string.Empty;

            this.logger.LogInformation("[RequestId: {0}][RequestMethod: {1}][RequestPath: {2}]", requestId, requestMethod, requestPath);

            return(this.next(context));
        }
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                Func <IDictionary <string, object>, Task> appFunc = async env =>
                {
                    var owinFeatures = new OwinFeatureCollection(env);
                    var features = new FeatureCollection(owinFeatures);

                    var owinHttpResponse = features.Get <IHttpResponseFeature>();
                    features.Set <IHttpResponseFeature>(new NoOnStartingHttpResponseFeature(owinHttpResponse));

                    var context = application.CreateContext(features);
                    try
                    {
                        await application.ProcessRequestAsync(context);
                    }
                    catch (Exception ex)
                    {
                        application.DisposeContext(context, ex);
                        throw;
                    }

                    application.DisposeContext(context, null);
                };

                // Convert OWIN WebSockets to ASP.NET Core WebSockets
                appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

                // Wrap this into a middleware handler that Fos can accept
                Func <IDictionary <string, object>, Func <IDictionary <string, object>, Task>, Task> middlewareHandler = async(env, next) =>
                {
                    await appFunc(env);

                    if (next != null)
                    {
                        await next(env);
                    }
                };

                cgiServer = new FosSelfHost(builder =>
                {
                    builder.Use(middlewareHandler);
                });

                cgiServer.Bind(IPAddress.Loopback, 9000);
                cgiServer.Start(true);
            }, cancellationToken));
        }
Ejemplo n.º 8
0
    public void OwinHttpEnvironmentCanBeCreated()
    {
        var env = new Dictionary <string, object>
        {
            { "owin.RequestMethod", HttpMethods.Post },
            { "owin.RequestPath", "/path" },
            { "owin.RequestPathBase", "/pathBase" },
            { "owin.RequestQueryString", "name=value" },
        };
        var features = new OwinFeatureCollection(env);

        var requestFeature = Get <IHttpRequestFeature>(features);

        Assert.Equal(requestFeature.Method, HttpMethods.Post);
        Assert.Equal("/path", requestFeature.Path);
        Assert.Equal("/pathBase", requestFeature.PathBase);
        Assert.Equal("?name=value", requestFeature.QueryString);
    }
        internal RequestDelegate Middleware(RequestDelegate nextMiddleware)
        {
            RequestDelegate appFunc =
                async(HttpContext context) =>
            {
                var environment = new OwinEnvironment(context);
                var features    = new OwinFeatureCollection(environment);

                if (!features.Environment.ContainsKey("its.SessionState"))
                {
                    var state = new Dictionary <string, object>( );
                    features.Environment["its.SessionState"] = state;
                }

                await this.InvokeAt(context, 0);
            };

            return(appFunc);
        }
Ejemplo n.º 10
0
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            // Note that this example does not take into account of Nowin's "server.OnSendingHeaders" callback.
            // Ideally we should ensure this method is fired before disposing the context.
            Invoke = async env =>
            {
                // try without wrapping aspnet trys to add so it doesn't work
                // var features = new OwinFeatureCollection(env);

                // The reason for 2 level of wrapping is because the OwinFeatureCollection isn't mutable
                // so features can't be added
                var owinFeatures = new OwinFeatureCollection(env);

                // enumerating the env owin.ResponseHeaders causes Content-Type to default to text/html which messes up content negotiation
                // set it to empty so AspNetCore can decide the final Content-Type
                owinFeatures.Get <IHttpResponseFeature>().Headers["Content-Type"] = "";

                var features = new FeatureCollection(owinFeatures);

                var context = application.CreateContext(features);

                try
                {
                    await application.ProcessRequestAsync(context);
                }
                catch (Exception ex)
                {
                    application.DisposeContext(context, ex);
                    throw;
                }

                application.DisposeContext(context, null);
            };

            // Add the web socket adapter so we can turn OWIN websockets into ASP.NET Core compatible web sockets.
            // The calling pattern is a bit different
            // Invoke = OwinWebSocketAcceptAdapter.AdaptWebSockets(Invoke);

            return(Task.CompletedTask);
        }
Ejemplo n.º 11
0
    public void OwinHttpEnvironmentCanBeModified()
    {
        var env = new Dictionary <string, object>
        {
            { "owin.RequestMethod", HttpMethods.Post },
            { "owin.RequestPath", "/path" },
            { "owin.RequestPathBase", "/pathBase" },
            { "owin.RequestQueryString", "name=value" },
        };
        var features = new OwinFeatureCollection(env);

        var requestFeature = Get <IHttpRequestFeature>(features);

        requestFeature.Method      = HttpMethods.Get;
        requestFeature.Path        = "/path2";
        requestFeature.PathBase    = "/pathBase2";
        requestFeature.QueryString = "?name=value2";

        Assert.Equal(HttpMethods.Get, Get <string>(env, "owin.RequestMethod"));
        Assert.Equal("/path2", Get <string>(env, "owin.RequestPath"));
        Assert.Equal("/pathBase2", Get <string>(env, "owin.RequestPathBase"));
        Assert.Equal("name=value2", Get <string>(env, "owin.RequestQueryString"));
    }