From 7a9899e064859a2c88bf2458e335a5bdc76812ca Mon Sep 17 00:00:00 2001 From: sta Date: Thu, 9 Nov 2017 16:38:56 +0900 Subject: [PATCH] [Modify] Throw exceptions --- websocket-sharp/WebSocket.cs | 86 ++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index a0506902..5b533138 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -3850,50 +3850,70 @@ namespace WebSocketSharp /// public void SetProxy (string url, string username, string password) { - 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); + string msg = null; - return; + if (!_client) { + msg = "This instance is not a client."; + throw new InvalidOperationException (msg); } - if (!checkParametersForSetProxy (url, username, password, out msg)) { - _logger.Error (msg); - error ("An error has occurred in setting the proxy.", null); + if (url == null) + throw new ArgumentNullException ("url"); + if (url.Length == 0) + throw new ArgumentException ("An empty string.", "url"); + + Uri uri; + if (!Uri.TryCreate (url, UriKind.Absolute, out uri)) { + msg = "Not an absolute URI string."; + throw new ArgumentException (msg, "url"); + } + + if (uri.Scheme != "http") { + msg = "The scheme part is not http."; + throw new ArgumentException (msg, "url"); + } + + if (uri.Segments.Length > 1) { + msg = "It includes the path segments."; + throw new ArgumentException (msg, "url"); + } + + if (!username.IsNullOrEmpty ()) { + if (username.Contains (':') || !username.IsText ()) { + msg = "It contains an invalid character."; + throw new ArgumentException (msg, "username"); + } + } + + if (!password.IsNullOrEmpty ()) { + if (!password.IsText ()) { + msg = "It contains an invalid character."; + throw new ArgumentException (msg, "password"); + } + } + + if (!canSet (out msg)) { + _logger.Warn (msg); return; } lock (_forState) { - if (!checkIfAvailable (true, false, false, true, out msg)) { - _logger.Error (msg); - error ("An error has occurred in setting the proxy.", null); - + if (!canSet (out msg)) { + _logger.Warn (msg); return; } - if (url.IsNullOrEmpty ()) { - _logger.Warn ("The url and credentials for the proxy are initialized."); - _proxyUri = null; - _proxyCredentials = null; - - return; - } - - _proxyUri = new Uri (url); - - if (username.IsNullOrEmpty ()) { - _logger.Warn ("The credentials for the proxy are initialized."); - _proxyCredentials = null; - - return; - } - - _proxyCredentials = - new NetworkCredential ( - username, password, String.Format ("{0}:{1}", _uri.DnsSafeHost, _uri.Port) - ); + _proxyUri = uri; + _proxyCredentials = !username.IsNullOrEmpty () + ? new NetworkCredential ( + username, + password, + String.Format ( + "{0}:{1}", _uri.DnsSafeHost, _uri.Port + ) + ) + : null; } }