Skip to content

Commit b8d2271

Browse files
Merge pull request #113 from make-software/sse-client-https
Modified SSE client to allow https connections
2 parents 09584f8 + b04c55e commit b8d2271

3 files changed

Lines changed: 73 additions & 13 deletions

File tree

Casper.Network.SDK.Test/ServerEventsClientTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class ServerEventsClientTest
1818
private KeyPair _faucetKey;
1919
private string nodeIpSSE;
2020
private int nodePortSSE;
21+
private string nodeUriSSE;
2122

2223
[SetUp]
2324
public void Setup()
@@ -41,6 +42,10 @@ public void Setup()
4142
Assert.IsNotNull(port,
4243
"Please, set environment variable CASPERNETSDK_NODE_SSE_PORT with a valid node ip.");
4344
Assert.IsTrue(int.TryParse(port, out nodePortSSE));
45+
46+
nodeUriSSE = Environment.GetEnvironmentVariable("CASPERNETSDK_NODE_SSE_URI");
47+
Assert.IsNotNull(nodeUriSSE,
48+
"Please, set environment variable CASPERNETSDK_NODE_SSE_URI with a valid node sse uri.");
4449
}
4550

4651
[Test]
@@ -117,7 +122,7 @@ public async Task SSEListen()
117122
int nDeployAccepted = 0;
118123
int nDeployProcessed = 0;
119124

120-
var sse = new ServerEventsClient(nodeIpSSE, nodePortSSE);
125+
var sse = new ServerEventsClient(nodeUriSSE);
121126

122127
sse.AddEventCallback(EventType.All, "catch-all-cb",
123128
(SSEvent evt) =>

Casper.Network.SDK.Test/test.runsettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<CASPERNETSDK_NODE_METRICS_PORT>14101</CASPERNETSDK_NODE_METRICS_PORT>
88
<CASPERNETSDK_NODE_SSE_IP>127.0.0.1</CASPERNETSDK_NODE_SSE_IP>
99
<CASPERNETSDK_NODE_SSE_PORT>18101</CASPERNETSDK_NODE_SSE_PORT>
10+
<CASPERNETSDK_NODE_SSE_URI>http://127.0.0.1:18101/events</CASPERNETSDK_NODE_SSE_URI>
1011
</EnvironmentVariables>
1112
</RunConfiguration>
1213
</RunSettings>

Casper.Network.SDK/SSE/ServerEventsClient.cs

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Net.Http;
5+
using System.Text;
56
using System.Text.Json;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -115,6 +116,7 @@ public class ServerEventsClient : ISSEClient
115116

116117
protected string _host;
117118
protected int _port;
119+
protected Uri _sseUri;
118120
protected int _nodeVersion = 1;
119121

120122
public ServerEventsClient()
@@ -150,6 +152,31 @@ public ServerEventsClient(string host, int port, int nodeVersion = 2) : this()
150152
_nodeVersion = nodeVersion;
151153
}
152154

155+
/// <summary>
156+
/// Instantiate the class indicating the full SSE stream URL of a node.
157+
/// Example: https://node.testnet.casper.network/events
158+
/// </summary>
159+
/// <param name="sseUrl">Full URL of the SSE stream.</param>
160+
/// <remarks>Use this constructor when the node is running version 2.x of the Casper protocol.</remarks>
161+
public ServerEventsClient(string sseUrl) : this()
162+
{
163+
if (string.IsNullOrWhiteSpace(sseUrl))
164+
throw new ArgumentException("SSE URL cannot be null or empty.", nameof(sseUrl));
165+
166+
if (!Uri.TryCreate(sseUrl, UriKind.Absolute, out var uri))
167+
throw new ArgumentException("SSE URL is not a valid absolute URI.", nameof(sseUrl));
168+
169+
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
170+
throw new ArgumentException("SSE URL must use the HTTP or HTTPS scheme.", nameof(sseUrl));
171+
172+
_sseUri = uri;
173+
_host = uri.Host;
174+
_port = uri.IsDefaultPort
175+
? (uri.Scheme == Uri.UriSchemeHttps ? 443 : 80)
176+
: uri.Port;
177+
_nodeVersion = 2;
178+
}
179+
153180
public int NodeVersion
154181
{
155182
get { return _nodeVersion; }
@@ -300,10 +327,44 @@ public bool IsRunning()
300327
protected virtual HttpClient _getHttpClient()
301328
{
302329
var client = new HttpClient();
303-
client.BaseAddress = new Uri($"http://{_host}:{_port}");
330+
client.BaseAddress = _sseUri != null
331+
? new Uri(_sseUri.GetLeftPart(UriPartial.Authority))
332+
: new Uri($"http://{_host}:{_port}");
304333
return client;
305334
}
306335

336+
private Uri BuildStreamUri(HttpClient client, ChannelType channelType, int? startFrom)
337+
{
338+
UriBuilder uriBuilder;
339+
if (_sseUri != null)
340+
{
341+
uriBuilder = new UriBuilder(_sseUri);
342+
}
343+
else
344+
{
345+
uriBuilder = new UriBuilder(new Uri(client.BaseAddress +
346+
$"events" +
347+
(_nodeVersion == 1 ? $"/{channelType.ToString().ToLowerInvariant()}" : "")));
348+
}
349+
350+
var queryBuilder = new StringBuilder();
351+
var existingQuery = uriBuilder.Query;
352+
if (!string.IsNullOrWhiteSpace(existingQuery))
353+
{
354+
queryBuilder.Append(existingQuery.TrimStart('?'));
355+
if (queryBuilder.Length > 0)
356+
queryBuilder.Append('&');
357+
}
358+
359+
if (startFrom != null && startFrom != int.MaxValue)
360+
queryBuilder.Append($"start_from={startFrom}");
361+
else
362+
queryBuilder.Append("start_from=0");
363+
364+
uriBuilder.Query = queryBuilder.ToString();
365+
return uriBuilder.Uri;
366+
}
367+
307368
private Task ListenChannelAsync(ChannelType channelType, int? startFrom, CancellationToken cancelToken)
308369
{
309370
var task = Task.Run(async () =>
@@ -317,17 +378,10 @@ private Task ListenChannelAsync(ChannelType channelType, int? startFrom, Cancell
317378
{
318379
try
319380
{
320-
var uriBuilder = new UriBuilder(new Uri(client.BaseAddress +
321-
$"events" +
322-
(_nodeVersion == 1 ? $"/{channelType.ToString().ToLowerInvariant()}" : "")));
323-
324-
if (startFrom != null && startFrom != int.MaxValue)
325-
uriBuilder.Query = $"start_from={startFrom}";
326-
else
327-
uriBuilder.Query = $"start_from={0}";
328-
381+
var streamUri = BuildStreamUri(client, channelType, startFrom);
382+
329383
using (var streamReader =
330-
new StreamReader(await client.GetStreamAsync(uriBuilder.Uri, cancelToken)))
384+
new StreamReader(await client.GetStreamAsync(streamUri, cancelToken)))
331385
{
332386
while (!streamReader.EndOfStream && !cancelToken.IsCancellationRequested)
333387
{
@@ -426,4 +480,4 @@ private void EmitEvent(EventData eventData)
426480
}
427481
}
428482
}
429-
}
483+
}

0 commit comments

Comments
 (0)