~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/CoreFoundation/CFProxySupport.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
652
652
#endif
653
653
                
654
654
                class CFWebProxy : IWebProxy {
 
655
                        ICredentials credentials;
 
656
                        bool userSpecified;
 
657
                        
655
658
                        public CFWebProxy ()
656
659
                        {
657
 
                                
658
660
                        }
659
661
                        
660
662
                        public ICredentials Credentials {
661
 
                                get; set;
 
663
                                get { return credentials; }
 
664
                                set {
 
665
                                        userSpecified = true;
 
666
                                        credentials = value;
 
667
                                }
662
668
                        }
663
669
                        
664
 
                        static Uri GetProxyUri (CFProxy proxy)
 
670
                        static Uri GetProxyUri (CFProxy proxy, out NetworkCredential credentials)
665
671
                        {
666
672
                                string protocol;
667
673
                                
674
680
                                        protocol = "http://";
675
681
                                        break;
676
682
                                default:
 
683
                                        credentials = null;
677
684
                                        return null;
678
685
                                }
679
686
                                
681
688
                                string password = proxy.Password;
682
689
                                string hostname = proxy.HostName;
683
690
                                int port = proxy.Port;
684
 
                                string userinfo;
685
691
                                string uri;
686
692
                                
687
 
                                if (username != null) {
688
 
                                        if (password != null)
689
 
                                                userinfo = Uri.EscapeDataString (username) + ':' + Uri.EscapeDataString (password) + '@';
690
 
                                        else
691
 
                                                userinfo = Uri.EscapeDataString (username) + '@';
692
 
                                } else {
693
 
                                        userinfo = string.Empty;
694
 
                                }
 
693
                                if (username != null)
 
694
                                        credentials = new NetworkCredential (username, password);
 
695
                                else
 
696
                                        credentials = null;
695
697
                                
696
 
                                uri = protocol + userinfo + hostname + (port != 0 ? ':' + port.ToString () : string.Empty);
 
698
                                uri = protocol + hostname + (port != 0 ? ':' + port.ToString () : string.Empty);
697
699
                                
698
700
                                return new Uri (uri, UriKind.Absolute);
699
701
                        }
700
702
                        
701
 
                        static Uri GetProxyUriFromScript (NSString script, Uri targetUri)
 
703
                        static Uri GetProxyUriFromScript (NSString script, Uri targetUri, out NetworkCredential credentials)
702
704
                        {
703
705
                                CFProxy[] proxies = CFNetwork.GetProxiesForAutoConfigurationScript (script, targetUri);
704
706
                                
705
 
                                if (proxies == null)
 
707
                                if (proxies == null) {
 
708
                                        credentials = null;
706
709
                                        return targetUri;
 
710
                                }
707
711
                                
708
712
                                for (int i = 0; i < proxies.Length; i++) {
709
713
                                        switch (proxies[i].ProxyType) {
711
715
                                        case CFProxyType.HTTP:
712
716
                                        case CFProxyType.FTP:
713
717
                                                // create a Uri based on the hostname/port/etc info
714
 
                                                return GetProxyUri (proxies[i]);
 
718
                                                return GetProxyUri (proxies[i], out credentials);
715
719
                                        case CFProxyType.SOCKS:
716
720
                                        default:
717
721
                                                // unsupported proxy type, try the next one
718
722
                                                break;
719
723
                                        case CFProxyType.None:
720
724
                                                // no proxy should be used
 
725
                                                credentials = null;
721
726
                                                return targetUri;
722
727
                                        }
723
728
                                }
724
729
                                
 
730
                                credentials = null;
 
731
                                
725
732
                                return null;
726
733
                        }
727
734
                        
728
735
                        public Uri GetProxy (Uri targetUri)
