Skip to content

Commit 4d30127

Browse files
committed
fb
1 parent 5f82e92 commit 4d30127

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal HttpParser(bool showErrorDetails, bool disableHttp1LineFeedTerminators)
5454
private const byte ByteQuestionMark = (byte)'?';
5555
private const byte BytePercentage = (byte)'%';
5656
private const int MinTlsRequestSize = 1; // We need at least 1 byte to check for a proper TLS request line
57-
private static ReadOnlySpan<byte> RequestLineDelimiters => new byte[] { ByteLF, 0 };
57+
private static ReadOnlySpan<byte> RequestLineDelimiters => [ByteLF, 0];
5858

5959
/// <summary>
6060
/// This API supports framework infrastructure and is not intended to be used

src/Servers/Kestrel/Core/src/Internal/Infrastructure/HttpUtilities.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,17 @@ public static string GetHeaderName(this ReadOnlySpan<byte> span)
9191
return string.Empty;
9292
}
9393

94-
var str = string.Create(span.Length, span, (destination, spanPtr) =>
94+
var str = string.Create(span.Length, span, static (destination, source) =>
9595
{
96-
if (Ascii.ToUtf16(spanPtr, destination, out _) != OperationStatus.Done)
96+
if (Ascii.ToUtf16(source, destination, out var written) != OperationStatus.Done
97+
|| destination.Contains('\0'))
9798
{
9899
KestrelBadHttpRequestException.Throw(RequestRejectionReason.InvalidCharactersInHeaderName);
99100
}
101+
102+
Debug.Assert(written == destination.Length);
100103
});
101104

102-
if (str.Contains('\0'))
103-
{
104-
KestrelBadHttpRequestException.Throw(RequestRejectionReason.InvalidCharactersInHeaderName);
105-
}
106105
return str;
107106
}
108107

@@ -133,19 +132,25 @@ public static string GetRequestHeaderString(this ReadOnlySpan<byte> span, string
133132

134133
if (invalidCharIndex >= 0)
135134
{
136-
if (result[invalidCharIndex] == 0)
137-
{
138-
throw new InvalidOperationException("Null characters are not allowed in request headers.");
139-
}
140-
else
141-
{
142-
throw new InvalidOperationException("Newline characters (CR/LF) are not allowed in request headers.");
143-
}
135+
ThrowForInvalidCharacter(result[invalidCharIndex]);
144136
}
145137

146138
return result;
147139
}
148140

141+
[MethodImpl(MethodImplOptions.NoInlining)]
142+
private static void ThrowForInvalidCharacter(char invalidCharacter)
143+
{
144+
if (invalidCharacter == 0)
145+
{
146+
throw new InvalidOperationException("Null characters are not allowed in request headers.");
147+
}
148+
else
149+
{
150+
throw new InvalidOperationException("Newline characters (CR/LF) are not allowed in request headers.");
151+
}
152+
}
153+
149154
private static string GetRequestHeaderStringWithoutDefaultEncodingCore(this ReadOnlySpan<byte> span, string name, Func<string, Encoding?> encodingSelector)
150155
{
151156
var encoding = encodingSelector(name);

src/Shared/ServerInfrastructure/StringUtilities.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ public static string GetAsciiOrUTF8String(this ReadOnlySpan<byte> span, Encoding
2323
return string.Empty;
2424
}
2525

26-
var resultString = string.Create(span.Length, span, (destination, spanPtr) =>
26+
var resultString = string.Create(span.Length, span, static (destination, source) =>
2727
{
28-
if (Ascii.ToUtf16(spanPtr, destination, out _) != OperationStatus.Done)
28+
if (Ascii.ToUtf16(source, destination, out var written) != OperationStatus.Done)
2929
{
3030
// Mark resultString for UTF-8 encoding
3131
destination[0] = '\0';
3232
}
33+
34+
Debug.Assert(destination[0] == '\0' || written == destination.Length);
3335
});
3436

3537
// If resultString is marked, perform UTF-8 encoding
@@ -51,12 +53,14 @@ public static string GetAsciiOrUTF8String(this ReadOnlySpan<byte> span, Encoding
5153
// Null checks must be done independently of this method (if required)
5254
public static string GetAsciiString(this ReadOnlySpan<byte> span)
5355
{
54-
return string.Create(span.Length, span, (destination, spanPtr) =>
56+
return string.Create(span.Length, span, static (destination, source) =>
5557
{
56-
if (Ascii.ToUtf16(spanPtr, destination, out _) != OperationStatus.Done)
58+
if (Ascii.ToUtf16(source, destination, out var written) != OperationStatus.Done)
5759
{
5860
throw new InvalidOperationException();
5961
}
62+
63+
Debug.Assert(written == destination.Length);
6064
});
6165
}
6266

0 commit comments

Comments
 (0)