Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 10 additions & 53 deletions websocket-sharp/Net/HttpListenerPrefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,12 @@ public int Port {

private void parse (string uriPrefix)
{
var defaultPort = uriPrefix.StartsWith ("https://") ? 443 : 80;
if (defaultPort == 443)
_secure = true;

var len = uriPrefix.Length;
var startHost = uriPrefix.IndexOf (':') + 3;
var colon = uriPrefix.IndexOf (':', startHost, len - startHost);
var root = 0;
if (colon > 0) {
root = uriPrefix.IndexOf ('/', colon, len - colon);
_host = uriPrefix.Substring (startHost, colon - startHost);
_port = (ushort) Int32.Parse (uriPrefix.Substring (colon + 1, root - colon - 1));
}
else {
root = uriPrefix.IndexOf ('/', startHost, len - startHost);
_host = uriPrefix.Substring (startHost, root - startHost);
_port = (ushort) defaultPort;
}
var uri = new System.Uri(uriPrefix);

_path = uriPrefix.Substring (root);
_port = (ushort)uri.Port;
_host = uri.Host;
_path = uri.PathAndQuery;
_secure = uri.Scheme.Equals("https");

var pathLen = _path.Length;
if (pathLen > 1)
Expand All @@ -152,42 +138,13 @@ private void parse (string uriPrefix)

public static void CheckPrefix (string uriPrefix)
{
if (uriPrefix == null)
throw new ArgumentNullException ("uriPrefix");

var len = uriPrefix.Length;
if (len == 0)
throw new ArgumentException ("An empty string.");

if (!(uriPrefix.StartsWith ("http://") || uriPrefix.StartsWith ("https://")))
throw new ArgumentException ("The scheme isn't 'http' or 'https'.");

var startHost = uriPrefix.IndexOf (':') + 3;
if (startHost >= len)
throw new ArgumentException ("No host is specified.");
var uri = new System.Uri(uriPrefix);

var colon = uriPrefix.IndexOf (':', startHost, len - startHost);
if (startHost == colon)
throw new ArgumentException ("No host is specified.");

if (colon > 0) {
var root = uriPrefix.IndexOf ('/', colon, len - colon);
if (root == -1)
throw new ArgumentException ("No path is specified.");

int port;
if (!Int32.TryParse (uriPrefix.Substring (colon + 1, root - colon - 1), out port) ||
!port.IsPortNumber ())
throw new ArgumentException ("An invalid port is specified.");
}
else {
var root = uriPrefix.IndexOf ('/', startHost, len - startHost);
if (root == -1)
throw new ArgumentException ("No path is specified.");
}
if (!uri.Scheme.Equals("http") && !uri.Scheme.Equals("https"))
throw new ArgumentException ("The scheme isn't 'http' or 'https'.");

if (uriPrefix[len - 1] != '/')
throw new ArgumentException ("Ends without '/'.");
if (!uri.PathAndQuery.EndsWith("/"))
throw new ArgumentException ("Ends without '/'.");
}

// The Equals and GetHashCode methods are required to detect duplicates in any collection.
Expand Down
15 changes: 9 additions & 6 deletions websocket-sharp/Server/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,14 @@ public HttpServer (string url)
throw new ArgumentException (msg, "url");

var host = uri.DnsSafeHost;
var addr = host.ToIPAddress ();
bool hostIsIpAddress = (uri.HostNameType == System.UriHostNameType.IPv4 || uri.HostNameType == System.UriHostNameType.IPv6);
var addr = hostIsIpAddress ? System.Net.IPAddress.Parse(uri.DnsSafeHost) : host.ToIPAddress();
if (!addr.IsLocal ())
throw new ArgumentException ("The host part isn't a local host name: " + url, "url");

init (host, addr, uri.Port, uri.Scheme == "https");
if (uri.HostNameType == System.UriHostNameType.IPv6) {
host = "[" + host + "]";
}
init(host, addr, uri.Port, uri.Scheme == "https");
}

/// <summary>
Expand Down Expand Up @@ -632,14 +635,14 @@ private string checkIfCertificateExists ()

private void init (string hostname, System.Net.IPAddress address, int port, bool secure)
{
_hostname = hostname ?? address.ToString ();
_hostname = hostname ?? (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 ? ("[" + address.ToString() + "]") : address.ToString());
_address = address;
_port = port;
_secure = secure;

_listener = new HttpListener ();
_listener.Prefixes.Add (
String.Format ("http{0}://{1}:{2}/", secure ? "s" : "", _hostname, port));
string prefix = String.Format ("http{0}://{1}:{2}/", secure ? "s" : "", _hostname, port);
_listener.Prefixes.Add (prefix);

_logger = _listener.Log;
_services = new WebSocketServiceManager (_logger);
Expand Down
5 changes: 3 additions & 2 deletions websocket-sharp/websocket-sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>websocket-sharp.snk</AssemblyOriginatorKeyFile>
<UseMSBuildEngine>False</UseMSBuildEngine>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -48,12 +49,12 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<GenerateDocumentation>true</GenerateDocumentation>
<CustomCommands>
<CustomCommands>
<Command type="AfterBuild" command="doc/doc.sh" workingdir="doc/" externalConsole="true" />
<Command type="AfterBuild" command="doc/doc.sh" workingdir="doc/" externalConsole="True" />
</CustomCommands>
</CustomCommands>
<DocumentationFile>bin\Release_Ubuntu\websocket-sharp.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down