public HttpContext Create(IFeatureCollection featureCollection)
        {
            if (featureCollection == null)
            {
                throw new ArgumentNullException(nameof(featureCollection));
            }

            var responseCookiesFeature = new ResponseCookiesFeature(featureCollection, _builderPool);
            featureCollection.Set<IResponseCookiesFeature>(responseCookiesFeature);

            PooledHttpContext httpContext = null;
            lock (_pool)
            {
                if (_pool.Count != 0)
                {
                    httpContext = _pool.Pop();
                }
            }

            if (httpContext == null)
            {
                httpContext = new PooledHttpContext(featureCollection);
            }
            else
            {
                httpContext.Initialize(featureCollection);
            }

            if (_httpContextAccessor != null)
            {
                _httpContextAccessor.HttpContext = httpContext;
            }
            return httpContext;
        }
        public HttpContext Create(IFeatureCollection featureCollection)
        {
            if (featureCollection == null)
            {
                throw new ArgumentNullException(nameof(featureCollection));
            }

            var responseCookiesFeature = new ResponseCookiesFeature(featureCollection, _builderPool);
            featureCollection.Set<IResponseCookiesFeature>(responseCookiesFeature);

            var httpContext = new DefaultHttpContext(featureCollection);
            if (_httpContextAccessor != null)
            {
                _httpContextAccessor.HttpContext = httpContext;
            }

            var formFeature = new FormFeature(httpContext.Request, _formOptions);
            featureCollection.Set<IFormFeature>(formFeature);

            return httpContext;
        }
        public HttpContext Create(IFeatureCollection featureCollection)
        {
            if (featureCollection == null)
            {
                throw new ArgumentNullException(nameof(featureCollection));
            }

            var responseCookiesFeature = new ResponseCookiesFeature(featureCollection, _builderPool);

            featureCollection.Set <IResponseCookiesFeature>(responseCookiesFeature);

            PooledHttpContext httpContext = null;

            lock (_pool)
            {
                if (_pool.Count != 0)
                {
                    httpContext = _pool.Pop();
                }
            }

            if (httpContext == null)
            {
                httpContext = new PooledHttpContext(featureCollection);
            }
            else
            {
                httpContext.Initialize(featureCollection);
            }

            if (_httpContextAccessor != null)
            {
                _httpContextAccessor.HttpContext = httpContext;
            }
            return(httpContext);
        }
Esempio n. 4
0
        public HttpContext Create(IFeatureCollection featureCollection)
        {
            if (featureCollection == null)
            {
                throw new ArgumentNullException(nameof(featureCollection));
            }

            var responseCookiesFeature = new ResponseCookiesFeature(featureCollection, _builderPool);

            featureCollection.Set <IResponseCookiesFeature>(responseCookiesFeature);

            var httpContext = new DefaultHttpContext(featureCollection);

            if (_httpContextAccessor != null)
            {
                _httpContextAccessor.HttpContext = httpContext;
            }

            var formFeature = new FormFeature(httpContext.Request, _formOptions);

            featureCollection.Set <IFormFeature>(formFeature);

            return(httpContext);
        }
