654
654
class CFWebProxy : IWebProxy {
655
ICredentials credentials;
655
658
public CFWebProxy ()
660
662
public ICredentials Credentials {
663
get { return credentials; }
665
userSpecified = true;
664
static Uri GetProxyUri (CFProxy proxy)
670
static Uri GetProxyUri (CFProxy proxy, out NetworkCredential credentials)
681
688
string password = proxy.Password;
682
689
string hostname = proxy.HostName;
683
690
int port = proxy.Port;
687
if (username != null) {
688
if (password != null)
689
userinfo = Uri.EscapeDataString (username) + ':' + Uri.EscapeDataString (password) + '@';
691
userinfo = Uri.EscapeDataString (username) + '@';
693
userinfo = string.Empty;
693
if (username != null)
694
credentials = new NetworkCredential (username, password);
696
uri = protocol + userinfo + hostname + (port != 0 ? ':' + port.ToString () : string.Empty);
698
uri = protocol + hostname + (port != 0 ? ':' + port.ToString () : string.Empty);
698
700
return new Uri (uri, UriKind.Absolute);
701
static Uri GetProxyUriFromScript (NSString script, Uri targetUri)
703
static Uri GetProxyUriFromScript (NSString script, Uri targetUri, out NetworkCredential credentials)
703
705
CFProxy[] proxies = CFNetwork.GetProxiesForAutoConfigurationScript (script, targetUri);
707
if (proxies == null) {
706
709
return targetUri;
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:
717
721
// unsupported proxy type, try the next one
719
723
case CFProxyType.None:
720
724
// no proxy should be used
721
726
return targetUri;
728
735
public Uri GetProxy (Uri targetUri)
737
NetworkCredential credentials = null;
730
740
if (targetUri == null)
731
741
throw new ArgumentNullException ("targetUri");
734
744
CFProxySettings settings = CFNetwork.GetSystemProxySettings ();
735
745
CFProxy[] proxies = CFNetwork.GetProxiesForUri (targetUri, settings);
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)
747
case CFProxyType.AutoConfigurationUrl:
748
// unsupported proxy type (requires fetching script from remote url)
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
758
case CFProxyType.None:
759
// no proxy should be used
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);
753
case CFProxyType.AutoConfigurationUrl:
754
// unsupported proxy type (requires fetching script from remote url)
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);
762
case CFProxyType.SOCKS:
763
// unsupported proxy type, try the next one
765
case CFProxyType.None:
766
// no proxy should be used
773
// no supported proxies for this Uri, fall back to trying to connect to targetUri directly
765
780
// ignore errors while retrieving proxy data
767
// no supported proxies for this Uri, fall back to trying to connect to targetUri directly.
785
this.credentials = credentials;
771
790
public bool IsBypassed (Uri targetUri)