Example #1
0
        public void ExecuteRequest(IDictionary<string, object> env, ResultDelegate result, Action<Exception> fault)
        {
            var environment = new Environment(env);
            var response = new Response(result);

            if (Verbose) Console.WriteLine("Received {0} - {1}", environment.Method, environment.Path);

            var routeData = determineRouteData(environment);
            if (routeData == null)
            {
                // TODO -- try to do it by mapping the files
                write404(response);
            }
            else
            {
                var request = new OwinRequestBody(environment);

                var bodyDelegate = environment["owin.RequestBody"] as BodyDelegate;
                if (bodyDelegate == null)
                {
                    executeRoute(routeData, response, request);
                }
                else
                {
                    var bodyBuilder = request.GetRequestBodyBuilder();
                    bodyDelegate(bodyBuilder, fault, () => executeRoute(routeData, response, request));
                }
            }

            if (Verbose) Console.WriteLine(" ({0})", response.Status);
        }
Example #2
0
        /// <summary>
        /// Based on http://owin.org/spec/owin-1.0.0draft5.html#URIReconstruction
        /// </summary>
        private Uri BuildUrl(Gate.Environment env)
        {
            string url = env.Scheme + "://" + env.Headers.GetHeader("Host") + env.PathBase + env.Path;

            if (!String.IsNullOrEmpty(env.QueryString))
            {
                url += "?" + env.QueryString;
            }

            return(new Uri(url));
        }
Example #3
0
        public OwinRequest(IDictionary <string, object> environment, string body)
        {
            var env     = new Gate.Environment(environment);
            var headers = env.Headers;

            Url     = BuildUrl(env);
            Headers = new NameValueCollection();

            foreach (var pair in headers)
            {
                foreach (var value in pair.Value)
                {
                    Headers.Add(pair.Key, value);
                }
            }

            _cookies    = new CookieManager();
            QueryString = HttpUtility.ParseDelimited(env.QueryString);
            Form        = HttpUtility.ParseDelimited(body);
        }
Example #4
0
        public OwinRequest(IDictionary<string, object> environment, string body)
        {
            var env = new Gate.Environment(environment);
            var headers = env.Headers;

            Url = BuildUrl(env);
            Headers = new NameValueCollection();

            foreach (var pair in headers)
            {
                foreach (var value in pair.Value)
                {
                    Headers.Add(pair.Key, value);
                }
            }

            _cookies = new CookieManager();
            QueryString = HttpUtility.ParseDelimited(env.QueryString);
            Form = HttpUtility.ParseDelimited(body);
        }
Example #5
0
        public OwinRequest(IDictionary<string, object> environment, string body)
        {
            var env = new Gate.Environment(environment);
            var headers = env.Headers;

            Url = BuildUrl(env);
            Headers = new NameValueCollection();

            foreach (var pair in headers)
            {
                foreach (var value in pair.Value)
                {
                    Headers.Add(pair.Key, value);
                }
            }

            // TODO: Add a cookie source that parses the cookie header
            Cookies = new RequestCookieCollection(new Cookie[0]);
            QueryString = ParseDelimited(env.QueryString);
            Form = ParseDelimited(body);
        }
Example #6
0
        public void ExecuteRequest(IDictionary <string, object> env, ResultDelegate result, Action <Exception> fault)
        {
            var environment = new Environment(env);
            var response    = new Response(result);


            if (Verbose)
            {
                Console.WriteLine("Received {0} - {1}", environment.Method, environment.Path);
            }

            var routeData = determineRouteData(environment);

            if (routeData == null)
            {
                // TODO -- try to do it by mapping the files
                write404(response);
            }
            else
            {
                var request = new OwinRequestBody(environment);

                var bodyDelegate = environment["owin.RequestBody"] as BodyDelegate;
                if (bodyDelegate == null)
                {
                    executeRoute(routeData, response, request);
                }
                else
                {
                    var bodyBuilder = request.GetRequestBodyBuilder();
                    bodyDelegate(bodyBuilder, fault, () => executeRoute(routeData, response, request));
                }
            }


            if (Verbose)
            {
                Console.WriteLine(" ({0})", response.Status);
            }
        }