729
736
                        {
 
737
                                NetworkCredential credentials = null;
 
738
                                Uri proxy = null;
 
739
                                
730
740
                                if (targetUri == null)
731
741
                                        throw new ArgumentNullException ("targetUri");
732
742
                                
733
743
                                try {
734
744
                                        CFProxySettings settings = CFNetwork.GetSystemProxySettings ();
735
745
                                        CFProxy[] proxies = CFNetwork.GetProxiesForUri (targetUri, settings);
736
 
                                        Uri uri;
737
 
                                        
738
 
                                        if (proxies == null)
739
 
                                                return targetUri;
740
 
                                        
741
 
                                        for (int i = 0; i < proxies.Length; i++) {
742
 
                                                switch (proxies[i].ProxyType) {
743
 
                                                case CFProxyType.AutoConfigurationJavaScript:
744
 
                                                        if ((uri = GetProxyUriFromScript (proxies[i].AutoConfigurationJavaScript, targetUri)) != null)
745
 
                                                                return uri;
746
 
                                                        break;
747
 
                                                case CFProxyType.AutoConfigurationUrl:
748
 
                                                        // unsupported proxy type (requires fetching script from remote url)
749
 
                                                        break;
750
 
                                                case CFProxyType.HTTPS:
751
 
                                                case CFProxyType.HTTP:
752
 
                                                case CFProxyType.FTP:
753
 
                                                        // create a Uri based on the hostname/port/etc info
754
 
                                                        return GetProxyUri (proxies[i]);
755
 
                                                case CFProxyType.SOCKS:
756
 
                                                        // unsupported proxy type, try the next one
757
 
                                                        break;
758
 
                                                case CFProxyType.None:
759
 
                                                        // no proxy should be used
760
 
                                                        return targetUri;
761
 
                                                }
 
746
                                        
 
747
                                        if (proxies != null) {
 
748
                                                for (int i = 0; i < proxies.Length && proxy == null; i++) {
 
749
                                                        switch (proxies[i].ProxyType) {
 
750
                                                        case CFProxyType.AutoConfigurationJavaScript:
 
751
                                                                proxy = GetProxyUriFromScript (proxies[i].AutoConfigurationJavaScript, targetUri, out credentials);
 
752
                                                                break;
 
753
                                                        case CFProxyType.AutoConfigurationUrl:
 
754
                                                                // unsupported proxy type (requires fetching script from remote url)
 
755
                                                                break;
 
756
                                                        case CFProxyType.HTTPS:
 
757
                                                        case CFProxyType.HTTP:
 
758
                                                        case CFProxyType.FTP:
 
759
                                                                // create a Uri based on the hostname/port/etc info
 
760
                                                                proxy = GetProxyUri (proxies[i], out credentials);
 
761
                                                                break;
 
762
                                                        case CFProxyType.SOCKS:
 
763
                                                                // unsupported proxy type, try the next one
 
764
                                                                break;
 
765
                                                        case CFProxyType.None:
 
766
                                                                // no proxy should be used
 
767
                                                                proxy = targetUri;
 
768
                                                                break;
 
769
                                                        }
 
770
                                                }
 
771
                                                
 
772
                                                if (proxy == null) {
 
773
                                                        // no supported proxies for this Uri, fall back to trying to connect to targetUri directly
 
774
                                                        proxy = targetUri;
 
775
                                                }
 
776
                                        } else {
 
777
                                                proxy = targetUri;
762
778
                                        }
763
 
                                }
764
 
                                catch {
 
779
                                } catch {
765
780
                                        // ignore errors while retrieving proxy data
 
781
                                        proxy = targetUri;
766
782
                                }
767
 
                                // no supported proxies for this Uri, fall back to trying to connect to targetUri directly.
768
 
                                return targetUri;
 
783
                                
 
784
                                if (!userSpecified)
 
785
                                        this.credentials = credentials;
 
786
                                
 
787
                                return proxy;
769
788
                        }
770
789
                        
771
790
                        public bool IsBypassed (Uri targetUri)
777
796
                        }
778
797
                }
779
798
                
780
 
                static CFWebProxy defaultWebProxy;
781
799
                public static IWebProxy GetDefaultProxy ()
782
800
                {
783
 
                        if (defaultWebProxy == null)
784
 
                                defaultWebProxy = new CFWebProxy ();
785
 
                        
786
 
                        return defaultWebProxy;
 
801
                        return new CFWebProxy ();
787
802
                }
788
803
        }
789
804
}