@@ -15,38 +15,32 @@ public Task ProcessRequestAsync(IFeatureCollection features)
15
15
16
16
if ( req . Method != "GET" )
17
17
{
18
- res . StatusCode = 405 ;
19
- var body = features . GetResponseBodyFeature ( ) ;
20
- return body . StartAsync ( ) . ContinueWith ( t => body . CompleteAsync ( ) ) ;
18
+ res . StatusCode = StatusCodes . Status405MethodNotAllowed ;
21
19
}
22
20
23
21
return req . Path switch
24
22
{
25
- "/plaintext" => Plaintext ( req , res , features ) ,
26
- "/json" => Json ( req , res , features ) ,
27
- "/" => Index ( req , res , features ) ,
28
- _ => NotFound ( req , res , features ) ,
23
+ "/plaintext" => Plaintext ( res , features ) ,
24
+ "/json" => Json ( res , features ) ,
25
+ "/json-chunked" => JsonChunked ( res , features ) ,
26
+ "/" => Index ( res , features ) ,
27
+ _ => NotFound ( res , features ) ,
29
28
} ;
30
29
}
31
30
32
- private static async Task NotFound ( IHttpRequestFeature req , IHttpResponseFeature res , IFeatureCollection features )
31
+ private static Task NotFound ( IHttpResponseFeature res , IFeatureCollection features )
33
32
{
34
- res . StatusCode = 404 ;
35
- res . Headers . ContentType = "text/plain" ;
36
-
37
- var body = features . GetResponseBodyFeature ( ) ;
38
-
39
- await body . StartAsync ( ) ;
40
- await body . CompleteAsync ( ) ;
33
+ res . StatusCode = StatusCodes . Status404NotFound ;
34
+ return Task . CompletedTask ;
41
35
}
42
36
43
37
public void DisposeContext ( IFeatureCollection features , Exception ? exception ) { }
44
38
45
39
private static ReadOnlySpan < byte > IndexPayload => "Running directly on Kestrel! Navigate to /plaintext and /json to see other endpoints."u8 ;
46
40
47
- private static async Task Index ( IHttpRequestFeature req , IHttpResponseFeature res , IFeatureCollection features )
41
+ private static async Task Index ( IHttpResponseFeature res , IFeatureCollection features )
48
42
{
49
- res . StatusCode = 200 ;
43
+ res . StatusCode = StatusCodes . Status200OK ;
50
44
res . Headers . ContentType = "text/plain" ;
51
45
res . Headers . ContentLength = IndexPayload . Length ;
52
46
@@ -59,9 +53,9 @@ private static async Task Index(IHttpRequestFeature req, IHttpResponseFeature re
59
53
60
54
private static ReadOnlySpan < byte > HelloWorldPayload => "Hello, World!"u8 ;
61
55
62
- private static async Task Plaintext ( IHttpRequestFeature req , IHttpResponseFeature res , IFeatureCollection features )
56
+ private static async Task Plaintext ( IHttpResponseFeature res , IFeatureCollection features )
63
57
{
64
- res . StatusCode = 200 ;
58
+ res . StatusCode = StatusCodes . Status200OK ;
65
59
res . Headers . ContentType = "text/plain" ;
66
60
res . Headers . ContentLength = HelloWorldPayload . Length ;
67
61
@@ -72,13 +66,25 @@ private static async Task Plaintext(IHttpRequestFeature req, IHttpResponseFeatur
72
66
}
73
67
74
68
private static readonly JsonSerializerOptions _jsonSerializerOptions = new ( JsonSerializerDefaults . Web ) ;
69
+
70
+ private static async Task JsonChunked ( IHttpResponseFeature res , IFeatureCollection features )
71
+ {
72
+ res . StatusCode = StatusCodes . Status200OK ;
73
+ res . Headers . ContentType = "application/json; charset=utf-8" ;
74
+
75
+ var body = features . GetResponseBodyFeature ( ) ;
76
+ await body . StartAsync ( ) ;
77
+ await JsonSerializer . SerializeAsync ( body . Writer , new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
78
+ await body . Writer . FlushAsync ( ) ;
79
+ }
80
+
75
81
private static readonly ObjectPoolProvider _objectPoolProvider = new DefaultObjectPoolProvider ( ) ;
76
82
private static readonly ObjectPool < ArrayBufferWriter < byte > > _bufferWriterPool = _objectPoolProvider . Create < ArrayBufferWriter < byte > > ( ) ;
77
83
private static readonly ObjectPool < Utf8JsonWriter > _jsonWriterPool = _objectPoolProvider . Create ( new Utf8JsonWriterPooledObjectPolicy ( ) ) ;
78
84
79
- private static async Task Json ( IHttpRequestFeature req , IHttpResponseFeature res , IFeatureCollection features )
85
+ private static async Task Json ( IHttpResponseFeature res , IFeatureCollection features )
80
86
{
81
- res . StatusCode = 200 ;
87
+ res . StatusCode = StatusCodes . Status200OK ;
82
88
res . Headers . ContentType = "application/json; charset=utf-8" ;
83
89
84
90
var bufferWriter = _bufferWriterPool . Get ( ) ;
0 commit comments