Esempio n. 5
0
            public static async Task SetResponse(RenderContext renderContext)
            {
                var feature  = renderContext.GetItem <IFeatureCollection>();
                var response = renderContext.Response;
                var res      = feature.Get <IHttpResponseFeature>();

                var request = feature.Get <IHttpRequestFeature>();

                res.Headers.Add("server", "http://www.kooboo.com");
                res.StatusCode = response.StatusCode;
                res.Headers.Add("Content-Type", response.ContentType);
                foreach (var item in response.Headers)
                {
                    res.Headers[item.Key] = item.Value;
                }


                #region cookie

                if (response.DeletedCookieNames.Any() || response.AppendedCookies.Any())
                {
                    var cookieres = feature.Get <IResponseCookiesFeature>();
                    if (cookieres == null)
                    {
                        cookieres = new ResponseCookiesFeature(feature);
                    }

                    foreach (var item in response.DeletedCookieNames)
                    {
                        var options = new CookieOptions()
                        {
                            Domain  = renderContext.Request.Host,
                            Path    = "/",
                            Expires = DateTime.Now.AddDays(-30)
                        };

                        cookieres.Cookies.Append(item, "", options);
                        response.AppendedCookies.RemoveAll(o => o.Name == item);
                    }

                    foreach (var item in response.AppendedCookies)
                    {
                        if (string.IsNullOrEmpty(item.Domain))
                        {
                            item.Domain = renderContext.Request.Host;
                        }
                        if (string.IsNullOrEmpty(item.Path))
                        {
                            item.Path = "/";
                        }


                        if (item.Expires == default(DateTime))
                        {
                            var time = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
                            time         = time.AddSeconds(-1);
                            item.Expires = time;
                        }

                        var options = new CookieOptions()
                        {
                            Domain  = item.Domain,
                            Path    = item.Path,
                            Expires = item.Expires
                        };

                        cookieres.Cookies.Append(item.Name, item.Value, options);
                    }
                }
                #endregion


                if (response.StatusCode == 200)
                {
                    if (response.Body != null && renderContext.Request.Method.ToLower() != "head")
                    {
                        try
                        {
                            res.Headers["Content-Length"] = response.Body.Length.ToString();
                            await res.Body.WriteAsync(response.Body, 0, response.Body.Length).ConfigureAwait(false);
                        }
                        catch (Exception)
                        {
                            res.Body.Close();
                        }
                    }
                    else
                    {
                        if (response.Stream != null)
                        {
                            await response.Stream.CopyToAsync(res.Body);
                        }
                        //else if (response.ContentType != null && response.ContentType.ToLower().Contains("javascript"))
                        //{
                        //    // TODO:???? what is this????
                        //}
                        else
                        {
                            // 404.
                            string filename = Lib.Helper.IOHelper.CombinePath(AppSettings.RootPath, Kooboo.DataConstants.Default404Page) + ".html";
                            if (System.IO.File.Exists(filename))
                            {
                                string text  = System.IO.File.ReadAllText(filename);
                                var    bytes = System.Text.Encoding.UTF8.GetBytes(text);
                                res.Headers["Content-Length"] = bytes.Length.ToString();
                                res.StatusCode = 404;
                                await res.Body.WriteAsync(bytes, 0, bytes.Length);
                            }
                        }
                    }
                }
                else
                {
                    string location = response.RedirectLocation;

                    if (!string.IsNullOrEmpty(location))
                    {
                        location = GetEncodedLocation(location);

                        var host = renderContext.Request.Port == 80 || renderContext.Request.Port == 443
                            ? renderContext.Request.Host
                            : string.Format("{0}:{1}", renderContext.Request.Host, renderContext.Request.Port);
                        string BaseUrl = renderContext.Request.Scheme + "://" + host + renderContext.Request.Path;
                        var    newUrl  = UrlHelper.Combine(BaseUrl, location);
                        if (response.StatusCode != 200)
                        {
                            res.StatusCode = response.StatusCode;
                        }
                        //status code doesn't start with 3xx,it'will not redirect.
                        if (!response.StatusCode.ToString().StartsWith("3"))
                        {
                            res.StatusCode = StatusCodes.Status302Found;
                        }

                        res.Headers["Content-Location"] = newUrl;

                        res.Body.Dispose();

                        Log(renderContext);
                        return;
                    }

                    if (response.Body != null && response.Body.Length > 0)
                    {
                        res.StatusCode = response.StatusCode;
                        await res.Body.WriteAsync(response.Body, 0, response.Body.Length).ConfigureAwait(false);

                        res.Body.Dispose();
                        Log(renderContext);
                        return;
                    }

                    res.StatusCode = response.StatusCode;
                    string responsebody = null;
                    switch (response.StatusCode)
                    {
                    case 404:
                        responsebody = " The requested resource not found";
                        break;

                    case 301:
                        responsebody = " The requested resource has moved.";
                        break;

                    case 302:
                        responsebody = " The requested resource has moved.";
                        break;

                    case 401:
                        responsebody = " Unauthorized access";
                        break;

                    case 403:
                        responsebody = " Unauthorized access";
                        break;

                    case 407:
                        responsebody = "Reach Limitation";
                        break;

                    case 500:
                        responsebody = "Internal server error";
                        break;

                    case 503:
                        responsebody = " Service Unavailable";
                        break;

                    default:
                        break;
                    }

                    if (string.IsNullOrEmpty(responsebody))
                    {
                        if (response.StatusCode >= 400 && response.StatusCode < 500)
                        {
                            responsebody = " Client error";
                        }
                        else if (response.StatusCode >= 500)
                        {
                            responsebody = " Server error";
                        }
                        else
                        {
                            responsebody = " Unknown error";
                        }
                    }
                    var bytes = Encoding.UTF8.GetBytes(responsebody);
                    await res.Body.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
                }


                res.Body.Dispose();
                Log(renderContext);
                res = null;
            }