diff options
| author | Erwin de Haan <EraYaN@users.noreply.github.com> | 2019-01-06 21:50:43 +0100 |
|---|---|---|
| committer | Erwin de Haan <EraYaN@users.noreply.github.com> | 2019-01-10 20:38:53 +0100 |
| commit | ec1f5dc317182582ebff843c9e8a4d5277405469 (patch) | |
| tree | 6514de336cc9aa94becb3fbd767285dfa61d0b1b /SocketHttpListener/Net | |
| parent | 3d867c2c46cec39b669bb8647efef677f32b8a8d (diff) | |
Mayor code cleanup
Add Argument*Exceptions now use proper nameof operators.
Added exception messages to quite a few Argument*Exceptions.
Fixed rethorwing to be proper syntax.
Added a ton of null checkes. (This is only a start, there are about 500 places that need proper null handling)
Added some TODOs to log certain exceptions.
Fix sln again.
Fixed all AssemblyInfo's and added proper copyright (where I could find them)
We live in *current year*.
Fixed the use of braces.
Fixed a ton of properties, and made a fair amount of functions static that should be and can be static.
Made more Methods that should be static static.
You can now use static to find bad functions!
Removed unused variable. And added one more proper XML comment.
Diffstat (limited to 'SocketHttpListener/Net')
| -rw-r--r-- | SocketHttpListener/Net/ChunkStream.cs | 4 | ||||
| -rw-r--r-- | SocketHttpListener/Net/CookieHelper.cs | 6 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpEndPointListener.cs | 1 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpListener.cs | 2 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpListenerBasicIdentity.cs | 6 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpListenerContext.cs | 4 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpListenerPrefixCollection.cs | 2 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpListenerRequest.Managed.cs | 4 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpListenerRequest.cs | 2 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpListenerResponse.cs | 6 | ||||
| -rw-r--r-- | SocketHttpListener/Net/HttpResponseStream.Managed.cs | 2 | ||||
| -rw-r--r-- | SocketHttpListener/Net/WebHeaderCollection.cs | 26 | ||||
| -rw-r--r-- | SocketHttpListener/Net/WebSockets/HttpWebSocket.cs | 12 |
13 files changed, 39 insertions, 38 deletions
diff --git a/SocketHttpListener/Net/ChunkStream.cs b/SocketHttpListener/Net/ChunkStream.cs index b41285dbc..4672f3071 100644 --- a/SocketHttpListener/Net/ChunkStream.cs +++ b/SocketHttpListener/Net/ChunkStream.cs @@ -271,7 +271,7 @@ namespace SocketHttpListener.Net { if (_saved.Length > 0) { - _chunkSize = Int32.Parse(RemoveChunkExtension(_saved.ToString()), NumberStyles.HexNumber); + _chunkSize = int.Parse(RemoveChunkExtension(_saved.ToString()), NumberStyles.HexNumber); } } catch (Exception) @@ -285,7 +285,7 @@ namespace SocketHttpListener.Net _chunkRead = 0; try { - _chunkSize = Int32.Parse(RemoveChunkExtension(_saved.ToString()), NumberStyles.HexNumber); + _chunkSize = int.Parse(RemoveChunkExtension(_saved.ToString()), NumberStyles.HexNumber); } catch (Exception) { diff --git a/SocketHttpListener/Net/CookieHelper.cs b/SocketHttpListener/Net/CookieHelper.cs index a32131956..6c1764e09 100644 --- a/SocketHttpListener/Net/CookieHelper.cs +++ b/SocketHttpListener/Net/CookieHelper.cs @@ -36,7 +36,7 @@ namespace SocketHttpListener.Net if (pair.StartsWith("version", StringComparison.OrdinalIgnoreCase)) { if (cookie != null) - cookie.Version = Int32.Parse(pair.GetValueInternal("=").Trim('"')); + cookie.Version = int.Parse(pair.GetValueInternal("=").Trim('"')); } else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase)) { @@ -58,7 +58,7 @@ namespace SocketHttpListener.Net } else if (pair.StartsWith("max-age", StringComparison.OrdinalIgnoreCase)) { - var max = Int32.Parse(pair.GetValueInternal("=").Trim('"')); + var max = int.Parse(pair.GetValueInternal("=").Trim('"')); var expires = DateTime.Now.AddSeconds((double)max); if (cookie != null) cookie.Expires = expires; @@ -113,7 +113,7 @@ namespace SocketHttpListener.Net cookies.Add(cookie); string name; - string val = String.Empty; + string val = string.Empty; var pos = pair.IndexOf('='); if (pos == -1) diff --git a/SocketHttpListener/Net/HttpEndPointListener.cs b/SocketHttpListener/Net/HttpEndPointListener.cs index fb093314c..ef960ba1d 100644 --- a/SocketHttpListener/Net/HttpEndPointListener.cs +++ b/SocketHttpListener/Net/HttpEndPointListener.cs @@ -168,6 +168,7 @@ namespace SocketHttpListener.Net } catch (ObjectDisposedException) { + // TODO Investigate or properly fix. } catch (Exception ex) { diff --git a/SocketHttpListener/Net/HttpListener.cs b/SocketHttpListener/Net/HttpListener.cs index 941b99f35..ce7e97bc4 100644 --- a/SocketHttpListener/Net/HttpListener.cs +++ b/SocketHttpListener/Net/HttpListener.cs @@ -248,7 +248,7 @@ namespace SocketHttpListener.Net internal void CheckDisposed() { if (disposed) - throw new ObjectDisposedException(GetType().ToString()); + throw new ObjectDisposedException(GetType().Name); } internal void RegisterContext(HttpListenerContext context) diff --git a/SocketHttpListener/Net/HttpListenerBasicIdentity.cs b/SocketHttpListener/Net/HttpListenerBasicIdentity.cs index faa26693d..d20e72777 100644 --- a/SocketHttpListener/Net/HttpListenerBasicIdentity.cs +++ b/SocketHttpListener/Net/HttpListenerBasicIdentity.cs @@ -26,7 +26,7 @@ namespace SocketHttpListener.Net public GenericIdentity(string name) { if (name == null) - throw new System.ArgumentNullException("name"); + throw new System.ArgumentNullException(nameof(name)); m_name = name; m_type = ""; @@ -35,9 +35,9 @@ namespace SocketHttpListener.Net public GenericIdentity(string name, string type) { if (name == null) - throw new System.ArgumentNullException("name"); + throw new System.ArgumentNullException(nameof(name)); if (type == null) - throw new System.ArgumentNullException("type"); + throw new System.ArgumentNullException(nameof(type)); m_name = name; m_type = type; diff --git a/SocketHttpListener/Net/HttpListenerContext.cs b/SocketHttpListener/Net/HttpListenerContext.cs index 0aaac1ad5..e3e6eb906 100644 --- a/SocketHttpListener/Net/HttpListenerContext.cs +++ b/SocketHttpListener/Net/HttpListenerContext.cs @@ -49,7 +49,7 @@ namespace SocketHttpListener.Net public GenericPrincipal(IIdentity identity, string[] roles) { if (identity == null) - throw new ArgumentNullException("identity"); + throw new ArgumentNullException(nameof(identity)); m_identity = identity; if (roles != null) @@ -81,7 +81,7 @@ namespace SocketHttpListener.Net for (int i = 0; i < m_roles.Length; ++i) { - if (m_roles[i] != null && String.Compare(m_roles[i], role, StringComparison.OrdinalIgnoreCase) == 0) + if (m_roles[i] != null && string.Compare(m_roles[i], role, StringComparison.OrdinalIgnoreCase) == 0) return true; } return false; diff --git a/SocketHttpListener/Net/HttpListenerPrefixCollection.cs b/SocketHttpListener/Net/HttpListenerPrefixCollection.cs index ed99af1a6..f0e496a5a 100644 --- a/SocketHttpListener/Net/HttpListenerPrefixCollection.cs +++ b/SocketHttpListener/Net/HttpListenerPrefixCollection.cs @@ -85,7 +85,7 @@ namespace SocketHttpListener.Net { listener.CheckDisposed(); if (uriPrefix == null) - throw new ArgumentNullException("uriPrefix"); + throw new ArgumentNullException(nameof(uriPrefix)); bool result = prefixes.Remove(uriPrefix); if (result && listener.IsListening) diff --git a/SocketHttpListener/Net/HttpListenerRequest.Managed.cs b/SocketHttpListener/Net/HttpListenerRequest.Managed.cs index 47a6dfcfd..8b68afe33 100644 --- a/SocketHttpListener/Net/HttpListenerRequest.Managed.cs +++ b/SocketHttpListener/Net/HttpListenerRequest.Managed.cs @@ -183,14 +183,14 @@ namespace SocketHttpListener.Net } } - if (String.Compare(Headers[HttpKnownHeaderNames.Expect], "100-continue", StringComparison.OrdinalIgnoreCase) == 0) + if (string.Compare(Headers[HttpKnownHeaderNames.Expect], "100-continue", StringComparison.OrdinalIgnoreCase) == 0) { HttpResponseStream output = _context.Connection.GetResponseStream(); output.InternalWrite(s_100continue, 0, s_100continue.Length); } } - internal static string Unquote(String str) + internal static string Unquote(string str) { int start = str.IndexOf('\"'); int end = str.LastIndexOf('\"'); diff --git a/SocketHttpListener/Net/HttpListenerRequest.cs b/SocketHttpListener/Net/HttpListenerRequest.cs index 1b369dfa8..16e245611 100644 --- a/SocketHttpListener/Net/HttpListenerRequest.cs +++ b/SocketHttpListener/Net/HttpListenerRequest.cs @@ -27,7 +27,7 @@ namespace SocketHttpListener.Net public string[] UserLanguages => Helpers.ParseMultivalueHeader(Headers[HttpKnownHeaderNames.AcceptLanguage]); - private CookieCollection ParseCookies(Uri uri, string setCookieHeader) + private static CookieCollection ParseCookies(Uri uri, string setCookieHeader) { CookieCollection cookies = new CookieCollection(); return cookies; diff --git a/SocketHttpListener/Net/HttpListenerResponse.cs b/SocketHttpListener/Net/HttpListenerResponse.cs index 1cbd6165e..351a206ee 100644 --- a/SocketHttpListener/Net/HttpListenerResponse.cs +++ b/SocketHttpListener/Net/HttpListenerResponse.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Net; @@ -71,7 +71,7 @@ namespace SocketHttpListener.Net public bool SendChunked { - get { return EntitySendFormat == EntitySendFormat.Chunked; ; } + get { return EntitySendFormat == EntitySendFormat.Chunked; } set { EntitySendFormat = value ? EntitySendFormat.Chunked : EntitySendFormat.ContentLength; } } @@ -104,7 +104,7 @@ namespace SocketHttpListener.Net } else { - throw new ArgumentOutOfRangeException("net_clsmall"); + throw new ArgumentOutOfRangeException(nameof(value)); } } } diff --git a/SocketHttpListener/Net/HttpResponseStream.Managed.cs b/SocketHttpListener/Net/HttpResponseStream.Managed.cs index e727f1b4a..d62758371 100644 --- a/SocketHttpListener/Net/HttpResponseStream.Managed.cs +++ b/SocketHttpListener/Net/HttpResponseStream.Managed.cs @@ -157,7 +157,7 @@ namespace SocketHttpListener.Net private static byte[] s_crlf = new byte[] { 13, 10 }; private static byte[] GetChunkSizeBytes(int size, bool final) { - string str = String.Format("{0:x}\r\n{1}", size, final ? "\r\n" : ""); + string str = string.Format("{0:x}\r\n{1}", size, final ? "\r\n" : ""); return Encoding.ASCII.GetBytes(str); } diff --git a/SocketHttpListener/Net/WebHeaderCollection.cs b/SocketHttpListener/Net/WebHeaderCollection.cs index 4bed81404..150a6f6ce 100644 --- a/SocketHttpListener/Net/WebHeaderCollection.cs +++ b/SocketHttpListener/Net/WebHeaderCollection.cs @@ -93,10 +93,10 @@ namespace SocketHttpListener.Net public void Add(string header) { if (header == null) - throw new ArgumentNullException("header"); + throw new ArgumentNullException(nameof(header)); int pos = header.IndexOf(':'); if (pos == -1) - throw new ArgumentException("no colon found", "header"); + throw new ArgumentException("no colon found", nameof(header)); this.Add(header.Substring(0, pos), header.Substring(pos + 1)); } @@ -104,7 +104,7 @@ namespace SocketHttpListener.Net public override void Add(string name, string value) { if (name == null) - throw new ArgumentNullException("name"); + throw new ArgumentNullException(nameof(name)); this.AddWithoutValidate(name, value); } @@ -112,13 +112,13 @@ namespace SocketHttpListener.Net protected void AddWithoutValidate(string headerName, string headerValue) { if (!IsHeaderName(headerName)) - throw new ArgumentException("invalid header name: " + headerName, "headerName"); + throw new ArgumentException("invalid header name: " + headerName, nameof(headerName)); if (headerValue == null) - headerValue = String.Empty; + headerValue = string.Empty; else headerValue = headerValue.Trim(); if (!IsHeaderValue(headerValue)) - throw new ArgumentException("invalid header value: " + headerValue, "headerValue"); + throw new ArgumentException("invalid header value: " + headerValue, nameof(headerValue)); AddValue(headerName, headerValue); } @@ -131,7 +131,7 @@ namespace SocketHttpListener.Net internal List<string> GetValues_internal(string header, bool split) { if (header == null) - throw new ArgumentNullException("header"); + throw new ArgumentNullException(nameof(header)); var values = base.GetValues(header); if (values == null || values.Count == 0) @@ -205,10 +205,10 @@ namespace SocketHttpListener.Net public static bool IsRestricted(string headerName, bool response) { if (headerName == null) - throw new ArgumentNullException("headerName"); + throw new ArgumentNullException(nameof(headerName)); if (headerName.Length == 0) - throw new ArgumentException("empty string", "headerName"); + throw new ArgumentException("empty string", nameof(headerName)); if (!IsHeaderName(headerName)) throw new ArgumentException("Invalid character in header"); @@ -224,11 +224,11 @@ namespace SocketHttpListener.Net public override void Set(string name, string value) { if (name == null) - throw new ArgumentNullException("name"); + throw new ArgumentNullException(nameof(name)); if (!IsHeaderName(name)) throw new ArgumentException("invalid header name"); if (value == null) - value = String.Empty; + value = string.Empty; else value = value.Trim(); if (!IsHeaderValue(value)) @@ -288,7 +288,7 @@ namespace SocketHttpListener.Net { int pos = header.IndexOf(':'); if (pos == -1) - throw new ArgumentException("no colon found", "header"); + throw new ArgumentException("no colon found", nameof(header)); SetInternal(header.Substring(0, pos), header.Substring(pos + 1)); } @@ -296,7 +296,7 @@ namespace SocketHttpListener.Net internal void SetInternal(string name, string value) { if (value == null) - value = String.Empty; + value = string.Empty; else value = value.Trim(); if (!IsHeaderValue(value)) diff --git a/SocketHttpListener/Net/WebSockets/HttpWebSocket.cs b/SocketHttpListener/Net/WebSockets/HttpWebSocket.cs index 9dc9143f8..6b6da5be0 100644 --- a/SocketHttpListener/Net/WebSockets/HttpWebSocket.cs +++ b/SocketHttpListener/Net/WebSockets/HttpWebSocket.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.Diagnostics.CodeAnalysis; @@ -86,27 +86,27 @@ namespace SocketHttpListener.Net.WebSockets if (receiveBufferSize < MinReceiveBufferSize) { - throw new ArgumentOutOfRangeException("net_WebSockets_ArgumentOutOfRange_TooSmall"); + throw new ArgumentOutOfRangeException(nameof(receiveBufferSize), "The receiveBufferSize was too small."); } if (sendBufferSize < MinSendBufferSize) { - throw new ArgumentOutOfRangeException("net_WebSockets_ArgumentOutOfRange_TooSmall"); + throw new ArgumentOutOfRangeException(nameof(sendBufferSize), "The sendBufferSize was too small."); } if (receiveBufferSize > MaxBufferSize) { - throw new ArgumentOutOfRangeException("net_WebSockets_ArgumentOutOfRange_TooBig"); + throw new ArgumentOutOfRangeException(nameof(receiveBufferSize), "The receiveBufferSize was too large."); } if (sendBufferSize > MaxBufferSize) { - throw new ArgumentOutOfRangeException("net_WebSockets_ArgumentOutOfRange_TooBig"); + throw new ArgumentOutOfRangeException(nameof(sendBufferSize), "The sendBufferSize was too large."); } if (keepAliveInterval < Timeout.InfiniteTimeSpan) // -1 millisecond { - throw new ArgumentOutOfRangeException("net_WebSockets_ArgumentOutOfRange_TooSmall"); + throw new ArgumentOutOfRangeException(nameof(keepAliveInterval), "The keepAliveInterval was too small."); } } |
