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

File diff suppressed because it is too large Load Diff

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,12 +689,12 @@ namespace WebSocketSharp.Net {
public override bool Equals (Object comparand)
{
var cookie = comparand as Cookie;
return !cookie.IsNull() &&
name.Equals (cookie.Name, StringComparison.InvariantCultureIgnoreCase) &&
val.Equals (cookie.Value, StringComparison.InvariantCulture) &&
path.Equals (cookie.Path, StringComparison.InvariantCulture) &&
domain.Equals (cookie.Domain, StringComparison.InvariantCultureIgnoreCase) &&
version == cookie.Version;
return cookie != null &&
name.Equals (cookie.Name, StringComparison.InvariantCultureIgnoreCase) &&
val.Equals (cookie.Value, StringComparison.InvariantCulture) &&
path.Equals (cookie.Path, StringComparison.InvariantCulture) &&
domain.Equals (cookie.Domain, StringComparison.InvariantCultureIgnoreCase) &&
version == cookie.Version;
}
/// <summary>

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);