Skip to content

Commit 66b84c4

Browse files
authored
IPv6 Support (#215)
* Resolving issues with parse Uri Ipv6 * Working on IPv6 connection * Add IPv6 test * Change to test * Update test * Change order setup * Update Program.cs * Configure Await * Minor issues * WIP * Correct Lazy for MIME * Major refactor * Test * Missing ConfigureAwait * Unify websocket testing * Conditional * Match handshake with netcore * Increate timeout
1 parent 409b761 commit 66b84c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+401
-512
lines changed

src/Unosquare.Labs.EmbedIO.Samples/AppDbContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using Unosquare.Labs.LiteLib;
2-
3-
namespace Unosquare.Labs.EmbedIO.Samples
1+
namespace Unosquare.Labs.EmbedIO.Samples
42
{
3+
using LiteLib;
4+
55
internal sealed class AppDbContext : LiteDbContext
66
{
7-
public AppDbContext() : base("mydbfile.db")
7+
public AppDbContext() : base("mydbfile.db", false)
88
{
99
// map this context to the database file mydbfile.db and don't use any logging capabilities.
1010
}

src/Unosquare.Labs.EmbedIO.Samples/Person.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using LiteLib;
44
using Swan;
5-
using System.ComponentModel.DataAnnotations.Schema;
65

76
/// <inheritdoc />
87
/// <summary>

src/Unosquare.Labs.EmbedIO.Samples/Program.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ internal class Program
1414
/// <param name="args">The arguments.</param>
1515
private static async Task Main(string[] args)
1616
{
17-
var url = "http://localhost:8787/";
18-
19-
if (args.Length > 0)
20-
url = args[0];
17+
var url = args.Length > 0 ? args[0] : "http://*:8787/";
2118

2219
AppDbContext.InitDatabase();
2320

@@ -37,7 +34,7 @@ private static async Task Main(string[] args)
3734

3835

3936
// Our web server is disposable.
40-
using (var server = new WebServer(new[] { url }, Constants.RoutingStrategy.Regex, HttpListenerMode.EmbedIO))
37+
using (var server = new WebServer(url))
4138
{
4239
// First, we will configure our web server by adding Modules.
4340
// Please note that order DOES matter.
@@ -86,7 +83,9 @@ private static async Task Main(string[] args)
8683
await server.RunAsync(ctSource.Token);
8784

8885
"Bye".Info();
86+
87+
Terminal.Flush();
8988
}
9089
}
9190
}
92-
}
91+
}

src/Unosquare.Labs.EmbedIO.Samples/StaticFilesSample.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public static void Setup(WebServer server, bool useGzip)
4141
server.RegisterModule(new StaticFilesModule(HtmlRootPath));
4242
// The static files module will cache small files in ram until it detects they have been modified.
4343
server.Module<StaticFilesModule>().UseRamCache = false;
44-
server.Module<StaticFilesModule>().DefaultExtension = ".html";
4544
server.Module<StaticFilesModule>().UseGzip = useGzip;
4645
}
4746
}

src/Unosquare.Labs.EmbedIO.Samples/Unosquare.Labs.EmbedIO.Samples.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="LiteLib" Version="0.17.2" />
22+
<PackageReference Include="LiteLib" Version="0.18.1" />
2323
<PackageReference Include="Tubular.ServerSide" Version="1.5.1" />
2424
</ItemGroup>
2525

@@ -28,7 +28,6 @@
2828
</ItemGroup>
2929

3030
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
31-
<Reference Include="System.ComponentModel.DataAnnotations" />
3231
<Reference Include="Microsoft.CSharp" />
3332
</ItemGroup>
3433

src/Unosquare.Labs.EmbedIO/Abstractions/IHttpListener.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Threading;
56
using System.Threading.Tasks;
67

78
/// <summary>
@@ -52,7 +53,10 @@ public interface IHttpListener : IDisposable
5253
/// <summary>
5354
/// Gets the HTTP context asynchronous.
5455
/// </summary>
55-
/// <returns>A task that represents the time delay for the HTTP Context.</returns>
56-
Task<IHttpContext> GetContextAsync();
56+
/// <param name="ct">The cancellation token.</param>
57+
/// <returns>
58+
/// A task that represents the time delay for the HTTP Context.
59+
/// </returns>
60+
Task<IHttpContext> GetContextAsync(CancellationToken ct);
5761
}
5862
}

src/Unosquare.Labs.EmbedIO/Abstractions/IWebSocket.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IWebSocket : IDisposable
2525
/// </summary>
2626
/// <param name="buffer">The buffer.</param>
2727
/// <param name="isText">if set to <c>true</c> [is text].</param>
28-
/// <param name="ct">The ct.</param>
28+
/// <param name="ct">The cancellation token.</param>
2929
/// <returns>
3030
/// A task that represents the asynchronous of send data using websocket.
3131
/// </returns>
@@ -34,7 +34,7 @@ public interface IWebSocket : IDisposable
3434
/// <summary>
3535
/// Closes the asynchronous.
3636
/// </summary>
37-
/// <param name="ct">The ct.</param>
37+
/// <param name="ct">The cancellation token.</param>
3838
/// <returns>The task object representing the asynchronous operation.</returns>
3939
Task CloseAsync(CancellationToken ct = default);
4040
}