Example #7
0
        private RouteData determineRouteData(Environment environment)
        {
            var context = new GateHttpContext(environment.Path, environment.Method);

            return(RouteTable.Routes.GetRouteData(context));
        }
Example #8
0
 private RouteData determineRouteData(Environment environment)
 {
     var context = new GateHttpContext(environment.Path, environment.Method);
     return RouteTable.Routes.GetRouteData(context);
 }
Example #9
0
        public void Execute(
            object transaction,
            String[] knownServerVariables,
            String[] knownRequestHeaders,
            String[] unknownRequestHeaderNames,
            String[] unknownRequestHeaderValues)
        {
            var requestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            for(var knownRequestHeader = 0; knownRequestHeader != CrosswalkModule.KnownRequestHeaderNames.Length; ++knownRequestHeader)
            {
                if (!string.IsNullOrEmpty(knownRequestHeaders[knownRequestHeader]))
                    requestHeaders[CrosswalkModule.KnownRequestHeaderNames[knownRequestHeader]] = knownRequestHeaders[knownRequestHeader];
            }
            for(var unknownRequestHeader = 0; unknownRequestHeader != unknownRequestHeaderNames.Length; ++unknownRequestHeader)
            {
                if (!string.IsNullOrEmpty(unknownRequestHeaderValues[unknownRequestHeader]))
                    requestHeaders[unknownRequestHeaderNames[unknownRequestHeader]] = unknownRequestHeaderValues[unknownRequestHeader];
            }

            var env = new Environment
            {
                Version = "1.0",
                Method = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.RequestMethod],
                Scheme = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.ServerProtocol],
                PathBase = "",
                Path = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.ScriptName],
                QueryString = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.QueryString],
                Headers = requestHeaders,
                Body = null,
            };
            for(var knownServerVariable = 0; knownServerVariable != CrosswalkModule.KnownServerVariableNames.Length; ++knownServerVariable)
            {
                env["server."+CrosswalkModule.KnownServerVariableNames[knownServerVariable]] = knownServerVariables[knownServerVariable];
            }

            _app(
                env,
                (status, headers, body) =>
                {
                    var headerNames = new string[headers.Count];
                    var headerValues = new string[headers.Count];
                    var headerCount = 0;
                    foreach (var kv in headers)
                    {
                        headerNames[headerCount] = kv.Key;
                        headerValues[headerCount] = kv.Value;
                        ++headerCount;
                    }
                    CrosswalkModule.Call.ResponseStart(transaction, status, headerCount, headerNames, headerValues);

                    // non-existant response body replaced with self-completing call
                    if (body == null)
                        body = (next, error, complete) => { complete(); return () => { }; };

                    body(
                        (data, continuation) =>
                        {
                            if (continuation == null)
                            {
                                bool ignored;
                                CrosswalkModule.Call.ResponseBody(transaction, data.Array, data.Offset, data.Count, null, out ignored);
                                return false;
                            }

                            var pins = new GCHandle[2];
                            CrosswalkModule.ContinuationDelegate callback = () =>
                            {
                                pins[0].Free();
                                pins[1].Free();
                                continuation();
                            };
                            pins[0] = GCHandle.Alloc(data.Array, GCHandleType.Pinned); // prevent byte[] from relocating while async send going on
                            pins[1] = GCHandle.Alloc(callback, GCHandleType.Normal); // prevent delegate from being collected while native callback pending

                            bool async;
                            CrosswalkModule.Call.ResponseBody(transaction, data.Array, data.Offset, data.Count, callback, out async);
                            if (async)
                            {
                                return true;
                            }

                            pins[0].Free();
                            pins[1].Free();
                            return false;
                        },
                        ex2 => CrosswalkModule.Call.ResponseComplete(transaction, Marshal.GetHRForException(ex2)),
                        () => CrosswalkModule.Call.ResponseComplete(transaction, 0));
                },
                ex => CrosswalkModule.Call.ResponseComplete(transaction, Marshal.GetHRForException(ex)));
        }
