[Modify] Throw exceptions

This commit is contained in:
sta 2017-11-09 16:38:56 +09:00
parent 018b73d82a
commit 7a9899e064

View File

@ -3850,50 +3850,70 @@ namespace WebSocketSharp
/// </param> /// </param>
public void SetProxy (string url, string username, string password) public void SetProxy (string url, string username, string password)
{ {
string msg; string msg = null;
if (!checkIfAvailable (true, false, true, false, false, true, out msg)) {
_logger.Error (msg);
error ("An error has occurred in setting the proxy.", null);
return; if (!_client) {
msg = "This instance is not a client.";
throw new InvalidOperationException (msg);
} }
if (!checkParametersForSetProxy (url, username, password, out msg)) { if (url == null)
_logger.Error (msg); throw new ArgumentNullException ("url");
error ("An error has occurred in setting the proxy.", null);
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; return;
} }
lock (_forState) { lock (_forState) {
if (!checkIfAvailable (true, false, false, true, out msg)) { if (!canSet (out msg)) {
_logger.Error (msg); _logger.Warn (msg);
error ("An error has occurred in setting the proxy.", null);
return; return;
} }
if (url.IsNullOrEmpty ()) { _proxyUri = uri;
_logger.Warn ("The url and credentials for the proxy are initialized."); _proxyCredentials = !username.IsNullOrEmpty ()
_proxyUri = null; ? new NetworkCredential (
_proxyCredentials = null; username,
password,
return; String.Format (
} "{0}:{1}", _uri.DnsSafeHost, _uri.Port
)
_proxyUri = new Uri (url); )
: null;
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)
);
} }
} }