Fix due to the added IsValidWsUri method to Ext.cs

This commit is contained in:
sta 2012-09-25 10:48:33 +09:00
parent 14f16577be
commit a75903c488
25 changed files with 62 additions and 56 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -7,7 +7,7 @@ namespace Example2
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
/* Single service server // Single service server
var wssv = new WebSocketServer<Echo>("ws://localhost:4649"); var wssv = new WebSocketServer<Echo>("ws://localhost:4649");
//var wssv = new WebSocketServer<Echo>(4649); //var wssv = new WebSocketServer<Echo>(4649);
//var wssv = new WebSocketServer<Chat>("ws://localhost:4649"); //var wssv = new WebSocketServer<Chat>("ws://localhost:4649");
@ -17,9 +17,9 @@ namespace Example2
Console.WriteLine( Console.WriteLine(
"WebSocket Server (url: {0})\n listening on address: {1} port: {2}\n", "WebSocket Server (url: {0})\n listening on address: {1} port: {2}\n",
wssv.Uri, wssv.Address, wssv.Port); wssv.Uri, wssv.Address, wssv.Port);
*/
// Multi services server
/* Multi services server
var wssv = new WebSocketServer(4649); var wssv = new WebSocketServer(4649);
wssv.AddService<Echo>("/Echo"); wssv.AddService<Echo>("/Echo");
wssv.AddService<Chat>("/Chat"); wssv.AddService<Chat>("/Chat");
@ -27,7 +27,7 @@ namespace Example2
wssv.Start(); wssv.Start();
Console.WriteLine( Console.WriteLine(
"WebSocket Server listening on port: {0}\n", wssv.Port); "WebSocket Server listening on port: {0}\n", wssv.Port);
*/
Console.WriteLine("Press any key to stop server..."); Console.WriteLine("Press any key to stop server...");
Console.ReadLine(); Console.ReadLine();

View File

@ -37,6 +37,7 @@ using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using WebSocketSharp.Net; using WebSocketSharp.Net;
@ -166,6 +167,45 @@ namespace WebSocketSharp
return false; return false;
} }
public static bool IsValidWsUri(this Uri uri, out string message)
{
if (!uri.IsAbsoluteUri)
{
message = "Not absolute uri: " + uri.ToString();
return false;
}
var scheme = uri.Scheme;
if (scheme != "ws" && scheme != "wss")
{
message = "Unsupported WebSocket URI scheme: " + scheme;
return false;
}
var port = uri.Port;
if (port > 0)
{
if ((scheme == "wss" && port != 443) ||
(scheme != "wss" && port == 443))
{
message = String.Format(
"Invalid pair of WebSocket URI scheme and port: {0}, {1}", scheme, port);
return false;
}
}
var host = uri.DnsSafeHost;
var addrs = Dns.GetHostAddresses(host);
if (addrs.Length == 0)
{
message = "Invalid WebSocket URI host: " + host;
return false;
}
message = String.Empty;
return true;
}
// Derived from System.Uri.MaybeUri method // Derived from System.Uri.MaybeUri method
public static bool MaybeUri(this string uriString) public static bool MaybeUri(this string uriString)
{ {
@ -414,7 +454,8 @@ namespace WebSocketSharp
return new Uri(uriString); return new Uri(uriString);
} }
public static void WriteContent(this HttpListenerResponse response, byte[] content) public static void WriteContent(
this WebSocketSharp.Net.HttpListenerResponse response, byte[] content)
{ {
var output = response.OutputStream; var output = response.OutputStream;
response.ContentLength64 = content.Length; response.ContentLength64 = content.Length;

View File

@ -55,17 +55,14 @@ namespace WebSocketSharp.Server {
protected WebSocketServerBase(string url) protected WebSocketServerBase(string url)
{ {
string msg; init(url);
if (!isValidUri(url, out msg))
throw new ArgumentException(msg, "url");
init();
} }
protected WebSocketServerBase(IPAddress address, int port) protected WebSocketServerBase(IPAddress address, int port)
{ {
_port = port <= 0 ? 80 : port;
_address = address; _address = address;
_port = port <= 0 ? 80 : port;
init(); init();
} }
@ -149,48 +146,26 @@ namespace WebSocketSharp.Server {
_isSelfHost = true; _isSelfHost = true;
} }
private bool isValidUri(string url, out string message) private void init(string url)
{ {
var uri = url.ToUri(); var uri = url.ToUri();
if (!uri.IsAbsoluteUri)
{ string msg;
message = "Not absolute uri: " + url; if (!uri.IsValidWsUri(out msg))
return false; throw new ArgumentException(msg, "url");
}
var scheme = uri.Scheme; var scheme = uri.Scheme;
var port = uri.Port; var port = uri.Port;
var host = uri.DnsSafeHost; var host = uri.DnsSafeHost;
var ips = Dns.GetHostAddresses(host); var addrs = Dns.GetHostAddresses(host);
if (scheme != "ws" && scheme != "wss")
{
message = "Unsupported WebSocket URI scheme: " + scheme;
return false;
}
if ((scheme == "wss" && port != 443) ||
(scheme != "wss" && port == 443))
{
message = String.Format(
"Invalid pair of WebSocket URI scheme and port: {0}, {1}", scheme, port);
return false;
}
if (ips.Length == 0)
{
message = "Invalid WebSocket URI host: " + host;
return false;
}
if (port <= 0) if (port <= 0)
port = scheme == "ws" ? 80 : 443; port = scheme == "ws" ? 80 : 443;
_address = ips[0]; _address = addrs[0];
_port = port; _port = port;
message = String.Empty; init();
return true;
} }
private void startAcceptClientThread() private void startAcceptClientThread()

View File

@ -142,17 +142,16 @@ namespace WebSocketSharp
public WebSocket(string url, params string[] protocols) public WebSocket(string url, params string[] protocols)
: this() : this()
{ {
var uri = new Uri(url); var uri = url.ToUri();
if (!isValidScheme(uri))
{ string msg;
var msg = "Unsupported WebSocket URI scheme: " + uri.Scheme; if (!uri.IsValidWsUri(out msg))
throw new ArgumentException(msg, "url"); throw new ArgumentException(msg, "url");
}
_uri = uri; _uri = uri;
_protocols = protocols.ToString(", "); _protocols = protocols.ToString(", ");
_isClient = true; _isClient = true;
_isSecure = _uri.Scheme == "wss" ? true : false; _isSecure = uri.Scheme == "wss" ? true : false;
} }
public WebSocket( public WebSocket(
@ -597,15 +596,6 @@ namespace WebSocketSharp
return true; return true;
} }
private bool isValidScheme(Uri uri)
{
string scheme = uri.Scheme;
if (scheme == "ws" || scheme == "wss")
return true;
return false;
}
private void message() private void message()
{ {
try try

Binary file not shown.