Refactored Ext.cs

This commit is contained in:
sta 2013-08-07 14:00:50 +09:00
parent 69c9be3eb5
commit ab2ef30f02
5 changed files with 430 additions and 507 deletions

View File

@ -1,8 +1,8 @@
#region License
/*
* Ext.cs
* IsPredefinedScheme and MaybeUri methods derived from System.Uri.cs
* GetStatusDescription method derived from System.Net.HttpListenerResponse.cs
* IsPredefinedScheme and MaybeUri methods are derived from System.Uri.cs
* GetStatusDescription method is derived from System.Net.HttpListenerResponse.cs
*
* The MIT License
*
@ -50,13 +50,13 @@ using System.Text;
using WebSocketSharp.Net;
using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp {
namespace WebSocketSharp
{
/// <summary>
/// Provides a set of static methods for the websocket-sharp.
/// </summary>
public static class Ext {
public static class Ext
{
#region Private Const Fields
private const string _tspecials = "()<>@,;:\\\"/[]?={} \t";
@ -273,8 +273,7 @@ namespace WebSocketSharp {
return "What we've got here is a failure to communicate in the WebSocket protocol.";
if (code == CloseStatusCode.TOO_BIG)
return String.Format(
"The size of received payload data is bigger than the allowable value ({0} bytes).",
return String.Format ("The size of received payload data is bigger than the allowable value ({0} bytes).",
PayloadData.MaxLength);
return String.Empty;
@ -299,52 +298,6 @@ namespace WebSocketSharp {
return value.StartsWith ("permessage-");
}
// <summary>
// Determines whether the specified object is <see langword="null"/>.
// </summary>
// <returns>
// <c>true</c> if <paramref name="obj"/> is <see langword="null"/>; otherwise, <c>false</c>.
// </returns>
// <param name="obj">
// An <b>object</b> to test.
// </param>
// <typeparam name="T">
// The type of <paramref name="obj"/> parameter.
// </typeparam>
internal static bool IsNull<T>(this T obj)
where T : class
{
return obj == null;
}
// <summary>
// Determines whether the specified object is <see langword="null"/>,
// and invokes the specified <see cref="Action"/> delegate if the object is <see langword="null"/>.
// </summary>
// <returns>
// <c>true</c> if <paramref name="obj"/> is <see langword="null"/>; otherwise, <c>false</c>.
// </returns>
// <param name="obj">
// A <b>class</b> to test.
// </param>
// <param name="action">
// An <see cref="Action"/> delegate that references the method(s) called if <paramref name="obj"/> is <see langword="null"/>.
// </param>
// <typeparam name="T">
// The type of <paramref name="obj"/>.
// </typeparam>
internal static bool IsNullDo<T>(this T obj, Action action)
where T : class
{
if (obj == null)
{
action();
return true;
}
return false;
}
internal static bool IsText (this string value)
{
int len = value.Length;
@ -545,10 +498,7 @@ namespace WebSocketSharp {
var start = value.IndexOf ('\"');
var end = value.LastIndexOf ('\"');
if (start < end)
{
value = value.Substring(start + 1, end - start - 1)
.Replace("\\\"", "\"");
}
value = value.Substring (start + 1, end - start - 1).Replace ("\\\"", "\"");
return value.Trim ();
}
@ -570,7 +520,8 @@ namespace WebSocketSharp {
/// in the specified array of <see cref="char"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="value"/> contains any of <paramref name="chars"/>; otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="value"/> contains any of <paramref name="chars"/>;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="value">
/// A <see cref="string"/> to test.
@ -596,7 +547,7 @@ namespace WebSocketSharp {
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="collection">
/// A <see cref="NameValueCollection"/> that contains the entries.
/// A <see cref="NameValueCollection"/> to test.
/// </param>
/// <param name="name">
/// A <see cref="string"/> that contains the key of the entry to find.
@ -613,11 +564,11 @@ namespace WebSocketSharp {
/// with the specified both <paramref name="name"/> and <paramref name="value"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="collection"/> contains the entry with both <paramref name="name"/> and <paramref name="value"/>;
/// otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="collection"/> contains the entry with both <paramref name="name"/>
/// and <paramref name="value"/>; otherwise, <c>false</c>.
/// </returns>
/// <param name="collection">
/// A <see cref="NameValueCollection"/> that contains the entries.
/// A <see cref="NameValueCollection"/> to test.
/// </param>
/// <param name="name">
/// A <see cref="string"/> that contains the key of the entry to find.
@ -642,16 +593,16 @@ namespace WebSocketSharp {
}
/// <summary>
/// Emit the specified <see cref="EventHandler"/> delegate if is not <see langword="null"/>.
/// Emits the specified <see cref="EventHandler"/> delegate if not <see langword="null"/>.
/// </summary>
/// <param name="eventHandler">
/// An <see cref="EventHandler"/> to emit.
/// A <see cref="EventHandler"/> to emit.
/// </param>
/// <param name="sender">
/// An <see cref="object"/> that emits the <paramref name="eventHandler"/>.
/// An <see cref="object"/> from which emits this <paramref name="eventHandler"/>.
/// </param>
/// <param name="e">
/// An <see cref="EventArgs"/> that contains no event data.
/// A <see cref="EventArgs"/> that contains no event data.
/// </param>
public static void Emit (
this EventHandler eventHandler, object sender, EventArgs e)
@ -661,13 +612,13 @@ namespace WebSocketSharp {
}
/// <summary>
/// Emit the specified <b>EventHandler&lt;TEventArgs&gt;</b> delegate if is not <see langword="null"/>.
/// Emits the specified <b>EventHandler&lt;TEventArgs&gt;</b> delegate if not <see langword="null"/>.
/// </summary>
/// <param name="eventHandler">
/// An <b>EventHandler&lt;TEventArgs&gt;</b> to emit.
/// </param>
/// <param name="sender">
/// An <see cref="object"/> that emits the <paramref name="eventHandler"/>.
/// An <see cref="object"/> from which emits this <paramref name="eventHandler"/>.
/// </param>
/// <param name="e">
/// A <b>TEventArgs</b> that contains the event data.
@ -687,10 +638,11 @@ namespace WebSocketSharp {
/// Gets the absolute path from the specified <see cref="Uri"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that contains the absolute path if got successfully; otherwise, <see langword="null"/>.
/// A <see cref="string"/> that contains the absolute path if gets successfully;
/// otherwise, <see langword="null"/>.
/// </returns>
/// <param name="uri">
/// A <see cref="Uri"/> that contains the URI to get the absolute path from.
/// A <see cref="Uri"/> that contains a URI to get the absolute path from.
/// </param>
public static string GetAbsolutePath (this Uri uri)
{
@ -733,13 +685,13 @@ namespace WebSocketSharp {
}
/// <summary>
/// Gets the description of the HTTP status code using the specified <see cref="WebSocketSharp.Net.HttpStatusCode"/>.
/// Gets the description of the HTTP status code using the specified <see cref="HttpStatusCode"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that contains the description of the HTTP status code.
/// A <see cref="string"/> that contains the description of an HTTP status code.
/// </returns>
/// <param name="code">
/// One of <see cref="WebSocketSharp.Net.HttpStatusCode"/> values that contains an HTTP status code.
/// One of <see cref="HttpStatusCode"/> values that indicates an HTTP status code.
/// </param>
public static string GetDescription (this HttpStatusCode code)
{
@ -768,13 +720,14 @@ namespace WebSocketSharp {
}
/// <summary>
/// Gets the name and value from the specified <see cref="string"/> that contains a pair of name and value are separated by a separator string.
/// Gets the name and value from the specified <see cref="string"/> that contains a pair of name and value
/// separated by a separator string.
/// </summary>
/// <returns>
/// A <b>KeyValuePair&lt;string, string&gt;</b> that contains the name and value if any.
/// </returns>
/// <param name="nameAndValue">
/// A <see cref="string"/> that contains a pair of name and value are separated by a separator string.
/// A <see cref="string"/> that contains a pair of name and value separated by a separator string.
/// </param>
/// <param name="separator">
/// A <see cref="string"/> that contains a separator string.
@ -792,7 +745,7 @@ namespace WebSocketSharp {
/// Gets the description of the HTTP status code using the specified <see cref="int"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that contains the description of the HTTP status code.
/// A <see cref="string"/> that contains the description of an HTTP status code.
/// </returns>
/// <param name="code">
/// An <see cref="int"/> that contains an HTTP status code.
@ -907,20 +860,6 @@ namespace WebSocketSharp {
: true;
}
/// <summary>
/// Determines whether the specified <see cref="string"/> is empty.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="value"/> is empty; otherwise, <c>false</c>.
/// </returns>
/// <param name="value">
/// A <see cref="string"/> to test.
/// </param>
public static bool IsEmpty(this string value)
{
return value.Length == 0;
}
/// <summary>
/// Determines whether the specified <see cref="string"/> is enclosed in the specified <see cref="char"/>.
/// </summary>
@ -941,13 +880,13 @@ namespace WebSocketSharp {
}
/// <summary>
/// Determines whether the specified <see cref="WebSocketSharp.ByteOrder"/> is host (this computer architecture) byte order.
/// Determines whether the specified <see cref="ByteOrder"/> is host (this computer architecture) byte order.
/// </summary>
/// <returns>
/// <c>true</c> if the <paramref name="order"/> parameter is host byte order; otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="order"/> is host byte order; otherwise, <c>false</c>.
/// </returns>
/// <param name="order">
/// A <see cref="WebSocketSharp.ByteOrder"/> to test.
/// A <see cref="ByteOrder"/> to test.
/// </param>
public static bool IsHostOrder (this ByteOrder order)
{
@ -989,7 +928,7 @@ namespace WebSocketSharp {
/// Determines whether the specified <see cref="string"/> is <see langword="null"/> or empty.
/// </summary>
/// <returns>
/// <c>true</c> if the <paramref name="value"/> parameter is <see langword="null"/> or empty; otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="value"/> is <see langword="null"/> or empty; otherwise, <c>false</c>.
/// </returns>
/// <param name="value">
/// A <see cref="string"/> to test.
@ -1000,10 +939,10 @@ namespace WebSocketSharp {
}
/// <summary>
/// Determines whether the specified <see cref="string"/> is predefined scheme.
/// Determines whether the specified <see cref="string"/> is a predefined scheme.
/// </summary>
/// <returns>
/// <c>true</c> if the <paramref name="scheme"/> parameter is the predefined scheme; otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="scheme"/> is a predefined scheme; otherwise, <c>false</c>.
/// </returns>
/// <param name="scheme">
/// A <see cref="string"/> to test.
@ -1015,39 +954,32 @@ namespace WebSocketSharp {
char c = scheme [0];
if (c == 'h')
return (scheme == "http" || scheme == "https");
return scheme == "http" || scheme == "https";
if (c == 'f')
return (scheme == "file" || scheme == "ftp");
return scheme == "file" || scheme == "ftp";
if (c == 'w')
return (scheme == "ws" || scheme == "wss");
return scheme == "ws" || scheme == "wss";
if (c == 'n')
{
c = scheme [1];
if (c == 'e')
return (scheme == "news" || scheme == "net.pipe" || scheme == "net.tcp");
if (scheme == "nntp")
return true;
return false;
return c == 'e'
? scheme == "news" || scheme == "net.pipe" || scheme == "net.tcp"
: scheme == "nntp";
}
if ((c == 'g' && scheme == "gopher") || (c == 'm' && scheme == "mailto"))
return true;
return false;
return (c == 'g' && scheme == "gopher") || (c == 'm' && scheme == "mailto");
}
/// <summary>
/// Determines whether the specified <see cref="HttpListenerRequest"/> is the HTTP Upgrade request
/// Determines whether the specified <see cref="HttpListenerRequest"/> is an HTTP Upgrade request
/// to switch to the specified <paramref name="protocol"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the specified <see cref="HttpListenerRequest"/> is the HTTP Upgrade request
/// to switch to the specified <paramref name="protocol"/>; otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="request"/> is an HTTP Upgrade request
/// to switch to <paramref name="protocol"/>; otherwise, <c>false</c>.
/// </returns>
/// <param name="request">
/// A <see cref="HttpListenerRequest"/> that contains an HTTP request information.
@ -1080,26 +1012,21 @@ namespace WebSocketSharp {
if (protocol.Length == 0)
throw new ArgumentException ("Must not be empty.", "protocol");
if (!request.Headers.Contains("Upgrade", protocol))
return false;
if (!request.Headers.Contains("Connection", "Upgrade"))
return false;
return true;
return request.Headers.Contains ("Upgrade", protocol) &&
request.Headers.Contains ("Connection", "Upgrade");
}
/// <summary>
/// Determines whether the specified <see cref="string"/> is valid absolute path.
/// </summary>
/// <returns>
/// <c>true</c> if the <paramref name="absPath"/> parameter is valid absolute path; otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="absPath"/> is valid absolute path; otherwise, <c>false</c>.
/// </returns>
/// <param name="absPath">
/// A <see cref="string"/> to test.
/// </param>
/// <param name="message">
/// A <see cref="string"/> that receives a message if the <paramref name="absPath"/> is invalid.
/// A <see cref="string"/> that receives a message if <paramref name="absPath"/> is invalid.
/// </param>
public static bool IsValidAbsolutePath (this string absPath, out string message)
{
@ -1131,7 +1058,7 @@ namespace WebSocketSharp {
/// Determines whether the specified <see cref="string"/> is a URI string.
/// </summary>
/// <returns>
/// <c>true</c> if the <paramref name="uriString"/> parameter is maybe a URI string; otherwise, <c>false</c>.
/// <c>true</c> if <paramref name="uriString"/> is maybe a URI string; otherwise, <c>false</c>.
/// </returns>
/// <param name="uriString">
/// A <see cref="string"/> to test.
@ -1194,7 +1121,8 @@ namespace WebSocketSharp {
}
/// <summary>
/// Retrieves a sub-array from the specified <paramref name="array"/>. A sub-array starts at the specified element position.
/// Retrieves a sub-array from the specified <paramref name="array"/>.
/// A sub-array starts at the specified element position.
/// </summary>
/// <returns>
/// An array of T that receives a sub-array, or an empty array of T if any problems with the parameters.
@ -1203,7 +1131,7 @@ namespace WebSocketSharp {
/// An array of T that contains the data to retrieve a sub-array.
/// </param>
/// <param name="startIndex">
/// An <see cref="int"/> that contains the zero-based starting position of a sub-array in the <paramref name="array"/>.
/// An <see cref="int"/> that contains the zero-based starting position of a sub-array in <paramref name="array"/>.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of elements to retrieve a sub-array.
@ -1373,7 +1301,7 @@ namespace WebSocketSharp {
/// An array of <see cref="byte"/> to convert.
/// </param>
/// <param name="srcOrder">
/// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of <paramref name="src"/>.
/// A <see cref="ByteOrder"/> that indicates the byte order of <paramref name="src"/>.
/// </param>
/// <typeparam name="T">
/// The type of the return. The T must be a value type.
@ -1426,7 +1354,7 @@ namespace WebSocketSharp {
/// A T to convert.
/// </param>
/// <param name="order">
/// A <see cref="WebSocketSharp.ByteOrder"/> that indicates the byte order of the return.
/// A <see cref="ByteOrder"/> that indicates the byte order of the return.
/// </param>
/// <typeparam name="T">
/// The type of <paramref name="value"/>. The T must be a value type.
@ -1495,7 +1423,7 @@ namespace WebSocketSharp {
/// </summary>
/// <returns>
/// A <see cref="string"/> converted from <paramref name="array"/>, or a <see cref="String.Empty"/>
/// if the length of <paramref name="array"/> is zero.
/// if <paramref name="array"/> is empty.
/// </returns>
/// <param name="array">
/// An array of T to convert.
@ -1531,11 +1459,11 @@ namespace WebSocketSharp {
}
/// <summary>
/// Converts the specified <see cref="string"/> to a <see cref="Uri"/> object.
/// Converts the specified <see cref="string"/> to a <see cref="Uri"/>.
/// </summary>
/// <returns>
/// A <see cref="Uri"/> converted from the <paramref name="uriString"/> parameter, or <see langword="null"/>
/// if the <paramref name="uriString"/> is <see langword="null"/> or <see cref="String.Empty"/>.
/// A <see cref="Uri"/> converted from <paramref name="uriString"/>, or <see langword="null"/>
/// if <paramref name="uriString"/> is <see langword="null"/> or <see cref="String.Empty"/>.
/// </returns>
/// <param name="uriString">
/// A <see cref="string"/> to convert.
@ -1550,22 +1478,24 @@ namespace WebSocketSharp {
}
/// <summary>
/// Tries to create a new WebSocket <see cref="Uri"/> using the specified <paramref name="uriString"/>.
/// Tries to create a new WebSocket <see cref="Uri"/> with the specified <paramref name="uriString"/>.
/// </summary>
/// <returns>
/// <c>true</c> if the WebSocket <see cref="Uri"/> was successfully created; otherwise, <c>false</c>.
/// <c>true</c> if a WebSocket <see cref="Uri"/> is successfully created; otherwise, <c>false</c>.
/// </returns>
/// <param name="uriString">
/// A <see cref="string"/> that contains a WebSocket URI.
/// </param>
/// <param name="result">
/// When this method returns, contains a created WebSocket <see cref="Uri"/> if the <paramref name="uriString"/> parameter is valid WebSocket URI; otherwise, <see langword="null"/>.
/// When this method returns, contains a created WebSocket <see cref="Uri"/>
/// if <paramref name="uriString"/> is valid WebSocket URI; otherwise, <see langword="null"/>.
/// </param>
/// <param name="message">
/// When this method returns, contains a error message <see cref="string"/> if the <paramref name="uriString"/> parameter is invalid WebSocket URI; otherwise, <c>String.Empty</c>.
/// When this method returns, contains a error message <see cref="string"/>
/// if <paramref name="uriString"/> is invalid WebSocket URI; otherwise, <c>String.Empty</c>.
/// </param>
/// <exception cref="ArgumentNullException">
/// Is thrown when the <paramref name="uriString"/> parameter passed to a method is invalid because it is <see langword="null"/>.
/// <paramref name="uriString"/> is <see langword="null"/>.
/// </exception>
public static bool TryCreateWebSocketUri (this string uriString, out Uri result, out string message)
{
@ -1606,16 +1536,13 @@ namespace WebSocketSharp {
if ((scheme == "ws" && port == 443) ||
(scheme == "wss" && port == 80))
{
message = String.Format(
"Invalid pair of scheme and port: {0}, {1}", scheme, port);
message = String.Format ("Invalid pair of scheme and port: {0}, {1}", scheme, port);
return false;
}
}
else
{
port = scheme == "ws"
? 80
: 443;
port = scheme == "ws" ? 80 : 443;
var url = String.Format ("{0}://{1}:{2}{3}", scheme, uri.Host, port, uri.PathAndQuery);
uri = url.ToUri ();
}
@ -1630,8 +1557,8 @@ namespace WebSocketSharp {
/// URL-decodes the specified <see cref="string"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that receives a decoded string, or the <paramref name="s"/> parameter
/// if the <paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/>.
/// A <see cref="string"/> that receives a decoded string, or the <paramref name="s"/>
/// if <paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/>.
/// </returns>
/// <param name="s">
/// A <see cref="string"/> to decode.
@ -1647,8 +1574,8 @@ namespace WebSocketSharp {
/// URL-encodes the specified <see cref="string"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that receives a encoded string, or the <paramref name="s"/> parameter
/// if the <paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/>.
/// A <see cref="string"/> that receives a encoded string, or the <paramref name="s"/>
/// if <paramref name="s"/> is <see langword="null"/> or <see cref="String.Empty"/>.
/// </returns>
/// <param name="s">
/// A <see cref="string"/> to encode.
@ -1661,16 +1588,16 @@ namespace WebSocketSharp {
}
/// <summary>
/// Writes the specified content data using the specified <see cref="WebSocketSharp.Net.HttpListenerResponse"/>.
/// Writes the specified content data using the specified <see cref="HttpListenerResponse"/>.
/// </summary>
/// <param name="response">
/// A <see cref="WebSocketSharp.Net.HttpListenerResponse"/> that contains a network stream to write a content data.
/// A <see cref="HttpListenerResponse"/> that contains a network stream to write a content data.
/// </param>
/// <param name="content">
/// An array of <see cref="byte"/> that contains a content data to write.
/// </param>
/// <exception cref="ArgumentNullException">
/// Is thrown when the <paramref name="response"/> parameter passed to a method is invalid because it is <see langword="null"/>.
/// <paramref name="response"/> is <see langword="null"/>.
/// </exception>
public static void WriteContent (this HttpListenerResponse response, byte [] content)
{

View File

@ -37,8 +37,8 @@ using System.Text;
using System.Globalization;
using System.Collections;
namespace WebSocketSharp.Net {
namespace WebSocketSharp.Net
{
/// <summary>
/// Provides a set of properties and methods used to manage an HTTP Cookie.
/// </summary>
@ -54,8 +54,8 @@ namespace WebSocketSharp.Net {
/// </para>
/// </remarks>
[Serializable]
public sealed class Cookie {
public sealed class Cookie
{
#region Static Private Fields
static char [] reservedCharsForName = new char [] {' ', '=', ';', ',', '\n', '\r', '\t'};
@ -270,7 +270,7 @@ namespace WebSocketSharp.Net {
/// </value>
public string Comment {
get { return comment; }
set { comment = value.IsNull () ? String.Empty : value; }
set { comment = value ?? String.Empty; }
}
/// <summary>
@ -395,7 +395,7 @@ namespace WebSocketSharp.Net {
/// </value>
public string Path {
get { return path; }
set { path = value.IsNull () ? String.Empty : value; }
set { path = value ?? String.Empty; }
}
/// <summary>
@ -480,9 +480,7 @@ namespace WebSocketSharp.Net {
if (!CanSetValue (value, out msg))
throw new CookieException (msg);
val = value.IsEmpty ()
? "\"\""
: value;
val = value.Length == 0 ? "\"\"" : value;
}
}
@ -528,7 +526,7 @@ namespace WebSocketSharp.Net {
static bool CanSetValue (string value, out string message)
{
if (value.IsNull ()) {
if (value == null) {
message = "Value must not be null.";
return false;
}
@ -595,7 +593,7 @@ namespace WebSocketSharp.Net {
if (!comment.IsNullOrEmpty ())
result.AppendFormat ("; Comment={0}", comment.UrlEncode ());
if (!commentUri.IsNull ())
if (commentUri != null)
result.AppendFormat ("; CommentURL={0}", commentUri.OriginalString.Quote ());
if (discard)
@ -614,7 +612,7 @@ namespace WebSocketSharp.Net {
for (int i = 0; i < values.Length; i++) {
tmp [i] = int.MinValue;
var v = values [i].Trim ();
if (v.IsEmpty ())
if (v.Length == 0)
continue;
if (!int.TryParse (v, out tmp [i])) {
@ -636,7 +634,7 @@ namespace WebSocketSharp.Net {
// From client to server
internal string ToRequestString (Uri uri)
{
if (name.IsEmpty ())
if (name.Length == 0)
return String.Empty;
if (version == 0)
@ -646,12 +644,12 @@ namespace WebSocketSharp.Net {
result.AppendFormat ("$Version={0}; {1}={2}", version, name, val);
if (!path.IsNullOrEmpty ())
result.AppendFormat ("; $Path={0}", path);
else if (!uri.IsNull ())
else if (uri != null)
result.AppendFormat ("; $Path={0}", uri.GetAbsolutePath ());
else
result.Append ("; $Path=/");
bool append_domain = uri.IsNull () || uri.Host != domain;
bool append_domain = uri == null || uri.Host != domain;
if (append_domain && !domain.IsNullOrEmpty ())
result.AppendFormat ("; $Domain={0}", domain);
@ -667,7 +665,7 @@ namespace WebSocketSharp.Net {
// From server to client
internal string ToResponseString ()
{
return name.IsEmpty ()
return name.Length == 0
? String.Empty
: version == 0
? ToResponseStringVersion0 ()
@ -691,7 +689,7 @@ namespace WebSocketSharp.Net {
public override bool Equals (Object comparand)
{
var cookie = comparand as Cookie;
return !cookie.IsNull() &&
return cookie != null &&
name.Equals (cookie.Name, StringComparison.InvariantCultureIgnoreCase) &&
val.Equals (cookie.Value, StringComparison.InvariantCulture) &&
path.Equals (cookie.Path, StringComparison.InvariantCulture) &&

View File

@ -179,7 +179,7 @@ namespace WebSocketSharp.Net {
/// </exception>
public Cookie this [string name] {
get {
if (name.IsNull ())
if (name == null)
throw new ArgumentNullException ("name");
foreach (var cookie in Sorted) {
@ -199,7 +199,7 @@ namespace WebSocketSharp.Net {
/// </value>
public Object SyncRoot {
get {
if (sync.IsNull ())
if (sync == null)
sync = new object ();
return sync;
@ -219,18 +219,18 @@ namespace WebSocketSharp.Net {
string [] pairs = Split(value).ToArray();
for (int i = 0; i < pairs.Length; i++) {
string pair = pairs [i].Trim ();
if (pair.IsEmpty ())
if (pair.Length == 0)
continue;
if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) {
version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
}
else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Path = pair.GetValueInternal ("=");
}
else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Domain = pair.GetValueInternal ("=");
}
else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) {
@ -238,11 +238,11 @@ namespace WebSocketSharp.Net {
? "\"\""
: pair.GetValueInternal ("=");
if (!cookie.IsNull ())
if (cookie != null)
cookie.Port = port;
}
else {
if (!cookie.IsNull ())
if (cookie != null)
cookies.Add (cookie);
string name;
@ -265,7 +265,7 @@ namespace WebSocketSharp.Net {
}
}
if (!cookie.IsNull ())
if (cookie != null)
cookies.Add (cookie);
return cookies;
@ -279,11 +279,11 @@ namespace WebSocketSharp.Net {
string [] pairs = Split(value).ToArray();
for (int i = 0; i < pairs.Length; i++) {
string pair = pairs [i].Trim ();
if (pair.IsEmpty ())
if (pair.Length == 0)
continue;
if (pair.StartsWith ("version", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
}
else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
@ -299,21 +299,21 @@ namespace WebSocketSharp.Net {
out expires))
expires = DateTime.Now;
if (!cookie.IsNull () && cookie.Expires == DateTime.MinValue)
if (cookie != null && cookie.Expires == DateTime.MinValue)
cookie.Expires = expires.ToLocalTime ();
}
else if (pair.StartsWith ("max-age", StringComparison.InvariantCultureIgnoreCase)) {
int max = Int32.Parse (pair.GetValueInternal ("=").Trim ('"'));
var expires = DateTime.Now.AddSeconds ((double) max);
if (!cookie.IsNull ())
if (cookie != null)
cookie.Expires = expires;
}
else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Path = pair.GetValueInternal ("=");
}
else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Domain = pair.GetValueInternal ("=");
}
else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) {
@ -321,31 +321,31 @@ namespace WebSocketSharp.Net {
? "\"\""
: pair.GetValueInternal ("=");
if (!cookie.IsNull ())
if (cookie != null)
cookie.Port = port;
}
else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Comment = pair.GetValueInternal ("=").UrlDecode ();
}
else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.CommentUri = pair.GetValueInternal ("=").Trim ('"').ToUri ();
}
else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Discard = true;
}
else if (pair.StartsWith ("secure", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.Secure = true;
}
else if (pair.StartsWith ("httponly", StringComparison.InvariantCultureIgnoreCase)) {
if (!cookie.IsNull ())
if (cookie != null)
cookie.HttpOnly = true;
}
else {
if (!cookie.IsNull ())
if (cookie != null)
cookies.Add (cookie);
string name;
@ -366,7 +366,7 @@ namespace WebSocketSharp.Net {
}
}
if (!cookie.IsNull ())
if (cookie != null)
cookies.Add (cookie);
return cookies;
@ -457,7 +457,7 @@ namespace WebSocketSharp.Net {
/// </exception>
public void Add (Cookie cookie)
{
if (cookie.IsNull ())
if (cookie == null)
throw new ArgumentNullException ("cookie");
int pos = SearchCookie (cookie);
@ -478,7 +478,7 @@ namespace WebSocketSharp.Net {
/// </exception>
public void Add (CookieCollection cookies)
{
if (cookies.IsNull ())
if (cookies == null)
throw new ArgumentNullException ("cookies");
foreach (Cookie cookie in cookies)
@ -519,7 +519,7 @@ namespace WebSocketSharp.Net {
/// </exception>
public void CopyTo (Array array, int index)
{
if (array.IsNull ())
if (array == null)
throw new ArgumentNullException ("array");
if (index < 0)
@ -561,7 +561,7 @@ namespace WebSocketSharp.Net {
/// </exception>
public void CopyTo (Cookie [] array, int index)
{
if (array.IsNull ())
if (array == null)
throw new ArgumentNullException ("array");
if (index < 0)

View File

@ -36,8 +36,8 @@ using System.Linq;
using System.Net;
using System.Text;
namespace WebSocketSharp.Net {
namespace WebSocketSharp.Net
{
/// <summary>
/// Provides access to a response to a request being processed by a <see cref="HttpListener"/> instance.
/// </summary>
@ -184,10 +184,10 @@ namespace WebSocketSharp.Net {
if (HeadersSent)
throw new InvalidOperationException ("Cannot be changed after headers are sent.");
if (value.IsNull ())
if (value == null)
throw new ArgumentNullException ("value");
if (value.IsEmpty ())
if (value.Length == 0)
throw new ArgumentException ("Must not be empty.", "value");
content_type = value;
@ -342,7 +342,7 @@ namespace WebSocketSharp.Net {
if (HeadersSent)
throw new InvalidOperationException ("Cannot be changed after headers are sent.");
if (value.IsEmpty ())
if (value.Length == 0)
throw new ArgumentException ("Must not be empty.", "value");
location = value;
@ -758,7 +758,7 @@ namespace WebSocketSharp.Net {
/// </exception>
public void SetCookie (Cookie cookie)
{
if (cookie.IsNull())
if (cookie == null)
throw new ArgumentNullException ("cookie");
if (!CanAddOrUpdate (cookie))

View File

@ -43,15 +43,15 @@ using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
namespace WebSocketSharp.Net {
namespace WebSocketSharp.Net
{
/// <summary>
/// Provides a collection of the HTTP headers associated with a request or response.
/// </summary>
[Serializable]
[ComVisible (true)]
public class WebHeaderCollection : NameValueCollection, ISerializable {
public class WebHeaderCollection : NameValueCollection, ISerializable
{
#region Fields
static readonly Dictionary<string, HttpHeaderInfo> headers;
@ -275,7 +275,7 @@ namespace WebSocketSharp.Net {
protected WebHeaderCollection (
SerializationInfo serializationInfo, StreamingContext streamingContext)
{
if (serializationInfo.IsNull ())
if (serializationInfo == null)
throw new ArgumentNullException ("serializationInfo");
try {
@ -582,9 +582,7 @@ namespace WebSocketSharp.Net {
static bool TryGetHeaderInfo (string name, out HttpHeaderInfo info)
{
info = GetHeaderInfo (name);
return info.IsNull ()
? false
: true;
return info != null;
}
#endregion
@ -932,7 +930,7 @@ namespace WebSocketSharp.Net {
public override string [] GetValues (string header)
{
string [] values = base.GetValues (header);
return values.IsNull () || values.Length == 0
return values == null || values.Length == 0
? null
: values;
}
@ -949,7 +947,7 @@ namespace WebSocketSharp.Net {
public override string [] GetValues (int index)
{
string [] values = base.GetValues (index);
return values.IsNull () || values.Length == 0
return values == null || values.Length == 0
? null
: values;
}
@ -971,7 +969,7 @@ namespace WebSocketSharp.Net {
public override void GetObjectData (
SerializationInfo serializationInfo, StreamingContext streamingContext)
{
if (serializationInfo.IsNull ())
if (serializationInfo == null)
throw new ArgumentNullException ("serializationInfo");
serializationInfo.AddValue ("InternallyCreated", internallyCreated);