Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark bytes as consumed #13394

Merged
merged 1 commit into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Http/WebUtilities/src/FormPipeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ private void ParseValuesSlow(
{
ParseFormValuesFast(keyValuePair.FirstSpan, ref accumulator, isFinalBlock: true, out var segmentConsumed);
Debug.Assert(segmentConsumed == keyValuePair.FirstSpan.Length);
consumedBytes = sequenceReader.Consumed;
consumed = sequenceReader.Position;
continue;
}

Expand Down
26 changes: 26 additions & 0 deletions src/Http/WebUtilities/test/FormPipeReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public void TryParseFormValues_SingleSegmentWorks(Encoding encoding)
var formReader = new FormPipeReader(null, encoding);

formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(2, accumulator.KeyCount);
var dict = accumulator.GetResults();
Expand All @@ -201,6 +202,7 @@ public void TryParseFormValues_Works(Encoding encoding)

var formReader = new FormPipeReader(null, encoding);
formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(3, accumulator.KeyCount);
var dict = accumulator.GetResults();
Expand All @@ -219,6 +221,7 @@ public void TryParseFormValues_SplitAcrossSegmentsWorks(Encoding encoding)

var formReader = new FormPipeReader(null, encoding);
formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(3, accumulator.KeyCount);
var dict = accumulator.GetResults();
Expand All @@ -237,6 +240,7 @@ public void TryParseFormValues_MultiSegmentWithArrayPoolAcrossSegmentsWorks(Enco

var formReader = new FormPipeReader(null, encoding);
formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(2, accumulator.KeyCount);
var dict = accumulator.GetResults();
Expand All @@ -254,6 +258,7 @@ public void TryParseFormValues_MultiSegmentSplitAcrossSegmentsWithPlusesWorks(En

var formReader = new FormPipeReader(null, encoding);
formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(3, accumulator.KeyCount);
var dict = accumulator.GetResults();
Expand All @@ -272,6 +277,7 @@ public void TryParseFormValues_DecodedPlusesWorks(Encoding encoding)

var formReader = new FormPipeReader(null, encoding);
formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(3, accumulator.KeyCount);
var dict = accumulator.GetResults();
Expand All @@ -290,13 +296,31 @@ public void TryParseFormValues_SplitAcrossSegmentsThatNeedDecodingWorks(Encoding

var formReader = new FormPipeReader(null, encoding);
formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(2, accumulator.KeyCount);
var dict = accumulator.GetResults();
Assert.Equal("\"%-.<>\\^_`{|}~", dict["\"%-.<>\\^_`{|}~"]);
Assert.Equal("wow", dict["\"%-.<>\\^_`{|}"]);
}

[Fact]
public void TryParseFormValues_MultiSegmentFastPathWorks()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this would have failed before the fix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this covers the repro scenario.

{
var readOnlySequence = ReadOnlySequenceFactory.CreateSegments(Encoding.UTF8.GetBytes("foo=bar&"), Encoding.UTF8.GetBytes("baz=boo"));

KeyValueAccumulator accumulator = default;

var formReader = new FormPipeReader(null);
formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

Assert.Equal(2, accumulator.KeyCount);
var dict = accumulator.GetResults();
Assert.Equal("bar", dict["foo"]);
Assert.Equal("boo", dict["baz"]);
}

[Fact]
public void TryParseFormValues_ExceedKeyLengthThrows()
{
Expand Down Expand Up @@ -411,6 +435,7 @@ public void ParseFormWithIncompleteKeyWhenIsFinalBlockSucceeds(ReadOnlySequence<
};

formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

IDictionary<string, StringValues> values = accumulator.GetResults();
Assert.Contains("fo", values);
Expand All @@ -431,6 +456,7 @@ public void ParseFormWithIncompleteValueWhenIsFinalBlockSucceeds(ReadOnlySequenc
};

formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
Assert.True(readOnlySequence.IsEmpty);

IDictionary<string, StringValues> values = accumulator.GetResults();
Assert.Contains("fo", values);
Expand Down