Skip to content

Commit 1236174

Browse files
committed
http test fixes
1 parent 6b941c9 commit 1236174

File tree

7 files changed

+33
-38
lines changed

7 files changed

+33
-38
lines changed

src/DotNetty.Buffers/DotNetty.Buffers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
<Reference Include="Microsoft.CSharp" />
4040
</ItemGroup>
4141
<ItemGroup>
42-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
42+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.1" />
4343
</ItemGroup>
4444
</Project>

src/DotNetty.Codecs.Http/DotNetty.Codecs.Http.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<Compile Include="..\shared\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
3131
</ItemGroup>
3232
<ItemGroup>
33-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
33+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.1" />
3434
</ItemGroup>
3535
<ItemGroup>
3636
<ProjectReference Include="..\DotNetty.Common\DotNetty.Common.csproj" />

src/DotNetty.Codecs.Http/HttpServerUpgradeHandler.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -254,34 +254,28 @@ bool Upgrade(IChannelHandlerContext ctx, IFullHttpRequest request)
254254
var upgradeEvent = new UpgradeEvent(upgradeProtocol, request);
255255

256256
IUpgradeCodec finalUpgradeCodec = upgradeCodec;
257-
ctx.WriteAndFlushAsync(upgradeResponse).ContinueWith(t =>
258-
{
259-
try
260-
{
261-
if (t.Status == TaskStatus.RanToCompletion)
262-
{
263-
// Perform the upgrade to the new protocol.
264-
this.sourceCodec.UpgradeFrom(ctx);
265-
finalUpgradeCodec.UpgradeTo(ctx, request);
266-
267-
// Notify that the upgrade has occurred. Retain the event to offset
268-
// the release() in the finally block.
269-
ctx.FireUserEventTriggered(upgradeEvent.Retain());
270-
271-
// Remove this handler from the pipeline.
272-
ctx.Channel.Pipeline.Remove(this);
273-
}
274-
else
275-
{
276-
ctx.Channel.CloseAsync();
277-
}
278-
}
279-
finally
280-
{
281-
// Release the event if the upgrade event wasn't fired.
282-
upgradeEvent.Release();
283-
}
284-
}, TaskContinuationOptions.ExecuteSynchronously);
257+
try
258+
{
259+
Task writeTask = ctx.WriteAndFlushAsync(upgradeResponse);
260+
261+
// Perform the upgrade to the new protocol.
262+
this.sourceCodec.UpgradeFrom(ctx);
263+
finalUpgradeCodec.UpgradeTo(ctx, request);
264+
265+
// Remove this handler from the pipeline.
266+
ctx.Channel.Pipeline.Remove(this);
267+
268+
// Notify that the upgrade has occurred. Retain the event to offset
269+
// the release() in the finally block.
270+
ctx.FireUserEventTriggered(upgradeEvent.Retain());
271+
272+
writeTask.CloseOnFailure(ctx.Channel);
273+
}
274+
finally
275+
{
276+
// Release the event if the upgrade event wasn't fired.
277+
upgradeEvent.Release();
278+
}
285279
return true;
286280
}
287281

src/DotNetty.Codecs.Http/WebSockets/Extensions/WebSocketServerExtensionHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public override ValueTask WriteAsync(IChannelHandlerContext ctx, object msg)
9595
}
9696

9797
Task task = base.WriteAsync(ctx, msg).AsTask();
98-
task.ContinueWith(this.upgradeCompletedContinuation, ctx);
98+
task.ContinueWith(this.upgradeCompletedContinuation, ctx, TaskContinuationOptions.ExecuteSynchronously);
9999
return new ValueTask(task);
100100
}
101101

src/DotNetty.Codecs/TaskExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public static Task CloseOnComplete(this Task task, IChannel channel)
3333

3434
public static Task CloseOnFailure(this Task task, IChannelHandlerContext ctx)
3535
=> task.ContinueWith(CloseOnFailureContinuation, ctx, TaskContinuationOptions.ExecuteSynchronously);
36+
37+
public static Task CloseOnFailure(this Task task, IChannel channel)
38+
=> task.ContinueWith(CloseOnFailureContinuation, channel, TaskContinuationOptions.ExecuteSynchronously);
3639

3740
static Task Close(Task task, object state)
3841
{

src/DotNetty.Common/DotNetty.Common.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</ItemGroup>
3232
<ItemGroup>
3333
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
34-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
34+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.1" />
3535
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" />
3636
</ItemGroup>
3737
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">

test/DotNetty.Codecs.Http.Tests/HttpServerUpgradeHandlerTest.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public override void ChannelRead(IChannelHandlerContext ctx, object msg)
5959
// written the upgrade response, and upgraded the pipeline.
6060
Assert.True(this.writeUpgradeMessage);
6161
Assert.False(this.writeFlushed);
62-
//Assert.Null(ctx.Channel.Pipeline.Get<HttpServerCodec>());
63-
//Assert.NotNull(ctx.Channel.Pipeline.Get("marker"));
62+
Assert.Null(ctx.Channel.Pipeline.Get<HttpServerCodec>());
63+
Assert.NotNull(ctx.Channel.Pipeline.Get("marker"));
6464
}
6565
finally
6666
{
@@ -111,13 +111,11 @@ public void UpgradesPipelineInSameMethodInvocation()
111111
IByteBuffer upgrade = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(UpgradeString));
112112

113113
Assert.False(channel.WriteInbound(upgrade));
114-
//Assert.Null(channel.Pipeline.Get<HttpServerCodec>());
115-
//Assert.NotNull(channel.Pipeline.Get("marker"));
116-
117-
channel.Flush();
118114
Assert.Null(channel.Pipeline.Get<HttpServerCodec>());
119115
Assert.NotNull(channel.Pipeline.Get("marker"));
120116

117+
channel.Flush();
118+
121119
var upgradeMessage = channel.ReadOutbound<IByteBuffer>();
122120
const string ExpectedHttpResponse = "HTTP/1.1 101 Switching Protocols\r\n" +
123121
"connection: upgrade\r\n" +

0 commit comments

Comments
 (0)