Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 594a3f9

Browse files
committed
Review feedback 3
1 parent 825b1e4 commit 594a3f9

File tree

3 files changed

+85
-156
lines changed

3 files changed

+85
-156
lines changed

src/Microsoft.Extensions.WebEncoders.Core/UrlPathDecoder.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,22 @@ private static int Unescape(char[] source, int start, byte[] bytesBuffer, ref ch
151151
return 0;
152152
}
153153

154-
byte first;
155-
if (!TryGetUnescapedByte(source[start + 1], source[start + 2], out first))
154+
byte firstByte;
155+
if (!TryGetUnescapedByte(source, start, out firstByte))
156156
{
157157
return 0;
158158
}
159159

160-
if (first <= 0x7F)
160+
if (firstByte <= 0x7F)
161161
{
162162
// first < U+007F, single byte ASCII
163163
if (output.Length < 1)
164164
{
165-
output = new char[] { (char)first };
165+
output = new char[] { (char)firstByte };
166166
}
167167
else
168168
{
169-
output[0] = (char)first;
169+
output[0] = (char)firstByte;
170170
}
171171

172172
count = 1;
@@ -177,25 +177,25 @@ private static int Unescape(char[] source, int start, byte[] bytesBuffer, ref ch
177177
int currentDecodeBits = 0;
178178
int bytesCount = 1;
179179
int expectValueMin = 0;
180-
bytesBuffer[0] = (byte)first;
181-
if ((first & 0xE0) == 0xC0)
180+
bytesBuffer[0] = (byte)firstByte;
181+
if ((firstByte & 0xE0) == 0xC0)
182182
{
183183
// 110x xxxx, expect 1 more byte
184-
currentDecodeBits = first & 0x1F;
184+
currentDecodeBits = firstByte & 0x1F;
185185
bytesCount = 2;
186186
expectValueMin = 0x80;
187187
}
188-
else if ((first & 0xF0) == 0xE0)
188+
else if ((firstByte & 0xF0) == 0xE0)
189189
{
190190
// 1110 xxxx, expect 2 more bytes
191-
currentDecodeBits = first & 0x0F;
191+
currentDecodeBits = firstByte & 0x0F;
192192
bytesCount = 3;
193193
expectValueMin = 0x800;
194194
}
195-
else if ((first & 0xF8) == 0xF0)
195+
else if ((firstByte & 0xF8) == 0xF0)
196196
{
197197
// 1111 0xxx, expect 3 more bytes
198-
currentDecodeBits = first & 0x07;
198+
currentDecodeBits = firstByte & 0x07;
199199
bytesCount = 4;
200200
expectValueMin = 0x10000;
201201
}
@@ -221,7 +221,7 @@ private static int Unescape(char[] source, int start, byte[] bytesBuffer, ref ch
221221
}
222222

223223
byte v;
224-
if (!TryGetUnescapedByte(source[start + 1], source[start + 2], out v))
224+
if (!TryGetUnescapedByte(source, start, out v))
225225
{
226226
return 0;
227227
}
@@ -309,17 +309,18 @@ private static int GetNextEncoded(int start, char[] array)
309309
return array.Length;
310310
}
311311

312-
private static bool TryGetUnescapedByte(char first, char second, out byte result)
312+
private static bool TryGetUnescapedByte(char[] buffer, int position, out byte result)
313313
{
314-
if (!IsHex(first) || !IsHex(second))
314+
if (!IsHex(buffer[position + 1]) || !IsHex(buffer[position + 2]))
315315
{
316316
result = default(byte);
317317
return false;
318318
}
319-
320-
// result in range [0000, 00FF]
321-
result = (byte)((HexToDec(first) << 4) + (HexToDec(second)));
322-
return true;
319+
else
320+
{
321+
result = (byte)((HexToDec(buffer[position + 1]) << 4) + HexToDec(buffer[position + 2]));
322+
return true;
323+
}
323324
}
324325

325326
private static bool IsHex(char value)

test/Microsoft.Extensions.WebEncoders.Tests/UrlPathDecoderTests.cs

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66

77
namespace Microsoft.Extensions.WebEncoders.Tests
88
{
9-
public class UrlPathDecoderTests
9+
public abstract class UrlPathDecoderTestBase
1010
{
11+
protected abstract void PositiveAssert(string raw, string expect);
12+
13+
protected abstract void PositiveAssert(string raw);
14+
15+
protected abstract void NegativeAssert(string raw);
16+
1117
[Fact]
1218
public void Empty()
1319
{
14-
Assert.Equal(string.Empty, UrlPathDecoder.Decode(string.Empty));
20+
PositiveAssert(string.Empty, string.Empty);
1521
}
1622

1723
[Fact]
1824
public void WhiteSpace()
1925
{
20-
Assert.Equal(" ", UrlPathDecoder.Decode(" "));
26+
PositiveAssert(" ", " ");
2127
}
2228

2329
[Fact]
@@ -33,7 +39,7 @@ public void ThrowNullArgument()
3339
[InlineData("/", "/")]
3440
public void NormalCases(string raw, string expect)
3541
{
36-
Assert.Equal(expect, UrlPathDecoder.Decode(raw));
42+
PositiveAssert(raw, expect);
3743
}
3844

3945
[Theory]
@@ -42,15 +48,15 @@ public void NormalCases(string raw, string expect)
4248
[InlineData("/foo%2F%20bar", "/foo%2F bar")]
4349
public void SkipForwardSlash(string raw, string expect)
4450
{
45-
Assert.Equal(expect, UrlPathDecoder.Decode(raw));
51+
PositiveAssert(raw, expect);
4652
}
4753

4854
[Theory]
4955
[InlineData("%C3%84ra%20Benetton", "Ära Benetton")]
5056
[InlineData("%E6%88%91%E8%87%AA%E6%A8%AA%E5%88%80%E5%90%91%E5%A4%A9%E7%AC%91%E5%8E%BB%E7%95%99%E8%82%9D%E8%83%86%E4%B8%A4%E6%98%86%E4%BB%91", "我自横刀向天笑去留肝胆两昆仑")]
5157
public void Internationalized(string raw, string expect)
5258
{
53-
Assert.Equal(expect, UrlPathDecoder.Decode(raw));
59+
PositiveAssert(raw, expect);
5460
}
5561

5662
[Theory]
@@ -65,7 +71,7 @@ public void Internationalized(string raw, string expect)
6571
// Overlong borderline cases
6672
public void ValidUTF8(string raw, string expect)
6773
{
68-
Assert.Equal(expect, UrlPathDecoder.Decode(raw));
74+
PositiveAssert(raw, expect);
6975
}
7076

7177
[Theory]
@@ -76,7 +82,7 @@ public void ValidUTF8(string raw, string expect)
7682
[InlineData("%F0%90%80%80")]
7783
public void ValidUTF8(string raw)
7884
{
79-
Assert.NotEqual(raw, UrlPathDecoder.Decode(raw));
85+
PositiveAssert(raw);
8086
}
8187

8288
[Theory]
@@ -92,10 +98,61 @@ public void ValidUTF8(string raw)
9298
[InlineData("%%")]
9399
[InlineData("%A")]
94100
[InlineData("%Y")]
101+
// [InlineData("http://xn--9zt52a.example.org/%e2%80%ae")]
95102
public void InvalidUTF8(string raw)
103+
{
104+
NegativeAssert(raw);
105+
}
106+
}
107+
108+
public class UrlPathDecoderTests : UrlPathDecoderTestBase
109+
{
110+
protected override void NegativeAssert(string raw)
96111
{
97112
// invalid sequence are left untouched
98113
Assert.Equal(raw, UrlPathDecoder.Decode(raw));
99114
}
115+
116+
protected override void PositiveAssert(string raw)
117+
{
118+
Assert.NotEqual(raw, UrlPathDecoder.Decode(raw));
119+
}
120+
121+
protected override void PositiveAssert(string raw, string expect)
122+
{
123+
Assert.Equal(expect, UrlPathDecoder.Decode(raw));
124+
}
125+
}
126+
127+
public class UrlPathInPlaceDecoderTests: UrlPathDecoderTestBase
128+
{
129+
protected override void PositiveAssert(string raw, string expect)
130+
{
131+
var buf = raw.ToCharArray();
132+
133+
var len = UrlPathDecoder.DecodeInPlace(buf);
134+
135+
Assert.Equal(expect.Length, len);
136+
Assert.Equal(expect.ToCharArray(), new ArraySegment<char>(buf, 0, len));
137+
}
138+
139+
protected override void PositiveAssert(string raw)
140+
{
141+
var buf = raw.ToCharArray();
142+
143+
var len = UrlPathDecoder.DecodeInPlace(buf);
144+
145+
Assert.NotEqual(raw.Length, len);
146+
}
147+
148+
protected override void NegativeAssert(string raw)
149+
{
150+
var buf = raw.ToCharArray();
151+
152+
var len = UrlPathDecoder.DecodeInPlace(buf);
153+
154+
Assert.Equal(raw.Length, len);
155+
Assert.Equal(raw.ToCharArray(), buf);
156+
}
100157
}
101158
}

test/Microsoft.Extensions.WebEncoders.Tests/UrlPathInPlaceDecoderTests.cs

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)