Skip to content

Commit b0b4dd9

Browse files
committed
Use one SequenceReader+Rewind rather than 2
1 parent fdba8a9 commit b0b4dd9

File tree

8 files changed

+207
-183
lines changed

8 files changed

+207
-183
lines changed

src/Servers/Kestrel/Core/ref/Microsoft.AspNetCore.Server.Kestrel.Core.netcoreapp3.0.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,8 @@ public partial class HttpParser<TRequestHandler> : Microsoft.AspNetCore.Server.K
228228
{
229229
public HttpParser() { }
230230
public HttpParser(bool showErrorDetails) { }
231-
bool Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser<TRequestHandler>.ParseHeaders(TRequestHandler handler, in System.Buffers.ReadOnlySequence<byte> buffer, out System.SequencePosition consumed, out System.SequencePosition examined, out int consumedBytes) { throw null; }
232231
bool Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser<TRequestHandler>.ParseRequestLine(TRequestHandler handler, in System.Buffers.ReadOnlySequence<byte> buffer, out System.SequencePosition consumed, out System.SequencePosition examined) { throw null; }
233-
public bool ParseHeaders(TRequestHandler handler, in System.Buffers.ReadOnlySequence<byte> buffer, out System.SequencePosition consumed, out System.SequencePosition examined, out int consumedBytes) { throw null; }
232+
public bool ParseHeaders(TRequestHandler handler, ref System.Buffers.SequenceReader<byte> reader) { throw null; }
234233
public bool ParseRequestLine(TRequestHandler handler, in System.Buffers.ReadOnlySequence<byte> buffer, out System.SequencePosition consumed, out System.SequencePosition examined) { throw null; }
235234
}
236235
public enum HttpScheme
@@ -253,7 +252,7 @@ public partial interface IHttpHeadersHandler
253252
}
254253
public partial interface IHttpParser<TRequestHandler> where TRequestHandler : Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpHeadersHandler, Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpRequestLineHandler
255254
{
256-
bool ParseHeaders(TRequestHandler handler, in System.Buffers.ReadOnlySequence<byte> buffer, out System.SequencePosition consumed, out System.SequencePosition examined, out int consumedBytes);
255+
bool ParseHeaders(TRequestHandler handler, ref System.Buffers.SequenceReader<byte> reader);
257256
bool ParseRequestLine(TRequestHandler handler, in System.Buffers.ReadOnlySequence<byte> buffer, out System.SequencePosition consumed, out System.SequencePosition examined);
258257
}
259258
public partial interface IHttpRequestLineHandler

src/Servers/Kestrel/Core/src/Internal/Http/Http1Connection.cs

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,32 +189,77 @@ public bool TakeStartLine(ReadOnlySequence<byte> buffer, out SequencePosition co
189189
return result;
190190
}
191191

192-
public bool TakeMessageHeaders(ReadOnlySequence<byte> buffer, bool trailers, out SequencePosition consumed, out SequencePosition examined)
192+
public bool TakeMessageHeaders(in ReadOnlySequence<byte> buffer, bool trailers, out SequencePosition consumed, out SequencePosition examined)
193193
{
194194
// Make sure the buffer is limited
195-
bool overLength = false;
196-
if (buffer.Length >= _remainingRequestHeadersBytesAllowed)
195+
if (buffer.Length > _remainingRequestHeadersBytesAllowed)
197196
{
198-
buffer = buffer.Slice(buffer.Start, _remainingRequestHeadersBytesAllowed);
199-
200-
// If we sliced it means the current buffer bigger than what we're
201-
// allowed to look at
202-
overLength = true;
197+
return TrimAndTakeMessageHeaders(buffer, trailers, out consumed, out examined);
203198
}
204199

205-
var result = _parser.ParseHeaders(new Http1ParsingHandler(this, trailers), buffer, out consumed, out examined, out var consumedBytes);
206-
_remainingRequestHeadersBytesAllowed -= consumedBytes;
207-
208-
if (!result && overLength)
200+
var reader = new SequenceReader<byte>(buffer);
201+
var result = false;
202+
try
209203
{
210-
BadHttpRequestException.Throw(RequestRejectionReason.HeadersExceedMaxTotalSize);
204+
result = _parser.ParseHeaders(new Http1ParsingHandler(this, trailers), ref reader);
205+
206+
if (result)
207+
{
208+
TimeoutControl.CancelTimeout();
209+
}
210+
211+
return result;
211212
}
212-
if (result)
213+
finally
213214
{
214-
TimeoutControl.CancelTimeout();
215+
consumed = reader.Position;
216+
_remainingRequestHeadersBytesAllowed -= (int)reader.Consumed;
217+
218+
if (result)
219+
{
220+
examined = consumed;
221+
}
222+
else
223+
{
224+
examined = buffer.End;
225+
}
215226
}
216227

217-
return result;
228+
bool TrimAndTakeMessageHeaders(in ReadOnlySequence<byte> buffer, bool trailers, out SequencePosition consumed, out SequencePosition examined)
229+
{
230+
var trimmedBuffer = buffer.Slice(buffer.Start, _remainingRequestHeadersBytesAllowed);
231+
232+
var reader = new SequenceReader<byte>(trimmedBuffer);
233+
var result = false;
234+
try
235+
{
236+
result = _parser.ParseHeaders(new Http1ParsingHandler(this, trailers), ref reader);
237+
238+
if (!result)
239+
{
240+
// We read the maximum allowed but didn't complete the headers.
241+
BadHttpRequestException.Throw(RequestRejectionReason.HeadersExceedMaxTotalSize);
242+
}
243+
244+
TimeoutControl.CancelTimeout();
245+
246+
return result;
247+
}
248+
finally
249+
{
250+
consumed = reader.Position;
251+
_remainingRequestHeadersBytesAllowed -= (int)reader.Consumed;
252+
253+
if (result)
254+
{
255+
examined = consumed;
256+
}
257+
else
258+
{
259+
examined = trimmedBuffer.End;
260+
}
261+
}
262+
}
218263
}
219264

220265
public void OnStartLine(HttpMethod method, HttpVersion version, Span<byte> target, Span<byte> path, Span<byte> query, Span<byte> customMethod, bool pathEncoded)

0 commit comments

Comments
 (0)