@@ -21,7 +21,7 @@ public class InputHandler : IInputHandler
2121 public static char [ ] HeaderKeys = { CR , LF , ':' } ;
2222 public const short MinBuffer = 21 ; // Minimum size of the buffer "Content-Length: X\r\n\r\n"
2323
24- private readonly TextReader _input ;
24+ private readonly Stream _input ;
2525 private readonly IOutputHandler _outputHandler ;
2626 private readonly IReciever _reciever ;
2727 private readonly IRequestProcessIdentifier _requestProcessIdentifier ;
@@ -32,14 +32,15 @@ public class InputHandler : IInputHandler
3232 private Thread _queueThread ;
3333
3434 public InputHandler (
35- TextReader input ,
35+ Stream input ,
3636 IOutputHandler outputHandler ,
3737 IReciever reciever ,
3838 IRequestProcessIdentifier requestProcessIdentifier ,
3939 IRequestRouter requestRouter ,
4040 IResponseRouter responseRouter
4141 )
4242 {
43+ if ( ! input . CanRead ) throw new ArgumentException ( $ "must provide a readable stream for { nameof ( input ) } ", nameof ( input ) ) ;
4344 _input = input ;
4445 _outputHandler = outputHandler ;
4546 _reciever = reciever ;
@@ -54,7 +55,7 @@ IResponseRouter responseRouter
5455 }
5556
5657 internal InputHandler (
57- TextReader input ,
58+ Stream input ,
5859 IOutputHandler outputHandler ,
5960 IReciever reciever ,
6061 IRequestProcessIdentifier requestProcessIdentifier ,
@@ -75,19 +76,23 @@ public void Start()
7576
7677 private async void ProcessInputStream ( )
7778 {
79+ // header is encoded in ASCII
80+ // "Content-Length: 0" counts bytes for the following content
81+ // content is encoded in UTF-8
7882 while ( true )
7983 {
8084 if ( _inputThread == null ) return ;
8185
82- var buffer = new char [ 300 ] ;
83- var current = await _input . ReadBlockAsync ( buffer , 0 , MinBuffer ) ;
84- while ( current < MinBuffer || buffer [ current - 4 ] != CR || buffer [ current - 3 ] != LF ||
86+ var buffer = new byte [ 300 ] ;
87+ var current = await _input . ReadAsync ( buffer , 0 , MinBuffer ) ;
88+ while ( current < MinBuffer ||
89+ buffer [ current - 4 ] != CR || buffer [ current - 3 ] != LF ||
8590 buffer [ current - 2 ] != CR || buffer [ current - 1 ] != LF )
8691 {
87- current += await _input . ReadBlockAsync ( buffer , current , 1 ) ;
92+ current += await _input . ReadAsync ( buffer , current , 1 ) ;
8893 }
8994
90- var headersContent = new string ( buffer , 0 , current ) ;
95+ var headersContent = System . Text . Encoding . ASCII . GetString ( buffer , 0 , current ) ;
9196 var headers = headersContent . Split ( HeaderKeys , StringSplitOptions . RemoveEmptyEntries ) ;
9297 long length = 0 ;
9398 for ( var i = 0 ; i < headers . Length ; i += 2 )
@@ -100,11 +105,11 @@ private async void ProcessInputStream()
100105 }
101106 }
102107
103- var requestBuffer = new char [ length ] ;
108+ var requestBuffer = new byte [ length ] ;
104109
105- await _input . ReadBlockAsync ( requestBuffer , 0 , requestBuffer . Length ) ;
110+ await _input . ReadAsync ( requestBuffer , 0 , requestBuffer . Length ) ;
106111
107- var payload = new string ( requestBuffer ) ;
112+ var payload = System . Text . Encoding . UTF8 . GetString ( requestBuffer ) ;
108113
109114 HandleRequest ( payload ) ;
110115 }
0 commit comments