Ejemplo n.º 1
0
        public void HttpProxy_CredentialParsing_Basic()
        {
            RemoteExecutor.Invoke(() =>
            {
                IWebProxy p;

                Environment.SetEnvironmentVariable("all_proxy", "http://*****:*****@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Assert.NotNull(p.Credentials);

                // Use user only without password.
                Environment.SetEnvironmentVariable("all_proxy", "http://[email protected]:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Assert.NotNull(p.Credentials);

                // Use different user for http and https
                Environment.SetEnvironmentVariable("https_proxy", "http://*****:*****@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Uri u = p.GetProxy(fooHttp);
                Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
                u = p.GetProxy(fooHttps);
                Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
                // This should not match Proxy Uri
                Assert.Null(p.Credentials.GetCredential(fooHttp, "Basic"));
                Assert.Null(p.Credentials.GetCredential(null, null));
            }).Dispose();
        }
        public void HttpProxy_CredentialParsing_Basic()
        {
            Environment.SetEnvironmentVariable("all_proxy", "http://*****:*****@1.1.1.1:3000");
            Assert.True(HttpEnvironmentProxy.TryCreate(out IWebProxy p));
            Assert.NotNull(p);
            Assert.NotNull(p.Credentials);

            // Use user only without password.
            Environment.SetEnvironmentVariable("all_proxy", "http://[email protected]:3000");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);
            Assert.NotNull(p.Credentials);

            // Use different user for http and https
            Environment.SetEnvironmentVariable("https_proxy", "http://*****:*****@1.1.1.1:3000");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);
            Uri u = p.GetProxy(s_fooHttp);

            Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
            u = p.GetProxy(s_fooHttps);
            Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
            // This should not match Proxy Uri
            Assert.Null(p.Credentials.GetCredential(s_fooHttp, "Basic"));
            Assert.Null(p.Credentials.GetCredential(null, null));
        }
Ejemplo n.º 3
0
        public void HttpProxy_CredentialParsing_Basic()
        {
            RemoteInvoke(() =>
            {
                IWebProxy p;

                Environment.SetEnvironmentVariable("all_proxy", "http://*****:*****@1.1.1.1:3000");
                p = HttpEnvironmentProxy.TryToCreate();
                Assert.NotNull(p);
                Assert.NotNull(p.Credentials);

                // Use user only without password.
                Environment.SetEnvironmentVariable("all_proxy", "http://[email protected]:3000");
                p = HttpEnvironmentProxy.TryToCreate();
                Assert.NotNull(p);
                Assert.NotNull(p.Credentials);

                // Use different user for http and https
                Environment.SetEnvironmentVariable("https_proxy", "http://*****:*****@1.1.1.1:3000");
                p = HttpEnvironmentProxy.TryToCreate();
                Assert.NotNull(p);
                Uri u = p.GetProxy(fooHttp);
                Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
                u = p.GetProxy(fooHttps);
                Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
                // This should not match Proxy Uri
                Assert.Null(p.Credentials.GetCredential(fooHttp, "Basic"));
                Assert.Null(p.Credentials.GetCredential(null, null));

                return(SuccessExitCode);
            }).Dispose();
        }
Ejemplo n.º 4
0
        public void HttpProxy_EnvironmentProxy_Loaded()
        {
            IWebProxy p;
            Uri       u;

            // It should not return object if there are no variables set.
            Assert.False(HttpEnvironmentProxy.TryCreate(out p));

            Environment.SetEnvironmentVariable("all_proxy", "http://1.1.1.1:3000");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);
            Assert.Null(p.Credentials);

            u = p.GetProxy(fooHttp);
            Assert.True(u != null && u.Host == "1.1.1.1");
            u = p.GetProxy(fooHttps);
            Assert.True(u != null && u.Host == "1.1.1.1");

            Environment.SetEnvironmentVariable("http_proxy", "http://1.1.1.2:3001");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);

            // Protocol specific variables should take precedence over all_
            // and https should still use all_proxy.
            u = p.GetProxy(fooHttp);
            Assert.True(u != null && u.Host == "1.1.1.2" && u.Port == 3001);
            u = p.GetProxy(fooHttps);
            Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000);

            // Set https to invalid strings and use only IP & port for http.
            Environment.SetEnvironmentVariable("http_proxy", "1.1.1.3:3003");
            Environment.SetEnvironmentVariable("https_proxy", "ab!cd");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);

            u = p.GetProxy(fooHttp);
            Assert.True(u != null && u.Host == "1.1.1.3" && u.Port == 3003);
            u = p.GetProxy(fooHttps);
            Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000);

            // Try valid URI with unsupported protocol. It will be ignored
            // to mimic curl behavior.
            Environment.SetEnvironmentVariable("https_proxy", "socks5://1.1.1.4:3004");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);
            u = p.GetProxy(fooHttps);
            Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000);

            // Set https to valid URI but different from http.
            Environment.SetEnvironmentVariable("https_proxy", "http://1.1.1.5:3005");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);

            u = p.GetProxy(fooHttp);
            Assert.True(u != null && u.Host == "1.1.1.3" && u.Port == 3003);
            u = p.GetProxy(fooHttps);
            Assert.True(u != null && u.Host == "1.1.1.5" && u.Port == 3005);
        }