Example #10
0
        public void Execute(
            object transaction,
            String[] knownServerVariables,
            String[] knownRequestHeaders,
            String[] unknownRequestHeaderNames,
            String[] unknownRequestHeaderValues)
        {
            var requestHeaders = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            for (var knownRequestHeader = 0; knownRequestHeader != CrosswalkModule.KnownRequestHeaderNames.Length; ++knownRequestHeader)
            {
                if (!string.IsNullOrEmpty(knownRequestHeaders[knownRequestHeader]))
                {
                    requestHeaders[CrosswalkModule.KnownRequestHeaderNames[knownRequestHeader]] = knownRequestHeaders[knownRequestHeader];
                }
            }
            for (var unknownRequestHeader = 0; unknownRequestHeader != unknownRequestHeaderNames.Length; ++unknownRequestHeader)
            {
                if (!string.IsNullOrEmpty(unknownRequestHeaderValues[unknownRequestHeader]))
                {
                    requestHeaders[unknownRequestHeaderNames[unknownRequestHeader]] = unknownRequestHeaderValues[unknownRequestHeader];
                }
            }

            var env = new Environment
            {
                Version     = "1.0",
                Method      = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.RequestMethod],
                Scheme      = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.ServerProtocol],
                PathBase    = "",
                Path        = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.ScriptName],
                QueryString = knownServerVariables[(int)CrosswalkModule.KnownServerVariables.QueryString],
                Headers     = requestHeaders,
                Body        = null,
            };

            for (var knownServerVariable = 0; knownServerVariable != CrosswalkModule.KnownServerVariableNames.Length; ++knownServerVariable)
            {
                env["server." + CrosswalkModule.KnownServerVariableNames[knownServerVariable]] = knownServerVariables[knownServerVariable];
            }

            _app(
                env,
                (status, headers, body) =>
            {
                var headerNames  = new string[headers.Count];
                var headerValues = new string[headers.Count];
                var headerCount  = 0;
                foreach (var kv in headers)
                {
                    headerNames[headerCount]  = kv.Key;
                    headerValues[headerCount] = kv.Value;
                    ++headerCount;
                }
                CrosswalkModule.Call.ResponseStart(transaction, status, headerCount, headerNames, headerValues);

                // non-existant response body replaced with self-completing call
                if (body == null)
                {
                    body = (next, error, complete) => { complete(); return(() => { }); }
                }
                ;

                body(
                    (data, continuation) =>
                {
                    if (continuation == null)
                    {
                        bool ignored;
                        CrosswalkModule.Call.ResponseBody(transaction, data.Array, data.Offset, data.Count, null, out ignored);
                        return(false);
                    }

                    var pins = new GCHandle[2];
                    CrosswalkModule.ContinuationDelegate callback = () =>
                    {
                        pins[0].Free();
                        pins[1].Free();
                        continuation();
                    };
                    pins[0] = GCHandle.Alloc(data.Array, GCHandleType.Pinned);       // prevent byte[] from relocating while async send going on
                    pins[1] = GCHandle.Alloc(callback, GCHandleType.Normal);         // prevent delegate from being collected while native callback pending

                    bool async;
                    CrosswalkModule.Call.ResponseBody(transaction, data.Array, data.Offset, data.Count, callback, out async);
                    if (async)
                    {
                        return(true);
                    }

                    pins[0].Free();
                    pins[1].Free();
                    return(false);
                },
                    ex2 => CrosswalkModule.Call.ResponseComplete(transaction, Marshal.GetHRForException(ex2)),
                    () => CrosswalkModule.Call.ResponseComplete(transaction, 0));
            },
                ex => CrosswalkModule.Call.ResponseComplete(transaction, Marshal.GetHRForException(ex)));
        }