public void ApplyMatchingTamperingRules(IRequest request, TamperingContext context, string selectedServer)
        {
            List <ApplicationBinding> bindings;

            if (serverAppsBindings.TryGetValue(selectedServer, out bindings))
            {
                SortedList <float, ApplicationBinding> matchedBindings = new SortedList <float, ApplicationBinding>();
                foreach (var binding in bindings)
                {
                    float matchingPoints = 0;
                    if (context.IsIpAddressValidForRedirection(binding.HostnameOrIpAddress))
                    {
                        matchingPoints += 3;
                    }
                    if (context.IsPortValidForRedirection(binding.Port))
                    {
                        matchingPoints += 3;
                    }
                    if (IsHostMatchingBinding(binding, context.HostHeader ?? request.Host))
                    {
                        matchingPoints += 2 + binding.HostForHeader.Length / 100.0f;
                    }
                    if (string.Equals(binding.Protocol, request.Protocol, StringComparison.OrdinalIgnoreCase))
                    {
                        matchingPoints += 1;
                    }
                    if (matchingPoints > 1 && !matchedBindings.ContainsKey(matchingPoints))
                    {
                        matchedBindings.Add(matchingPoints, binding);
                    }
                }

                if (matchedBindings.Any())
                {
                    var bestFoundBinding = matchedBindings.Values.Last();
                    context.Protocol = context.Protocol ?? bestFoundBinding.Protocol;
                    context.ServerTcpAddressWithPort = string.Format("{0}:{1}", bestFoundBinding.HostnameOrIpAddress,
                                                                     bestFoundBinding.Port);
                    context.HostHeader = bestFoundBinding.HostForHeader;
                }
            }
        }
        public void ApplyMatchingTamperingRules(IRequest request, TamperingContext context)
        {
            foreach (var transform in transformations)
            {
                var hostMatch         = transform.RegexToMatchAgainsHost.Match(request.Host);
                var pathAndQueryMatch = transform.RegexToMatchAgainstPathAndQuery.Match(request.PathAndQuery);
                if (hostMatch.Success && pathAndQueryMatch.Success)
                {
                    var matchedPathAndQuery = new StringBuilder(transform.DestinationPathAndQuery);
                    for (int i = 1; i < pathAndQueryMatch.Groups.Count; i++)
                    {
                        matchedPathAndQuery = matchedPathAndQuery.Replace("$" + i, pathAndQueryMatch.Groups[i].Value);
                    }
                    matchedPathAndQuery.Insert(0, request.PathAndQuery.Substring(0, pathAndQueryMatch.Index));
                    matchedPathAndQuery.Append(request.PathAndQuery.Substring(pathAndQueryMatch.Index + pathAndQueryMatch.Length));

                    context.PathAndQuery            = matchedPathAndQuery.Length == 0 ? null : matchedPathAndQuery.ToString();
                    context.HostHeader              = transform.DestinationHostHeader;
                    context.CustomServerIpAddresses = transform.DestinationIpAddresses;
                    context.CustomServerPorts       = transform.DestinationPorts;
                    break;
                }
            }
        }