-
Notifications
You must be signed in to change notification settings - Fork 10.1k
/
NativeRequestContext.cs
851 lines (750 loc) · 30.1 KB
/
NativeRequestContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.HttpSys.Internal;
#pragma warning disable CA1852 // Seal internal types
internal unsafe class NativeRequestContext : IDisposable
#pragma warning restore CA1852 // Seal internal types
{
private const int AlignmentPadding = 8;
private const int DefaultBufferSize = 4096 - AlignmentPadding;
private IntPtr _originalBufferAddress;
private readonly bool _useLatin1;
private HttpApiTypes.HTTP_REQUEST* _nativeRequest;
private readonly IMemoryOwner<byte>? _backingBuffer;
private MemoryHandle _memoryHandle;
private readonly int _bufferAlignment;
private readonly bool _permanentlyPinned;
private bool _disposed;
private IReadOnlyDictionary<int, ReadOnlyMemory<byte>>? _requestInfo;
[MemberNotNullWhen(false, nameof(_backingBuffer))]
private bool PermanentlyPinned => _permanentlyPinned;
// To be used by HttpSys
internal NativeRequestContext(MemoryPool<byte> memoryPool, uint? bufferSize, ulong requestId, bool useLatin1)
{
// TODO:
// Apparently the HttpReceiveHttpRequest memory alignment requirements for non - ARM processors
// are different than for ARM processors. We have seen 4 - byte - aligned buffers allocated on
// virtual x64/x86 machines which were accepted by HttpReceiveHttpRequest without errors. In
// these cases the buffer alignment may cause reading values at invalid offset. Setting buffer
// alignment to 0 for now.
//
// _bufferAlignment = (int)(requestAddress.ToInt64() & 0x07);
_bufferAlignment = 0;
var newSize = (int)(bufferSize ?? DefaultBufferSize) + AlignmentPadding;
if (newSize <= memoryPool.MaxBufferSize)
{
_backingBuffer = memoryPool.Rent(newSize);
}
else
{
// No size limit
_backingBuffer = MemoryPool<byte>.Shared.Rent(newSize);
}
_backingBuffer.Memory.Span.Clear();
_memoryHandle = _backingBuffer.Memory.Pin();
_nativeRequest = (HttpApiTypes.HTTP_REQUEST*)((long)_memoryHandle.Pointer + _bufferAlignment);
RequestId = requestId;
_useLatin1 = useLatin1;
}
// To be used by IIS Integration.
internal NativeRequestContext(HttpApiTypes.HTTP_REQUEST* request, bool useLatin1)
{
_useLatin1 = useLatin1;
_nativeRequest = request;
_bufferAlignment = 0;
_permanentlyPinned = true;
}
public IReadOnlyDictionary<int, ReadOnlyMemory<byte>> RequestInfo => _requestInfo ??= GetRequestInfo();
public ReadOnlySpan<long> Timestamps
{
get
{
/*
Below is the definition of the timing info structure we are accessing the memory for.
ULONG is 32-bit and maps to int. ULONGLONG is 64-bit and maps to long.
typedef struct _HTTP_REQUEST_TIMING_INFO
{
ULONG RequestTimingCount;
ULONGLONG RequestTiming[HttpRequestTimingTypeMax];
} HTTP_REQUEST_TIMING_INFO, *PHTTP_REQUEST_TIMING_INFO;
*/
if (!RequestInfo.TryGetValue((int)HttpApiTypes.HTTP_REQUEST_INFO_TYPE.HttpRequestInfoTypeRequestTiming, out var timingInfo))
{
return ReadOnlySpan<long>.Empty;
}
var timingCount = MemoryMarshal.Read<int>(timingInfo.Span);
// Note that even though RequestTimingCount is an int, the compiler enforces alignment of data in the struct which causes 4 bytes
// of padding to be added after RequestTimingCount, so we need to skip 64-bits before we get to the start of the RequestTiming array
return MemoryMarshal.CreateReadOnlySpan(
ref Unsafe.As<byte, long>(ref MemoryMarshal.GetReference(timingInfo.Span.Slice(sizeof(long)))),
timingCount);
}
}
internal HttpApiTypes.HTTP_REQUEST* NativeRequest
{
get
{
Debug.Assert(_nativeRequest != null || _backingBuffer == null, "native request accessed after ReleasePins().");
return _nativeRequest;
}
}
internal HttpApiTypes.HTTP_REQUEST_V2* NativeRequestV2
{
get
{
Debug.Assert(_nativeRequest != null || _backingBuffer == null, "native request accessed after ReleasePins().");
return (HttpApiTypes.HTTP_REQUEST_V2*)_nativeRequest;
}
}
internal ulong RequestId
{
get { return NativeRequest->RequestId; }
set { NativeRequest->RequestId = value; }
}
internal ulong ConnectionId => NativeRequest->ConnectionId;
internal ulong RawConnectionId => NativeRequest->RawConnectionId;
internal HttpApiTypes.HTTP_VERB VerbId => NativeRequest->Verb;
internal ulong UrlContext => NativeRequest->UrlContext;
internal ushort UnknownHeaderCount => NativeRequest->Headers.UnknownHeaderCount;
internal SslStatus SslStatus
{
get
{
return NativeRequest->pSslInfo == null ? SslStatus.Insecure :
NativeRequest->pSslInfo->SslClientCertNegotiated == 0 ? SslStatus.NoClientCert :
SslStatus.ClientCert;
}
}
internal bool IsHttp2 => NativeRequest->Flags.HasFlag(HttpApiTypes.HTTP_REQUEST_FLAGS.Http2);
internal bool IsHttp3 => NativeRequest->Flags.HasFlag(HttpApiTypes.HTTP_REQUEST_FLAGS.Http3);
// Assumes memory isn't pinned. Will fail if called by IIS.
internal uint Size
{
get
{
Debug.Assert(_backingBuffer != null);
return (uint)_backingBuffer.Memory.Length - AlignmentPadding;
}
}
// ReleasePins() should be called exactly once. It must be called before Dispose() is called, which means it must be called
// before an object (Request) which closes the RequestContext on demand is returned to the application.
internal void ReleasePins()
{
Debug.Assert(_nativeRequest != null, "RequestContextBase::ReleasePins()|ReleasePins() called twice.");
_originalBufferAddress = (IntPtr)_nativeRequest;
_memoryHandle.Dispose();
_memoryHandle = default;
_nativeRequest = null;
}
public bool TryGetTimestamp(HttpSysRequestTimingType timestampType, out long timestamp)
{
var index = (int)timestampType;
var timestamps = Timestamps;
if (index < timestamps.Length && timestamps[index] > 0)
{
timestamp = timestamps[index];
return true;
}
timestamp = default;
return false;
}
public bool TryGetElapsedTime(HttpSysRequestTimingType startingTimestampType, HttpSysRequestTimingType endingTimestampType, out TimeSpan elapsed)
{
if (TryGetTimestamp(startingTimestampType, out long startTimestamp) && TryGetTimestamp(endingTimestampType, out long endTimestamp))
{
elapsed = Stopwatch.GetElapsedTime(startTimestamp, endTimestamp);
return true;
}
elapsed = default;
return false;
}
public virtual void Dispose()
{
if (!_disposed)
{
_disposed = true;
Debug.Assert(_nativeRequest == null, "RequestContextBase::Dispose()|Dispose() called before ReleasePins().");
_memoryHandle.Dispose();
_backingBuffer?.Dispose();
}
}
// These methods require the HTTP_REQUEST to still be pinned in its original location.
internal string? GetVerb()
{
var verb = NativeRequest->Verb;
if (verb > HttpApiTypes.HTTP_VERB.HttpVerbUnknown && verb < HttpApiTypes.HTTP_VERB.HttpVerbMaximum)
{
return HttpApiTypes.HttpVerbs[(int)verb];
}
else if (verb == HttpApiTypes.HTTP_VERB.HttpVerbUnknown && NativeRequest->pUnknownVerb != null)
{
// Never use Latin1 for the VERB
return HeaderEncoding.GetString(NativeRequest->pUnknownVerb, NativeRequest->UnknownVerbLength, useLatin1: false);
}
return null;
}
internal string? GetRawUrl()
{
if (NativeRequest->pRawUrl != null && NativeRequest->RawUrlLength > 0)
{
return Marshal.PtrToStringAnsi((IntPtr)NativeRequest->pRawUrl, NativeRequest->RawUrlLength);
}
return null;
}
internal Span<byte> GetRawUrlInBytes()
{
if (NativeRequest->pRawUrl != null && NativeRequest->RawUrlLength > 0)
{
return new Span<byte>(NativeRequest->pRawUrl, NativeRequest->RawUrlLength);
}
return default;
}
internal CookedUrl GetCookedUrl()
{
return new CookedUrl(NativeRequest->CookedUrl);
}
internal Version GetVersion()
{
if (IsHttp3)
{
return HttpVersion.Version30;
}
if (IsHttp2)
{
return HttpVersion.Version20;
}
var major = NativeRequest->Version.MajorVersion;
var minor = NativeRequest->Version.MinorVersion;
if (major == 1 && minor == 1)
{
return HttpVersion.Version11;
}
else if (major == 1 && minor == 0)
{
return HttpVersion.Version10;
}
return new Version(major, minor);
}
internal bool CheckAuthenticated()
{
var requestInfo = NativeRequestV2->pRequestInfo;
var infoCount = NativeRequestV2->RequestInfoCount;
for (int i = 0; i < infoCount; i++)
{
var info = &requestInfo[i];
if (info != null
&& info->InfoType == HttpApiTypes.HTTP_REQUEST_INFO_TYPE.HttpRequestInfoTypeAuth)
{
var authInfo = (HttpApiTypes.HTTP_REQUEST_AUTH_INFO*)info->pInfo;
if (authInfo->AuthStatus == HttpApiTypes.HTTP_AUTH_STATUS.HttpAuthStatusSuccess)
{
return true;
}
}
}
return false;
}
internal WindowsPrincipal GetUser()
{
var requestInfo = NativeRequestV2->pRequestInfo;
var infoCount = NativeRequestV2->RequestInfoCount;
for (int i = 0; i < infoCount; i++)
{
var info = &requestInfo[i];
if (info != null
&& info->InfoType == HttpApiTypes.HTTP_REQUEST_INFO_TYPE.HttpRequestInfoTypeAuth)
{
var authInfo = (HttpApiTypes.HTTP_REQUEST_AUTH_INFO*)info->pInfo;
if (authInfo->AuthStatus == HttpApiTypes.HTTP_AUTH_STATUS.HttpAuthStatusSuccess)
{
// Duplicates AccessToken
var identity = new WindowsIdentity(authInfo->AccessToken, GetAuthTypeFromRequest(authInfo->AuthType));
// Close the original
UnsafeNclNativeMethods.SafeNetHandles.CloseHandle(authInfo->AccessToken);
return new WindowsPrincipal(identity);
}
}
}
return new WindowsPrincipal(WindowsIdentity.GetAnonymous()); // Anonymous / !IsAuthenticated
}
internal HttpApiTypes.HTTP_SSL_PROTOCOL_INFO GetTlsHandshake()
{
var requestInfo = NativeRequestV2->pRequestInfo;
var infoCount = NativeRequestV2->RequestInfoCount;
for (int i = 0; i < infoCount; i++)
{
var info = &requestInfo[i];
if (info != null
&& info->InfoType == HttpApiTypes.HTTP_REQUEST_INFO_TYPE.HttpRequestInfoTypeSslProtocol)
{
var authInfo = *((HttpApiTypes.HTTP_SSL_PROTOCOL_INFO*)info->pInfo);
SetSslProtocol(&authInfo);
return authInfo;
}
}
return default;
}
private static void SetSslProtocol(HttpApiTypes.HTTP_SSL_PROTOCOL_INFO* protocolInfo)
{
var protocol = protocolInfo->Protocol;
// The OS considers client and server TLS as different enum values. SslProtocols choose to combine those for some reason.
// We need to fill in the client bits so the enum shows the expected protocol.
// https://learn.microsoft.com/windows/desktop/api/schannel/ns-schannel-_secpkgcontext_connectioninfo
// Compare to https://referencesource.microsoft.com/#System/net/System/Net/SecureProtocols/_SslState.cs,8905d1bf17729de3
#pragma warning disable CS0618 // Type or member is obsolete
if ((protocol & SslProtocols.Ssl2) != 0)
{
protocol |= SslProtocols.Ssl2;
}
if ((protocol & SslProtocols.Ssl3) != 0)
{
protocol |= SslProtocols.Ssl3;
}
#pragma warning restore CS0618 // Type or Prmember is obsolete
#pragma warning disable SYSLIB0039 // TLS 1.0 and 1.1 are obsolete
if ((protocol & SslProtocols.Tls) != 0)
{
protocol |= SslProtocols.Tls;
}
if ((protocol & SslProtocols.Tls11) != 0)
{
protocol |= SslProtocols.Tls11;
}
#pragma warning restore SYSLIB0039
if ((protocol & SslProtocols.Tls12) != 0)
{
protocol |= SslProtocols.Tls12;
}
if ((protocol & SslProtocols.Tls13) != 0)
{
protocol |= SslProtocols.Tls13;
}
protocolInfo->Protocol = protocol;
}
private static string GetAuthTypeFromRequest(HttpApiTypes.HTTP_REQUEST_AUTH_TYPE input)
{
switch (input)
{
case HttpApiTypes.HTTP_REQUEST_AUTH_TYPE.HttpRequestAuthTypeBasic:
return "Basic";
case HttpApiTypes.HTTP_REQUEST_AUTH_TYPE.HttpRequestAuthTypeNTLM:
return "NTLM";
// case HttpApi.HTTP_REQUEST_AUTH_TYPE.HttpRequestAuthTypeDigest:
// return "Digest";
case HttpApiTypes.HTTP_REQUEST_AUTH_TYPE.HttpRequestAuthTypeNegotiate:
return "Negotiate";
case HttpApiTypes.HTTP_REQUEST_AUTH_TYPE.HttpRequestAuthTypeKerberos:
return "Kerberos";
default:
throw new NotImplementedException(input.ToString());
}
}
internal bool HasKnownHeader(HttpSysRequestHeader header)
{
if (PermanentlyPinned)
{
return HasKnowHeaderHelper(header, 0, _nativeRequest);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST*)(pMemoryBlob + _bufferAlignment);
long fixup = pMemoryBlob - (byte*)_originalBufferAddress;
return HasKnowHeaderHelper(header, fixup, request);
}
}
}
private bool HasKnowHeaderHelper(HttpSysRequestHeader header, long fixup, HttpApiTypes.HTTP_REQUEST* request)
{
int headerIndex = (int)header;
HttpApiTypes.HTTP_KNOWN_HEADER* pKnownHeader = (&request->Headers.KnownHeaders) + headerIndex;
// For known headers, when header value is empty, RawValueLength will be 0 and
// pRawValue will point to empty string ("\0")
if (pKnownHeader->RawValueLength > 0)
{
return true;
}
return false;
}
// These methods are for accessing the request structure after it has been unpinned. They need to adjust addresses
// in case GC has moved the original object.
internal string? GetKnownHeader(HttpSysRequestHeader header)
{
if (PermanentlyPinned)
{
return GetKnowHeaderHelper(header, 0, _nativeRequest);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST*)(pMemoryBlob + _bufferAlignment);
long fixup = pMemoryBlob - (byte*)_originalBufferAddress;
return GetKnowHeaderHelper(header, fixup, request);
}
}
}
private string? GetKnowHeaderHelper(HttpSysRequestHeader header, long fixup, HttpApiTypes.HTTP_REQUEST* request)
{
int headerIndex = (int)header;
string? value = null;
HttpApiTypes.HTTP_KNOWN_HEADER* pKnownHeader = (&request->Headers.KnownHeaders) + headerIndex;
// For known headers, when header value is empty, RawValueLength will be 0 and
// pRawValue will point to empty string ("\0")
if (pKnownHeader->RawValueLength > 0)
{
value = HeaderEncoding.GetString(pKnownHeader->pRawValue + fixup, pKnownHeader->RawValueLength, _useLatin1);
}
return value;
}
internal void GetUnknownKeys(Span<string> destination)
{
if (PermanentlyPinned)
{
PopulateUnknownKeys(_nativeRequest, 0, destination);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST*)(pMemoryBlob + _bufferAlignment);
long fixup = pMemoryBlob - (byte*)_originalBufferAddress;
PopulateUnknownKeys(request, fixup, destination);
}
}
}
private void PopulateUnknownKeys(HttpApiTypes.HTTP_REQUEST* request, long fixup, Span<string> destination)
{
if (request->Headers.UnknownHeaderCount == 0)
{
return;
}
var pUnknownHeader = (HttpApiTypes.HTTP_UNKNOWN_HEADER*)(fixup + (byte*)request->Headers.pUnknownHeaders);
for (int index = 0; index < request->Headers.UnknownHeaderCount; index++)
{
if (pUnknownHeader->pName != null && pUnknownHeader->NameLength > 0)
{
var headerName = HeaderEncoding.GetString(pUnknownHeader->pName + fixup, pUnknownHeader->NameLength, _useLatin1);
destination[index] = headerName;
}
pUnknownHeader++;
}
}
internal int CountUnknownHeaders()
{
if (PermanentlyPinned)
{
return CountUnknownHeaders(_nativeRequest, 0);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST*)(pMemoryBlob + _bufferAlignment);
long fixup = pMemoryBlob - (byte*)_originalBufferAddress;
return CountUnknownHeaders(request, fixup);
}
}
}
private int CountUnknownHeaders(HttpApiTypes.HTTP_REQUEST* request, long fixup)
{
if (request->Headers.UnknownHeaderCount == 0)
{
return 0;
}
int count = 0;
var pUnknownHeader = (HttpApiTypes.HTTP_UNKNOWN_HEADER*)(fixup + (byte*)request->Headers.pUnknownHeaders);
for (int index = 0; index < request->Headers.UnknownHeaderCount; index++)
{
// For unknown headers, when header value is empty, RawValueLength will be 0 and
// pRawValue will be null.
if (pUnknownHeader->pName != null && pUnknownHeader->NameLength > 0)
{
count++;
}
pUnknownHeader++;
}
return count;
}
internal void GetUnknownHeaders(IDictionary<string, StringValues> unknownHeaders)
{
if (PermanentlyPinned)
{
GetUnknownHeadersHelper(unknownHeaders, 0, _nativeRequest);
}
else
{
// Return value.
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST*)(pMemoryBlob + _bufferAlignment);
long fixup = pMemoryBlob - (byte*)_originalBufferAddress;
GetUnknownHeadersHelper(unknownHeaders, fixup, request);
}
}
}
private void GetUnknownHeadersHelper(IDictionary<string, StringValues> unknownHeaders, long fixup, HttpApiTypes.HTTP_REQUEST* request)
{
int index;
// unknown headers
if (request->Headers.UnknownHeaderCount != 0)
{
var pUnknownHeader = (HttpApiTypes.HTTP_UNKNOWN_HEADER*)(fixup + (byte*)request->Headers.pUnknownHeaders);
for (index = 0; index < request->Headers.UnknownHeaderCount; index++)
{
// For unknown headers, when header value is empty, RawValueLength will be 0 and
// pRawValue will be null.
if (pUnknownHeader->pName != null && pUnknownHeader->NameLength > 0)
{
var headerName = HeaderEncoding.GetString(pUnknownHeader->pName + fixup, pUnknownHeader->NameLength, _useLatin1);
string headerValue;
if (pUnknownHeader->pRawValue != null && pUnknownHeader->RawValueLength > 0)
{
headerValue = HeaderEncoding.GetString(pUnknownHeader->pRawValue + fixup, pUnknownHeader->RawValueLength, _useLatin1);
}
else
{
headerValue = string.Empty;
}
// Note that Http.Sys currently collapses all headers of the same name to a single coma separated string,
// so we can just call Set.
unknownHeaders[headerName] = headerValue;
}
pUnknownHeader++;
}
}
}
internal SocketAddress? GetRemoteEndPoint()
{
return GetEndPoint(localEndpoint: false);
}
internal SocketAddress? GetLocalEndPoint()
{
return GetEndPoint(localEndpoint: true);
}
private SocketAddress? GetEndPoint(bool localEndpoint)
{
if (PermanentlyPinned)
{
return GetEndPointHelper(localEndpoint, _nativeRequest, (byte*)0);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST*)(pMemoryBlob + _bufferAlignment);
return GetEndPointHelper(localEndpoint, request, pMemoryBlob);
}
}
}
private SocketAddress? GetEndPointHelper(bool localEndpoint, HttpApiTypes.HTTP_REQUEST* request, byte* pMemoryBlob)
{
var source = localEndpoint ? (byte*)request->Address.pLocalAddress : (byte*)request->Address.pRemoteAddress;
if (source == null)
{
return null;
}
var address = (IntPtr)(pMemoryBlob + _bufferAlignment - (byte*)_originalBufferAddress + source);
return CopyOutAddress(address);
}
private static SocketAddress? CopyOutAddress(IntPtr address)
{
ushort addressFamily = *((ushort*)address);
if (addressFamily == (ushort)AddressFamily.InterNetwork)
{
var v4address = new SocketAddress(AddressFamily.InterNetwork, SocketAddress.IPv4AddressSize);
fixed (byte* pBuffer = v4address.Buffer)
{
for (int index = 2; index < SocketAddress.IPv4AddressSize; index++)
{
pBuffer[index] = ((byte*)address)[index];
}
}
return v4address;
}
if (addressFamily == (ushort)AddressFamily.InterNetworkV6)
{
var v6address = new SocketAddress(AddressFamily.InterNetworkV6, SocketAddress.IPv6AddressSize);
fixed (byte* pBuffer = v6address.Buffer)
{
for (int index = 2; index < SocketAddress.IPv6AddressSize; index++)
{
pBuffer[index] = ((byte*)address)[index];
}
}
return v6address;
}
return null;
}
internal uint GetChunks(ref int dataChunkIndex, ref uint dataChunkOffset, byte[] buffer, int offset, int size)
{
// Return value.
if (PermanentlyPinned)
{
return GetChunksHelper(ref dataChunkIndex, ref dataChunkOffset, buffer, offset, size, 0, _nativeRequest);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST*)(pMemoryBlob + _bufferAlignment);
long fixup = pMemoryBlob - (byte*)_originalBufferAddress;
return GetChunksHelper(ref dataChunkIndex, ref dataChunkOffset, buffer, offset, size, fixup, request);
}
}
}
private uint GetChunksHelper(ref int dataChunkIndex, ref uint dataChunkOffset, byte[] buffer, int offset, int size, long fixup, HttpApiTypes.HTTP_REQUEST* request)
{
uint dataRead = 0;
if (request->EntityChunkCount > 0 && dataChunkIndex < request->EntityChunkCount && dataChunkIndex != -1)
{
var pDataChunk = (HttpApiTypes.HTTP_DATA_CHUNK*)(fixup + (byte*)&request->pEntityChunks[dataChunkIndex]);
fixed (byte* pReadBuffer = buffer)
{
byte* pTo = &pReadBuffer[offset];
while (dataChunkIndex < request->EntityChunkCount && dataRead < size)
{
if (dataChunkOffset >= pDataChunk->fromMemory.BufferLength)
{
dataChunkOffset = 0;
dataChunkIndex++;
pDataChunk++;
}
else
{
byte* pFrom = (byte*)pDataChunk->fromMemory.pBuffer + dataChunkOffset + fixup;
uint bytesToRead = pDataChunk->fromMemory.BufferLength - (uint)dataChunkOffset;
if (bytesToRead > (uint)size)
{
bytesToRead = (uint)size;
}
for (uint i = 0; i < bytesToRead; i++)
{
*(pTo++) = *(pFrom++);
}
dataRead += bytesToRead;
dataChunkOffset += bytesToRead;
}
}
}
}
// we're finished.
if (dataChunkIndex == request->EntityChunkCount)
{
dataChunkIndex = -1;
}
return dataRead;
}
internal IReadOnlyDictionary<int, ReadOnlyMemory<byte>> GetRequestInfo()
{
if (PermanentlyPinned)
{
return GetRequestInfo((IntPtr)_nativeRequest, (HttpApiTypes.HTTP_REQUEST_V2*)_nativeRequest);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST_V2*)(pMemoryBlob + _bufferAlignment);
return GetRequestInfo(_originalBufferAddress, request);
}
}
}
private IReadOnlyDictionary<int, ReadOnlyMemory<byte>> GetRequestInfo(IntPtr baseAddress, HttpApiTypes.HTTP_REQUEST_V2* nativeRequest)
{
var count = nativeRequest->RequestInfoCount;
if (count == 0)
{
return ImmutableDictionary<int, ReadOnlyMemory<byte>>.Empty;
}
var info = new Dictionary<int, ReadOnlyMemory<byte>>(count);
for (var i = 0; i < count; i++)
{
var requestInfo = nativeRequest->pRequestInfo[i];
var memory = PermanentlyPinned
? new PointerMemoryManager<byte>((byte*)requestInfo.pInfo, (int)requestInfo.InfoLength).Memory
: _backingBuffer.Memory.Slice((int)((long)requestInfo.pInfo - (long)baseAddress), (int)requestInfo.InfoLength);
info.Add((int)requestInfo.InfoType, memory);
}
return new ReadOnlyDictionary<int, ReadOnlyMemory<byte>>(info);
}
internal X509Certificate2? GetClientCertificate()
{
if (PermanentlyPinned)
{
return GetClientCertificate((IntPtr)_nativeRequest, (HttpApiTypes.HTTP_REQUEST_V2*)_nativeRequest);
}
else
{
fixed (byte* pMemoryBlob = _backingBuffer.Memory.Span)
{
var request = (HttpApiTypes.HTTP_REQUEST_V2*)(pMemoryBlob + _bufferAlignment);
return GetClientCertificate(_originalBufferAddress, request);
}
}
}
// Throws CryptographicException
private X509Certificate2? GetClientCertificate(IntPtr baseAddress, HttpApiTypes.HTTP_REQUEST_V2* nativeRequest)
{
var request = nativeRequest->Request;
long fixup = (byte*)nativeRequest - (byte*)baseAddress;
if (request.pSslInfo == null)
{
return null;
}
var sslInfo = (HttpApiTypes.HTTP_SSL_INFO*)((byte*)request.pSslInfo + fixup);
if (sslInfo->SslClientCertNegotiated == 0 || sslInfo->pClientCertInfo == null)
{
return null;
}
var clientCertInfo = (HttpApiTypes.HTTP_SSL_CLIENT_CERT_INFO*)((byte*)sslInfo->pClientCertInfo + fixup);
if (clientCertInfo->pCertEncoded == null)
{
return null;
}
var clientCert = clientCertInfo->pCertEncoded + fixup;
byte[] certEncoded = new byte[clientCertInfo->CertEncodedSize];
Marshal.Copy((IntPtr)clientCert, certEncoded, 0, certEncoded.Length);
return new X509Certificate2(certEncoded);
}
// Copied from https://github.com/dotnet/runtime/blob/main/src/libraries/Common/src/System/Memory/PointerMemoryManager.cs
private sealed unsafe class PointerMemoryManager<T> : MemoryManager<T> where T : struct
{
private readonly void* _pointer;
private readonly int _length;
internal PointerMemoryManager(void* pointer, int length)
{
_pointer = pointer;
_length = length;
}
protected override void Dispose(bool disposing)
{
}
public override Span<T> GetSpan()
{
return new Span<T>(_pointer, _length);
}
public override MemoryHandle Pin(int elementIndex = 0)
{
throw new NotSupportedException();
}
public override void Unpin()
{
}
}
}