Esempio n. 1
0
        public static RedirectTarget Create(string target, bool permanent, bool queryProcessRequired)
        {
            var item = new RedirectTarget();

            item.Update(target, permanent, queryProcessRequired);
            return(item);
        }
Esempio n. 2
0
        public static RedirectTarget CreateEmpty()
        {
            var item = new RedirectTarget {
                QueryProcess = RedirectQueryProcess.Ignored
            };

            return(item);
        }
Esempio n. 3
0
        public static RedirectTarget CreateDefaultRecord2()
        {
            var item = new RedirectTarget()
            {
                Target = ">" + DefaultRecordKey
            };

            return(item);
        }
Esempio n. 4
0
        public static RedirectTarget CreateDefault()
        {
            var item = new RedirectTarget()
            {
                Target = "https://www.google.com/",
                //Permanent = false,
                QueryProcess = RedirectQueryProcess.Ignored
            };

            return(item);
        }
Esempio n. 5
0
        public static RedirectTarget CreateDefaultRecord()
        {
            var item = new RedirectTarget()
            {
                Target = "https://www.github.com/",
                //Permanent = false,
                QueryProcess = RedirectQueryProcess.AppendDirectly
            };

            return(item);
        }
Esempio n. 6
0
        public static DomainSetting CreateEmpty(string managementKey = DefaultManagementKey)
        {
            var item = new DomainSetting
            {
                ManagementKey          = managementKey,
                DefaultTarget          = RedirectTarget.CreateEmpty(),
                IgnoreCaseWhenMatching = true,
                Redirects = new Dictionary <string, RedirectTarget>(StringComparer.OrdinalIgnoreCase)
            };

            return(item);
        }
Esempio n. 7
0
        public static DomainSetting CreateDefaultRecord()
        {
            var item = new DomainSetting
            {
                ManagementKey          = DefaultManagementKey,
                DefaultTarget          = RedirectTarget.CreateDefault(),
                IgnoreCaseWhenMatching = true,
                Redirects = new Dictionary <string, RedirectTarget>(StringComparer.OrdinalIgnoreCase)
                {
                    [RedirectTarget.DefaultRecordKey]  = RedirectTarget.CreateDefaultRecord(),
                    [RedirectTarget.DefaultRecordKey2] = RedirectTarget.CreateDefaultRecord2()
                }
            };

            return(item);
        }
Esempio n. 8
0
        static OtherResult HttpAddRedirect(HttpContext context, DomainSetting domain)
        {
            var address      = context.GetQueryTextParameter("Address");
            var target       = context.GetQueryTextParameter("Target");
            var permanent    = context.GetQueryBooleanParameter("Permanent");
            var queryProcess = context.GetQueryBooleanParameter("QueryProcess");

            var redirect = RedirectTarget.Create(target, permanent, queryProcess);

            if (domain.Redirects.TryAdd(address, redirect))
            {
                SettingHost.SaveSetting();
                return(new Status200Result <RedirectTargetWithAddress>(new RedirectTargetWithAddress(address, redirect)));
            }
            else
            {
                return(new Status409Result());
            }
        }
Esempio n. 9
0
        public static ServiceSetting CreateDefault()
        {
            var item = new ServiceSetting
            {
                //KestrelUrl = "http://localhost:40020",
                EnableStaticFiles            = true,
                PreferXForwardedHost         = true,
                DefaultTarget                = RedirectTarget.CreateDefault(),
                GlobalManagementKey          = DefaultGlobalManagementKey,
                GlobalManagementEnabledHosts = new HashSet <string>(StringComparer.OrdinalIgnoreCase),
                Domains = new Dictionary <string, DomainSetting>(StringComparer.OrdinalIgnoreCase)
                {
                    [DomainSetting.DefaultRecordKey] = DomainSetting.CreateDefaultRecord()
                },
                Aliases = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    ["localhost:8088"] = "localhost"
                }
            };

            return(item);
        }
