コード例 #1
0
ファイル: AspNetResponse.cs プロジェクト: kevinswiber/NRack
        public static AspNetResponse Create(dynamic[] parameters)
        {
            var response = new AspNetResponse
                               {
                                   StatusCode = GetStatusCode(parameters),
                                   Headers = GetHeaders(parameters),
                                   Body = GetBody(parameters)
                               };

            return response;
        }
コード例 #2
0
ファイル: AspNetResponse.cs プロジェクト: parhelia512/NRack
        public static AspNetResponse Create(dynamic[] parameters)
        {
            var response = new AspNetResponse
            {
                StatusCode = GetStatusCode(parameters),
                Headers    = GetHeaders(parameters),
                Body       = GetBody(parameters)
            };

            return(response);
        }
コード例 #3
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;
                }
            }
        }