@@ -32,6 +32,8 @@ public partial class Frame : FrameContext, IFrameControl
32
32
private static readonly byte [ ] _bytesConnectionClose = Encoding . ASCII . GetBytes ( "Connection: close\r \n \r \n " ) ;
33
33
private static readonly byte [ ] _bytesConnectionKeepAlive = Encoding . ASCII . GetBytes ( "Connection: keep-alive\r \n \r \n " ) ;
34
34
private static readonly byte [ ] _bytesTransferEncodingChunked = Encoding . ASCII . GetBytes ( "Transfer-Encoding: chunked\r \n " ) ;
35
+ private static readonly byte [ ] _bytesHttpVersion1_0 = Encoding . ASCII . GetBytes ( "HTTP/1.0 " ) ;
36
+ private static readonly byte [ ] _bytesHttpVersion1_1 = Encoding . ASCII . GetBytes ( "HTTP/1.1 " ) ;
35
37
private static readonly byte [ ] _bytesContentLengthZero = Encoding . ASCII . GetBytes ( "Content-Length: 0\r \n " ) ;
36
38
private static readonly byte [ ] _bytesSpace = Encoding . ASCII . GetBytes ( " " ) ;
37
39
@@ -53,6 +55,8 @@ public partial class Frame : FrameContext, IFrameControl
53
55
private bool _keepAlive ;
54
56
private bool _autoChunk ;
55
57
private Exception _applicationException ;
58
+
59
+ private HttpVersionType _httpVersion ;
56
60
57
61
private readonly IPEndPoint _localEndPoint ;
58
62
private readonly IPEndPoint _remoteEndPoint ;
@@ -78,7 +82,36 @@ public Frame(ConnectionContext context,
78
82
public string RequestUri { get ; set ; }
79
83
public string Path { get ; set ; }
80
84
public string QueryString { get ; set ; }
81
- public string HttpVersion { get ; set ; }
85
+ public string HttpVersion
86
+ {
87
+ get
88
+ {
89
+ if ( _httpVersion == HttpVersionType . Http1_1 )
90
+ {
91
+ return "HTTP/1.1" ;
92
+ }
93
+ if ( _httpVersion == HttpVersionType . Http1_0 )
94
+ {
95
+ return "HTTP/1.0" ;
96
+ }
97
+ return "" ;
98
+ }
99
+ set
100
+ {
101
+ if ( value == "HTTP/1.1" )
102
+ {
103
+ _httpVersion = HttpVersionType . Http1_1 ;
104
+ }
105
+ else if ( value == "HTTP/1.0" )
106
+ {
107
+ _httpVersion = HttpVersionType . Http1_0 ;
108
+ }
109
+ else
110
+ {
111
+ _httpVersion = HttpVersionType . Unknown ;
112
+ }
113
+ }
114
+ }
82
115
public IHeaderDictionary RequestHeaders { get ; set ; }
83
116
public MessageBody MessageBody { get ; set ; }
84
117
public Stream RequestBody { get ; set ; }
@@ -113,7 +146,7 @@ public void Reset()
113
146
RequestUri = null ;
114
147
Path = null ;
115
148
QueryString = null ;
116
- HttpVersion = null ;
149
+ _httpVersion = HttpVersionType . Unknown ;
117
150
RequestHeaders = _requestHeaders ;
118
151
MessageBody = null ;
119
152
RequestBody = null ;
@@ -454,7 +487,7 @@ public void ProduceContinue()
454
487
if ( _responseStarted ) return ;
455
488
456
489
StringValues expect ;
457
- if ( HttpVersion . Equals ( "HTTP/1.1" ) &&
490
+ if ( _httpVersion == HttpVersionType . Http1_1 &&
458
491
RequestHeaders . TryGetValue ( "Expect" , out expect ) &&
459
492
( expect . FirstOrDefault ( ) ?? "" ) . Equals ( "100-continue" , StringComparison . OrdinalIgnoreCase ) )
460
493
{
@@ -575,8 +608,8 @@ private Task CreateResponseHeader(
575
608
{
576
609
var blockRemaining = memoryBlock . Data . Count ;
577
610
578
- OutputAsciiBlock ( HttpVersion , memoryBlock , SocketOutput ) ;
579
- OutputAsciiBlock ( _bytesSpace , memoryBlock , SocketOutput ) ;
611
+ OutputAsciiBlock ( _httpVersion == HttpVersionType . Http1_1 ? _bytesHttpVersion1_1 : _bytesHttpVersion1_0 , memoryBlock , SocketOutput ) ;
612
+
580
613
OutputAsciiBlock ( statusBytes , memoryBlock , SocketOutput ) ;
581
614
582
615
foreach ( var header in _responseHeaders . AsOutputEnumerable ( ) )
@@ -610,7 +643,7 @@ private Task CreateResponseHeader(
610
643
}
611
644
else
612
645
{
613
- if ( HttpVersion == "HTTP/1.1" )
646
+ if ( _httpVersion == HttpVersionType . Http1_1 )
614
647
{
615
648
_autoChunk = true ;
616
649
OutputAsciiBlock ( _bytesTransferEncodingChunked , memoryBlock , SocketOutput ) ;
@@ -622,11 +655,11 @@ private Task CreateResponseHeader(
622
655
}
623
656
}
624
657
625
- if ( _keepAlive == false && _responseHeaders . HasConnection == false && HttpVersion == "HTTP/1.1" )
658
+ if ( _keepAlive == false && _responseHeaders . HasConnection == false && _httpVersion == HttpVersionType . Http1_1 )
626
659
{
627
660
OutputAsciiBlock ( _bytesConnectionClose , memoryBlock , SocketOutput ) ;
628
661
}
629
- else if ( _keepAlive && _responseHeaders . HasConnection == false && HttpVersion == "HTTP/1.0" )
662
+ else if ( _keepAlive && _responseHeaders . HasConnection == false && _httpVersion == HttpVersionType . Http1_0 )
630
663
{
631
664
OutputAsciiBlock ( _bytesConnectionKeepAlive , memoryBlock , SocketOutput ) ;
632
665
}
@@ -849,5 +882,12 @@ private void ReportApplicationError(Exception ex)
849
882
_applicationException = ex ;
850
883
Log . ApplicationError ( ex ) ;
851
884
}
885
+
886
+ private enum HttpVersionType
887
+ {
888
+ Unknown = - 1 ,
889
+ Http1_0 = 0 ,
890
+ Http1_1 = 1
891
+ }
852
892
}
853
893
}
0 commit comments