http_box/http2/
parser.rs

1// +-----------------------------------------------------------------------------------------------+
2// | Copyright 2016 Sean Kerr                                                                      |
3// |                                                                                               |
4// | Licensed under the Apache License, Version 2.0 (the "License");                               |
5// | you may not use this file except in compliance with the License.                              |
6// | You may obtain a copy of the License at                                                       |
7// |                                                                                               |
8// |  http://www.apache.org/licenses/LICENSE-2.0                                                   |
9// |                                                                                               |
10// | Unless required by applicable law or agreed to in writing, software                           |
11// | distributed under the License is distributed on an "AS IS" BASIS,                             |
12// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                      |
13// | See the License for the specific language governing permissions and                           |
14// | limitations under the License.                                                                |
15// +-----------------------------------------------------------------------------------------------+
16
17//! HTTP 2.x parser.
18
19#![allow(dead_code)]
20
21use fsm::{ ParserValue, Success };
22use http2::flags::{ FL_PADDED, FL_PRIORITY };
23use http2::frame_type::{ FR_CONTINUATION,
24                         FR_DATA,
25                         FR_GO_AWAY,
26                         FR_HEADERS,
27                         FR_PING,
28                         FR_PRIORITY,
29                         FR_PUSH_PROMISE,
30                         FR_RST_STREAM,
31                         FR_SETTINGS,
32                         FR_WINDOW_UPDATE };
33use http2::http_handler::HttpHandler;
34use http2::parser_state::ParserState;
35
36use byte_slice::ByteStream;
37use std::fmt;
38
39// -------------------------------------------------------------------------------------------------
40// MACROS
41// -------------------------------------------------------------------------------------------------
42
43/// Retrieve the remaining frame payload length sans padding data.
44macro_rules! actual_length {
45    ($parser:expr) => ({
46        payload_length!($parser) - pad_length!($parser)
47    });
48}
49
50/// Decrease payload length.
51macro_rules! dec_payload_length {
52    ($parser:expr, $length:expr) => ({
53        $parser.bit_data32a = ($parser.bit_data32a & 0xFF)
54                            | (($parser.bit_data32a >> 8) - $length) << 8;
55    });
56}
57
58/// Retrieve a u8.
59macro_rules! get_u8 {
60    ($context:expr) => ({
61        bs_jump!($context, 1);
62
63        $context.stream[$context.stream_index - 1]
64    });
65}
66
67/// Indicates that a flag is set.
68macro_rules! has_flag {
69    ($parser:expr, $flag:expr) => (
70        $parser.bit_data16a as u8 & $flag == $flag
71    );
72}
73
74/// Retrieve the pad length.
75macro_rules! pad_length {
76    ($parser:expr) => ({
77        $parser.bit_data32a & 0xFF
78    });
79}
80
81/// Retrieve the remaining frame payload length.
82macro_rules! payload_length {
83    ($parser:expr) => ({
84        $parser.bit_data32a >> 8
85    });
86}
87
88/// Parse payload data.
89macro_rules! parse_payload_data {
90    ($parser:expr, $handler:expr, $context:expr, $callback:ident) => ({
91        if bs_available!($context) >= actual_length!($parser) as usize {
92            // collect remaining data
93            bs_jump!($context, actual_length!($parser) as usize);
94
95            dec_payload_length!($parser, actual_length!($parser));
96
97            if $parser.bit_data32a & 0xFF > 0 {
98                set_state!($parser, FramePadding, frame_padding);
99            } else {
100                set_state!($parser, FrameLength1, frame_length1);
101            }
102
103            if $handler.$callback(bs_slice!($context), true) {
104                transition!($parser, $context);
105            } else {
106                exit_callback!($parser, $context);
107            }
108        }
109
110        // collect remaining slice
111        dec_payload_length!($parser, bs_available!($context) as u32);
112
113        bs_jump!($context, bs_available!($context));
114
115        if $handler.$callback(bs_slice!($context), false) {
116            exit_eos!($parser, $context);
117        } else {
118            exit_callback!($parser, $context);
119        }
120    });
121}
122
123/// Read a u16.
124macro_rules! read_u16 {
125    ($context:expr, $into:expr) => ({
126        $into |= ($context.stream[$context.stream_index] as u16) << 8;
127        $into |= $context.stream[$context.stream_index + 1] as u16;
128
129        bs_jump!($context, 2);
130    });
131}
132
133/// Read a u32.
134macro_rules! read_u32 {
135    ($context:expr, $into:expr) => ({
136        $into |= ($context.stream[$context.stream_index] as u32) << 24;
137        $into |= ($context.stream[$context.stream_index + 1] as u32) << 16;
138        $into |= ($context.stream[$context.stream_index + 2] as u32) << 8;
139        $into |= $context.stream[$context.stream_index + 3] as u32;
140
141        bs_jump!($context, 4);
142    });
143}
144
145// -------------------------------------------------------------------------------------------------
146
147/// Parser error messages.
148#[derive(Clone,Copy,PartialEq)]
149pub enum ParserError {
150    /// Parsing has failed.
151    Dead
152}
153
154impl ParserError {
155    /// Format this for debug and display purposes.
156    fn format(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
157        match *self {
158            ParserError::Dead => {
159                write!(formatter, "<ParserError::Dead>")
160            },
161        }
162    }
163}
164
165impl fmt::Debug for ParserError {
166    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
167        self.format(formatter)
168    }
169}
170
171impl fmt::Display for ParserError {
172    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
173        self.format(formatter)
174    }
175}
176
177// -------------------------------------------------------------------------------------------------
178
179/// HTTP 2.x parser.
180pub struct Parser<'a, T: HttpHandler + 'a> {
181    /// Bit data that stores parser state details.
182    bit_data32a: u32,
183
184    /// Bit data that stores parser state details.
185    bit_data32b: u32,
186
187    /// Bit data that stores parser state details.
188    bit_data16a: u16,
189
190    /// Bit data that stores parser state details.
191    bit_data16b: u16,
192
193    /// Total byte count processed.
194    byte_count: usize,
195
196    /// Current state.
197    state: ParserState,
198
199    /// Current state function.
200    state_function: fn(&mut Parser<'a, T>, &mut T, &mut ByteStream)
201    -> Result<ParserValue, ParserError>
202}
203
204impl<'a, T: HttpHandler + 'a> Parser<'a, T> {
205    /// Create a new `Parser`.
206    pub fn new() -> Parser<'a, T> {
207        Parser{ bit_data32a:    0,
208                bit_data32b:    0,
209                bit_data16a:    0,
210                bit_data16b:    0,
211                byte_count:     0,
212                state:          ParserState::FrameLength1,
213                state_function: Parser::frame_length1 }
214    }
215
216    /// Retrieve the total byte count processed since the instantiation of `Parser`.
217    ///
218    /// The byte count is updated when `resume()` completes. This means that if a
219    /// call to `byte_count()` is executed from within a callback, it will be accurate within
220    /// `stream.len()` bytes. For precise accuracy, the best time to retrieve the byte count is
221    /// outside of all callbacks.
222    pub fn byte_count(&self) -> usize {
223        self.byte_count
224    }
225
226    /// Reset `Parser` to its initial state.
227    pub fn reset(&mut self) {
228        self.byte_count     = 0;
229        self.state          = ParserState::FrameLength1;
230        self.state_function = Parser::frame_length1;
231
232        self.reset_bit_data();
233    }
234
235    /// Reset bit data.
236    fn reset_bit_data(&mut self) {
237        self.bit_data32a = 0;
238        self.bit_data32b = 0;
239        self.bit_data16a = 0;
240        self.bit_data16b = 0;
241    }
242
243    /// Resume parsing an additional slice of data.
244    ///
245    /// # Arguments
246    ///
247    /// **`handler`**
248    ///
249    /// The handler implementation.
250    ///
251    /// **`stream`**
252    ///
253    /// The stream of data to be parsed.
254    #[inline]
255    pub fn resume(&mut self, mut handler: &mut T, stream: &[u8]) -> Result<Success, ParserError> {
256        let mut context = ByteStream::new(stream);
257
258        loop {
259            match (self.state_function)(self, &mut handler, &mut context) {
260                Ok(ParserValue::Continue) => {
261                },
262                Ok(ParserValue::Exit(success)) => {
263                    self.byte_count += context.stream_index;
264
265                    if let Success::Finished(_) = success {
266                        self.state = ParserState::Finished;
267                    }
268
269                    return Ok(success);
270                },
271                Err(error) => {
272                    self.byte_count     += context.stream_index;
273                    self.state           = ParserState::Dead;
274                    self.state_function  = Parser::dead;
275
276                    return Err(error);
277                }
278            }
279        }
280    }
281
282    /// Retrieve the current state.
283    pub fn state(&self) -> ParserState {
284        self.state
285    }
286
287    // ---------------------------------------------------------------------------------------------
288    // FRAME STATES
289    // ---------------------------------------------------------------------------------------------
290
291    #[inline]
292    fn frame_length1(&mut self, _handler: &mut T, context: &mut ByteStream)
293    -> Result<ParserValue, ParserError> {
294        exit_if_eos!(self, context);
295
296        self.reset_bit_data();
297
298        if bs_available!(context) >= 4 {
299            // read entire length and type
300            read_u32!(context, self.bit_data32a);
301
302            transition!(
303                self,
304                context,
305                FrameFlags,
306                frame_flags
307            );
308        }
309
310        // read first length byte
311        self.bit_data32a |= (get_u8!(context) as u32) << 24;
312
313        transition!(
314            self,
315            context,
316            FrameLength2,
317            frame_length2
318        );
319    }
320
321    #[inline]
322    fn frame_length2(&mut self, _handler: &mut T, context: &mut ByteStream)
323    -> Result<ParserValue, ParserError> {
324        exit_if_eos!(self, context);
325
326        self.bit_data32a |= (get_u8!(context) as u32) << 16;
327
328        transition!(
329            self,
330            context,
331            FrameLength3,
332            frame_length3
333        );
334    }
335
336    #[inline]
337    fn frame_length3(&mut self, _handler: &mut T, context: &mut ByteStream)
338    -> Result<ParserValue, ParserError> {
339        exit_if_eos!(self, context);
340
341        self.bit_data32a |= (get_u8!(context) as u32) << 8;
342
343        transition!(
344            self,
345            context,
346            FrameType,
347            frame_type
348        );
349    }
350
351    #[inline]
352    fn frame_type(&mut self, _handler: &mut T, context: &mut ByteStream)
353    -> Result<ParserValue, ParserError> {
354        exit_if_eos!(self, context);
355
356        self.bit_data32a |= get_u8!(context) as u32;
357
358        transition!(
359            self,
360            context,
361            FrameFlags,
362            frame_flags
363        );
364    }
365
366    #[inline]
367    fn frame_flags(&mut self, _handler: &mut T, context: &mut ByteStream)
368    -> Result<ParserValue, ParserError> {
369        exit_if_eos!(self, context);
370
371        self.bit_data16a = get_u8!(context) as u16;
372
373        transition!(
374            self,
375            context,
376            FrameStreamId1,
377            frame_stream_id1
378        );
379    }
380
381    #[inline]
382    fn frame_stream_id1(&mut self, _handler: &mut T, context: &mut ByteStream)
383    -> Result<ParserValue, ParserError> {
384        exit_if_eos!(self, context);
385
386        if bs_available!(context) >= 4 {
387            // read entire stream id
388            read_u32!(context, self.bit_data32b);
389
390            transition!(
391                self,
392                context,
393                FrameFormatEnd,
394                frame_format_end
395            );
396        }
397
398        // read first stream id byte
399        self.bit_data32b = (get_u8!(context) as u32) << 24;
400
401        transition!(
402            self,
403            context,
404            FrameStreamId2,
405            frame_stream_id2
406        );
407    }
408
409    #[inline]
410    fn frame_stream_id2(&mut self, _handler: &mut T, context: &mut ByteStream)
411    -> Result<ParserValue, ParserError> {
412        exit_if_eos!(self, context);
413
414        self.bit_data32b = (get_u8!(context) as u32) << 16;
415
416        transition!(
417            self,
418            context,
419            FrameStreamId3,
420            frame_stream_id3
421        );
422    }
423
424    #[inline]
425    fn frame_stream_id3(&mut self, _handler: &mut T, context: &mut ByteStream)
426    -> Result<ParserValue, ParserError> {
427        exit_if_eos!(self, context);
428
429        self.bit_data32b = (get_u8!(context) as u32) << 8;
430
431        transition!(
432            self,
433            context,
434            FrameStreamId4,
435            frame_stream_id4
436        );
437    }
438
439    #[inline]
440    fn frame_stream_id4(&mut self, _handler: &mut T, context: &mut ByteStream)
441    -> Result<ParserValue, ParserError> {
442        exit_if_eos!(self, context);
443
444        self.bit_data32b |= get_u8!(context) as u32;
445
446        transition!(
447            self,
448            context,
449            FrameFormatEnd,
450            frame_format_end
451        );
452    }
453
454    #[inline]
455    fn frame_format_end(&mut self, handler: &mut T, context: &mut ByteStream)
456    -> Result<ParserValue, ParserError> {
457        // match frame type
458        match (self.bit_data32a & 0xFF) as u8 {
459            FR_DATA => {
460                if has_flag!(self, FL_PADDED) {
461                    set_state!(self, DataPadLength, data_pad_length);
462                } else {
463                    set_state!(self, DataData, data_data);
464                }
465            },
466            FR_HEADERS => {
467                if has_flag!(self, FL_PADDED) {
468                    if has_flag!(self, FL_PRIORITY) {
469                        set_state!(
470                            self,
471                            HeadersPadLengthWithPriority,
472                            headers_pad_length_with_priority
473                        );
474                    } else {
475                        set_state!(
476                            self,
477                            HeadersPadLengthWithoutPriority,
478                            headers_pad_length_without_priority
479                        );
480                    }
481                } else if has_flag!(self, FL_PRIORITY) {
482                    set_state!(self, HeadersStreamId1, headers_stream_id1);
483                } else {
484                    set_state!(self, HeadersFragment, headers_fragment);
485                }
486            },
487            FR_PRIORITY => {
488                set_state!(self, PriorityStreamId1, priority_stream_id1);
489            },
490            FR_RST_STREAM => {
491                set_state!(self, RstStreamErrorCode1, rst_stream_error_code1);
492            },
493            FR_SETTINGS => {
494                set_state!(self, SettingsId1, settings_id1);
495            },
496            FR_PUSH_PROMISE => {
497                if has_flag!(self, FL_PADDED) {
498                    set_state!(self, PushPromisePadLength, push_promise_pad_length);
499                } else {
500                    set_state!(self, PushPromiseStreamId1, push_promise_stream_id1);
501                }
502            },
503            FR_PING => {
504                set_state!(self, PingData, ping_data);
505            },
506            FR_GO_AWAY => {
507                set_state!(self, GoAwayStreamId1, go_away_stream_id1);
508            },
509            FR_WINDOW_UPDATE => {
510                set_state!(self, WindowUpdateIncrement1, window_update_increment1);
511            },
512            FR_CONTINUATION => {
513                set_state!(self, HeadersFragment, headers_fragment);
514            },
515            _ => {
516                // unsupported frame type
517                if has_flag!(self, FL_PADDED) {
518                    set_state!(self, UnsupportedPadLength, unsupported_pad_length);
519                } else {
520                    set_state!(self, UnsupportedData, unsupported_data);
521                }
522            }
523        }
524
525        if handler.on_frame_format(
526            payload_length!(self),
527            (self.bit_data32a & 0xFF) as u8,
528            self.bit_data16a as u8,
529            self.bit_data32b & 0x7FFFFFFF
530        ) {
531            self.bit_data16a  = 0;
532            self.bit_data32a &= 0xFFFFFF00;
533            self.bit_data32b  = 0;
534
535            transition!(self, context);
536        } else {
537            self.bit_data16a  = 0;
538            self.bit_data32a &= 0xFFFFFF00;
539            self.bit_data32b  = 0;
540
541            exit_callback!(self, context);
542        }
543    }
544
545    #[inline]
546    fn frame_padding(&mut self, _handler: &mut T, context: &mut ByteStream)
547    -> Result<ParserValue, ParserError> {
548        exit_if_eos!(self, context);
549
550        if bs_available!(context) >= payload_length!(self) as usize {
551            // consume remaining padding
552            bs_jump!(context, payload_length!(self) as usize);
553
554            transition!(
555                self,
556                context,
557                FrameLength1,
558                frame_length1
559            );
560        }
561
562        // consume remaining stream
563        dec_payload_length!(self, bs_available!(context) as u32);
564
565        bs_jump!(context, bs_available!(context));
566
567        exit_eos!(self, context);
568    }
569
570    // ---------------------------------------------------------------------------------------------
571    // DATA STATES
572    // ---------------------------------------------------------------------------------------------
573
574    #[inline]
575    fn data_pad_length(&mut self, _handler: &mut T, context: &mut ByteStream)
576    -> Result<ParserValue, ParserError> {
577        exit_if_eos!(self, context);
578
579        self.bit_data32a |= get_u8!(context) as u32;
580
581        dec_payload_length!(self, 1);
582
583        transition!(
584            self,
585            context,
586            DataData,
587            data_data
588        );
589    }
590
591    #[inline]
592    fn data_data(&mut self, handler: &mut T, context: &mut ByteStream)
593    -> Result<ParserValue, ParserError> {
594        exit_if_eos!(self, context);
595
596        parse_payload_data!(
597            self,
598            handler,
599            context,
600            on_data
601        );
602    }
603
604    // ---------------------------------------------------------------------------------------------
605    // GO AWAY STATES
606    // ---------------------------------------------------------------------------------------------
607
608    #[inline]
609    fn go_away_stream_id1(&mut self, _handler: &mut T, context: &mut ByteStream)
610    -> Result<ParserValue, ParserError> {
611        exit_if_eos!(self, context);
612
613        if bs_available!(context) >= 4 {
614            // read reserved bit and entire stream id
615            read_u32!(context, self.bit_data32b);
616
617            transition!(
618                self,
619                context,
620                GoAwayErrorCode1,
621                go_away_error_code1
622            );
623        }
624
625        // read reserved bit and first stream id byte
626        self.bit_data32b |= (get_u8!(context) as u32) << 24;
627
628        transition!(
629            self,
630            context,
631            GoAwayStreamId2,
632            go_away_stream_id2
633        );
634    }
635
636    #[inline]
637    fn go_away_stream_id2(&mut self, _handler: &mut T, context: &mut ByteStream)
638    -> Result<ParserValue, ParserError> {
639        exit_if_eos!(self, context);
640
641        self.bit_data32b |= (get_u8!(context) as u32) << 16;
642
643        transition!(
644            self,
645            context,
646            GoAwayStreamId3,
647            go_away_stream_id3
648        );
649    }
650
651    #[inline]
652    fn go_away_stream_id3(&mut self, _handler: &mut T, context: &mut ByteStream)
653    -> Result<ParserValue, ParserError> {
654        exit_if_eos!(self, context);
655
656        self.bit_data32b |= (get_u8!(context) as u32) << 8;
657
658        transition!(
659            self,
660            context,
661            GoAwayStreamId3,
662            go_away_stream_id3
663        );
664    }
665
666    #[inline]
667    fn go_away_stream_id4(&mut self, _handler: &mut T, context: &mut ByteStream)
668    -> Result<ParserValue, ParserError> {
669        exit_if_eos!(self, context);
670
671        self.bit_data32b |= get_u8!(context) as u32;
672
673        transition!(
674            self,
675            context,
676            GoAwayErrorCode1,
677            go_away_error_code1
678        );
679    }
680
681    #[inline]
682    fn go_away_error_code1(&mut self, _handler: &mut T, context: &mut ByteStream)
683    -> Result<ParserValue, ParserError> {
684        exit_if_eos!(self, context);
685
686        if bs_available!(context) >= 4 {
687            // read entire error code
688            read_u16!(context, self.bit_data16a);
689            read_u16!(context, self.bit_data16b);
690
691            transition!(
692                self,
693                context,
694                GoAwayCallback,
695                go_away_callback
696            );
697        }
698
699        // read first stream error code byte
700        self.bit_data16a |= (get_u8!(context) as u16) << 8;
701
702        transition!(
703            self,
704            context,
705            GoAwayErrorCode2,
706            go_away_error_code2
707        );
708    }
709
710    #[inline]
711    fn go_away_error_code2(&mut self, _handler: &mut T, context: &mut ByteStream)
712    -> Result<ParserValue, ParserError> {
713        exit_if_eos!(self, context);
714
715        self.bit_data16a |= get_u8!(context) as u16;
716
717        transition!(
718            self,
719            context,
720            GoAwayErrorCode3,
721            go_away_error_code3
722        );
723    }
724
725    #[inline]
726    fn go_away_error_code3(&mut self, _handler: &mut T, context: &mut ByteStream)
727    -> Result<ParserValue, ParserError> {
728        exit_if_eos!(self, context);
729
730        self.bit_data16b |= (get_u8!(context) as u16) << 8;
731
732        transition!(
733            self,
734            context,
735            GoAwayErrorCode4,
736            go_away_error_code4
737        );
738    }
739
740    #[inline]
741    fn go_away_error_code4(&mut self, _handler: &mut T, context: &mut ByteStream)
742    -> Result<ParserValue, ParserError> {
743        exit_if_eos!(self, context);
744
745        self.bit_data16b |= get_u8!(context) as u16;
746
747        transition!(
748            self,
749            context,
750            GoAwayCallback,
751            go_away_callback
752        );
753    }
754
755    #[inline]
756    fn go_away_callback(&mut self, handler: &mut T, context: &mut ByteStream)
757    -> Result<ParserValue, ParserError> {
758        dec_payload_length!(self, 8);
759
760        if payload_length!(self) > 0 {
761            set_state!(self, GoAwayDebugData, go_away_debug_data);
762        } else {
763            set_state!(self, FrameLength1, frame_length1);
764        }
765
766        if handler.on_go_away(
767            self.bit_data32b & 0x7FFFFFFF,
768            (self.bit_data16a as u32) << 16 | self.bit_data16b as u32
769        ) {
770            transition!(self, context);
771        } else {
772            exit_callback!(self, context);
773        }
774    }
775
776    #[inline]
777    fn go_away_debug_data(&mut self, handler: &mut T, context: &mut ByteStream)
778    -> Result<ParserValue, ParserError> {
779        exit_if_eos!(self, context);
780
781        parse_payload_data!(
782            self,
783            handler,
784            context,
785            on_go_away_debug_data
786        );
787    }
788
789    // ---------------------------------------------------------------------------------------------
790    // HEADERS STATES
791    // ---------------------------------------------------------------------------------------------
792
793    #[inline]
794    fn headers_pad_length_with_priority(&mut self, _handler: &mut T, context: &mut ByteStream)
795    -> Result<ParserValue, ParserError> {
796        exit_if_eos!(self, context);
797
798        self.bit_data32a |= get_u8!(context) as u32;
799
800        dec_payload_length!(self, 1);
801
802        transition!(
803            self,
804            context,
805            HeadersStreamId1,
806            headers_stream_id1
807        );
808    }
809
810    #[inline]
811    fn headers_pad_length_without_priority(&mut self, _handler: &mut T, context: &mut ByteStream)
812    -> Result<ParserValue, ParserError> {
813        exit_if_eos!(self, context);
814
815        self.bit_data32a |= get_u8!(context) as u32;
816
817        dec_payload_length!(self, 1);
818
819        transition!(
820            self,
821            context,
822            HeadersCallback,
823            headers_callback
824        );
825    }
826
827    #[inline]
828    fn headers_stream_id1(&mut self, _handler: &mut T, context: &mut ByteStream)
829    -> Result<ParserValue, ParserError> {
830        exit_if_eos!(self, context);
831
832        if bs_available!(context) >= 4 {
833            // read exclusive bit and entire stream id
834            read_u32!(context, self.bit_data32b);
835
836            transition!(
837                self,
838                context,
839                HeadersWeight,
840                headers_weight
841            );
842        }
843
844        // read exclusive bit and first stream id byte
845        self.bit_data32b |= (get_u8!(context) as u32) << 24;
846
847        transition!(
848            self,
849            context,
850            HeadersStreamId2,
851            headers_stream_id2
852        );
853    }
854
855    #[inline]
856    fn headers_stream_id2(&mut self, _handler: &mut T, context: &mut ByteStream)
857    -> Result<ParserValue, ParserError> {
858        exit_if_eos!(self, context);
859
860        self.bit_data32b |= (get_u8!(context) as u32) << 16;
861
862        transition!(
863            self,
864            context,
865            HeadersStreamId3,
866            headers_stream_id3
867        );
868    }
869
870    #[inline]
871    fn headers_stream_id3(&mut self, _handler: &mut T, context: &mut ByteStream)
872    -> Result<ParserValue, ParserError> {
873        exit_if_eos!(self, context);
874
875        self.bit_data32b |= (get_u8!(context) as u32) << 8;
876
877        transition!(
878            self,
879            context,
880            HeadersStreamId4,
881            headers_stream_id4
882        );
883    }
884
885    #[inline]
886    fn headers_stream_id4(&mut self, _handler: &mut T, context: &mut ByteStream)
887    -> Result<ParserValue, ParserError> {
888        exit_if_eos!(self, context);
889
890        self.bit_data32b |= get_u8!(context) as u32;
891
892        transition!(
893            self,
894            context,
895            HeadersWeight,
896            headers_weight
897        );
898    }
899
900    #[inline]
901    fn headers_weight(&mut self, _handler: &mut T, context: &mut ByteStream)
902    -> Result<ParserValue, ParserError> {
903        exit_if_eos!(self, context);
904
905        self.bit_data16a = (get_u8!(context) as u16) << 8;
906
907        // decrease payload by stream id (4) and weight (1)
908        dec_payload_length!(self, 5);
909
910        transition!(
911            self,
912            context,
913            HeadersCallback,
914            headers_callback
915        );
916    }
917
918    #[inline]
919    fn headers_callback(&mut self, handler: &mut T, context: &mut ByteStream)
920    -> Result<ParserValue, ParserError> {
921        if handler.on_headers(
922            self.bit_data32b >> 31 == 1,
923            self.bit_data32b & 0x7FFFFFFF,
924            (self.bit_data16a >> 8) as u8
925        ) {
926            transition!(
927                self,
928                context,
929                HeadersFragment,
930                headers_fragment
931            );
932        }
933
934        exit_callback!(
935            self,
936            context,
937            HeadersFragment,
938            headers_fragment
939        );
940    }
941
942    #[inline]
943    fn headers_fragment(&mut self, handler: &mut T, context: &mut ByteStream)
944    -> Result<ParserValue, ParserError> {
945        exit_if_eos!(self, context);
946
947        parse_payload_data!(
948            self,
949            handler,
950            context,
951            on_headers_fragment
952        );
953    }
954
955    // ---------------------------------------------------------------------------------------------
956    // PING STATES
957    // ---------------------------------------------------------------------------------------------
958
959    #[inline]
960    fn ping_data(&mut self, handler: &mut T, context: &mut ByteStream)
961    -> Result<ParserValue, ParserError> {
962        exit_if_eos!(self, context);
963
964        parse_payload_data!(
965            self,
966            handler,
967            context,
968            on_ping
969        );
970    }
971
972    // ---------------------------------------------------------------------------------------------
973    // PRIORITY STATES
974    // ---------------------------------------------------------------------------------------------
975
976    #[inline]
977    fn priority_stream_id1(&mut self, _handler: &mut T, context: &mut ByteStream)
978    -> Result<ParserValue, ParserError> {
979        exit_if_eos!(self, context);
980
981        if bs_available!(context) >= 4 {
982            // read exclusive bit and entire stream id
983            read_u32!(context, self.bit_data32b);
984
985            transition!(
986                self,
987                context,
988                PriorityWeight,
989                priority_weight
990            );
991        }
992
993        // read exclusive bit and first stream id byte
994        self.bit_data32b |= (get_u8!(context) as u32) << 24;
995
996        transition!(
997            self,
998            context,
999            PriorityStreamId2,
1000            priority_stream_id2
1001        );
1002    }
1003
1004    #[inline]
1005    fn priority_stream_id2(&mut self, _handler: &mut T, context: &mut ByteStream)
1006    -> Result<ParserValue, ParserError> {
1007        exit_if_eos!(self, context);
1008
1009        self.bit_data32b |= (get_u8!(context) as u32) << 16;
1010
1011        transition!(
1012            self,
1013            context,
1014            PriorityStreamId3,
1015            priority_stream_id3
1016        );
1017    }
1018
1019    #[inline]
1020    fn priority_stream_id3(&mut self, _handler: &mut T, context: &mut ByteStream)
1021    -> Result<ParserValue, ParserError> {
1022        exit_if_eos!(self, context);
1023
1024        self.bit_data32b |= (get_u8!(context) as u32) << 8;
1025
1026        transition!(
1027            self,
1028            context,
1029            PriorityStreamId4,
1030            priority_stream_id4
1031        );
1032    }
1033
1034    #[inline]
1035    fn priority_stream_id4(&mut self, _handler: &mut T, context: &mut ByteStream)
1036    -> Result<ParserValue, ParserError> {
1037        exit_if_eos!(self, context);
1038
1039        self.bit_data32b |= get_u8!(context) as u32;
1040
1041        transition!(
1042            self,
1043            context,
1044            PriorityWeight,
1045            priority_weight
1046        );
1047    }
1048
1049    #[inline]
1050    fn priority_weight(&mut self, handler: &mut T, context: &mut ByteStream)
1051    -> Result<ParserValue, ParserError> {
1052        exit_if_eos!(self, context);
1053
1054        if handler.on_priority(
1055            (self.bit_data32b >> 31) == 1,
1056            self.bit_data32b & 0x7FFFFFFF,
1057            get_u8!(context)
1058        ) {
1059            transition!(
1060                self,
1061                context,
1062                FrameLength1,
1063                frame_length1
1064            );
1065        }
1066
1067        exit_callback!(
1068            self,
1069            context,
1070            FrameLength1,
1071            frame_length1
1072        );
1073    }
1074
1075    // ---------------------------------------------------------------------------------------------
1076    // PUSH PROMISE STATES
1077    // ---------------------------------------------------------------------------------------------
1078
1079    #[inline]
1080    fn push_promise_pad_length(&mut self, _handler: &mut T, context: &mut ByteStream)
1081    -> Result<ParserValue, ParserError> {
1082        exit_if_eos!(self, context);
1083
1084        self.bit_data32a |= get_u8!(context) as u32;
1085
1086        dec_payload_length!(self, 1);
1087
1088        transition!(
1089            self,
1090            context,
1091            PushPromiseStreamId1,
1092            push_promise_stream_id1
1093        );
1094    }
1095
1096    #[inline]
1097    fn push_promise_stream_id1(&mut self, _handler: &mut T, context: &mut ByteStream)
1098    -> Result<ParserValue, ParserError> {
1099        exit_if_eos!(self, context);
1100
1101        if bs_available!(context) >= 4 {
1102            // read entire promised stream id
1103            read_u32!(context, self.bit_data32b);
1104
1105            transition!(
1106                self,
1107                context,
1108                PushPromiseCallback,
1109                push_promise_callback
1110            );
1111        }
1112
1113        // read first promised stream id byte
1114        self.bit_data32b |= (get_u8!(context) as u32) << 24;
1115
1116        transition!(
1117            self,
1118            context,
1119            PushPromiseStreamId2,
1120            push_promise_stream_id2
1121        );
1122    }
1123
1124    #[inline]
1125    fn push_promise_stream_id2(&mut self, _handler: &mut T, context: &mut ByteStream)
1126    -> Result<ParserValue, ParserError> {
1127        exit_if_eos!(self, context);
1128
1129        self.bit_data32b |= (get_u8!(context) as u32) << 16;
1130
1131        transition!(
1132            self,
1133            context,
1134            PushPromiseStreamId3,
1135            push_promise_stream_id3
1136        );
1137    }
1138
1139    #[inline]
1140    fn push_promise_stream_id3(&mut self, _handler: &mut T, context: &mut ByteStream)
1141    -> Result<ParserValue, ParserError> {
1142        exit_if_eos!(self, context);
1143
1144        self.bit_data32b |= (get_u8!(context) as u32) << 8;
1145
1146        transition!(
1147            self,
1148            context,
1149            PushPromiseStreamId4,
1150            push_promise_stream_id4
1151        );
1152    }
1153
1154    #[inline]
1155    fn push_promise_stream_id4(&mut self, _handler: &mut T, context: &mut ByteStream)
1156    -> Result<ParserValue, ParserError> {
1157        exit_if_eos!(self, context);
1158
1159        self.bit_data32b |= get_u8!(context) as u32;
1160
1161        transition!(
1162            self,
1163            context,
1164            PushPromiseCallback,
1165            push_promise_callback
1166        );
1167    }
1168
1169    #[inline]
1170    fn push_promise_callback(&mut self, handler: &mut T, context: &mut ByteStream)
1171    -> Result<ParserValue, ParserError> {
1172        exit_if_eos!(self, context);
1173
1174        // decrease payload by stream id (4)
1175        dec_payload_length!(self, 4);
1176
1177        if handler.on_push_promise(self.bit_data32b) {
1178            transition!(
1179                self,
1180                context,
1181                HeadersFragment,
1182                headers_fragment
1183            );
1184        }
1185
1186        exit_callback!(
1187            self,
1188            context,
1189            HeadersFragment,
1190            headers_fragment
1191        );
1192    }
1193
1194    // ---------------------------------------------------------------------------------------------
1195    // RST STREAM STATES
1196    // ---------------------------------------------------------------------------------------------
1197
1198    #[inline]
1199    fn rst_stream_error_code1(&mut self, _handler: &mut T, context: &mut ByteStream)
1200    -> Result<ParserValue, ParserError> {
1201        exit_if_eos!(self, context);
1202
1203        if bs_available!(context) >= 4 {
1204            // read entire error code
1205            read_u32!(context, self.bit_data32b);
1206
1207            transition!(
1208                self,
1209                context,
1210                RstStreamCallback,
1211                rst_stream_callback
1212            );
1213        }
1214
1215        // read first error code byte
1216        self.bit_data32b |= (get_u8!(context) as u32) << 24;
1217
1218        transition!(
1219            self,
1220            context,
1221            RstStreamErrorCode2,
1222            rst_stream_error_code2
1223        );
1224    }
1225
1226    #[inline]
1227    fn rst_stream_error_code2(&mut self, _handler: &mut T, context: &mut ByteStream)
1228    -> Result<ParserValue, ParserError> {
1229        exit_if_eos!(self, context);
1230
1231        self.bit_data32b |= (get_u8!(context) as u32) << 16;
1232
1233        transition!(
1234            self,
1235            context,
1236            RstStreamErrorCode3,
1237            rst_stream_error_code3
1238        );
1239    }
1240
1241    #[inline]
1242    fn rst_stream_error_code3(&mut self, _handler: &mut T, context: &mut ByteStream)
1243    -> Result<ParserValue, ParserError> {
1244        exit_if_eos!(self, context);
1245
1246        self.bit_data32b |= (get_u8!(context) as u32) << 8;
1247
1248        transition!(
1249            self,
1250            context,
1251            RstStreamErrorCode4,
1252            rst_stream_error_code4
1253        );
1254    }
1255
1256    #[inline]
1257    fn rst_stream_error_code4(&mut self, _handler: &mut T, context: &mut ByteStream)
1258    -> Result<ParserValue, ParserError> {
1259        exit_if_eos!(self, context);
1260
1261        self.bit_data32b |= get_u8!(context) as u32;
1262
1263        transition!(
1264            self,
1265            context,
1266            RstStreamCallback,
1267            rst_stream_callback
1268        );
1269    }
1270
1271    #[inline]
1272    fn rst_stream_callback(&mut self, handler: &mut T, context: &mut ByteStream)
1273    -> Result<ParserValue, ParserError> {
1274        if handler.on_rst_stream(self.bit_data32b) {
1275            transition!(
1276                self,
1277                context,
1278                FrameLength1,
1279                frame_length1
1280            );
1281        }
1282
1283        exit_callback!(
1284            self,
1285            context,
1286            FrameLength1,
1287            frame_length1
1288        );
1289    }
1290
1291    // ---------------------------------------------------------------------------------------------
1292    // SETTINGS STATES
1293    // ---------------------------------------------------------------------------------------------
1294
1295    #[inline]
1296    fn settings_id1(&mut self, _handler: &mut T, context: &mut ByteStream)
1297    -> Result<ParserValue, ParserError> {
1298        exit_if_eos!(self, context);
1299
1300        if bs_available!(context) >= 2 {
1301            // read entire identifier
1302            read_u16!(context, self.bit_data16a);
1303
1304            transition!(
1305                self,
1306                context,
1307                SettingsValue1,
1308                settings_value1
1309            );
1310        }
1311
1312        // read first identifier byte
1313        self.bit_data16a |= (get_u8!(context) as u16) << 8;
1314
1315        transition!(
1316            self,
1317            context,
1318            SettingsId2,
1319            settings_id2
1320        );
1321    }
1322
1323    #[inline]
1324    fn settings_id2(&mut self, _handler: &mut T, context: &mut ByteStream)
1325    -> Result<ParserValue, ParserError> {
1326        exit_if_eos!(self, context);
1327
1328        self.bit_data16a |= get_u8!(context) as u16;
1329
1330        transition!(
1331            self,
1332            context,
1333            SettingsValue1,
1334            settings_value1
1335        );
1336    }
1337
1338    #[inline]
1339    fn settings_value1(&mut self, _handler: &mut T, context: &mut ByteStream)
1340    -> Result<ParserValue, ParserError> {
1341        exit_if_eos!(self, context);
1342
1343        if bs_available!(context) >= 4 {
1344            // read entire value
1345            read_u32!(context, self.bit_data32b);
1346
1347            transition!(
1348                self,
1349                context,
1350                SettingsCallback,
1351                settings_callback
1352            );
1353        }
1354
1355        // read first value byte
1356        self.bit_data32b |= (get_u8!(context) as u32) << 24;
1357
1358        transition!(
1359            self,
1360            context,
1361            SettingsValue2,
1362            settings_value2
1363        );
1364    }
1365
1366    #[inline]
1367    fn settings_value2(&mut self, _handler: &mut T, context: &mut ByteStream)
1368    -> Result<ParserValue, ParserError> {
1369        exit_if_eos!(self, context);
1370
1371        self.bit_data32b |= (get_u8!(context) as u32) << 16;
1372
1373        transition!(
1374            self,
1375            context,
1376            SettingsValue3,
1377            settings_value3
1378        );
1379    }
1380
1381    #[inline]
1382    fn settings_value3(&mut self, _handler: &mut T, context: &mut ByteStream)
1383    -> Result<ParserValue, ParserError> {
1384        exit_if_eos!(self, context);
1385
1386        self.bit_data32b |= (get_u8!(context) as u32) << 8;
1387
1388        transition!(
1389            self,
1390            context,
1391            SettingsValue3,
1392            settings_value3
1393        );
1394    }
1395
1396    #[inline]
1397    fn settings_value4(&mut self, _handler: &mut T, context: &mut ByteStream)
1398    -> Result<ParserValue, ParserError> {
1399        exit_if_eos!(self, context);
1400
1401        self.bit_data32b |= get_u8!(context) as u32;
1402
1403        transition!(
1404            self,
1405            context,
1406            SettingsCallback,
1407            settings_callback
1408        );
1409    }
1410
1411    #[inline]
1412    fn settings_callback(&mut self, handler: &mut T, context: &mut ByteStream)
1413    -> Result<ParserValue, ParserError> {
1414        if handler.on_settings(self.bit_data16a, self.bit_data32b) {
1415            transition!(
1416                self,
1417                context,
1418                FrameLength1,
1419                frame_length1
1420            )
1421        }
1422
1423        exit_callback!(
1424            self,
1425            context,
1426            FrameLength1,
1427            frame_length1
1428        );
1429    }
1430
1431    // ---------------------------------------------------------------------------------------------
1432    // WINDOW UPDATE STATES
1433    // ---------------------------------------------------------------------------------------------
1434
1435    #[inline]
1436    fn window_update_increment1(&mut self, _handler: &mut T, context: &mut ByteStream)
1437    -> Result<ParserValue, ParserError> {
1438        exit_if_eos!(self, context);
1439
1440        if bs_available!(context) >= 4 {
1441            // read entire size increment
1442            read_u32!(context, self.bit_data32b);
1443
1444            transition!(
1445                self,
1446                context,
1447                WindowUpdateCallback,
1448                window_update_callback
1449            );
1450        }
1451
1452        // read first size increment byte
1453        self.bit_data32b |= (get_u8!(context) as u32) << 24;
1454
1455        transition!(
1456            self,
1457            context,
1458            WindowUpdateIncrement2,
1459            window_update_increment2
1460        );
1461    }
1462
1463    #[inline]
1464    fn window_update_increment2(&mut self, _handler: &mut T, context: &mut ByteStream)
1465    -> Result<ParserValue, ParserError> {
1466        exit_if_eos!(self, context);
1467
1468        self.bit_data32b |= (get_u8!(context) as u32) << 16;
1469
1470        transition!(
1471            self,
1472            context,
1473            WindowUpdateIncrement3,
1474            window_update_increment3
1475        );
1476    }
1477
1478    #[inline]
1479    fn window_update_increment3(&mut self, _handler: &mut T, context: &mut ByteStream)
1480    -> Result<ParserValue, ParserError> {
1481        exit_if_eos!(self, context);
1482
1483        self.bit_data32b |= (get_u8!(context) as u32) << 8;
1484
1485        transition!(
1486            self,
1487            context,
1488            WindowUpdateIncrement4,
1489            window_update_increment4
1490        );
1491    }
1492
1493    #[inline]
1494    fn window_update_increment4(&mut self, _handler: &mut T, context: &mut ByteStream)
1495    -> Result<ParserValue, ParserError> {
1496        exit_if_eos!(self, context);
1497
1498        self.bit_data32b |= get_u8!(context) as u32;
1499
1500        transition!(
1501            self,
1502            context,
1503            WindowUpdateCallback,
1504            window_update_callback
1505        );
1506    }
1507
1508    #[inline]
1509    fn window_update_callback(&mut self, handler: &mut T, context: &mut ByteStream)
1510    -> Result<ParserValue, ParserError> {
1511        if handler.on_window_update(self.bit_data32b) {
1512            transition!(
1513                self,
1514                context,
1515                FrameLength1,
1516                frame_length1
1517            );
1518        }
1519
1520        exit_callback!(
1521            self,
1522            context,
1523            FrameLength1,
1524            frame_length1
1525        );
1526    }
1527
1528    // ---------------------------------------------------------------------------------------------
1529    // UNSUPPORTED STATES
1530    // ---------------------------------------------------------------------------------------------
1531
1532    #[inline]
1533    fn unsupported_pad_length(&mut self, _handler: &mut T, context: &mut ByteStream)
1534    -> Result<ParserValue, ParserError> {
1535        exit_if_eos!(self, context);
1536
1537        self.bit_data32a |= get_u8!(context) as u32;
1538
1539        dec_payload_length!(self, 1);
1540
1541        transition!(
1542            self,
1543            context,
1544            UnsupportedData,
1545            unsupported_data
1546        );
1547    }
1548
1549    #[inline]
1550    fn unsupported_data(&mut self, handler: &mut T, context: &mut ByteStream)
1551    -> Result<ParserValue, ParserError> {
1552        exit_if_eos!(self, context);
1553
1554        parse_payload_data!(
1555            self,
1556            handler,
1557            context,
1558            on_unsupported
1559        );
1560    }
1561
1562    // ---------------------------------------------------------------------------------------------
1563    // DEAD AND FINISHED STATE
1564    // ---------------------------------------------------------------------------------------------
1565
1566    #[inline]
1567    fn dead(&mut self, _handler: &mut T, _context: &mut ByteStream)
1568    -> Result<ParserValue, ParserError> {
1569        exit_error!(Dead);
1570    }
1571
1572    #[inline]
1573    fn finished(&mut self, _handler: &mut T, context: &mut ByteStream)
1574    -> Result<ParserValue, ParserError> {
1575        exit_finished!(self, context);
1576    }
1577}