Ejemplo n.º 5
0
        public void Ctor_NoEnvironmentVariables_NotHttpEnvironmentProxy()
        {
            RemoteExecutor.Invoke(() =>
            {
                IWebProxy proxy = SystemProxyInfo.ConstructSystemProxy();
                HttpEnvironmentProxy envProxy = proxy as HttpEnvironmentProxy;
                Assert.Null(envProxy);

                return(RemoteExecutor.SuccessExitCode);
            }).Dispose();
        }
Ejemplo n.º 6
0
        public void Ctor_ProxyEnvironmentVariableSet_IsHttpEnvironmentProxy()
        {
            var options = new RemoteInvokeOptions();

            options.StartInfo.EnvironmentVariables.Add("http_proxy", "http://proxy.contoso.com");
            RemoteExecutor.Invoke(() =>
            {
                IWebProxy proxy = SystemProxyInfo.ConstructSystemProxy();
                HttpEnvironmentProxy envProxy = proxy as HttpEnvironmentProxy;
                Assert.NotNull(envProxy);
            }, options).Dispose();
        }
Ejemplo n.º 7
0
        public void HttpProxy_Exceptions_Match()
        {
            IWebProxy p;

            Environment.SetEnvironmentVariable("no_proxy", ".test.com,, foo.com");
            Environment.SetEnvironmentVariable("all_proxy", "http://*****:*****@1.1.1.1:3000");
            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);

            Assert.True(p.IsBypassed(fooHttp));
            Assert.True(p.IsBypassed(fooHttps));
            Assert.True(p.IsBypassed(new Uri("http://test.com")));
            Assert.False(p.IsBypassed(new Uri("http://1test.com")));
            Assert.True(p.IsBypassed(new Uri("http://www.test.com")));
        }
Ejemplo n.º 8
0
        public static bool TryCreate(out IWebProxy proxy)
        {
            // Get environment variables. Protocol specific take precedence over
            // general all_*, lower case variable has precedence over upper case.
            // Note that curl uses HTTPS_PROXY but not HTTP_PROXY.

            Uri httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyLC));

            if (httpProxy == null && Environment.GetEnvironmentVariable(EnvCGI) == null)
            {
                httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyUC));
            }

            Uri httpsProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyLC)) ??
                             GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyUC));

            if (httpProxy == null || httpsProxy == null)
            {
                Uri allProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyLC)) ??
                               GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyUC));

                if (httpProxy == null)
                {
                    httpProxy = allProxy;
                }

                if (httpsProxy == null)
                {
                    httpsProxy = allProxy;
                }
            }

            // Do not instantiate if nothing is set.
            // Caller may pick some other proxy type.
            if (httpProxy == null && httpsProxy == null)
            {
                proxy = null;
                return(false);
            }

            string noProxy = Environment.GetEnvironmentVariable(EnvNoProxyLC) ??
                             Environment.GetEnvironmentVariable(EnvNoProxyUC);

            proxy = new HttpEnvironmentProxy(httpProxy, httpsProxy, noProxy);

            return(true);
        }
