7
7
using System . Linq ;
8
8
using System . Net ;
9
9
using System . Numerics ;
10
- using System . Text ;
11
10
using System . Threading ;
12
11
using System . Threading . Tasks ;
13
12
using Microsoft . AspNet . Http ;
@@ -22,24 +21,6 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
22
21
{
23
22
public abstract partial class Frame : FrameContext , IFrameControl
24
23
{
25
- private static readonly Encoding _ascii = Encoding . ASCII ;
26
- private static readonly ArraySegment < byte > _endChunkBytes = CreateAsciiByteArraySegment ( "\r \n " ) ;
27
- private static readonly ArraySegment < byte > _endChunkedResponseBytes = CreateAsciiByteArraySegment ( "0\r \n \r \n " ) ;
28
- private static readonly ArraySegment < byte > _continueBytes = CreateAsciiByteArraySegment ( "HTTP/1.1 100 Continue\r \n \r \n " ) ;
29
- private static readonly ArraySegment < byte > _emptyData = new ArraySegment < byte > ( new byte [ 0 ] ) ;
30
- private static readonly byte [ ] _hex = Encoding . ASCII . GetBytes ( "0123456789abcdef" ) ;
31
-
32
- private static readonly byte [ ] _bytesConnectionClose = Encoding . ASCII . GetBytes ( "\r \n Connection: close" ) ;
33
- private static readonly byte [ ] _bytesConnectionKeepAlive = Encoding . ASCII . GetBytes ( "\r \n Connection: keep-alive" ) ;
34
- private static readonly byte [ ] _bytesTransferEncodingChunked = Encoding . ASCII . GetBytes ( "\r \n Transfer-Encoding: chunked" ) ;
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 " ) ;
37
- private static readonly byte [ ] _bytesContentLengthZero = Encoding . ASCII . GetBytes ( "\r \n Content-Length: 0" ) ;
38
- private static readonly byte [ ] _bytesSpace = Encoding . ASCII . GetBytes ( " " ) ;
39
- private static readonly byte [ ] _bytesServer = Encoding . ASCII . GetBytes ( "\r \n Server: Kestrel" ) ;
40
- private static readonly byte [ ] _bytesDate = Encoding . ASCII . GetBytes ( "Date: " ) ;
41
- private static readonly byte [ ] _bytesEndHeaders = Encoding . ASCII . GetBytes ( "\r \n \r \n " ) ;
42
-
43
24
private static Vector < byte > _vectorCRs = new Vector < byte > ( ( byte ) '\r ' ) ;
44
25
private static Vector < byte > _vectorColons = new Vector < byte > ( ( byte ) ':' ) ;
45
26
private static Vector < byte > _vectorSpaces = new Vector < byte > ( ( byte ) ' ' ) ;
@@ -258,7 +239,7 @@ public void ResetResponseHeaders()
258
239
DateHeaderValueManager . GetDateHeaderValueBytes ( ) ) ;
259
240
_responseHeaders . SetRawServer (
260
241
"Kestrel" ,
261
- _bytesServer ) ;
242
+ Constants . HeaderBytesServer ) ;
262
243
}
263
244
264
245
/// <summary>
@@ -404,13 +385,13 @@ protected async Task FireOnCompleted()
404
385
public void Flush ( )
405
386
{
406
387
ProduceStartAndFireOnStarting ( immediate : false ) . GetAwaiter ( ) . GetResult ( ) ;
407
- SocketOutput . Write ( _emptyData , immediate : true ) ;
388
+ SocketOutput . Write ( new ArraySegment < byte > ( Constants . HeaderEmptyDataBytes ) , immediate : true ) ;
408
389
}
409
390
410
391
public async Task FlushAsync ( CancellationToken cancellationToken )
411
392
{
412
393
await ProduceStartAndFireOnStarting ( immediate : false ) ;
413
- await SocketOutput . WriteAsync ( _emptyData , immediate : true , cancellationToken : cancellationToken ) ;
394
+ await SocketOutput . WriteAsync ( new ArraySegment < byte > ( Constants . HeaderEmptyDataBytes ) , immediate : true , cancellationToken : cancellationToken ) ;
414
395
}
415
396
416
397
public void Write ( ArraySegment < byte > data )
@@ -474,28 +455,28 @@ private void WriteChunked(ArraySegment<byte> data)
474
455
{
475
456
SocketOutput . Write ( BeginChunkBytes ( data . Count ) , immediate : false ) ;
476
457
SocketOutput . Write ( data , immediate : false ) ;
477
- SocketOutput . Write ( _endChunkBytes , immediate : true ) ;
458
+ SocketOutput . Write ( new ArraySegment < byte > ( Constants . HeaderEndChunkBytes ) , immediate : true ) ;
478
459
}
479
460
480
461
private async Task WriteChunkedAsync ( ArraySegment < byte > data , CancellationToken cancellationToken )
481
462
{
482
463
await SocketOutput . WriteAsync ( BeginChunkBytes ( data . Count ) , immediate : false , cancellationToken : cancellationToken ) ;
483
464
await SocketOutput . WriteAsync ( data , immediate : false , cancellationToken : cancellationToken ) ;
484
- await SocketOutput . WriteAsync ( _endChunkBytes , immediate : true , cancellationToken : cancellationToken ) ;
465
+ await SocketOutput . WriteAsync ( new ArraySegment < byte > ( Constants . HeaderEndChunkBytes ) , immediate : true , cancellationToken : cancellationToken ) ;
485
466
}
486
467
487
468
public static ArraySegment < byte > BeginChunkBytes ( int dataCount )
488
469
{
489
470
var bytes = new byte [ 10 ]
490
471
{
491
- _hex [ ( ( dataCount >> 0x1c ) & 0x0f ) ] ,
492
- _hex [ ( ( dataCount >> 0x18 ) & 0x0f ) ] ,
493
- _hex [ ( ( dataCount >> 0x14 ) & 0x0f ) ] ,
494
- _hex [ ( ( dataCount >> 0x10 ) & 0x0f ) ] ,
495
- _hex [ ( ( dataCount >> 0x0c ) & 0x0f ) ] ,
496
- _hex [ ( ( dataCount >> 0x08 ) & 0x0f ) ] ,
497
- _hex [ ( ( dataCount >> 0x04 ) & 0x0f ) ] ,
498
- _hex [ ( ( dataCount >> 0x00 ) & 0x0f ) ] ,
472
+ Constants . HexBytes [ ( ( dataCount >> 0x1c ) & 0x0f ) ] ,
473
+ Constants . HexBytes [ ( ( dataCount >> 0x18 ) & 0x0f ) ] ,
474
+ Constants . HexBytes [ ( ( dataCount >> 0x14 ) & 0x0f ) ] ,
475
+ Constants . HexBytes [ ( ( dataCount >> 0x10 ) & 0x0f ) ] ,
476
+ Constants . HexBytes [ ( ( dataCount >> 0x0c ) & 0x0f ) ] ,
477
+ Constants . HexBytes [ ( ( dataCount >> 0x08 ) & 0x0f ) ] ,
478
+ Constants . HexBytes [ ( ( dataCount >> 0x04 ) & 0x0f ) ] ,
479
+ Constants . HexBytes [ ( ( dataCount >> 0x00 ) & 0x0f ) ] ,
499
480
( byte ) '\r ' ,
500
481
( byte ) '\n ' ,
501
482
} ;
@@ -515,13 +496,7 @@ public static ArraySegment<byte> BeginChunkBytes(int dataCount)
515
496
516
497
private void WriteChunkedResponseSuffix ( )
517
498
{
518
- SocketOutput . Write ( _endChunkedResponseBytes , immediate : true ) ;
519
- }
520
-
521
- private static ArraySegment < byte > CreateAsciiByteArraySegment ( string text )
522
- {
523
- var bytes = Encoding . ASCII . GetBytes ( text ) ;
524
- return new ArraySegment < byte > ( bytes ) ;
499
+ SocketOutput . Write ( new ArraySegment < byte > ( Constants . HeaderEndChunkedResponseBytes ) , immediate : true ) ;
525
500
}
526
501
527
502
public void ProduceContinue ( )
@@ -533,7 +508,7 @@ public void ProduceContinue()
533
508
RequestHeaders . TryGetValue ( "Expect" , out expect ) &&
534
509
( expect . FirstOrDefault ( ) ?? "" ) . Equals ( "100-continue" , StringComparison . OrdinalIgnoreCase ) )
535
510
{
536
- SocketOutput . Write ( _continueBytes ) ;
511
+ SocketOutput . Write ( new ArraySegment < byte > ( Constants . HeaderContinueBytes ) ) ;
537
512
}
538
513
}
539
514
@@ -596,7 +571,7 @@ protected Task ProduceEnd()
596
571
ReasonPhrase = null ;
597
572
598
573
ResetResponseHeaders ( ) ;
599
- _responseHeaders . SetRawContentLength ( "0" , _bytesContentLengthZero ) ;
574
+ _responseHeaders . SetRawContentLength ( "0" , Constants . HeaderBytesContentLengthZero ) ;
600
575
}
601
576
}
602
577
@@ -660,15 +635,15 @@ private Task CreateResponseHeader(
660
635
{
661
636
// Since the app has completed and we are only now generating
662
637
// the headers we can safely set the Content-Length to 0.
663
- _responseHeaders . SetRawContentLength ( "0" , _bytesContentLengthZero ) ;
638
+ _responseHeaders . SetRawContentLength ( "0" , Constants . HeaderBytesContentLengthZero ) ;
664
639
}
665
640
}
666
641
else
667
642
{
668
643
if ( _httpVersion == HttpVersionType . Http1_1 )
669
644
{
670
645
_autoChunk = true ;
671
- _responseHeaders . SetRawTransferEncoding ( "chunked" , _bytesTransferEncodingChunked ) ;
646
+ _responseHeaders . SetRawTransferEncoding ( "chunked" , Constants . HeaderBytesTransferEncodingChunked ) ;
672
647
}
673
648
else
674
649
{
@@ -679,17 +654,17 @@ private Task CreateResponseHeader(
679
654
680
655
if ( _keepAlive == false && _responseHeaders . HasConnection == false && _httpVersion == HttpVersionType . Http1_1 )
681
656
{
682
- _responseHeaders . SetRawConnection ( "close" , _bytesConnectionClose ) ;
657
+ _responseHeaders . SetRawConnection ( "close" , Constants . HeaderBytesConnectionClose ) ;
683
658
}
684
659
else if ( _keepAlive && _responseHeaders . HasConnection == false && _httpVersion == HttpVersionType . Http1_0 )
685
660
{
686
- _responseHeaders . SetRawConnection ( "keep-alive" , _bytesConnectionKeepAlive ) ;
661
+ _responseHeaders . SetRawConnection ( "keep-alive" , Constants . HeaderBytesConnectionKeepAlive ) ;
687
662
}
688
663
689
- end . CopyFrom ( _httpVersion == HttpVersionType . Http1_1 ? _bytesHttpVersion1_1 : _bytesHttpVersion1_0 ) ;
664
+ end . CopyFrom ( _httpVersion == HttpVersionType . Http1_1 ? Constants . HeaderBytesHttpVersion1_1 : Constants . HeaderBytesHttpVersion1_0 ) ;
690
665
end . CopyFrom ( statusBytes ) ;
691
666
_responseHeaders . CopyTo ( ref end ) ;
692
- end . CopyFrom ( _bytesEndHeaders , 0 , _bytesEndHeaders . Length ) ;
667
+ end . CopyFrom ( Constants . HeaderBytesEndHeaders , 0 , Constants . HeaderBytesEndHeaders . Length ) ;
693
668
694
669
SocketOutput . ProducingComplete ( end ) ;
695
670
0 commit comments