Skip to content

Commit abc29ac

Browse files
committed
use arraypool instead memorypool
1 parent cea7951 commit abc29ac

File tree

7 files changed

+65
-39
lines changed

7 files changed

+65
-39
lines changed

cadente/Sisk.Cadente/HttpConnection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public HttpConnection ( Stream connectionStream, HttpHost host, IPEndPoint endpo
3939
public async Task<HttpConnectionState> HandleConnectionEvents () {
4040
bool connectionCloseRequested = false;
4141

42-
var requestBuffer = MemoryPool<byte>.Shared.Rent ( REQUEST_BUFFER_SIZE );
43-
var responseHeadersBuffer = MemoryPool<byte>.Shared.Rent ( RESPONSE_BUFFER_SIZE );
42+
var requestBuffer = ArrayPool<byte>.Shared.Rent ( REQUEST_BUFFER_SIZE );
43+
var responseHeadersBuffer = ArrayPool<byte>.Shared.Rent ( RESPONSE_BUFFER_SIZE );
4444

4545
try {
4646

@@ -111,8 +111,8 @@ public async Task<HttpConnectionState> HandleConnectionEvents () {
111111
return HttpConnectionState.ConnectionClosed;
112112
}
113113
finally {
114-
responseHeadersBuffer.Dispose ();
115-
requestBuffer.Dispose ();
114+
ArrayPool<byte>.Shared.Return ( responseHeadersBuffer );
115+
ArrayPool<byte>.Shared.Return ( requestBuffer );
116116
}
117117
}
118118

cadente/Sisk.Cadente/HttpHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private void Dispose ( bool disposing ) {
151151

152152
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
153153
internal void InvokeContextCreated ( HttpHostContext context ) {
154-
this.ContextCreated!.Invoke ( this, context );
154+
this.ContextCreated?.Invoke ( this, context );
155155
}
156156

157157
/// <inheritdoc/>

cadente/Sisk.Cadente/HttpHostContext.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// File name: HttpHostContext.cs
88
// Repository: https://github.com/sisk-http/core
99

10-
using System.Buffers;
1110
using System.Runtime.CompilerServices;
1211
using System.Text;
1312
using Sisk.Cadente.HttpSerializer;
@@ -20,7 +19,7 @@ namespace Sisk.Cadente;
2019
/// </summary>
2120
public sealed class HttpHostContext {
2221

23-
private IMemoryOwner<byte> _responseBuffer;
22+
private byte [] _responseBuffer;
2423
private Stream _connectionStream;
2524

2625
internal bool ResponseHeadersAlreadySent = false;
@@ -32,7 +31,7 @@ internal bool WriteHttpResponseHeaders () {
3231
}
3332

3433
this.ResponseHeadersAlreadySent = true;
35-
return HttpResponseSerializer.WriteHttpResponseHeaders ( this._connectionStream, this._responseBuffer.Memory.Span, this.Response );
34+
return HttpResponseSerializer.WriteHttpResponseHeaders ( this._connectionStream, this._responseBuffer, this.Response );
3635
}
3736

3837
/// <summary>
@@ -50,7 +49,7 @@ internal bool WriteHttpResponseHeaders () {
5049
/// </summary>
5150
public bool KeepAlive { get; set; } = true;
5251

53-
internal HttpHostContext ( HttpRequestBase baseRequest, Stream connectionStream, IMemoryOwner<byte> responseBuffer ) {
52+
internal HttpHostContext ( HttpRequestBase baseRequest, Stream connectionStream, byte [] responseBuffer ) {
5453
this._connectionStream = connectionStream;
5554
this._responseBuffer = responseBuffer;
5655

@@ -64,6 +63,7 @@ internal HttpHostContext ( HttpRequestBase baseRequest, Stream connectionStream,
6463
/// </summary>
6564
public sealed class HttpRequest {
6665

66+
bool wasExpectationSent = false;
6767
private HttpRequestStream _requestStream;
6868
private HttpRequestBase _baseRequest;
6969

@@ -91,6 +91,14 @@ public sealed class HttpRequest {
9191
/// Gets the stream containing the content of the request.
9292
/// </summary>
9393
public Stream GetRequestStream () {
94+
95+
if (this._baseRequest.IsExpecting100 && !this.wasExpectationSent) {
96+
this.wasExpectationSent = HttpResponseSerializer.WriteExpectationContinue ( this._requestStream );
97+
98+
if (!this.wasExpectationSent)
99+
throw new InvalidOperationException ( "Unable to obtain the input stream for the request." );
100+
}
101+
94102
return this._requestStream;
95103
}
96104

@@ -150,7 +158,7 @@ public HttpEventStreamWriter GetEventStream ( Encoding encoding ) {
150158
this.Headers.Set ( new HttpHeader ( "Cache-Control", "no-cache" ) );
151159

152160
if (this._session.WriteHttpResponseHeaders () == false) {
153-
throw new InvalidOperationException ( "Unable to obtain an output stream for the response." );
161+
throw new InvalidOperationException ( "Unable to obtain the output stream for the response." );
154162
}
155163

156164
return new HttpEventStreamWriter ( this._baseOutputStream, encoding );

cadente/Sisk.Cadente/HttpSerializer/HttpRequestBase.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ sealed class HttpRequestBase {
1616
private string? _path;
1717
private HttpHeader []? _headers;
1818

19+
public bool IsExpecting100;
20+
21+
public long ContentLength;
22+
public bool CanKeepAlive;
23+
24+
public required ReadOnlyMemory<byte> BufferedContent;
25+
1926
public required ReadOnlyMemory<byte> MethodRef;
2027
public required ReadOnlyMemory<byte> PathRef;
2128

@@ -41,9 +48,4 @@ public HttpHeader [] HeadersAR {
4148
return this._headers;
4249
}
4350
}
44-
45-
public long ContentLength;
46-
public bool CanKeepAlive;
47-
48-
public required ReadOnlyMemory<byte> BufferedContent;
4951
}

cadente/Sisk.Cadente/HttpSerializer/HttpRequestReader.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// File name: HttpRequestReader.cs
88
// Repository: https://github.com/sisk-http/core
99

10-
using System.Buffers;
1110
using System.Runtime.CompilerServices;
1211
using System.Runtime.InteropServices;
1312
using System.Text;
@@ -17,21 +16,21 @@ namespace Sisk.Cadente.HttpSerializer;
1716
sealed class HttpRequestReader {
1817

1918
Stream _stream;
20-
IMemoryOwner<byte> bufferOwnership;
19+
byte [] buffer;
2120

2221
const byte SPACE = 0x20;
2322
const byte CARRIAGE_RETURN = 0x0D; //\r
2423
const byte DOUBLE_DOTS = 0x3A; // :
2524

26-
public HttpRequestReader ( Stream stream, ref IMemoryOwner<byte> bufferOwnership ) {
25+
public HttpRequestReader ( Stream stream, ref byte [] bufferOwnership ) {
2726
this._stream = stream;
28-
this.bufferOwnership = bufferOwnership;
27+
this.buffer = bufferOwnership;
2928
}
3029

3130
public async Task<(HttpRequestReadState, HttpRequestBase?)> ReadHttpRequest () {
3231
try {
3332

34-
int read = await this._stream.ReadAsync ( this.bufferOwnership.Memory );
33+
int read = await this._stream.ReadAsync ( this.buffer );
3534

3635
if (read == 0) {
3736
return (HttpRequestReadState.StreamZero, null);
@@ -56,7 +55,7 @@ public HttpRequestReader ( Stream stream, ref IMemoryOwner<byte> bufferOwnership
5655

5756
bool requestStreamFinished = false;
5857

59-
Memory<byte> memory = this.bufferOwnership.Memory;
58+
Memory<byte> memory = this.buffer;
6059
Span<byte> memSpan = memory.Span;
6160

6261
ReadOnlyMemory<byte> method = null!;
@@ -80,6 +79,7 @@ public HttpRequestReader ( Stream stream, ref IMemoryOwner<byte> bufferOwnership
8079

8180
int headerSize = -1;
8281
bool keepAliveEnabled = true;
82+
bool expect100 = false;
8383
long contentLength = 0;
8484

8585
List<HttpHeader> headers = new List<HttpHeader> ( 64 );
@@ -141,7 +141,10 @@ public HttpRequestReader ( Stream stream, ref IMemoryOwner<byte> bufferOwnership
141141
contentLength = long.Parse ( Encoding.ASCII.GetString ( headerLineValue ) );
142142
}
143143
else if (Ascii.EqualsIgnoreCase ( headerLineName, "Connection"u8 )) {
144-
keepAliveEnabled = !headerLineValue.SequenceEqual ( "close"u8 );
144+
keepAliveEnabled = !Ascii.Equals ( headerLineValue, "close"u8 );
145+
}
146+
else if (Ascii.EqualsIgnoreCase ( headerLineName, "Expect"u8 )) {
147+
expect100 = Ascii.Equals ( headerLineValue, "100-continue"u8 );
145148
}
146149

147150
headers.Add ( new HttpHeader ( headerLineName.ToArray (), headerLineValue.ToArray () ) );
@@ -155,14 +158,16 @@ public HttpRequestReader ( Stream stream, ref IMemoryOwner<byte> bufferOwnership
155158
}
156159

157160
return new HttpRequestBase () {
158-
BufferedContent = this.bufferOwnership.Memory [ headerSize.. ],
161+
BufferedContent = expect100 ? Memory<byte>.Empty : memory [ headerSize.. ],
159162

160163
Headers = headers.ToArray (),
161164
MethodRef = method,
162165
PathRef = path,
163166

164167
ContentLength = contentLength,
165-
CanKeepAlive = keepAliveEnabled
168+
CanKeepAlive = keepAliveEnabled,
169+
170+
IsExpecting100 = expect100
166171
};
167172
}
168173
}

cadente/Sisk.Cadente/HttpSerializer/HttpResponseSerializer.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ internal static class HttpResponseSerializer {
1717

1818
static ASCIIEncoding _headerDataEncoding = new ASCIIEncoding ();
1919

20-
[MethodImpl ( MethodImplOptions.AggressiveOptimization )]
20+
const byte _H = (byte) 'H';
21+
const byte _T = (byte) 'T';
22+
const byte _P = (byte) 'P';
23+
const byte _1 = (byte) '1';
24+
const byte _DOT = (byte) '.';
25+
const byte _SPACE = (byte) ' ';
26+
const byte _CR = (byte) '\r';
27+
const byte _LF = (byte) '\n';
28+
const byte _COLON = (byte) ':';
29+
const byte _SLASH = (byte) '/';
30+
31+
[MethodImpl ( MethodImplOptions.AggressiveInlining )]
2132
public static int GetResponseHeadersBytes ( scoped Span<byte> buffer, HttpHostContext.HttpResponse response ) {
2233

23-
const byte _H = (byte) 'H';
24-
const byte _T = (byte) 'T';
25-
const byte _P = (byte) 'P';
26-
const byte _1 = (byte) '1';
27-
const byte _DOT = (byte) '.';
28-
const byte _SPACE = (byte) ' ';
29-
const byte _CR = (byte) '\r';
30-
const byte _LF = (byte) '\n';
31-
const byte _COLON = (byte) ':';
32-
const byte _SLASH = (byte) '/';
33-
3434
// HTTP/1.1
3535
buffer [ 0 ] = _H;
3636
buffer [ 1 ] = _T;
@@ -93,4 +93,15 @@ public static bool WriteHttpResponseHeaders ( Stream outgoingStream, in Span<byt
9393
return false;
9494
}
9595
}
96+
97+
public static bool WriteExpectationContinue ( Stream outgoingStream ) {
98+
try {
99+
outgoingStream.Write ( "HTTP/1.1 100 Continue\r\n\r\n"u8 );
100+
101+
return true;
102+
}
103+
catch (Exception) {
104+
return false;
105+
}
106+
}
96107
}

cadente/Sisk.Cadente/Sisk.Cadente.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545

4646
<!-- version info -->
4747
<PropertyGroup>
48-
<AssemblyVersion>0.1.43</AssemblyVersion>
49-
<FileVersion>0.1.43</FileVersion>
50-
<Version>0.1.43-alpha2</Version>
48+
<AssemblyVersion>0.1.52</AssemblyVersion>
49+
<FileVersion>0.1.52</FileVersion>
50+
<Version>0.1.52-alpha3</Version>
5151
</PropertyGroup>
5252

5353
<!-- licensing, readme, signing -->

0 commit comments

Comments
 (0)