Skip to content

Commit 989c2be

Browse files
authored
Pre 1.13.0 (#147)
* Code Review * Upgrade SWAN before publish
1 parent ebb69cd commit 989c2be

File tree

11 files changed

+58
-172
lines changed

11 files changed

+58
-172
lines changed

src/Unosquare.Labs.EmbedIO/Modules/StaticFilesModule.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ public StaticFilesModule(
9595
_virtualPaths = new VirtualPaths(Path.GetFullPath(fileSystemPath), useDirectoryBrowser);
9696

9797
UseGzip = true;
98-
#if DEBUG // When debugging, disable RamCache
98+
#if DEBUG
99+
// When debugging, disable RamCache
99100
UseRamCache = false;
100-
#else // Otherwise, enable it by default
101+
#else
102+
// Otherwise, enable it by default
101103
UseRamCache = true;
102104
#endif
103105
DefaultDocument = DefaultDocumentName;

src/Unosquare.Labs.EmbedIO/System.Net/HttpBase.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,30 @@ private static async Task<byte[]> ReadEntityBodyAsync(Stream stream, string leng
157157
? await stream.ReadBytesAsync((int) len, ct)
158158
: null;
159159
}
160-
161-
private static bool EqualsWith(int value, char c, Action<int> action)
162-
{
163-
action(value);
164-
return value == c - 0;
165-
}
166-
160+
167161
private static string[] ReadHeaders(Stream stream)
168162
{
169163
var buff = new List<byte>();
170164
var cnt = 0;
171-
172-
void Add(int i)
165+
166+
bool EqualsWith(int i, char c)
173167
{
174168
if (i == -1)
175169
throw new EndOfStreamException("The header cannot be read from the data source.");
176170

177171
buff.Add((byte) i);
178172
cnt++;
173+
174+
return i == c - 0;
179175
}
180176

181177
var read = false;
182178
while (cnt < HeadersMaxLength)
183179
{
184-
if (EqualsWith(stream.ReadByte(), '\r', Add) &&
185-
EqualsWith(stream.ReadByte(), '\n', Add) &&
186-
EqualsWith(stream.ReadByte(), '\r', Add) &&
187-
EqualsWith(stream.ReadByte(), '\n', Add))
180+
if (EqualsWith(stream.ReadByte(), '\r') &&
181+
EqualsWith(stream.ReadByte(), '\n') &&
182+
EqualsWith(stream.ReadByte(), '\r') &&
183+
EqualsWith(stream.ReadByte(), '\n'))
188184
{
189185
read = true;
190186
break;

src/Unosquare.Labs.EmbedIO/System.Net/HttpListener.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,11 @@ internal void RegisterContext(HttpListenerContext context)
234234
throw new Exception("Unable to register context");
235235
}
236236

237-
internal void UnregisterContext(HttpListenerContext context)
238-
{
239-
_ctxQueue.TryRemove(context.Id, out var removedContext);
240-
}
237+
internal void UnregisterContext(HttpListenerContext context) => _ctxQueue.TryRemove(context.Id, out var _);
241238

242239
internal void AddConnection(HttpConnection cnc) => _connections[cnc] = cnc;
243240

244-
internal void RemoveConnection(HttpConnection cnc) => _connections.TryRemove(cnc, out var instance);
241+
internal void RemoveConnection(HttpConnection cnc) => _connections.TryRemove(cnc, out var _);
245242

246243
private void Close(bool closeExisting)
247244
{

src/Unosquare.Labs.EmbedIO/System.Net/HttpListenerRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace Unosquare.Net
4848
/// </summary>
4949
public sealed class HttpListenerRequest
5050
{
51-
private static readonly byte[] _100Continue = Encoding.UTF8.GetBytes("HTTP/1.1 100 Continue\r\n\r\n");
51+
private static readonly byte[] HttpStatus100 = Encoding.UTF8.GetBytes("HTTP/1.1 100 Continue\r\n\r\n");
5252
private static readonly char[] Separators = { ' ' };
5353

5454
private readonly HttpListenerContext _context;
@@ -419,7 +419,7 @@ internal void FinishInitialization()
419419

420420
if (string.Compare(Headers["Expect"], "100-continue", StringComparison.OrdinalIgnoreCase) == 0)
421421
{
422-
_context.Connection.GetResponseStream().InternalWrite(_100Continue, 0, _100Continue.Length);
422+
_context.Connection.GetResponseStream().InternalWrite(HttpStatus100, 0, HttpStatus100.Length);
423423
}
424424
}
425425

src/Unosquare.Labs.EmbedIO/System.Net/HttpSysSettings.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Unosquare.Labs.EmbedIO/System.Net/NetExtensions.cs

Lines changed: 7 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ internal static string Unquote(this string str)
7070

7171
return str.Trim();
7272
}
73-
74-
internal static bool IsData(this byte opcode) => opcode == 0x1 || opcode == 0x2;
75-
76-
internal static bool IsData(this Opcode opcode) => opcode == Opcode.Text || opcode == Opcode.Binary;
77-
73+
7874
internal static byte[] InternalToByteArray(this ushort value, Endianness order)
7975
{
8076
var bytes = BitConverter.GetBytes(value);
@@ -92,76 +88,22 @@ internal static byte[] InternalToByteArray(this ulong value, Endianness order)
9288

9389
return bytes;
9490
}
95-
96-
internal static bool IsControl(this byte opcode) => opcode > 0x7 && opcode < 0x10;
97-
98-
internal static bool IsReserved(this CloseStatusCode code)
99-
{
100-
return code == CloseStatusCode.Undefined ||
101-
code == CloseStatusCode.NoStatus ||
102-
code == CloseStatusCode.Abnormal ||
103-
code == CloseStatusCode.TlsHandshakeFailure;
104-
}
105-
106-
/// <summary>
107-
/// Converts the order of the specified array of <see cref="byte"/> to the host byte order.
108-
/// </summary>
109-
/// <returns>
110-
/// An array of <see cref="byte"/> converted from <paramref name="source"/>.
111-
/// </returns>
112-
/// <param name="source">
113-
/// An array of <see cref="byte"/> to convert.
114-
/// </param>
115-
/// <param name="sourceOrder">
116-
/// One of the <see cref="Endianness"/> enum values, specifies the byte order of
117-
/// <paramref name="source"/>.
118-
/// </param>
119-
/// <exception cref="ArgumentNullException">
120-
/// <paramref name="source"/> is <see langword="null"/>.
121-
/// </exception>
91+
12292
internal static byte[] ToHostOrder(this byte[] source, Endianness sourceOrder)
12393
{
12494
if (source == null)
12595
throw new ArgumentNullException(nameof(source));
12696

12797
return source.Length > 1 && !sourceOrder.IsHostOrder() ? source.Reverse().ToArray() : source;
12898
}
129-
130-
/// <summary>
131-
/// Determines whether the specified <see cref="Endianness"/> is host (this computer
132-
/// architecture) byte order.
133-
/// </summary>
134-
/// <returns>
135-
/// <c>true</c> if <paramref name="order"/> is host byte order; otherwise, <c>false</c>.
136-
/// </returns>
137-
/// <param name="order">
138-
/// One of the <see cref="Endianness"/> enum values, to test.
139-
/// </param>
99+
140100
internal static bool IsHostOrder(this Endianness order)
141101
{
142102
// true: !(true ^ true) or !(false ^ false)
143103
// false: !(true ^ false) or !(false ^ true)
144104
return !(BitConverter.IsLittleEndian ^ (order == Endianness.Little));
145105
}
146-
147-
/// <summary>
148-
/// Tries to create a <see cref="Uri"/> for WebSocket with
149-
/// the specified <paramref name="uriString"/>.
150-
/// </summary>
151-
/// <returns>
152-
/// <c>true</c> if a <see cref="Uri"/> is successfully created; otherwise, <c>false</c>.
153-
/// </returns>
154-
/// <param name="uriString">
155-
/// A <see cref="string"/> that represents a WebSocket URL to try.
156-
/// </param>
157-
/// <param name="result">
158-
/// When this method returns, a <see cref="Uri"/> that represents a WebSocket URL,
159-
/// or <see langword="null"/> if <paramref name="uriString"/> is invalid.
160-
/// </param>
161-
/// <param name="message">
162-
/// When this method returns, a <see cref="string"/> that represents an error message,
163-
/// or <see cref="String.Empty"/> if <paramref name="uriString"/> is valid.
164-
/// </param>
106+
165107
internal static bool TryCreateWebSocketUri(
166108
this string uriString, out Uri result, out string message)
167109
{
@@ -211,20 +153,7 @@ internal static bool TryCreateWebSocketUri(
211153

212154
internal static bool IsToken(this string value) =>
213155
value.All(c => c >= 0x20 && c < 0x7f && !Tspecials.Contains(c));
214-
215-
/// <summary>
216-
/// Gets the collection of the HTTP cookies from the specified HTTP <paramref name="headers"/>.
217-
/// </summary>
218-
/// <returns>
219-
/// A <see cref="CookieCollection"/> that receives a collection of the HTTP cookies.
220-
/// </returns>
221-
/// <param name="headers">
222-
/// A <see cref="NameValueCollection"/> that contains a collection of the HTTP headers.
223-
/// </param>
224-
/// <param name="response">
225-
/// <c>true</c> if <paramref name="headers"/> is a collection of the response headers;
226-
/// otherwise, <c>false</c>.
227-
/// </param>
156+
228157
internal static CookieCollection GetCookies(this NameValueCollection headers, bool response)
229158
{
230159
var name = response ? "Set-Cookie" : Headers.Cookie;
@@ -242,42 +171,11 @@ internal static string ToExtensionString(this CompressionMethod method, params s
242171

243172
return parameters == null || parameters.Length == 0 ? m : $"{m}; {string.Join("; ", parameters)}";
244173
}
245-
246-
/// <summary>
247-
/// Determines whether the specified <see cref="NameValueCollection"/> contains the entry with
248-
/// the specified both <paramref name="name"/> and <paramref name="value"/>.
249-
/// </summary>
250-
/// <returns>
251-
/// <c>true</c> if <paramref name="collection"/> contains the entry with both
252-
/// <paramref name="name"/> and <paramref name="value"/>; otherwise, <c>false</c>.
253-
/// </returns>
254-
/// <param name="collection">
255-
/// A <see cref="NameValueCollection"/> to test.
256-
/// </param>
257-
/// <param name="name">
258-
/// A <see cref="string"/> that represents the key of the entry to find.
259-
/// </param>
260-
/// <param name="value">
261-
/// A <see cref="string"/> that represents the value of the entry to find.
262-
/// </param>
174+
263175
internal static bool Contains(this NameValueCollection collection, string name, string value)
264176
=> collection[name]?.Split(Strings.CommaSplitChar)
265177
.Any(val => val.Trim().Equals(value, StringComparison.OrdinalIgnoreCase)) == true;
266-
267-
/// <summary>
268-
/// Determines whether the specified <see cref="string"/> contains any of characters in
269-
/// the specified array of <see cref="char"/>.
270-
/// </summary>
271-
/// <returns>
272-
/// <c>true</c> if <paramref name="value"/> contains any of <paramref name="chars"/>;
273-
/// otherwise, <c>false</c>.
274-
/// </returns>
275-
/// <param name="value">
276-
/// A <see cref="string"/> to test.
277-
/// </param>
278-
/// <param name="chars">
279-
/// An array of <see cref="char"/> that contains characters to find.
280-
/// </param>
178+
281179
internal static bool Contains(this string value, params char[] chars)
282180
=> chars?.Length == 0 || (!string.IsNullOrEmpty(value) && value.IndexOfAny(chars) > -1);
283181

src/Unosquare.Labs.EmbedIO/System.Net/PayloadData.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Unosquare.Net
3737

3838
internal class PayloadData : IEnumerable<byte>
3939
{
40-
public static readonly ulong MaxLength;
40+
public static readonly ulong MaxLength = long.MaxValue;
4141

4242
private readonly byte[] _data;
4343
private readonly long _length;
@@ -46,11 +46,6 @@ internal class PayloadData : IEnumerable<byte>
4646
private string _reason;
4747
private bool _reasonSet;
4848

49-
static PayloadData()
50-
{
51-
MaxLength = Int64.MaxValue;
52-
}
53-
5449
internal PayloadData()
5550
{
5651
_code = 1005;
@@ -136,15 +131,9 @@ internal string Reason
136131
}
137132
}
138133

139-
IEnumerator IEnumerable.GetEnumerator()
140-
{
141-
return GetEnumerator();
142-
}
134+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
143135

144-
public IEnumerator<byte> GetEnumerator()
145-
{
146-
return ((IEnumerable<byte>)_data).GetEnumerator();
147-
}
136+
public IEnumerator<byte> GetEnumerator() => ((IEnumerable<byte>)_data).GetEnumerator();
148137

149138
public override string ToString() => BitConverter.ToString(_data);
150139

src/Unosquare.Labs.EmbedIO/System.Net/WebSocket.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public class WebSocket : IDisposable
9292
/// Represents the random number generator used internally.
9393
/// </summary>
9494
internal static readonly RandomNumberGenerator RandomNumber = RandomNumberGenerator.Create();
95-
95+
9696
private const string Guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
97-
97+
9898
private readonly Action<MessageEventArgs> _message;
9999
private readonly bool _client;
100100
private readonly object _forState = new object();
@@ -165,7 +165,7 @@ public WebSocket(string url)
165165
IsSecure = _uri.Scheme == "wss";
166166
#endif
167167
_waitTime = TimeSpan.FromSeconds(5);
168-
_forMessageEventQueue = ((ICollection) _messageEventQueue).SyncRoot;
168+
_forMessageEventQueue = ((ICollection)_messageEventQueue).SyncRoot;
169169
_validator = new WebSocketValidator(this);
170170
}
171171

@@ -183,7 +183,7 @@ internal WebSocket(WebSocketContext context)
183183
_forMessageEventQueue = ((ICollection)_messageEventQueue).SyncRoot;
184184
_validator = new WebSocketValidator(this);
185185
}
186-
186+
187187
/// <summary>
188188
/// Occurs when the WebSocket connection has been closed.
189189
/// </summary>
@@ -203,7 +203,7 @@ internal WebSocket(WebSocketContext context)
203203
/// Occurs when the WebSocket connection has been established.
204204
/// </summary>
205205
public event EventHandler OnOpen;
206-
206+
207207
/// <summary>
208208
/// Gets or sets the compression method used to compress a message on the WebSocket connection.
209209
/// </summary>
@@ -513,7 +513,7 @@ public async Task CloseAsync(CloseStatusCode code = CloseStatusCode.Undefined, s
513513
return;
514514
}
515515

516-
var send = !code.IsReserved();
516+
var send = !IsOpcodeReserved(code);
517517
await InternalCloseAsync(new CloseEventArgs(code, reason), send, send, ct: ct);
518518
}
519519

@@ -669,7 +669,7 @@ public void SetCookie(Cookie cookie)
669669
CookieCollection.Add(cookie);
670670
}
671671
}
672-
672+
673673
/// <inheritdoc />
674674
public void Dispose()
675675
{
@@ -795,6 +795,14 @@ private static HttpResponse CreateHandshakeFailureResponse(HttpStatusCode code)
795795
return ret;
796796
}
797797

798+
private static bool IsOpcodeReserved(CloseStatusCode code)
799+
{
800+
return code == CloseStatusCode.Undefined ||
801+
code == CloseStatusCode.NoStatus ||
802+
code == CloseStatusCode.Abnormal ||
803+
code == CloseStatusCode.TlsHandshakeFailure;
804+
}
805+
798806
// As server
799807
private async Task<bool> AcceptHandshakeAsync()
800808
{
@@ -992,7 +1000,7 @@ private void Fatal(string message, Exception exception = null)
9921000
private void Fatal(string message, CloseStatusCode code)
9931001
{
9941002
// TODO: Wait?
995-
InternalCloseAsync(new CloseEventArgs(code, message), !code.IsReserved(), false).Wait();
1003+
InternalCloseAsync(new CloseEventArgs(code, message), !IsOpcodeReserved(code), false).Wait();
9961004
}
9971005

9981006
private void Message()
@@ -1561,7 +1569,7 @@ private async Task SetClientStream()
15611569
#endif
15621570
}
15631571
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
1564-
1572+
15651573
private void StartReceiving()
15661574
{
15671575
if (_messageEventQueue.Count > 0)

0 commit comments

Comments
 (0)