Ejemplo n.º 9
0
        public void HttpProxy_Exceptions_Match()
        {
            RemoteExecutor.Invoke(() =>
            {
                IWebProxy p;

                Environment.SetEnvironmentVariable("no_proxy", ".test.com,, foo.com");
                Environment.SetEnvironmentVariable("all_proxy", "http://*****:*****@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);

                Assert.True(p.IsBypassed(fooHttp));
                Assert.True(p.IsBypassed(fooHttps));
                Assert.True(p.IsBypassed(new Uri("http://test.com")));
                Assert.False(p.IsBypassed(new Uri("http://1test.com")));
                Assert.True(p.IsBypassed(new Uri("http://www.test.com")));
            }).Dispose();
        }
Ejemplo n.º 10
0
        public void HttpProxy_CredentialParsing_DefaultCredentials()
        {
            RemoteExecutor.Invoke(() =>
            {
                IWebProxy p;

                Environment.SetEnvironmentVariable("all_proxy", "http://:@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Assert.Equal(CredentialCache.DefaultCredentials, p.Credentials.GetCredential(p.GetProxy(fooHttp), ""));
                Assert.Equal(CredentialCache.DefaultCredentials, p.Credentials.GetCredential(p.GetProxy(fooHttps), ""));

                Environment.SetEnvironmentVariable("http_proxy", "http://:@[::1]:80");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Assert.Equal(CredentialCache.DefaultCredentials, p.Credentials.GetCredential(p.GetProxy(fooHttp), ""));
            }).Dispose();
        }
        public static bool TryCreate(out IWebProxy proxy)
        {
            // Get environment variables. Protocol specific take precedence over
            // general all_*. On Windows, environment variables are case insensitive.

            Uri httpProxy = null;

            if (Environment.GetEnvironmentVariable(EnvCGI) == null)
            {
                httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyUC));
            }

            Uri httpsProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyUC));

            if (httpProxy == null || httpsProxy == null)
            {
                Uri allProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyUC));

                if (httpProxy == null)
                {
                    httpProxy = allProxy;
                }

                if (httpsProxy == null)
                {
                    httpsProxy = allProxy;
                }
            }

            // Do not instantiate if nothing is set.
            // Caller may pick some other proxy type.
            if (httpProxy == null && httpsProxy == null)
            {
                proxy = null;
                return(false);
            }

            string noProxy = Environment.GetEnvironmentVariable(EnvNoProxyUC);

            proxy = new HttpEnvironmentProxy(httpProxy, httpsProxy, noProxy);

            return(true);
        }
Ejemplo n.º 12
0
        public void HttpProxy_Exceptions_Match()
        {
            RemoteInvoke(() =>
            {
                IWebProxy p;

                Environment.SetEnvironmentVariable("no_proxy", ".test.com,, foo.com");
                Environment.SetEnvironmentVariable("all_proxy", "http://*****:*****@1.1.1.1:3000");
                p = HttpEnvironmentProxy.TryToCreate();
                Assert.NotNull(p);

                Assert.True(p.IsBypassed(fooHttp));
                Assert.True(p.IsBypassed(fooHttps));
                Assert.True(p.IsBypassed(new Uri("http://test.com")));
                Assert.False(p.IsBypassed(new Uri("http://1test.com")));
                Assert.True(p.IsBypassed(new Uri("http://www.test.com")));

                return(SuccessExitCode);
            }).Dispose();
        }
Ejemplo n.º 13
0
        public void HttpProxy_Uri_Parsing(string input, string host, string port, string user, string password)
        {
            Environment.SetEnvironmentVariable("all_proxy", input);
            IWebProxy p;
            Uri       u;

            Assert.True(HttpEnvironmentProxy.TryCreate(out p));
            Assert.NotNull(p);

            u = p.GetProxy(fooHttp);
            Assert.AreEqual(host, u.Host);
            Assert.AreEqual(Convert.ToInt32(port), u.Port);

            if (user != null)
            {
                NetworkCredential nc = p.Credentials.GetCredential(u, "Basic");
                Assert.NotNull(nc);
                Assert.AreEqual(user, nc.UserName);
                Assert.AreEqual(password, nc.Password);
            }
        }
Ejemplo n.º 14
0
        public void HttpProxy_Uri_Parsing(string _input, string _host, string _port, string _user, string _password)
        {
            RemoteExecutor.Invoke((input, host, port, user, password) =>
            {
                // Remote exec does not allow to pass null at this moment.
                if (user == "null")
                {
                    user = null;
                }
                if (password == "null")
                {
                    password = null;
                }

                Environment.SetEnvironmentVariable("all_proxy", input);
                IWebProxy p;
                Uri u;

                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);

                u = p.GetProxy(fooHttp);
                Assert.Equal(host, u.Host);
                Assert.Equal(Convert.ToInt32(port), u.Port);

                if (user != null)
                {
                    NetworkCredential nc = p.Credentials.GetCredential(u, "Basic");
                    Assert.NotNull(nc);
                    Assert.Equal(user, nc.UserName);
                    Assert.Equal(password, nc.Password);
                }

                return(RemoteExecutor.SuccessExitCode);
            }, _input, _host, _port, _user ?? "null", _password ?? "null").Dispose();
        }
 // On Unix we get default proxy configuration from environment variables
 private static IWebProxy ConstructSystemProxy()
 {
     return(HttpEnvironmentProxy.TryToCreate());
 }