예제 #1
0
        public bool ShouldTryHandle(Kooboo.Data.Context.RenderContext context, JsTestOption Options)
        {
            if (string.IsNullOrEmpty(Options.RequestPrefix))
            {
                return(true);
            }
            string RelativeUrl = context.Request.RawRelativeUrl;

            if (RelativeUrl.ToLower().StartsWith(Options.RequestPrefix.ToLower()))
            {
                return(true);
            }
            return(false);
        }
예제 #2
0
        public static string GetSetCulture(WebSite WebSite, RenderContext context)
        {
            if (WebSite == null)
            {
                return(null);
            }

            if (!WebSite.EnableMultilingual)
            {
                return(WebSite.DefaultCulture);
            }

            var request = context.Request;

            if (!string.IsNullOrWhiteSpace(request.Culture))
            {
                SetCultureCookie(context, request.Culture);
                return(request.Culture);
            }
            string culture = request.GetValue("lang", "language");

            if (!string.IsNullOrEmpty(culture) && WebSite.Culture.ContainsKeyIgnoreCase(culture))
            {
                return(culture);
            }

            if (WebSite.EnableSitePath)
            {
                if (context.Request.RawRelativeUrl.Length > 3)
                {
                    int    NextSlash = request.RawRelativeUrl.IndexOf("/", 1);
                    string path      = string.Empty;
                    if (NextSlash > 0)
                    {
                        path = request.RawRelativeUrl.Substring(1, NextSlash - 1);
                    }
                    else
                    {
                        path = request.RawRelativeUrl.Substring(1);
                    }

                    if (!string.IsNullOrEmpty(path))
                    {
                        string lowerpath = path.ToLower();
                        foreach (var item in WebSite.SitePath)
                        {
                            if (!string.IsNullOrEmpty(item.Value))
                            {
                                if (item.Value.ToLower() == lowerpath)
                                {
                                    request.RelativeUrl = request.RawRelativeUrl.Substring(NextSlash);

                                    SetCultureCookie(context, item.Key);
                                    return(item.Key);
                                }
                            }
                        }
                    }
                }
            }


            if (context.Request.Cookies.ContainsKey("_kooboo_culture"))
            {
                var cookieculture = context.Request.Cookies.GetValue("_kooboo_culture");

                if (WebSite.Culture.ContainsKey(cookieculture))
                {
                    return(cookieculture);
                }
            }

            // Auto Detect culture .
            if (WebSite.AutoDetectCulture)
            {
                string detected = DetectCulture(WebSite, context.Request);
                SetCultureCookie(context, detected);
                return(detected);
            }
            else
            {
                return(WebSite.DefaultCulture);
            }
        }
예제 #3
0
 public DataContext(RenderContext context)
 {
     this.renderContext = context;
 }
예제 #4
0
        public static async Task SetResponse(HttpContext context, RenderContext renderContext)
        {
            var response = renderContext.Response;

            var header = context.Features.Response.Headers as Kooboo.HttpServer.Http.HttpResponseHeaders;

            context.Features.Response.StatusCode = response.StatusCode;

            if (response.StatusCode == 200)
            {
                context.Features.Response.Headers["Server"] = "http://www.kooboo.com";

                foreach (var item in response.Headers)
                {
                    context.Features.Response.Headers[item.Key] = item.Value;
                }

                foreach (var item in response.DeletedCookieNames)
                {
                    // context.Features.Response.Cookies.Delete(item);
                    var options = new CookieOptions()
                    {
                        Domain  = renderContext.Request.Host,
                        Path    = "/",
                        Expires = DateTime.Now.AddDays(-30)
                    };
                    context.Features.Response.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))
                    //{
                    //    context.Features.Response.Cookies.Append(item.Name, item.Value);
                    //}
                    //else
                    //{

                    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
                    };

                    context.Features.Response.Cookies.Append(item.Name, item.Value, options);
                    // }
                }

                header.HeaderContentType = response.ContentType;

                if (response.Body != null && context.Features.Request.Method.ToLower() != "head")
                {
                    //int len = response.Body.Length;
                    //if (len > 0)
                    //{
                    try
                    {
                        header.ContentLength = response.Body.Length;
                        await context.Features.Response.Body.WriteAsync(response.Body, 0, response.Body.Length).ConfigureAwait(false);
                    }
                    catch (Exception)
                    {
                        context.Features.Response.Body.Close();
                    }
                    //}
                }
                else
                {
                    if (response.Stream != null)
                    {
                        response.Stream.CopyTo(context.Features.Response.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);
                            header.ContentLength = bytes.Length;
                            context.Features.Response.StatusCode = 404;
                            await context.Features.Response.Body.WriteAsync(bytes, 0, bytes.Length);
                        }
                    }
                }
            }
            else
            {
                string location = response.RedirectLocation;

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

                    context.Features.Response.Cookies.Append(item, "", options);
                }

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

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

                    context.Features.Response.Cookies.Append(item.Name, item.Value, options);
                }

                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)
                    {
                        context.Features.Response.StatusCode = response.StatusCode;
                    }
                    //status code doesn't start with 3xx,it'will not redirect.
                    if (!response.StatusCode.ToString().StartsWith("3"))
                    {
                        context.Features.Response.StatusCode = StatusCodes.Status302Found;
                    }

                    header.HeaderLocation = newUrl;

                    context.Features.Response.Body.Dispose();

                    Log(renderContext);
                    return;
                }

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

                    context.Features.Response.Body.Dispose();
                    Log(renderContext);
                    return;
                }

                context.Features.Response.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 context.Features.Response.Body.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
            }


            context.Features.Response.Body.Dispose();
            Log(renderContext);
            context = null;
        }