|
1 | 1 | using System.Text; |
2 | | -using Decoder = TurboHTTP.Protocol.Http11.Decoder; |
| 2 | +using TurboHTTP.Protocol.Syntax; |
| 3 | +using TurboHTTP.Protocol.Syntax.Http11.Client; |
| 4 | +using TurboHTTP.Protocol.Syntax.Http11.Options; |
3 | 5 |
|
4 | 6 | namespace TurboHTTP.Tests.Security; |
5 | 7 |
|
6 | 8 | public sealed class Http11FuzzBodySpec |
7 | 9 | { |
8 | 10 | private const int IterationsPerSeed = 100; |
9 | 11 | private const long MaxBytesPerIteration = 1_048_576; |
| 12 | + private static readonly Http11ClientDecoderOptions DecoderOptions = Http11ClientDecoderOptions.Default; |
10 | 13 |
|
11 | | - private static void AssertDecodeNeverCrashes(Decoder decoder, ReadOnlyMemory<byte> data) |
| 14 | + private static void AssertDecodeNeverCrashes(Http11ClientDecoder decoder, ReadOnlyMemory<byte> data) |
12 | 15 | { |
13 | 16 | try |
14 | 17 | { |
15 | | - decoder.TryDecodeHeaders(data, out var response, out _, out _); |
16 | | - response?.Dispose(); |
| 18 | + var outcome = decoder.Feed(data.Span, requestMethodWasHead: false, out _); |
| 19 | + if (outcome == DecodeOutcome.Complete) |
| 20 | + { |
| 21 | + var response = decoder.GetResponse(); |
| 22 | + response.Dispose(); |
| 23 | + decoder.Reset(); |
| 24 | + } |
17 | 25 | } |
18 | 26 | catch (HttpProtocolException) |
19 | 27 | { |
20 | | - // Expected — malformed input correctly classified by our decoder. |
21 | 28 | } |
22 | 29 | catch (FormatException) |
23 | 30 | { |
24 | | - // Expected — .NET's HttpResponseMessage rejects invalid reason phrases |
25 | | - // (newlines, NUL) that random bytes produce. Not a decoder bug. |
26 | 31 | } |
27 | 32 | } |
28 | 33 |
|
29 | | - private static void AssertDecodeEofNeverCrashes(Decoder decoder) |
| 34 | + private static void AssertDecodeEofNeverCrashes(Http11ClientDecoder decoder) |
30 | 35 | { |
31 | 36 | try |
32 | 37 | { |
33 | | - decoder.TryDecodeEof(out var response); |
34 | | - response?.Dispose(); |
| 38 | + if (decoder.SignalEof() || decoder.IsBodyComplete) |
| 39 | + { |
| 40 | + var response = decoder.GetResponse(); |
| 41 | + response?.Dispose(); |
| 42 | + decoder.Reset(); |
| 43 | + } |
35 | 44 | } |
36 | 45 | catch (HttpProtocolException) |
37 | 46 | { |
38 | | - // Expected — malformed input correctly classified by our decoder. |
39 | 47 | } |
40 | 48 | catch (FormatException) |
41 | 49 | { |
42 | | - // Expected — .NET's HttpResponseMessage rejects invalid reason phrases. |
43 | 50 | } |
44 | 51 | } |
45 | 52 |
|
@@ -102,7 +109,7 @@ public void Http11Decoder_should_handle_mixed_transfer_encoding_and_content_leng |
102 | 109 | using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
103 | 110 | var allocBefore = GC.GetAllocatedBytesForCurrentThread(); |
104 | 111 |
|
105 | | - using var decoder = new Decoder(); |
| 112 | + var decoder = new Http11ClientDecoder(DecoderOptions); |
106 | 113 |
|
107 | 114 | var sb = new StringBuilder(); |
108 | 115 | sb.Append("HTTP/1.1 200 OK\r\n"); |
@@ -151,7 +158,7 @@ public void Http11Decoder_should_handle_extremely_large_content_length_without_o |
151 | 158 | using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
152 | 159 | var allocBefore = GC.GetAllocatedBytesForCurrentThread(); |
153 | 160 |
|
154 | | - using var decoder = new Decoder(); |
| 161 | + var decoder = new Http11ClientDecoder(DecoderOptions); |
155 | 162 |
|
156 | 163 | var claimedLengths = new[] |
157 | 164 | { |
@@ -207,7 +214,7 @@ public void Http11Decoder_should_handle_connection_close_with_trailing_data(int |
207 | 214 | using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
208 | 215 | var allocBefore = GC.GetAllocatedBytesForCurrentThread(); |
209 | 216 |
|
210 | | - using var decoder = new Decoder(); |
| 217 | + var decoder = new Http11ClientDecoder(DecoderOptions); |
211 | 218 |
|
212 | 219 | var body = "Hello, World!"; |
213 | 220 | var validResponse = BuildValidResponse(200, "OK", body, |
@@ -250,7 +257,7 @@ public void Http11Decoder_should_maintain_consistent_state_with_fragmented_deliv |
250 | 257 | using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
251 | 258 | var allocBefore = GC.GetAllocatedBytesForCurrentThread(); |
252 | 259 |
|
253 | | - using var decoder = new Decoder(); |
| 260 | + var decoder = new Http11ClientDecoder(DecoderOptions); |
254 | 261 |
|
255 | 262 | byte[] fullResponse; |
256 | 263 | if (rng.Next(2) == 0) |
|
0 commit comments