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

Commit 83f55c5

Browse files
author
Cesar Blum Silveira
committed
Add explanatory comment for perfect hashing.
1 parent 2650322 commit 83f55c5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2Extensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ public static ArraySegment<byte> GetArraySegment(this MemoryPoolIterator2 start,
262262
return new ArraySegment<byte>(array, 0, length);
263263
}
264264

265+
/// <summary>
266+
/// Checks that up to 8 bytes between <paramref name="begin"/> and <paramref name="end"/> correspond to a known HTTP string.
267+
/// </summary>
268+
/// <remarks>
269+
/// A "known HTTP string" can be an HTTP method name defined in the HTTP/1.1 RFC or an HTTP version (HTTP/1.0 or HTTP/1.1).
270+
/// Since all of those fit in at most 8 bytes, they can be optimally looked up by reading those bytes as a long. Once
271+
/// in that format, uninteresting bits are cleared and the remaining long modulo 37 is looked up in a table.
272+
/// The number 37 was chosen because that number allows for a perfect hash of the set of
273+
/// "known strings" (CONNECT, DELETE, GET, HEAD, PATCH, POST, PUT, OPTIONS, TRACE, HTTP/1.0 and HTTP/1.1, where strings
274+
/// with less than 8 characters have 0s appended to their ends to fill for the missing bytes).
275+
/// </remarks>
276+
/// <param name="begin">The iterator from which to start the known string lookup.</param>
277+
/// <param name="end">The iterator pointing to the end of the input string.</param>
278+
/// <param name="knownString">A reference to a pre-allocated known string, if the input matches any.</param>
279+
/// <returns><c>true</c> if the input matches a known string, <c>false</c> otherwise.</returns>
265280
public static bool GetKnownString(this MemoryPoolIterator2 begin, MemoryPoolIterator2 end, out string knownString)
266281
{
267282
knownString = null;

0 commit comments

Comments
 (0)