Esempio n. 10
0
        static OtherResult HttpUpdateRedirect(HttpContext context, DomainSetting domain)
        {
            var address      = context.GetQueryTextParameter("Address");
            var newAddress   = context.GetQueryOptionalTextParameter("NewAddress");
            var target       = context.GetQueryTextParameter("Target");
            var permanent    = context.GetQueryBooleanParameter("Permanent");
            var queryProcess = context.GetQueryBooleanParameter("QueryProcess");

            if (newAddress != null && newAddress != address)
            {
                //Change domain name
                if (domain.Redirects.ContainsKey(newAddress))
                {
                    return(new Status409Result());
                }
                else if (domain.Redirects.Remove(address))
                {
                    var redirect = RedirectTarget.Create(target, permanent, queryProcess);
                    domain.Redirects.Add(newAddress, redirect);
                    SettingHost.SaveSetting();
                    return(new Status200Result <RedirectTargetWithAddress>(new RedirectTargetWithAddress(address, redirect)));
                }
                else
                {
                    return(new Status410Result());
                }
            }
            else if (domain.Redirects.TryGetValue(address, out RedirectTarget redirect))
            {
                redirect.Update(target, permanent, queryProcess);
                SettingHost.SaveSetting();
                return(new Status200Result <RedirectTargetWithAddress>(new RedirectTargetWithAddress(address, redirect)));
            }
            else
            {
                return(new Status410Result());
            }
        }
Esempio n. 11
0
        static void Redirect(this HttpContext context, RedirectTarget target)
        {
            var url = target.Target;

            var query = context.Request.QueryString;

            if (query.HasValue && query.Value.Length > 0)
            {
                if (target.QueryProcess == RedirectQueryProcess.AppendDirectly)
                {
                    url += query.Value;
                }
                else if (target.QueryProcess == RedirectQueryProcess.AppendRemovingLeadingQuestionMark)
                {
                    if (query.Value.StartsWith("?"))
                    {
                        url += "&" + query.Value.Substring(1);
                    }
                    else
                    {
                        url += "&" + query.Value;
                    }
                }
            }

            context.Response.Headers[HeaderNames.Location] = url;

            if (target.Permanent)
            {
                context.Response.StatusCode = StatusCodes.Status308PermanentRedirect;
            }
            else
            {
                context.Response.StatusCode = StatusCodes.Status307TemporaryRedirect;
            }
        }
Esempio n. 12
0
 static bool TryLocateRedirect(string address, IReadOnlyDictionary <string, RedirectTarget> records, out RedirectTarget target)
 {
     if (records.TryGetValue(address, out var redirectTarget))
     {
         if (redirectTarget.Target.StartsWith(">"))
         {
             const int maxRedirectLimit = 16;
             var       maxRedirect      = maxRedirectLimit;
             address = redirectTarget.Target[1..];
Esempio n. 13
0
        static bool TryLocateRedirect(string address, Dictionary <string, RedirectTarget> records, out RedirectTarget target)
        {
            if (records.TryGetValue(address, out RedirectTarget redirectTarget))
            {
                if (redirectTarget.Target.StartsWith(">"))
                {
                    const int MaxRedirect = 16;
                    int       maxRedirect = MaxRedirect;
                    address = redirectTarget.Target.Substring(1);

                    while (records.TryGetValue(address, out redirectTarget))
                    {
                        if (redirectTarget.Target.StartsWith(">"))
                        {
                            if (maxRedirect == 0)
                            {
                                //max redirect exceeded
                                target = null;
                                return(false);
                            }
                            else
                            {
                                address = redirectTarget.Target.Substring(1);
                                maxRedirect--;
                            }
                        }
                        else
                        {
                            //redirect remap found
                            target = redirectTarget;
                            return(true);
                        }
                    }

                    //redirect remap not found
                    target = null;
                    return(false);
                }
                else
                {
                    //no remap
                    target = redirectTarget;
                    return(true);
                }
            }
            else
            {
                //No record
                target = null;
                return(false);
            }
        }