7
7
// File name: HttpConnection.cs
8
8
// Repository: https://github.com/sisk-http/core
9
9
10
- using System . Buffers ;
11
10
using System . Net ;
12
11
using Sisk . Cadente . HttpSerializer ;
13
12
using Sisk . Cadente . Streams ;
@@ -27,7 +26,8 @@ sealed class HttpConnection : IDisposable {
27
26
public readonly int Id = 0 ;
28
27
#endif
29
28
30
- public const int REQUEST_BUFFER_SIZE = 8192 ; // buffer dedicated to headers. more than it? return 400.
29
+ // buffer dedicated to headers.
30
+ public const int REQUEST_BUFFER_SIZE = 8192 ;
31
31
public const int RESPONSE_BUFFER_SIZE = 4096 ;
32
32
33
33
public HttpConnection ( Stream connectionStream , HttpHost host , IPEndPoint endpoint ) {
@@ -39,78 +39,64 @@ public HttpConnection ( Stream connectionStream, HttpHost host, IPEndPoint endpo
39
39
public async Task < HttpConnectionState > HandleConnectionEvents ( ) {
40
40
bool connectionCloseRequested = false ;
41
41
42
- var requestBuffer = ArrayPool < byte > . Shared . Rent ( REQUEST_BUFFER_SIZE ) ;
42
+ while ( this . _connectionStream . CanRead && ! this . disposedValue ) {
43
43
44
- try {
44
+ HttpRequestReader requestReader = new HttpRequestReader ( this . _connectionStream ) ;
45
+ Stream ? responseStream = null ;
45
46
46
- while ( this . _connectionStream . CanRead && ! this . disposedValue ) {
47
+ try {
48
+ if ( requestReader . TryReadHttpRequest ( out HttpRequestBase ? nextRequest ) == false ) {
49
+ return HttpConnectionState . ConnectionClosed ;
50
+ }
47
51
48
- HttpRequestReader requestReader = new HttpRequestReader ( this . _connectionStream , ref requestBuffer ) ;
49
- Stream ? responseStream = null ;
52
+ HttpHostContext managedSession = new HttpHostContext ( nextRequest , this . _endpoint , this . _connectionStream ) ;
53
+ await this . _host . InvokeContextCreated ( managedSession ) ;
50
54
51
- try {
52
- var readRequestState = await requestReader . ReadHttpRequest ( ) ;
53
- var nextRequest = readRequestState . Item2 ;
55
+ if ( ! managedSession . KeepAlive || ! nextRequest . CanKeepAlive ) {
56
+ connectionCloseRequested = true ;
57
+ managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . Connection , "close" ) ) ;
58
+ }
54
59
55
- if ( nextRequest is null ) {
56
- return readRequestState . Item1 switch {
57
- HttpRequestReadState . StreamZero => HttpConnectionState . ConnectionClosedByStreamRead ,
58
- _ => HttpConnectionState . BadRequest
59
- } ;
60
- }
60
+ if ( managedSession . Response . ResponseStream is Stream { } s ) {
61
+ responseStream = s ;
62
+ }
63
+ else {
64
+ managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . ContentLength , "0" ) ) ;
65
+ }
61
66
62
- HttpHostContext managedSession = new HttpHostContext ( nextRequest , this . _endpoint , this . _connectionStream ) ;
63
- this . _host . InvokeContextCreated ( managedSession ) ;
67
+ Stream outputStream = this . _connectionStream ;
68
+ if ( responseStream is not null ) {
64
69
65
- if ( ! managedSession . KeepAlive || ! nextRequest . CanKeepAlive ) {
66
- connectionCloseRequested = true ;
67
- managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . Connection , "close" ) ) ;
68
- }
70
+ if ( managedSession . Response . SendChunked || ! responseStream . CanSeek ) {
69
71
70
- if ( managedSession . Response . ResponseStream is Stream { } s ) {
71
- responseStream = s ;
72
+ managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . TransferEncoding , "chunked" ) ) ;
73
+ responseStream = new HttpChunkedStream ( responseStream ) ;
72
74
}
73
75
else {
74
- managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . ContentLength , "0" ) ) ;
75
- }
76
-
77
- Stream outputStream = this . _connectionStream ;
78
- if ( responseStream is not null ) {
79
-
80
- if ( managedSession . Response . SendChunked || ! responseStream . CanSeek ) {
81
-
82
- managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . TransferEncoding , "chunked" ) ) ;
83
- responseStream = new HttpChunkedStream ( responseStream ) ;
84
- }
85
- else {
86
- managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . ContentLength , responseStream . Length . ToString ( ) ) ) ;
87
- }
76
+ managedSession . Response . Headers . Set ( new HttpHeader ( HttpHeaderName . ContentLength , responseStream . Length . ToString ( ) ) ) ;
88
77
}
78
+ }
89
79
90
- if ( managedSession . ResponseHeadersAlreadySent == false && ! managedSession . WriteHttpResponseHeaders ( ) ) {
91
- return HttpConnectionState . ResponseWriteException ;
92
- }
80
+ if ( managedSession . ResponseHeadersAlreadySent == false && ! managedSession . WriteHttpResponseHeaders ( ) ) {
81
+ return HttpConnectionState . ResponseWriteException ;
82
+ }
93
83
94
- if ( responseStream is not null ) {
95
- await responseStream . CopyToAsync ( outputStream ) ;
96
- }
84
+ if ( responseStream is not null ) {
85
+ await responseStream . CopyToAsync ( outputStream ) ;
86
+ }
97
87
98
- this . _connectionStream . Flush ( ) ;
88
+ await this . _connectionStream . FlushAsync ( ) ;
99
89
100
- if ( connectionCloseRequested ) {
101
- break ;
102
- }
103
- }
104
- finally {
105
- responseStream ? . Dispose ( ) ;
90
+ if ( connectionCloseRequested ) {
91
+ break ;
106
92
}
107
93
}
108
-
109
- return HttpConnectionState . ConnectionClosed ;
110
- }
111
- finally {
112
- ArrayPool < byte > . Shared . Return ( requestBuffer ) ;
94
+ finally {
95
+ responseStream ? . Dispose ( ) ;
96
+ }
113
97
}
98
+
99
+ return HttpConnectionState . ConnectionClosed ;
114
100
}
115
101
116
102
private void Dispose ( bool disposing ) {
0 commit comments