|
1 | 1 | using System.Buffers; |
| 2 | +using System.IO.Pipelines; |
2 | 3 | using System.Net; |
3 | 4 | using System.Net.Sockets; |
4 | 5 | using System.Text; |
@@ -35,85 +36,92 @@ static async Task HandleClientAsync(TcpClient client, CancellationToken ct) |
35 | 36 | using (client) |
36 | 37 | await using (var stream = client.GetStream()) |
37 | 38 | { |
38 | | - var buffer = new byte[65536]; |
39 | | - var filled = 0; |
40 | 39 | var limits = ParserLimits.Default; |
| 40 | + var reader = PipeReader.Create(stream); |
41 | 41 | using var request = new BinaryRequest(); |
42 | 42 |
|
43 | 43 | try |
44 | 44 | { |
45 | 45 | while (!ct.IsCancellationRequested) |
46 | 46 | { |
47 | | - if (filled >= buffer.Length) |
48 | | - { |
49 | | - // Buffer full but headers still incomplete — request is too large |
50 | | - await stream.WriteAsync(MakeErrorResponse(431, "Request Header Fields Too Large"), ct); |
51 | | - return; |
52 | | - } |
| 47 | + var result = await reader.ReadAsync(ct); |
| 48 | + var buffer = result.Buffer; |
53 | 49 |
|
54 | | - var read = await stream.ReadAsync(buffer.AsMemory(filled), ct); |
55 | | - if (read == 0) break; |
56 | | - filled += read; |
| 50 | + if (result.IsCompleted && buffer.IsEmpty) |
| 51 | + break; |
57 | 52 |
|
58 | | - while (filled > 0) |
59 | | - { |
60 | | - var sequence = new ReadOnlySequence<byte>(buffer, 0, filled); |
| 53 | + var sequence = buffer; |
61 | 54 |
|
62 | | - try |
| 55 | + try |
| 56 | + { |
| 57 | + if (!HardenedParser.TryExtractFullHeader(ref sequence, request, in limits, out var bytesRead)) |
63 | 58 | { |
64 | | - if (!HardenedParser.TryExtractFullHeader(ref sequence, request, in limits, out var bytesRead)) |
65 | | - break; // Need more data |
66 | | - |
67 | | - // Post-parse semantic validation |
68 | | - if (RequestSemantics.HasTransferEncodingWithContentLength(request) || |
69 | | - RequestSemantics.HasConflictingContentLength(request) || |
70 | | - RequestSemantics.HasConflictingCommaSeparatedContentLength(request) || |
71 | | - RequestSemantics.HasInvalidContentLengthFormat(request) || |
72 | | - RequestSemantics.HasContentLengthWithLeadingZeros(request) || |
73 | | - RequestSemantics.HasInvalidHostHeaderCount(request) || |
74 | | - RequestSemantics.HasInvalidTransferEncoding(request) || |
75 | | - RequestSemantics.HasDotSegments(request) || |
76 | | - RequestSemantics.HasFragmentInRequestTarget(request) || |
77 | | - RequestSemantics.HasBackslashInPath(request) || |
78 | | - RequestSemantics.HasDoubleEncoding(request) || |
79 | | - RequestSemantics.HasEncodedNullByte(request) || |
80 | | - RequestSemantics.HasOverlongUtf8(request)) |
| 59 | + if (buffer.Length > limits.MaxTotalHeaderBytes) |
81 | 60 | { |
82 | | - await stream.WriteAsync(MakeErrorResponse(400, "Bad Request"), ct); |
83 | | - return; |
| 61 | + reader.AdvanceTo(buffer.End); |
| 62 | + await stream.WriteAsync(MakeErrorResponse(431, "Request Header Fields Too Large"), ct); |
| 63 | + break; |
84 | 64 | } |
85 | 65 |
|
86 | | - var method = Encoding.ASCII.GetString(request.Method.Span); |
87 | | - var path = Encoding.ASCII.GetString(request.Path.Span); |
88 | | - var responseBytes = BuildResponse(method, path); |
89 | | - await stream.WriteAsync(responseBytes, ct); |
| 66 | + // Tell the pipe: consumed nothing, examined everything |
| 67 | + reader.AdvanceTo(buffer.Start, buffer.End); |
90 | 68 |
|
91 | | - // Consume parsed bytes and reset for keep-alive |
92 | | - if (bytesRead > 0 && bytesRead <= filled) |
93 | | - { |
94 | | - Buffer.BlockCopy(buffer, bytesRead, buffer, 0, filled - bytesRead); |
95 | | - filled -= bytesRead; |
96 | | - } |
97 | | - else |
98 | | - { |
99 | | - filled = 0; |
100 | | - } |
| 69 | + if (result.IsCompleted) |
| 70 | + break; |
101 | 71 |
|
102 | | - request.Clear(); |
| 72 | + continue; |
103 | 73 | } |
104 | | - catch (HttpParseException ex) |
| 74 | + |
| 75 | + // Post-parse semantic validation (must happen before AdvanceTo — request |
| 76 | + // holds ReadOnlyMemory slices into the pipe's buffer) |
| 77 | + if (RequestSemantics.HasTransferEncodingWithContentLength(request) || |
| 78 | + RequestSemantics.HasConflictingContentLength(request) || |
| 79 | + RequestSemantics.HasConflictingCommaSeparatedContentLength(request) || |
| 80 | + RequestSemantics.HasInvalidContentLengthFormat(request) || |
| 81 | + RequestSemantics.HasContentLengthWithLeadingZeros(request) || |
| 82 | + RequestSemantics.HasInvalidHostHeaderCount(request) || |
| 83 | + RequestSemantics.HasInvalidTransferEncoding(request) || |
| 84 | + RequestSemantics.HasDotSegments(request) || |
| 85 | + RequestSemantics.HasFragmentInRequestTarget(request) || |
| 86 | + RequestSemantics.HasBackslashInPath(request) || |
| 87 | + RequestSemantics.HasDoubleEncoding(request) || |
| 88 | + RequestSemantics.HasEncodedNullByte(request) || |
| 89 | + RequestSemantics.HasOverlongUtf8(request)) |
105 | 90 | { |
106 | | - var (code, reason) = ex.IsLimitViolation |
107 | | - ? (431, "Request Header Fields Too Large") |
108 | | - : (400, "Bad Request"); |
109 | | - await stream.WriteAsync(MakeErrorResponse(code, reason), ct); |
110 | | - return; |
| 91 | + reader.AdvanceTo(buffer.End); |
| 92 | + await stream.WriteAsync(MakeErrorResponse(400, "Bad Request"), ct); |
| 93 | + break; |
111 | 94 | } |
| 95 | + |
| 96 | + // Extract strings while buffer is still valid |
| 97 | + var method = Encoding.ASCII.GetString(request.Method.Span); |
| 98 | + var path = Encoding.ASCII.GetString(request.Path.Span); |
| 99 | + |
| 100 | + // Advance past consumed bytes, then respond |
| 101 | + reader.AdvanceTo(buffer.GetPosition(bytesRead)); |
| 102 | + |
| 103 | + var responseBytes = BuildResponse(method, path); |
| 104 | + await stream.WriteAsync(responseBytes, ct); |
| 105 | + |
| 106 | + request.Clear(); |
| 107 | + } |
| 108 | + catch (HttpParseException ex) |
| 109 | + { |
| 110 | + var (code, reason) = ex.IsLimitViolation |
| 111 | + ? (431, "Request Header Fields Too Large") |
| 112 | + : (400, "Bad Request"); |
| 113 | + reader.AdvanceTo(buffer.End); |
| 114 | + await stream.WriteAsync(MakeErrorResponse(code, reason), ct); |
| 115 | + break; |
112 | 116 | } |
113 | 117 | } |
114 | 118 | } |
115 | 119 | catch (OperationCanceledException) { } |
116 | 120 | catch (IOException) { } |
| 121 | + finally |
| 122 | + { |
| 123 | + await reader.CompleteAsync(); |
| 124 | + } |
117 | 125 | } |
118 | 126 | } |
119 | 127 |
|
|
0 commit comments