55using System ;
66using System . Collections . Generic ;
77using System . IO ;
8+ using System . Linq ;
9+ using System . Net ;
810using System . Threading . Tasks ;
911
1012namespace CefSharp
@@ -14,6 +16,11 @@ namespace CefSharp
1416 /// </summary>
1517 public static class RequestContextExtensions
1618 {
19+ /// <summary>
20+ /// Array of valid proxy schemes
21+ /// </summary>
22+ private static string [ ] ProxySchemes = new string [ ] { "http" , "socks" , "socks4" , "socks5" } ;
23+
1724 /// <summary>
1825 /// Load an extension from the given directory. To load a crx file you must unzip it first.
1926 /// For further details see <seealso cref="IRequestContext.LoadExtension(string, string, IExtensionHandler)"/>
@@ -55,34 +62,39 @@ public static void LoadExtensionsFromDirectory(this IRequestContext requestConte
5562 /// </summary>
5663 /// <param name="requestContext">request context</param>
5764 /// <param name="scheme">is the protocol of the proxy server, and is one of: 'http', 'socks', 'socks4', 'socks5'. Also note that 'socks' is equivalent to 'socks5'.</param>
58- /// <param name="host">host</param>
59- /// <param name="port">post </param>
65+ /// <param name="host">proxy host</param>
66+ /// <param name="port">proxy port </param>
6067 /// <param name="errorMessage">error message</param>
6168 /// <returns>returns true if successfull, false otherwise.</returns>
6269 /// <remarks>Internally calls <seealso cref="IRequestContext.SetPreference(string, object, out string)"/> with
6370 /// preference 'proxy' and mode of 'fixed_servers'</remarks>
64- public static bool SetProxy ( this IRequestContext requestContext , string scheme , string host , string port , out string errorMessage )
71+ public static bool SetProxy ( this IRequestContext requestContext , string scheme , string host , int ? port , out string errorMessage )
6572 {
66- if ( string . IsNullOrWhiteSpace ( host ) )
73+ //Default to using http scheme if non provided
74+ if ( string . IsNullOrWhiteSpace ( scheme ) )
6775 {
68- throw new ArgumentException ( "Cannot be null or empty" , "host" ) ;
76+ scheme = "http" ;
6977 }
7078
71- if ( string . IsNullOrWhiteSpace ( port ) )
79+ if ( ! ProxySchemes . Contains ( scheme . ToLower ( ) ) )
7280 {
73- throw new ArgumentException ( "Cannot be null or empty " , port ) ;
81+ throw new ArgumentException ( "Invalid Scheme, see https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md#markdown-header-proxy-resolution for a list of valid schemes. " , "scheme" ) ;
7482 }
7583
76- //Default to using http scheme if non provided
77- if ( string . IsNullOrWhiteSpace ( scheme ) )
84+ if ( string . IsNullOrWhiteSpace ( host ) )
7885 {
79- scheme = "http" ;
86+ throw new ArgumentException ( "Cannot be null or empty" , "host" ) ;
87+ }
88+
89+ if ( port . HasValue && ( port < IPEndPoint . MinPort || port > IPEndPoint . MaxPort ) )
90+ {
91+ throw new ArgumentOutOfRangeException ( "port" , port , "Invalid TCP Port" ) ;
8092 }
8193
8294 var v = new Dictionary < string , object >
8395 {
8496 [ "mode" ] = "fixed_servers" ,
85- [ "server" ] = scheme + "://" + host + ":" + port
97+ [ "server" ] = scheme + "://" + host + ( port . HasValue ? ( ":" + port ) : "" )
8698 } ;
8799
88100 return requestContext . SetPreference ( "proxy" , v , out errorMessage ) ;
@@ -94,17 +106,33 @@ public static bool SetProxy(this IRequestContext requestContext, string scheme,
94106 /// MUST be called on the CEF UI Thread
95107 /// </summary>
96108 /// <param name="requestContext">request context</param>
97- /// <param name="host">host</param>
98- /// <param name="port">post </param>
109+ /// <param name="host">proxy host</param>
110+ /// <param name="port">proxy port </param>
99111 /// <param name="errorMessage">error message</param>
100112 /// <returns>returns true if successfull, false otherwise.</returns>
101113 /// <remarks>Internally calls <seealso cref="IRequestContext.SetPreference(string, object, out string)"/> with
102114 /// preference 'proxy' and mode of 'fixed_servers'</remarks>
103- public static bool SetProxy ( this IRequestContext requestContext , string host , string port , out string errorMessage )
115+ public static bool SetProxy ( this IRequestContext requestContext , string host , int ? port , out string errorMessage )
104116 {
105117 return requestContext . SetProxy ( null , host , port , out errorMessage ) ;
106118 }
107119
120+ /// <summary>
121+ /// Sets the proxy server for the specified <see cref="IRequestContext"/>.
122+ /// Protocol for the proxy server is http
123+ /// MUST be called on the CEF UI Thread
124+ /// </summary>
125+ /// <param name="requestContext">request context</param>
126+ /// <param name="host">proxy host</param>
127+ /// <param name="errorMessage">error message</param>
128+ /// <returns>returns true if successfull, false otherwise.</returns>
129+ /// <remarks>Internally calls <seealso cref="IRequestContext.SetPreference(string, object, out string)"/> with
130+ /// preference 'proxy' and mode of 'fixed_servers'</remarks>
131+ public static bool SetProxy ( this IRequestContext requestContext , string host , out string errorMessage )
132+ {
133+ return requestContext . SetProxy ( null , host , null , out errorMessage ) ;
134+ }
135+
108136 /// <summary>
109137 /// Clears all HTTP authentication credentials that were added as part of handling
110138 /// <see cref="IRequestHandler.GetAuthCredentials(IWebBrowser, IBrowser, string, bool, string, int, string, string, IAuthCallback)"/>.
0 commit comments