Winify

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 46  →  ?path2? @ 47
/trunk/Winify/Gotify/GotifyConnection.cs
@@ -65,32 +65,41 @@
(httpRequestMessage, cert, cetChain, policyErrors) => true;
}
 
_httpClient = new HttpClient(httpClientHandler)
_httpClient = new HttpClient(httpClientHandler);
if (!string.IsNullOrEmpty(_server.Username) && !string.IsNullOrEmpty(_server.Password))
{
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.Default.GetBytes($"{_server.Username}:{_server.Password}")))
}
};
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.Default.GetBytes($"{_server.Username}:{_server.Password}")));
}
 
if (Uri.TryCreate(_server.Url, UriKind.Absolute, out _httpUri))
if (!Uri.TryCreate(_server.Url, UriKind.Absolute, out _httpUri))
{
// Build the web sockets URI.
var webSocketsUriBuilder = new UriBuilder(_httpUri);
switch (webSocketsUriBuilder.Scheme.ToUpperInvariant())
{
case "HTTP":
webSocketsUriBuilder.Scheme = "ws";
break;
case "HTTPS":
webSocketsUriBuilder.Scheme = "wss";
break;
}
Log.Error($"No HTTP URL could be built out of the supplied server URI {_server.Url}.");
return;
}
 
// Build the web sockets URI.
var webSocketsUriBuilder = new UriBuilder(_httpUri);
switch (webSocketsUriBuilder.Scheme.ToUpperInvariant())
{
case "HTTP":
webSocketsUriBuilder.Scheme = "ws";
break;
case "HTTPS":
webSocketsUriBuilder.Scheme = "wss";
break;
}
 
try
{
webSocketsUriBuilder.Path = Path.Combine(webSocketsUriBuilder.Path, "stream");
_webSocketsUri = webSocketsUriBuilder.Uri;
}
catch (ArgumentException exception)
{
Log.Error($"No WebSockets URL could be built from the provided URL {_server.Url} due to {exception.Message}.");
}
 
_webSocketsUri = webSocketsUriBuilder.Uri;
}
 
public void Dispose()
@@ -114,6 +123,12 @@
 
public void Start()
{
if (_webSocketsUri == null || _httpUri == null)
{
Log.Error("Could not start connection to server due to unreadable URLs.");
return;
}
 
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
 
@@ -125,7 +140,11 @@
private void Connect()
{
_webSocketSharp = new WebSocket(_webSocketsUri.AbsoluteUri);
_webSocketSharp.SetCredentials(_server.Username, _server.Password, true);
if (!string.IsNullOrEmpty(_server.Username) && !string.IsNullOrEmpty(_server.Password))
{
_webSocketSharp.SetCredentials(_server.Username, _server.Password, true);
}
 
if (_configuration.IgnoreSelfSignedCertificates)
{
_webSocketSharp.SslConfiguration.ServerCertificateValidationCallback +=