5
5
using System ;
6
6
using System . Collections . Generic ;
7
7
using System . IO ;
8
+ using System . Linq ;
9
+ using System . Net ;
8
10
using System . Threading . Tasks ;
9
11
10
12
namespace CefSharp
@@ -14,6 +16,11 @@ namespace CefSharp
14
16
/// </summary>
15
17
public static class RequestContextExtensions
16
18
{
19
+ /// <summary>
20
+ /// Array of valid proxy schemes
21
+ /// </summary>
22
+ private static string [ ] ProxySchemes = new string [ ] { "http" , "socks" , "socks4" , "socks5" } ;
23
+
17
24
/// <summary>
18
25
/// Load an extension from the given directory. To load a crx file you must unzip it first.
19
26
/// For further details see <seealso cref="IRequestContext.LoadExtension(string, string, IExtensionHandler)"/>
@@ -55,34 +62,39 @@ public static void LoadExtensionsFromDirectory(this IRequestContext requestConte
55
62
/// </summary>
56
63
/// <param name="requestContext">request context</param>
57
64
/// <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>
60
67
/// <param name="errorMessage">error message</param>
61
68
/// <returns>returns true if successfull, false otherwise.</returns>
62
69
/// <remarks>Internally calls <seealso cref="IRequestContext.SetPreference(string, object, out string)"/> with
63
70
/// 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 )
65
72
{
66
- if ( string . IsNullOrWhiteSpace ( host ) )
73
+ //Default to using http scheme if non provided
74
+ if ( string . IsNullOrWhiteSpace ( scheme ) )
67
75
{
68
- throw new ArgumentException ( "Cannot be null or empty" , "host" ) ;
76
+ scheme = "http" ;
69
77
}
70
78
71
- if ( string . IsNullOrWhiteSpace ( port ) )
79
+ if ( ! ProxySchemes . Contains ( scheme . ToLower ( ) ) )
72
80
{
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" ) ;
74
82
}
75
83
76
- //Default to using http scheme if non provided
77
- if ( string . IsNullOrWhiteSpace ( scheme ) )
84
+ if ( string . IsNullOrWhiteSpace ( host ) )
78
85
{
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" ) ;
80
92
}
81
93
82
94
var v = new Dictionary < string , object >
83
95
{
84
96
[ "mode" ] = "fixed_servers" ,
85
- [ "server" ] = scheme + "://" + host + ":" + port
97
+ [ "server" ] = scheme + "://" + host + ( port . HasValue ? ( ":" + port ) : "" )
86
98
} ;
87
99
88
100
return requestContext . SetPreference ( "proxy" , v , out errorMessage ) ;
@@ -94,17 +106,33 @@ public static bool SetProxy(this IRequestContext requestContext, string scheme,
94
106
/// MUST be called on the CEF UI Thread
95
107
/// </summary>
96
108
/// <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>
99
111
/// <param name="errorMessage">error message</param>
100
112
/// <returns>returns true if successfull, false otherwise.</returns>
101
113
/// <remarks>Internally calls <seealso cref="IRequestContext.SetPreference(string, object, out string)"/> with
102
114
/// 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 )
104
116
{
105
117
return requestContext . SetProxy ( null , host , port , out errorMessage ) ;
106
118
}
107
119
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
+
108
136
/// <summary>
109
137
/// Clears all HTTP authentication credentials that were added as part of handling
110
138
/// <see cref="IRequestHandler.GetAuthCredentials(IWebBrowser, IBrowser, string, bool, string, int, string, string, IAuthCallback)"/>.
0 commit comments