@@ -73,62 +73,51 @@ public unsafe bool ParseRequestLine(TRequestHandler handler, in ReadOnlySequence
73
73
private unsafe void ParseRequestLine ( TRequestHandler handler , byte * data , int length )
74
74
{
75
75
// Get Method and set the offset
76
- var method = HttpUtilities . GetKnownMethod ( data , length , out var offset ) ;
76
+ var method = HttpUtilities . GetKnownMethod ( data , length , out var pathStartOffset ) ;
77
77
78
- Span < byte > customMethod = method == HttpMethod . Custom ?
79
- GetUnknownMethod ( data , length , out offset ) :
80
- default ;
78
+ Span < byte > customMethod = default ;
79
+ if ( method == HttpMethod . Custom )
80
+ {
81
+ customMethod = GetUnknownMethod ( data , length , out pathStartOffset ) ;
82
+ }
81
83
84
+ // Use a new offset var as pathStartOffset needs to be on stack
85
+ // as its passed by reference above so can't be in register.
82
86
// Skip space
83
- offset ++ ;
87
+ var offset = pathStartOffset + 1 ;
88
+ if ( offset >= length )
89
+ {
90
+ // Start of path not found
91
+ RejectRequestLine ( data , length ) ;
92
+ }
93
+
94
+ byte ch = data [ offset ] ;
95
+ if ( ch == ByteSpace || ch == ByteQuestionMark || ch == BytePercentage )
96
+ {
97
+ // Empty path is illegal, or path starting with percentage
98
+ RejectRequestLine ( data , length ) ;
99
+ }
84
100
85
- byte ch = 0 ;
86
101
// Target = Path and Query
87
102
var pathEncoded = false ;
88
- var pathStart = - 1 ;
103
+ var pathStart = offset ;
104
+
105
+ // Skip first char (just checked)
106
+ offset ++ ;
107
+
108
+ // Find end of path and if path is encoded
89
109
for ( ; offset < length ; offset ++ )
90
110
{
91
111
ch = data [ offset ] ;
92
- if ( ch == ByteSpace )
112
+ if ( ch == ByteSpace || ch == ByteQuestionMark )
93
113
{
94
- if ( pathStart == - 1 )
95
- {
96
- // Empty path is illegal
97
- RejectRequestLine ( data , length ) ;
98
- }
99
-
100
- break ;
101
- }
102
- else if ( ch == ByteQuestionMark )
103
- {
104
- if ( pathStart == - 1 )
105
- {
106
- // Empty path is illegal
107
- RejectRequestLine ( data , length ) ;
108
- }
109
-
114
+ // End of path
110
115
break ;
111
116
}
112
117
else if ( ch == BytePercentage )
113
118
{
114
- if ( pathStart == - 1 )
115
- {
116
- // Path starting with % is illegal
117
- RejectRequestLine ( data , length ) ;
118
- }
119
-
120
119
pathEncoded = true ;
121
120
}
122
- else if ( pathStart == - 1 )
123
- {
124
- pathStart = offset ;
125
- }
126
- }
127
-
128
- if ( pathStart == - 1 )
129
- {
130
- // Start of path not found
131
- RejectRequestLine ( data , length ) ;
132
121
}
133
122
134
123
var pathBuffer = new Span < byte > ( data + pathStart , offset - pathStart ) ;
0 commit comments