@@ -91,13 +91,15 @@ public static string GetHeaderName(this ReadOnlySpan<byte> span)
91
91
92
92
var str = string . Create ( span . Length , span , static ( destination , source ) =>
93
93
{
94
- if ( Ascii . ToUtf16 ( source , destination , out var written ) != OperationStatus . Done
95
- || destination . Contains ( ' \0 ' ) )
94
+ if ( source . Contains ( ( byte ) 0 )
95
+ || Ascii . ToUtf16 ( source , destination , out var written ) != OperationStatus . Done )
96
96
{
97
97
KestrelBadHttpRequestException . Throw ( RequestRejectionReason . InvalidCharactersInHeaderName ) ;
98
98
}
99
-
100
- Debug . Assert ( written == destination . Length ) ;
99
+ else
100
+ {
101
+ Debug . Assert ( written == destination . Length ) ;
102
+ }
101
103
} ) ;
102
104
103
105
return str ;
@@ -125,28 +127,28 @@ public static string GetRequestHeaderString(this ReadOnlySpan<byte> span, string
125
127
126
128
// New Line characters (CR, LF) are considered invalid at this point.
127
129
// Null characters are also not allowed.
128
- var invalidCharIndex = checkForNewlineChars ?
129
- ( ( ReadOnlySpan < char > ) result ) . IndexOfAny ( '\r ' , '\n ' , '\0 ' )
130
- : ( ( ReadOnlySpan < char > ) result ) . IndexOf ( '\0 ' ) ;
130
+ var hasInvalidChar = checkForNewlineChars ?
131
+ ( ( ReadOnlySpan < char > ) result ) . ContainsAny ( '\r ' , '\n ' , '\0 ' )
132
+ : ( ( ReadOnlySpan < char > ) result ) . Contains ( '\0 ' ) ;
131
133
132
- if ( invalidCharIndex >= 0 )
134
+ if ( hasInvalidChar )
133
135
{
134
- ThrowForInvalidCharacter ( result [ invalidCharIndex ] ) ;
136
+ ThrowForInvalidCharacter ( checkForNewlineChars ) ;
135
137
}
136
138
137
139
return result ;
138
140
}
139
141
140
142
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
141
- private static void ThrowForInvalidCharacter ( char invalidCharacter )
143
+ private static void ThrowForInvalidCharacter ( bool checkForNewlines )
142
144
{
143
- if ( invalidCharacter == 0 )
145
+ if ( checkForNewlines )
144
146
{
145
- throw new InvalidOperationException ( "Null characters are not allowed in request headers." ) ;
147
+ throw new InvalidOperationException ( "Newline characters (CR/LF) or Null are not allowed in request headers." ) ;
146
148
}
147
149
else
148
150
{
149
- throw new InvalidOperationException ( "Newline characters (CR/LF) are not allowed in request headers." ) ;
151
+ throw new InvalidOperationException ( "Null characters are not allowed in request headers." ) ;
150
152
}
151
153
}
152
154
0 commit comments