6
6
using System . Text ;
7
7
using BenchmarkDotNet . Attributes ;
8
8
using Microsoft . AspNetCore . HttpSys . Internal ;
9
- using Windows . Win32 . Foundation ;
10
- using Windows . Win32 . Networking . HttpServer ;
11
9
using RequestHeaders = Microsoft . AspNetCore . HttpSys . Internal . RequestHeaders ;
12
10
13
11
[ SimpleJob , MemoryDiagnoser ]
@@ -56,7 +54,7 @@ private unsafe RequestHeaders CreateRequestHeader(int unknowHeaderCount)
56
54
var nativeContext = new NativeRequestContext ( MemoryPool < byte > . Shared , null , 0 , false ) ;
57
55
var nativeMemory = new Span < byte > ( nativeContext . NativeRequest , ( int ) nativeContext . Size + 8 ) ;
58
56
59
- var requestStructure = new HTTP_REQUEST_V1 ( ) ;
57
+ var requestStructure = new HttpApiTypes . HTTP_REQUEST ( ) ;
60
58
var remainingMemory = SetUnknownHeaders ( nativeMemory , ref requestStructure , GenerateUnknownHeaders ( unknowHeaderCount ) ) ;
61
59
SetHostHeader ( remainingMemory , ref requestStructure ) ;
62
60
MemoryMarshal . Write ( nativeMemory , in requestStructure ) ;
@@ -66,64 +64,64 @@ private unsafe RequestHeaders CreateRequestHeader(int unknowHeaderCount)
66
64
return requestHeaders ;
67
65
}
68
66
69
- private unsafe Span < byte > SetHostHeader ( Span < byte > nativeMemory , ref HTTP_REQUEST_V1 requestStructure )
67
+ private unsafe Span < byte > SetHostHeader ( Span < byte > nativeMemory , ref HttpApiTypes . HTTP_REQUEST requestStructure )
70
68
{
71
69
// Writing localhost to Host header
72
- var dataDestination = nativeMemory [ Marshal . SizeOf < HTTP_REQUEST_V1 > ( ) .. ] ;
73
- var length = Encoding . ASCII . GetBytes ( "localhost:5001" , dataDestination ) ;
70
+ var dataDestination = nativeMemory . Slice ( Marshal . SizeOf < HttpApiTypes . HTTP_REQUEST > ( ) ) ;
71
+ int length = Encoding . ASCII . GetBytes ( "localhost:5001" , dataDestination ) ;
74
72
fixed ( byte * address = & MemoryMarshal . GetReference ( dataDestination ) )
75
73
{
76
- requestStructure . Headers . KnownHeaders . _28 . pRawValue = ( PCSTR ) address ;
77
- requestStructure . Headers . KnownHeaders . _28 . RawValueLength = ( ushort ) length ;
74
+ requestStructure . Headers . KnownHeaders_29 . pRawValue = address ;
75
+ requestStructure . Headers . KnownHeaders_29 . RawValueLength = ( ushort ) length ;
78
76
}
79
77
return dataDestination ;
80
78
}
81
79
82
80
/// <summary>
83
81
/// Writes an array HTTP_UNKNOWN_HEADER and an array of header key-value pairs to nativeMemory. Pointers in the HTTP_UNKNOWN_HEADER structure points to the corresponding key-value pair.
84
82
/// </summary>
85
- private unsafe Span < byte > SetUnknownHeaders ( Span < byte > nativeMemory , ref HTTP_REQUEST_V1 requestStructure , IReadOnlyCollection < ( string Key , string Value ) > headerNames )
83
+ private unsafe Span < byte > SetUnknownHeaders ( Span < byte > nativeMemory , ref HttpApiTypes . HTTP_REQUEST requestStructure , IReadOnlyCollection < ( string Key , string Value ) > headerNames )
86
84
{
87
- var unknownHeaderStructureDestination = nativeMemory [ Marshal . SizeOf < HTTP_REQUEST_V1 > ( ) .. ] ;
85
+ var unknownHeaderStructureDestination = nativeMemory . Slice ( Marshal . SizeOf < HttpApiTypes . HTTP_REQUEST > ( ) ) ;
88
86
fixed ( byte * address = & MemoryMarshal . GetReference ( unknownHeaderStructureDestination ) )
89
87
{
90
- requestStructure . Headers . pUnknownHeaders = ( HTTP_UNKNOWN_HEADER * ) address ;
88
+ requestStructure . Headers . pUnknownHeaders = ( HttpApiTypes . HTTP_UNKNOWN_HEADER * ) address ;
91
89
}
92
90
requestStructure . Headers . UnknownHeaderCount += ( ushort ) headerNames . Count ;
93
91
94
- var unknownHeadersSize = Marshal . SizeOf < HTTP_UNKNOWN_HEADER > ( ) ;
95
- var dataDestination = unknownHeaderStructureDestination [ ( unknownHeadersSize * headerNames . Count ) .. ] ;
96
- foreach ( var ( headerKey , headerValue ) in headerNames )
92
+ var unknownHeadersSize = Marshal . SizeOf < HttpApiTypes . HTTP_UNKNOWN_HEADER > ( ) ;
93
+ var dataDestination = unknownHeaderStructureDestination . Slice ( unknownHeadersSize * headerNames . Count ) ;
94
+ foreach ( var headerName in headerNames )
97
95
{
98
- var unknownHeaderStructure = new HTTP_UNKNOWN_HEADER ( ) ;
99
- var nameLength = Encoding . ASCII . GetBytes ( headerKey , dataDestination ) ;
96
+ var unknownHeaderStructure = new HttpApiTypes . HTTP_UNKNOWN_HEADER ( ) ;
97
+ int nameLength = Encoding . ASCII . GetBytes ( headerName . Key , dataDestination ) ;
100
98
fixed ( byte * address = & MemoryMarshal . GetReference ( dataDestination ) )
101
99
{
102
- unknownHeaderStructure . pName = ( PCSTR ) address ;
100
+ unknownHeaderStructure . pName = address ;
103
101
unknownHeaderStructure . NameLength = ( ushort ) nameLength ;
104
102
}
105
- dataDestination = dataDestination [ nameLength .. ] ;
103
+ dataDestination = dataDestination . Slice ( nameLength ) ;
106
104
107
- if ( ! string . IsNullOrEmpty ( headerValue ) )
105
+ if ( ! string . IsNullOrEmpty ( headerName . Value ) )
108
106
{
109
- var valueLength = Encoding . ASCII . GetBytes ( headerValue , dataDestination ) ;
107
+ int valueLength = Encoding . ASCII . GetBytes ( headerName . Value , dataDestination ) ;
110
108
fixed ( byte * address = & MemoryMarshal . GetReference ( dataDestination ) )
111
109
{
112
- unknownHeaderStructure . pRawValue = ( PCSTR ) address ;
110
+ unknownHeaderStructure . pRawValue = address ;
113
111
unknownHeaderStructure . RawValueLength = ( ushort ) valueLength ;
114
112
}
115
- dataDestination = dataDestination [ nameLength .. ] ;
113
+ dataDestination = dataDestination . Slice ( nameLength ) ;
116
114
}
117
115
MemoryMarshal . Write ( unknownHeaderStructureDestination , in unknownHeaderStructure ) ;
118
- unknownHeaderStructureDestination = unknownHeaderStructureDestination [ unknownHeadersSize .. ] ;
116
+ unknownHeaderStructureDestination = unknownHeaderStructureDestination . Slice ( unknownHeadersSize ) ;
119
117
}
120
118
return dataDestination ;
121
119
}
122
120
123
- private static List < ( string , string ) > GenerateUnknownHeaders ( int count )
121
+ private IReadOnlyCollection < ( string , string ) > GenerateUnknownHeaders ( int count )
124
122
{
125
123
var result = new List < ( string , string ) > ( ) ;
126
- for ( var i = 0 ; i < count ; i ++ )
124
+ for ( int i = 0 ; i < count ; i ++ )
127
125
{
128
126
result . Add ( ( $ "X-Custom-{ i } ", $ "Value-{ i } ") ) ;
129
127
}
0 commit comments