Skip to content

Commit d79011f

Browse files
committed
handle oregin better
1 parent 78251e5 commit d79011f

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

src/WebSocket4Net/WebSocket.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO.Pipelines;
66
using System.Net;
7+
using System.Net.Sockets;
78
using System.Security.Cryptography;
89
using System.Text;
910
using System.Threading;
@@ -66,24 +67,18 @@ public Dictionary<string, string> Headers
6667

6768
public WebSocketState State { get; private set; } = WebSocketState.None;
6869

69-
public WebSocket(string url)
70-
: this(url, NullLogger.Instance)
70+
static ConnectionOptions PrepareConnectionOptions(ConnectionOptions connectionOptions, ILogger logger)
7171
{
72-
73-
}
74-
75-
public WebSocket(string url, ILogger logger)
76-
: this(url, new ConnectionOptions { Logger = logger })
77-
{
78-
72+
connectionOptions ??= new ConnectionOptions();
73+
connectionOptions.Logger = logger ?? NullLogger.Instance;
74+
return connectionOptions;
7975
}
8076

81-
public WebSocket(string url, ConnectionOptions connectionOptions)
82-
: base(new HandshakePipelineFilter(), connectionOptions)
77+
public WebSocket(string url, string origin = null, ILogger logger = null, ConnectionOptions connectionOptions = null)
78+
: base(new HandshakePipelineFilter(), PrepareConnectionOptions(connectionOptions, logger))
8379
{
8480
Uri = new Uri(url);
85-
86-
_origin = Uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
81+
_origin = origin;
8782

8883
if ("ws".Equals(Uri.Scheme, StringComparison.OrdinalIgnoreCase))
8984
{
@@ -113,7 +108,7 @@ private EndPoint ResolveUri(Uri uri, int defaultPort)
113108
if (IPAddress.TryParse(uri.Host, out IPAddress ipAddress))
114109
remoteEndPoint = new IPEndPoint(ipAddress, port);
115110
else
116-
remoteEndPoint = new DnsEndPoint(uri.Host, port);
111+
remoteEndPoint = new DnsEndPoint(uri.Host, port, AddressFamily.InterNetwork);
117112

118113
return remoteEndPoint;
119114
}
@@ -201,7 +196,11 @@ private void WriteHandshakeRequest(PipeWriter writer, string secKey)
201196
writer.Write($"{WebSocketConstant.ResponseUpgradeLine}", _asciiEncoding);
202197
writer.Write($"{WebSocketConstant.ResponseConnectionLine}", _asciiEncoding);
203198
writer.Write($"{WebSocketConstant.SecWebSocketKey}: {secKey}\r\n", _asciiEncoding);
204-
writer.Write($"{WebSocketConstant.Origin}: {_origin}\r\n", _asciiEncoding);
199+
200+
if (!string.IsNullOrEmpty(_origin))
201+
{
202+
writer.Write($"{WebSocketConstant.Origin}: {_origin}\r\n", _asciiEncoding);
203+
}
205204

206205
var subProtocols = _subProtocols;
207206

0 commit comments

Comments
 (0)