@@ -573,21 +573,16 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
573
573
int numKnownMultiHeaders = 0 ;
574
574
foreach ( var headerPair in Headers )
575
575
{
576
- if ( headerPair . Value . Count == 0 )
577
- {
578
- // TODO: Have the collection exclude empty headers.
579
- continue ;
580
- }
581
576
// See if this is an unknown header
582
577
lookup = HttpApi . HTTP_RESPONSE_HEADER_ID . IndexOfKnownHeader ( headerPair . Key ) ;
583
578
584
579
// Http.Sys doesn't let us send the Connection: Upgrade header as a Known header.
585
580
if ( lookup == - 1 ||
586
581
( isOpaqueUpgrade && lookup == ( int ) HttpApi . HTTP_RESPONSE_HEADER_ID . Enum . HttpHeaderConnection ) )
587
582
{
588
- numUnknownHeaders += headerPair . Value . Count ;
583
+ numUnknownHeaders += headerPair . Value . Where ( s => s != null ) . Count ( ) ;
589
584
}
590
- else if ( headerPair . Value . Count > 1 )
585
+ else if ( headerPair . Value . Where ( s => ! string . IsNullOrEmpty ( s ) ) . Count ( ) > 1 )
591
586
{
592
587
numKnownMultiHeaders ++ ;
593
588
}
@@ -600,11 +595,11 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
600
595
{
601
596
foreach ( var headerPair in Headers )
602
597
{
603
- if ( headerPair . Value . Count == 0 )
598
+ if ( headerPair . Value . Where ( s => s != null ) . Count ( ) == 0 )
604
599
{
605
- // TODO: Have the collection exclude empty headers.
606
600
continue ;
607
601
}
602
+
608
603
headerName = headerPair . Key ;
609
604
StringValues headerValues = headerPair . Value ;
610
605
lookup = HttpApi . HTTP_RESPONSE_HEADER_ID . IndexOfKnownHeader ( headerName ) ;
@@ -623,6 +618,13 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
623
618
624
619
for ( int headerValueIndex = 0 ; headerValueIndex < headerValues . Count ; headerValueIndex ++ )
625
620
{
621
+ headerValue = headerValues [ headerValueIndex ] ;
622
+
623
+ if ( headerValue == null )
624
+ {
625
+ continue ;
626
+ }
627
+
626
628
// Add Name
627
629
bytes = new byte [ HeaderEncoding . GetByteCount ( headerName ) ] ;
628
630
unknownHeaders [ _nativeResponse . Response_V1 . Headers . UnknownHeaderCount ] . NameLength = ( ushort ) bytes . Length ;
@@ -632,7 +634,6 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
632
634
unknownHeaders [ _nativeResponse . Response_V1 . Headers . UnknownHeaderCount ] . pName = ( sbyte * ) gcHandle . AddrOfPinnedObject ( ) ;
633
635
634
636
// Add Value
635
- headerValue = headerValues [ headerValueIndex ] ;
636
637
bytes = new byte [ HeaderEncoding . GetByteCount ( headerValue ) ] ;
637
638
unknownHeaders [ _nativeResponse . Response_V1 . Headers . UnknownHeaderCount ] . RawValueLength = ( ushort ) bytes . Length ;
638
639
HeaderEncoding . GetBytes ( headerValue , 0 , bytes . Length , bytes , 0 ) ;
@@ -642,20 +643,17 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
642
643
_nativeResponse . Response_V1 . Headers . UnknownHeaderCount ++ ;
643
644
}
644
645
}
645
- else if ( headerPair . Value . Count == 1 )
646
+ else if ( headerPair . Value . Where ( s => ! string . IsNullOrEmpty ( s ) ) . Count ( ) == 1 )
646
647
{
647
648
headerValue = headerValues [ 0 ] ;
648
- if ( headerValue != null )
649
- {
650
- bytes = new byte [ HeaderEncoding . GetByteCount ( headerValue ) ] ;
651
- pKnownHeaders [ lookup ] . RawValueLength = ( ushort ) bytes . Length ;
652
- HeaderEncoding . GetBytes ( headerValue , 0 , bytes . Length , bytes , 0 ) ;
653
- gcHandle = GCHandle . Alloc ( bytes , GCHandleType . Pinned ) ;
654
- pinnedHeaders . Add ( gcHandle ) ;
655
- pKnownHeaders [ lookup ] . pRawValue = ( sbyte * ) gcHandle . AddrOfPinnedObject ( ) ;
656
- }
649
+ bytes = new byte [ HeaderEncoding . GetByteCount ( headerValue ) ] ;
650
+ pKnownHeaders [ lookup ] . RawValueLength = ( ushort ) bytes . Length ;
651
+ HeaderEncoding . GetBytes ( headerValue , 0 , bytes . Length , bytes , 0 ) ;
652
+ gcHandle = GCHandle . Alloc ( bytes , GCHandleType . Pinned ) ;
653
+ pinnedHeaders . Add ( gcHandle ) ;
654
+ pKnownHeaders [ lookup ] . pRawValue = ( sbyte * ) gcHandle . AddrOfPinnedObject ( ) ;
657
655
}
658
- else
656
+ else if ( headerPair . Value . Where ( s => ! string . IsNullOrEmpty ( s ) ) . Count ( ) > 1 )
659
657
{
660
658
if ( knownHeaderInfo == null )
661
659
{
@@ -673,15 +671,21 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
673
671
header . HeaderId = ( HttpApi . HTTP_RESPONSE_HEADER_ID . Enum ) lookup ;
674
672
header . Flags = HttpApi . HTTP_RESPONSE_INFO_FLAGS . PreserveOrder ; // TODO: The docs say this is for www-auth only.
675
673
676
- HttpApi . HTTP_KNOWN_HEADER [ ] nativeHeaderValues = new HttpApi . HTTP_KNOWN_HEADER [ headerValues . Count ] ;
674
+ HttpApi . HTTP_KNOWN_HEADER [ ] nativeHeaderValues = new HttpApi . HTTP_KNOWN_HEADER [ headerValues . Where ( s => s != null ) . Count ( ) ] ;
677
675
gcHandle = GCHandle . Alloc ( nativeHeaderValues , GCHandleType . Pinned ) ;
678
676
pinnedHeaders . Add ( gcHandle ) ;
679
677
header . KnownHeaders = ( HttpApi . HTTP_KNOWN_HEADER * ) gcHandle . AddrOfPinnedObject ( ) ;
680
678
681
679
for ( int headerValueIndex = 0 ; headerValueIndex < headerValues . Count ; headerValueIndex ++ )
682
680
{
683
- // Add Value
684
681
headerValue = headerValues [ headerValueIndex ] ;
682
+
683
+ if ( string . IsNullOrEmpty ( headerValue ) )
684
+ {
685
+ continue ;
686
+ }
687
+
688
+ // Add Valuegit
685
689
bytes = new byte [ HeaderEncoding . GetByteCount ( headerValue ) ] ;
686
690
nativeHeaderValues [ header . KnownHeaderCount ] . RawValueLength = ( ushort ) bytes . Length ;
687
691
HeaderEncoding . GetBytes ( headerValue , 0 , bytes . Length , bytes , 0 ) ;
0 commit comments