コード例 #1
0
ファイル: OwinHandler.cs プロジェクト: parhelia512/NRack
        public void ProcessRequest(IDictionary <string, object> environment,
                                   ResponseCallBack responseCallBack, Action <Exception> errorCallback)
        {
            Dictionary <string, dynamic> nrackEnvironment = GetNrackEnvironment(environment);

            var config   = ConfigResolver.GetRackConfigInstance();
            var builder  = new Builder(config.ExecuteStart);
            var response = new OwinResponseAdapter(builder.Call(nrackEnvironment));

            responseCallBack(response.Status, response.Headers.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToString()),
                             (next, error, complete) =>
            {
                try
                {
                    next(response.GetBody(), null);
                    complete();
                }
                catch (Exception ex)
                {
                    error(ex);
                }

                return(() => { });
            });
        }
コード例 #2
0
        public void ProcessRequest(HttpContext context)
        {
            if (_config == null)
            {
                _config = ConfigResolver.GetRackConfigInstance();
                _getBuilderInContext = () => new Builder(_config.ExecuteStart);
            }

            var rawEnvironment = context.Request.ServerVariables;
            Dictionary <string, dynamic> environment =
                rawEnvironment.AllKeys.ToDictionary(key => key, key => (object)rawEnvironment[key]);

            environment["SCRIPT_NAME"] = string.Empty;

            //if ((string)environment["SCRIPT_NAME"] == string.Empty)
            //{
            //    environment["SCRIPT_NAME"] = "/";
            //}

            var rackEnvs = new Dictionary <string, dynamic>
            {
                { "rack.version", RackVersion.Version },
                { "rack.input", context.Request.InputStream },
                { "rack.errors", Console.OpenStandardError() },
                { "rack.multithread", true },
                { "rack.multiprocess", false },
                { "rack.run_once", false },
                { "rack.url_scheme", context.Request.IsSecureConnection ? "https" : "http" }
            };

            environment = environment.Union(rackEnvs).ToDictionary(key => key.Key, val => val.Value);

            //if (!environment.ContainsKey("SCRIPT_NAME"))
            //{
            //    environment["SCRIPT_NAME"] = string.Empty;
            //}

            var builder       = _getBuilderInContext();
            var responseArray = builder.Call(environment);

            var response = AspNetResponse.Create(responseArray);

            context.Response.StatusCode = response.StatusCode;

            //context.Response.Headers.Add(response.Headers);
            if (response.Headers != null)
            {
                foreach (var key in response.Headers.AllKeys)
                {
                    context.Response.AddHeader(key, response.Headers[key]);
                }
            }

            response.Body.Each((Action <dynamic>)(body =>
            {
                if (body is string)
                {
                    context.Response.Write(body);
                }
                else if (body is byte[])
                {
                    context.Response.BinaryWrite(body);
                }
            }));

            var methodInfos = (IEnumerable <MethodInfo>)response.Body.GetType().GetMethods();

            var closeMethods = Enumerable.Where(methodInfos, method => method.Name == "Close");

            foreach (var method in closeMethods)
            {
                if (method.GetParameters().Length == 0 && method.ReturnType.Equals(typeof(void)))
                {
                    method.Invoke(response.Body, null);
                    break;
                }
            }
        }