2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Buffers . Binary ;
5
6
using System . Buffers . Text ;
6
7
using System . IO . Pipelines ;
7
8
using System . Threading . Tasks ;
@@ -51,6 +52,8 @@ public static class Paths
51
52
public readonly static AsciiString Plaintext = "/p" ;
52
53
public readonly static AsciiString Updates = "/updates/queries=" ;
53
54
public readonly static AsciiString MultipleQueries = "/queries/queries=" ;
55
+ public const ushort JsonPath = 0x6a2f ;
56
+ public const ushort PlaintextPath = 0x702f ;
54
57
}
55
58
56
59
private RequestType _requestType ;
@@ -59,45 +62,55 @@ public static class Paths
59
62
#endif
60
63
public void OnStartLine ( HttpMethod method , HttpVersion version , Span < byte > target , Span < byte > path , Span < byte > query , Span < byte > customMethod , bool pathEncoded )
61
64
{
62
- var requestType = RequestType . NotRecognized ;
63
- if ( method == HttpMethod . Get )
64
- {
65
+
65
66
#if ! DATABASE
66
- if ( path . Length >= 2 && path [ 0 ] == '/' )
67
+ if ( method == HttpMethod . Get && BinaryPrimitives . TryReadUInt16LittleEndian ( path , out var value ) )
68
+ {
69
+ if ( value == Paths . PlaintextPath )
67
70
{
68
- if ( path [ 1 ] == 'j' )
69
- {
70
- requestType = RequestType . Json ;
71
- }
72
- else if ( path [ 1 ] == 'p' )
73
- {
74
- requestType = RequestType . PlainText ;
75
- }
71
+ _requestType = RequestType . PlainText ;
72
+ return ;
76
73
}
74
+ else if ( value == Paths . JsonPath )
75
+ {
76
+ _requestType = RequestType . Json ;
77
+ return ;
78
+ }
79
+ }
80
+
81
+ _requestType = RequestType . NotRecognized ;
77
82
#else
83
+ if ( method == HttpMethod . Get )
84
+ {
78
85
var pathLength = path . Length ;
79
86
if ( Paths . SingleQuery . Length <= pathLength && path . StartsWith ( Paths . SingleQuery ) )
80
87
{
81
- requestType = RequestType . SingleQuery ;
88
+ _requestType = RequestType . SingleQuery ;
82
89
}
83
90
else if ( Paths . Fortunes . Length <= pathLength && path . StartsWith ( Paths . Fortunes ) )
84
91
{
85
- requestType = RequestType . Fortunes ;
92
+ _requestType = RequestType . Fortunes ;
86
93
}
87
94
else if ( Paths . Updates . Length <= pathLength && path . StartsWith ( Paths . Updates ) )
88
95
{
89
96
_queries = ParseQueries ( path , Paths . Updates . Length ) ;
90
- requestType = RequestType . Updates ;
97
+ _requestType = RequestType . Updates ;
91
98
}
92
99
else if ( Paths . MultipleQueries . Length <= pathLength && path . StartsWith ( Paths . MultipleQueries ) )
93
100
{
94
101
_queries = ParseQueries ( path , Paths . MultipleQueries . Length ) ;
95
- requestType = RequestType . MultipleQueries ;
102
+ _requestType = RequestType . MultipleQueries ;
103
+ }
104
+ else
105
+ {
106
+ _requestType = RequestType . NotRecognized ;
96
107
}
97
- #endif
98
108
}
99
-
100
- _requestType = requestType ;
109
+ else
110
+ {
111
+ _requestType = RequestType . NotRecognized ;
112
+ }
113
+ #endif
101
114
}
102
115
103
116
0 commit comments