@@ -11,6 +11,7 @@ internal sealed partial class HttpConnection : IDisposable
11
11
{
12
12
private sealed class ChunkedEncodingWriteStream : HttpContentWriteStream
13
13
{
14
+ private static readonly byte [ ] s_crlfBytes = "\r \n "u8 . ToArray ( ) ;
14
15
private static readonly byte [ ] s_finalChunkBytes = "0\r \n \r \n "u8 . ToArray ( ) ;
15
16
16
17
public ChunkedEncodingWriteStream ( HttpConnection connection ) : base ( connection )
@@ -31,12 +32,14 @@ public override void Write(ReadOnlySpan<byte> buffer)
31
32
}
32
33
33
34
// Write chunk length in hex followed by \r\n
34
- connection . WriteHexInt32Async ( buffer . Length , async: false ) . GetAwaiter ( ) . GetResult ( ) ;
35
- connection . WriteTwoBytesAsync ( ( byte ) '\r ' , ( byte ) '\n ' , async: false ) . GetAwaiter ( ) . GetResult ( ) ;
35
+ ValueTask writeTask = connection . WriteHexInt32Async ( buffer . Length , async: false ) ;
36
+ Debug . Assert ( writeTask . IsCompleted ) ;
37
+ writeTask . GetAwaiter ( ) . GetResult ( ) ;
38
+ connection . Write ( s_crlfBytes ) ;
36
39
37
40
// Write chunk contents followed by \r\n
38
41
connection . Write ( buffer ) ;
39
- connection . WriteTwoBytesAsync ( ( byte ) ' \r ' , ( byte ) ' \n ' , async : false ) . GetAwaiter ( ) . GetResult ( ) ;
42
+ connection . Write ( s_crlfBytes ) ;
40
43
}
41
44
42
45
public override ValueTask WriteAsync ( ReadOnlyMemory < byte > buffer , CancellationToken ignored )
@@ -62,11 +65,11 @@ static async ValueTask WriteChunkAsync(HttpConnection connection, ReadOnlyMemory
62
65
{
63
66
// Write chunk length in hex followed by \r\n
64
67
await connection . WriteHexInt32Async ( buffer . Length , async: true ) . ConfigureAwait ( false ) ;
65
- await connection . WriteTwoBytesAsync ( ( byte ) ' \r ' , ( byte ) ' \n ' , async : true ) . ConfigureAwait ( false ) ;
68
+ await connection . WriteAsync ( s_crlfBytes ) . ConfigureAwait ( false ) ;
66
69
67
70
// Write chunk contents followed by \r\n
68
- await connection . WriteAsync ( buffer , async : true ) . ConfigureAwait ( false ) ;
69
- await connection . WriteTwoBytesAsync ( ( byte ) ' \r ' , ( byte ) ' \n ' , async : true ) . ConfigureAwait ( false ) ;
71
+ await connection . WriteAsync ( buffer ) . ConfigureAwait ( false ) ;
72
+ await connection . WriteAsync ( s_crlfBytes ) . ConfigureAwait ( false ) ;
70
73
}
71
74
}
72
75
@@ -75,7 +78,16 @@ public override Task FinishAsync(bool async)
75
78
// Send 0 byte chunk to indicate end, then final CrLf
76
79
HttpConnection connection = GetConnectionOrThrow ( ) ;
77
80
_connection = null ;
78
- return connection . WriteBytesAsync ( s_finalChunkBytes , async ) ;
81
+
82
+ if ( async)
83
+ {
84
+ return connection . WriteAsync( s_finalChunkBytes ) . AsTask ( ) ;
85
+ }
86
+ else
87
+ {
88
+ connection . Write ( s_finalChunkBytes ) ;
89
+ return Task . CompletedTask;
90
+ }
79
91
}
80
92
}
81
93
}
0 commit comments