[Modify] Throw exceptions

master
sta 8 years ago
parent 018b73d82a
commit 7a9899e064

@ -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);
return; if (url.Length == 0)
} throw new ArgumentException ("An empty string.", "url");
lock (_forState) { Uri uri;
if (!checkIfAvailable (true, false, false, true, out msg)) { if (!Uri.TryCreate (url, UriKind.Absolute, out uri)) {
_logger.Error (msg); msg = "Not an absolute URI string.";
error ("An error has occurred in setting the proxy.", null); throw new ArgumentException (msg, "url");
}
return; if (uri.Scheme != "http") {
} msg = "The scheme part is not http.";
throw new ArgumentException (msg, "url");
}
if (url.IsNullOrEmpty ()) { if (uri.Segments.Length > 1) {
_logger.Warn ("The url and credentials for the proxy are initialized."); msg = "It includes the path segments.";
_proxyUri = null; throw new ArgumentException (msg, "url");
_proxyCredentials = null; }
return; if (!username.IsNullOrEmpty ()) {
if (username.Contains (':') || !username.IsText ()) {
msg = "It contains an invalid character.";
throw new ArgumentException (msg, "username");
} }
}
_proxyUri = new Uri (url); if (!password.IsNullOrEmpty ()) {
if (!password.IsText ()) {
msg = "It contains an invalid character.";
throw new ArgumentException (msg, "password");
}
}
if (username.IsNullOrEmpty ()) { if (!canSet (out msg)) {
_logger.Warn ("The credentials for the proxy are initialized."); _logger.Warn (msg);
_proxyCredentials = null; return;
}
lock (_forState) {
if (!canSet (out msg)) {
_logger.Warn (msg);
return; return;
} }
_proxyCredentials = _proxyUri = uri;
new NetworkCredential ( _proxyCredentials = !username.IsNullOrEmpty ()
username, password, String.Format ("{0}:{1}", _uri.DnsSafeHost, _uri.Port) ? new NetworkCredential (
); username,
password,
String.Format (
"{0}:{1}", _uri.DnsSafeHost, _uri.Port
)
)
: null;
} }
} }

Loading…
Cancel
Save