@@ -30,14 +30,19 @@ public static void Main(string[] args)
3030 engine . Run ( ) ;
3131 }
3232
33- private static ValueTask RequestHandler ( Connection connection )
33+ private static unsafe ValueTask RequestHandler ( Connection connection )
3434 {
35- /*if(connection.HashedRoute == 291830056) // /json
35+ var route = connection . BinaryH1HeaderData . Route . AsSpan ( ) ;
36+ if ( route [ 1 ] == ( byte ) 'j' )
37+ {
3638 CommitJsonResponse ( connection ) ;
39+ }
40+ else
41+ {
42+ CommitPlainTextResponse ( connection ) ;
43+ }
3744
38- else if (connection.HashedRoute == 3454831873) // /plaintext
39- CommitPlainTextResponse(connection);*/
40-
45+ /*
4146 if (connection.H1HeaderData.Route.Equals("/json"))
4247 {
4348 CommitJsonResponse(connection);
@@ -47,38 +52,60 @@ private static ValueTask RequestHandler(Connection connection)
4752 {
4853 CommitPlainTextResponse(connection);
4954 }
55+ */
5056
5157 return ValueTask . CompletedTask ;
5258 }
5359
54- [ ThreadStatic ] private static Utf8JsonWriter ? _tUtf8JsonWriter ;
60+ [ ThreadStatic ] private static Utf8JsonWriter ? t_utf8JsonWriter ;
5561 private static readonly JsonContext SerializerContext = JsonContext . Default ;
56- private static void CommitJsonResponse ( Connection connection )
62+ private static unsafe void CommitJsonResponse ( Connection connection )
5763 {
64+ var tail = connection . WriteBuffer . Tail ;
5865 connection . WriteBuffer . WriteUnmanaged ( "HTTP/1.1 200 OK\r \n "u8 +
59- "Server: W \r \n "u8 +
60- "Content-Type: application/json; charset=UTF-8 \r \n "u8 +
61- "Content-Length: 27 \r \n "u8 ) ;
66+ "Content-Length: \r \n "u8 +
67+ "Server: U \r \n "u8 +
68+ "Content-Type: application/json; charset=UTF-8 \r \n "u8 ) ;
6269 connection . WriteBuffer . WriteUnmanaged ( DateHelper . HeaderBytes ) ;
6370
64- _tUtf8JsonWriter ??= new Utf8JsonWriter ( connection . WriteBuffer , new JsonWriterOptions { SkipValidation = true } ) ;
65- _tUtf8JsonWriter . Reset ( connection . WriteBuffer ) ;
71+ t_utf8JsonWriter ??= new Utf8JsonWriter ( connection . WriteBuffer , new JsonWriterOptions { SkipValidation = true } ) ;
72+ t_utf8JsonWriter . Reset ( connection . WriteBuffer ) ;
6673
6774 // Creating(Allocating) a new JsonMessage every request
6875 var message = new JsonMessage { Message = "Hello, World!" } ;
6976 // Serializing it every request
70- JsonSerializer . Serialize ( _tUtf8JsonWriter , message , SerializerContext . JsonMessage ) ;
77+ JsonSerializer . Serialize ( t_utf8JsonWriter , message , SerializerContext . JsonMessage ) ;
78+
79+ var contentLength = ( int ) t_utf8JsonWriter . BytesCommitted ;
80+
81+ byte * dst = connection . WriteBuffer . Ptr + tail + 33 ;
82+ int tens = contentLength / 10 ;
83+ int ones = contentLength - tens * 10 ;
84+
85+ dst [ 0 ] = ( byte ) ( '0' + tens ) ;
86+ dst [ 1 ] = ( byte ) ( '0' + ones ) ;
87+
7188 }
7289
73- private static void CommitPlainTextResponse ( Connection connection )
90+ private static ReadOnlySpan < byte > s_plainTextBody => "Hello, World!"u8 ;
91+
92+ private static unsafe void CommitPlainTextResponse ( Connection connection )
7493 {
94+ var tail = connection . WriteBuffer . Tail ;
95+ var contentLength = s_plainTextBody . Length ;
7596 connection . WriteBuffer . WriteUnmanaged ( "HTTP/1.1 200 OK\r \n "u8 +
76- "Server: W\r \n "u8 +
77- "Content-Type: text/plain\r \n "u8 +
78- //"Content-Length: 13\r\n\r\nHello, World!"u8);
79- "Content-Length: 13\r \n "u8 ) ;
97+ "Content-Length: \r \n "u8 +
98+ "Server: U\r \n "u8 +
99+ "Content-Type: text/plain\r \n "u8 ) ;
80100 connection . WriteBuffer . WriteUnmanaged ( DateHelper . HeaderBytes ) ;
81- connection . WriteBuffer . WriteUnmanaged ( "Hello, World!"u8 ) ;
101+ connection . WriteBuffer . WriteUnmanaged ( s_plainTextBody ) ;
102+
103+ byte * dst = connection . WriteBuffer . Ptr + tail + 33 ;
104+ int tens = contentLength / 10 ;
105+ int ones = contentLength - tens * 10 ;
106+
107+ dst [ 0 ] = ( byte ) ( '0' + tens ) ;
108+ dst [ 1 ] = ( byte ) ( '0' + ones ) ;
82109 }
83110}
84111
0 commit comments