src/Unosquare.Labs.EmbedIO/Constants/MimeTypes.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Unosquare.Labs.EmbedIO.Constants
22
{
3+
using System;
34
using System.Collections.Generic;
45

56
/// <summary>
@@ -13,8 +14,8 @@ public static class MimeTypes
1314
/// <remarks>
1415
/// Originally started from: http://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension.
1516
/// </remarks>
16-
public static readonly Dictionary<string, string> DefaultMimeTypes =
17-
new Dictionary<string, string>(Strings.StandardStringComparer)
17+
public static readonly Lazy<Dictionary<string, string>> DefaultMimeTypes =
18+
new Lazy<Dictionary<string, string>>(() => new Dictionary<string, string>(Strings.StandardStringComparer)
1819
{
1920
#region Big list of MIME types
2021

@@ -582,6 +583,6 @@ public static class MimeTypes
582583
{".zip", "application/x-zip-compressed"},
583584

584585
#endregion
585-
};
586+
});
586587
}
587588
}

src/Unosquare.Labs.EmbedIO/Constants/Responses.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ internal static class Responses
1818
internal const string Response405Html = "<html><head></head><body><h1>405 - Method Not Allowed</h1></body></html>";
1919

2020
/// <summary>
21-
/// Default Http Status 500 response output
21+
/// Default Http Status 500 response output:
22+
///
2223
/// The first format argument takes the error message.
2324
/// The second format argument takes the stack trace.
2425
/// </summary>

src/Unosquare.Labs.EmbedIO/Extensions.cs

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public static async Task<bool> StringResponseAsync(
440440
var buffer = (encoding ?? Encoding.UTF8).GetBytes(content);
441441

442442
context.Response.ContentType = contentType;
443-
await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
443+
await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
444444

445445
return true;
446446
}
@@ -512,8 +512,8 @@ public static async Task<MemoryStream> CompressAsync(
512512
{
513513
using (var compressor = new DeflateStream(targetStream, CompressionMode.Compress, true))
514514
{
515-
await buffer.CopyToAsync(compressor, 1024, cancellationToken);
516-
await buffer.CopyToAsync(compressor);
515+
await buffer.CopyToAsync(compressor, 1024, cancellationToken).ConfigureAwait(false);
516+
await buffer.CopyToAsync(compressor).ConfigureAwait(false);
517517

518518
// WebSocket use this
519519
targetStream.Write(LastByte, 0, 1);
@@ -524,7 +524,7 @@ public static async Task<MemoryStream> CompressAsync(
524524
{
525525
using (var compressor = new DeflateStream(buffer, CompressionMode.Decompress))
526526
{
527-
await compressor.CopyToAsync(targetStream);
527+
await compressor.CopyToAsync(targetStream).ConfigureAwait(false);
528528
}
529529
}
530530

@@ -534,63 +534,20 @@ public static async Task<MemoryStream> CompressAsync(
534534
{
535535
using (var compressor = new GZipStream(targetStream, CompressionMode.Compress, true))
536536
{
537-
await buffer.CopyToAsync(compressor);
537+
await buffer.CopyToAsync(compressor).ConfigureAwait(false);
538538
}
539539
}
540540
else
541541
{
542542
using (var compressor = new GZipStream(buffer, CompressionMode.Decompress))
543543
{
544-
await compressor.CopyToAsync(targetStream);
544+
await compressor.CopyToAsync(targetStream).ConfigureAwait(false);
545545
}
546546
}
547547

548548
break;
549549
case CompressionMethod.None:
550-
await buffer.CopyToAsync(targetStream);
551-
break;
552-
default:
553-
throw new ArgumentOutOfRangeException(nameof(method), method, null);
554-
}
555-
556-
return targetStream;
557-
}
558-
559-
/// <summary>
560-
/// Compresses the specified buffer stream using the G-Zip compression algorithm.
561-
/// </summary>
562-
/// <param name="buffer">The buffer.</param>
563-
/// <param name="method">The method.</param>
564-
/// <returns>
565-
/// A block of bytes of compressed stream.
566-
/// </returns>
567-
public static MemoryStream Compress(
568-
this Stream buffer,
569-
CompressionMethod method = CompressionMethod.Gzip)
570-
{
571-
buffer.Position = 0;
572-
var targetStream = new MemoryStream();
573-
574-
switch (method)
575-
{
576-
case CompressionMethod.Deflate:
577-
using (var compressor = new DeflateStream(targetStream, CompressionMode.Compress, true))
578-
{
579-
buffer.CopyTo(compressor, 1024);
580-
buffer.CopyTo(compressor);
581-
582-
// WebSocket use this
583-
targetStream.Write(LastByte, 0, 1);
584-
targetStream.Position = 0;
585-
}
586-
587-
break;
588-
case CompressionMethod.Gzip:
589-
using (var compressor = new GZipStream(targetStream, CompressionMode.Compress, true))
590-
{
591-
buffer.CopyTo(compressor);
592-
}
593-
550+
await buffer.CopyToAsync(targetStream).ConfigureAwait(false);
594551
break;
595552
default:
596553
throw new ArgumentOutOfRangeException(nameof(method), method, null);

0 commit comments

Comments
 (0)