|
1 | 1 | using System.Buffers; |
| 2 | +using System.Runtime.CompilerServices; |
2 | 3 | using System.Text; |
3 | 4 | using System.Text.Json; |
4 | 5 | using System.Text.Json.Serialization; |
@@ -35,35 +36,24 @@ public Task ProcessRequestAsync(IFeatureCollection features) |
35 | 36 | { |
36 | 37 | return Json(res, features); |
37 | 38 | } |
38 | | - else if (pathSpan.StartsWith("json-string", StringComparison.OrdinalIgnoreCase)) |
| 39 | + else if (Paths.IsPath(pathSpan, Paths.JsonString)) |
39 | 40 | { |
40 | 41 | return JsonString(res, features); |
41 | 42 | } |
42 | | - else if (pathSpan.StartsWith("json-utf8bytes", StringComparison.OrdinalIgnoreCase)) |
| 43 | + else if (Paths.IsPath(pathSpan, Paths.JsonUtf8Bytes)) |
43 | 44 | { |
44 | 45 | return JsonUtf8Bytes(res, features); |
45 | 46 | } |
46 | | - else if (pathSpan.StartsWith("json-chunked", StringComparison.OrdinalIgnoreCase)) |
| 47 | + else if (Paths.IsPath(pathSpan, Paths.JsonChunked)) |
47 | 48 | { |
48 | 49 | return JsonChunked(res, features); |
49 | 50 | } |
50 | | - else if (pathSpan.IsEmpty || pathSpan.Equals("/", StringComparison.OrdinalIgnoreCase)) |
| 51 | + else if (pathSpan.IsEmpty || Paths.IsPath(pathSpan, Paths.Index)) |
51 | 52 | { |
52 | 53 | return Index(res, features); |
53 | 54 | } |
54 | 55 |
|
55 | 56 | return NotFound(res, features); |
56 | | - |
57 | | - //return req.Path switch |
58 | | - //{ |
59 | | - // "/plaintext" => Plaintext(res, features), |
60 | | - // "/json" => Json(res, features), |
61 | | - // "/json-string" => JsonString(res, features), |
62 | | - // "/json-utf8bytes" => JsonUtf8Bytes(res, features), |
63 | | - // "/json-chunked" => JsonChunked(res, features), |
64 | | - // "/" => Index(res, features), |
65 | | - // _ => NotFound(res, features), |
66 | | - //}; |
67 | 57 | } |
68 | 58 |
|
69 | 59 | private static Task NotFound(IHttpResponseFeature res, IFeatureCollection features) |
@@ -160,8 +150,8 @@ private static async Task Json(IHttpResponseFeature res, IFeatureCollection feat |
160 | 150 |
|
161 | 151 | body.Writer.Write(messageSpan); |
162 | 152 |
|
163 | | - await body.StartAsync(); |
164 | | - await body.Writer.FlushAsync(); |
| 153 | + //await body.StartAsync(); |
| 154 | + //await body.Writer.FlushAsync(); |
165 | 155 | } |
166 | 156 |
|
167 | 157 | [ThreadStatic] |
@@ -201,7 +191,12 @@ private static class Paths |
201 | 191 | { |
202 | 192 | public static ReadOnlySpan<char> Plaintext => "/plaintext"; |
203 | 193 | public static ReadOnlySpan<char> Json => "/json"; |
| 194 | + public static ReadOnlySpan<char> JsonString => "/json-string"; |
| 195 | + public static ReadOnlySpan<char> JsonChunked => "/json-chunked"; |
| 196 | + public static ReadOnlySpan<char> JsonUtf8Bytes => "/json-utf8bytes"; |
| 197 | + public static ReadOnlySpan<char> Index => "/"; |
204 | 198 |
|
| 199 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
205 | 200 | public static bool IsPath(ReadOnlySpan<char> path, ReadOnlySpan<char> targetPath) => path.SequenceEqual(targetPath); |
206 | 201 | } |
207 | 202 | } |
0 commit comments