diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 28c42c14..09f8206e 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -2867,49 +2867,60 @@ namespace WebSocketSharp public void SetProxy (string url, string username, string password) { lock (_forConn) { - var msg = checkIfAvailable (true, false, true, false, false, true); - if (msg == null) { - if (url.IsNullOrEmpty ()) { - _proxyUri = null; - _proxyCredentials = null; - _logger.Warn ("The proxy url and credentials were set back to the default."); - - return; - } - - Uri uri; - if (!Uri.TryCreate (url, UriKind.Absolute, out uri) || - uri.Scheme != "http" || - uri.Segments.Length > 1) { - msg = "The syntax of a proxy url must be 'http://[:]'."; - } - else { - _proxyUri = uri; - - if (username.IsNullOrEmpty ()) { - _proxyCredentials = null; - _logger.Warn ("The proxy credentials were set back to the default."); - - return; - } - - msg = username.Contains (':') || !username.IsText () - ? "'username' contains an invalid character." - : !password.IsNullOrEmpty () && !password.IsText () - ? "'password' contains an invalid character." - : null; - } - } - - if (msg != null) { + string msg; + if (!checkIfAvailable (true, false, true, false, false, true, out msg)) { _logger.Error (msg); error ("An error has occurred in setting the proxy.", null); return; } - _proxyCredentials = new NetworkCredential ( - username, password, String.Format ("{0}:{1}", _uri.DnsSafeHost, _uri.Port)); + if (url.IsNullOrEmpty ()) { + _proxyUri = null; + _proxyCredentials = null; + _logger.Warn ("The proxy url and credentials were set back to the default."); + + return; + } + + Uri uri; + if (!Uri.TryCreate (url, UriKind.Absolute, out uri) + || uri.Scheme != "http" + || uri.Segments.Length > 1 + ) { + _logger.Error ("The syntax of a proxy url must be 'http://[:]'."); + error ("An error has occurred in setting the proxy.", null); + + return; + } + + if (username.IsNullOrEmpty ()) { + _proxyUri = uri; + _proxyCredentials = null; + _logger.Warn ("The proxy credentials were set back to the default."); + + return; + } + + if (username.Contains (':') || !username.IsText ()) { + _logger.Error ("'username' contains an invalid character."); + error ("An error has occurred in setting the proxy.", null); + + return; + } + + if (!password.IsNullOrEmpty () && !password.IsText ()) { + _logger.Error ("'password' contains an invalid character."); + error ("An error has occurred in setting the proxy.", null); + + return; + } + + _proxyUri = uri; + _proxyCredentials = + new NetworkCredential ( + username, password, String.Format ("{0}:{1}", _uri.DnsSafeHost, _uri.Port) + ); } }