@@ -16,7 +16,7 @@ public class InputHandler : IInputHandler
1616 public static char [ ] HeaderKeys = { CR , LF , ':' } ;
1717 public const short MinBuffer = 21 ; // Minimum size of the buffer "Content-Length: X\r\n\r\n"
1818
19- private readonly TextReader _input ;
19+ private readonly Stream _input ;
2020 private readonly IOutputHandler _outputHandler ;
2121 private readonly IReciever _reciever ;
2222 private readonly IRequestProcessIdentifier _requestProcessIdentifier ;
@@ -26,14 +26,15 @@ public class InputHandler : IInputHandler
2626 private readonly IScheduler _scheduler ;
2727
2828 public InputHandler (
29- TextReader input ,
29+ Stream input ,
3030 IOutputHandler outputHandler ,
3131 IReciever reciever ,
3232 IRequestProcessIdentifier requestProcessIdentifier ,
3333 IRequestRouter requestRouter ,
3434 IResponseRouter responseRouter
3535 )
3636 {
37+ if ( ! input . CanRead ) throw new ArgumentException ( $ "must provide a readable stream for { nameof ( input ) } ", nameof ( input ) ) ;
3738 _input = input ;
3839 _outputHandler = outputHandler ;
3940 _reciever = reciever ;
@@ -54,23 +55,26 @@ public void Start()
5455
5556 private async void ProcessInputStream ( )
5657 {
58+ // header is encoded in ASCII
59+ // "Content-Length: 0" counts bytes for the following content
60+ // content is encoded in UTF-8
5761 while ( true )
5862 {
5963 if ( _inputThread == null ) return ;
6064
61- var buffer = new char [ 300 ] ;
62- var current = await _input . ReadBlockAsync ( buffer , 0 , MinBuffer ) ;
65+ var buffer = new byte [ 300 ] ;
66+ var current = await _input . ReadAsync ( buffer , 0 , MinBuffer ) ;
6367 if ( current == 0 ) return ; // no more _input
64-
65- while ( current < MinBuffer || buffer [ current - 4 ] != CR || buffer [ current - 3 ] != LF ||
68+ while ( current < MinBuffer ||
69+ buffer [ current - 4 ] != CR || buffer [ current - 3 ] != LF ||
6670 buffer [ current - 2 ] != CR || buffer [ current - 1 ] != LF )
6771 {
68- var n = await _input . ReadBlockAsync ( buffer , current , 1 ) ;
72+ var n = await _input . ReadAsync ( buffer , current , 1 ) ;
6973 if ( n == 0 ) return ; // no more _input, mitigates endless loop here.
7074 current += n ;
7175 }
7276
73- var headersContent = new string ( buffer , 0 , current ) ;
77+ var headersContent = System . Text . Encoding . ASCII . GetString ( buffer , 0 , current ) ;
7478 var headers = headersContent . Split ( HeaderKeys , StringSplitOptions . RemoveEmptyEntries ) ;
7579 long length = 0 ;
7680 for ( var i = 1 ; i < headers . Length ; i += 2 )
@@ -91,15 +95,16 @@ private async void ProcessInputStream()
9195 }
9296 else
9397 {
94- var requestBuffer = new char [ length ] ;
98+ var requestBuffer = new byte [ length ] ;
9599 var received = 0 ;
96100 while ( received < length )
97101 {
98- var n = await _input . ReadBlockAsync ( requestBuffer , received , requestBuffer . Length - received ) ;
102+ var n = await _input . ReadAsync ( requestBuffer , received , requestBuffer . Length - received ) ;
99103 if ( n == 0 ) return ; // no more _input
100104 received += n ;
101105 }
102- var payload = new string ( requestBuffer ) ;
106+ // TODO sometimes: encoding should be based on the respective header (including the wrong "utf8" value)
107+ var payload = System . Text . Encoding . UTF8 . GetString ( requestBuffer ) ;
103108 HandleRequest ( payload ) ;
104109 }
105110 }
0 commit comments