@@ -200,19 +200,7 @@ impl HttpConnectUdpRecvBuffer {
200200
201201 fn parse_header ( & mut self ) -> Result < ( ) , HttpConnectUdpRecvError > {
202202 let left_data = & self . buffer [ self . parse_start ..self . read_start ] ;
203-
204- // Context ID
205203 let mut offset = 0 ;
206- match VarInt :: parse ( left_data) {
207- Some ( data) => {
208- let context_id = data. value ( ) ;
209- if context_id != 0 {
210- return Err ( HttpConnectUdpRecvError :: InvalidContextId ( context_id) ) ;
211- }
212- offset += data. encoded_len ( ) ;
213- }
214- None => return Ok ( ( ) ) ,
215- }
216204
217205 // Capsule Type
218206 match VarInt :: parse ( & left_data[ offset..] ) {
@@ -227,19 +215,39 @@ impl HttpConnectUdpRecvBuffer {
227215 }
228216
229217 // Capsule Length
218+ let capsule_len = match VarInt :: parse ( & left_data[ offset..] ) {
219+ Some ( data) => {
220+ let capsule_length = data. value ( ) ;
221+ offset += data. encoded_len ( ) ;
222+ capsule_length
223+ }
224+ None => return Ok ( ( ) ) ,
225+ } ;
226+
227+ // Context ID
230228 if let Some ( data) = VarInt :: parse ( & left_data[ offset..] ) {
231- let capsule_length = data. value ( ) ;
232- if capsule_length > self . max_packet_size as u64 {
233- return Err ( HttpConnectUdpRecvError :: InvalidPacketSize ( capsule_length) ) ;
229+ let context_id = data. value ( ) ;
230+ if context_id != 0 {
231+ return Err ( HttpConnectUdpRecvError :: InvalidContextId ( context_id) ) ;
232+ }
233+ let context_id_len = data. encoded_len ( ) ;
234+ if context_id_len > capsule_len as usize {
235+ return Err ( HttpConnectUdpRecvError :: InvalidPacketSize ( capsule_len) ) ;
236+ }
237+ let datagram_len = capsule_len as usize - context_id_len;
238+ if datagram_len > self . max_packet_size {
239+ return Err ( HttpConnectUdpRecvError :: InvalidPacketSize (
240+ datagram_len as u64 ,
241+ ) ) ;
234242 }
235- let datagram_len = capsule_length as usize ;
236- offset += data. encoded_len ( ) ;
237- let datagram = Datagram {
243+
244+ offset += context_id_len;
245+
246+ self . datagram = Some ( Datagram {
238247 length : datagram_len,
239248 start : self . parse_start + offset,
240249 left : datagram_len,
241- } ;
242- self . datagram = Some ( datagram) ;
250+ } ) ;
243251 }
244252
245253 Ok ( ( ) )
@@ -257,9 +265,9 @@ mod tests {
257265 fn capsule ( payload : & [ u8 ] ) -> Vec < u8 > {
258266 let mut encoder = VarIntEncoder :: default ( ) ;
259267 let mut buf = Vec :: with_capacity ( payload. len ( ) + 6 ) ;
260- buf. push ( 0 ) ; // Context ID
261268 buf. push ( 0 ) ; // Capsule Type: Datagram
262- buf. extend_from_slice ( encoder. encode_u16 ( payload. len ( ) as u16 ) ) ;
269+ buf. extend_from_slice ( encoder. encode_u16 ( payload. len ( ) as u16 + 1 ) ) ;
270+ buf. push ( 0 ) ; // Context ID
263271 buf. extend_from_slice ( payload) ;
264272 buf
265273 }
@@ -361,7 +369,7 @@ mod tests {
361369
362370 #[ tokio:: test]
363371 async fn reject_non_zero_context_id ( ) {
364- let data = [ 1 , 0 , 0 ] ;
372+ let data = [ 0 , 1 , 1 ] ; // Capsule Type: 0 (Datagram), Capsule Length: 1, Context ID: 1
365373 let mut reader = MockIoBuilder :: new ( ) . read ( & data) . build ( ) ;
366374 let mut buffer = HttpConnectUdpRecvBuffer :: new ( 8 , 128 ) ;
367375
@@ -371,7 +379,7 @@ mod tests {
371379
372380 #[ tokio:: test]
373381 async fn reject_non_datagram_capsule_type ( ) {
374- let data = [ 0 , 1 , 0 ] ;
382+ let data = [ 1 , 0 ] ; // Capsule Type: 1 (non-datagram), Capsule Length: 0
375383 let mut reader = MockIoBuilder :: new ( ) . read ( & data) . build ( ) ;
376384 let mut buffer = HttpConnectUdpRecvBuffer :: new ( 8 , 128 ) ;
377385
0 commit comments