Home | History | Annotate | Download | only in transport
      1 /*
      2  *
      3  * Copyright 2015 gRPC authors.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 #ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_PARSER_H
     20 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_PARSER_H
     21 
     22 #include <grpc/support/port_platform.h>
     23 
     24 #include <stddef.h>
     25 
     26 #include "src/core/ext/transport/chttp2/transport/frame.h"
     27 #include "src/core/ext/transport/chttp2/transport/hpack_table.h"
     28 #include "src/core/lib/transport/metadata.h"
     29 
     30 typedef struct grpc_chttp2_hpack_parser grpc_chttp2_hpack_parser;
     31 
     32 typedef grpc_error* (*grpc_chttp2_hpack_parser_state)(
     33     grpc_chttp2_hpack_parser* p, const uint8_t* beg, const uint8_t* end);
     34 
     35 typedef struct {
     36   bool copied;
     37   struct {
     38     grpc_slice referenced;
     39     struct {
     40       char* str;
     41       uint32_t length;
     42       uint32_t capacity;
     43     } copied;
     44   } data;
     45 } grpc_chttp2_hpack_parser_string;
     46 
     47 struct grpc_chttp2_hpack_parser {
     48   /* user specified callback for each header output */
     49   void (*on_header)(void* user_data, grpc_mdelem md);
     50   void* on_header_user_data;
     51 
     52   grpc_error* last_error;
     53 
     54   /* current parse state - or a function that implements it */
     55   grpc_chttp2_hpack_parser_state state;
     56   /* future states dependent on the opening op code */
     57   const grpc_chttp2_hpack_parser_state* next_state;
     58   /* what to do after skipping prioritization data */
     59   grpc_chttp2_hpack_parser_state after_prioritization;
     60   /* the refcount of the slice that we're currently parsing */
     61   grpc_slice_refcount* current_slice_refcount;
     62   /* the value we're currently parsing */
     63   union {
     64     uint32_t* value;
     65     grpc_chttp2_hpack_parser_string* str;
     66   } parsing;
     67   /* string parameters for each chunk */
     68   grpc_chttp2_hpack_parser_string key;
     69   grpc_chttp2_hpack_parser_string value;
     70   /* parsed index */
     71   uint32_t index;
     72   /* length of source bytes for the currently parsing string */
     73   uint32_t strlen;
     74   /* number of source bytes read for the currently parsing string */
     75   uint32_t strgot;
     76   /* huffman decoding state */
     77   int16_t huff_state;
     78   /* is the string being decoded binary? */
     79   uint8_t binary;
     80   /* is the current string huffman encoded? */
     81   uint8_t huff;
     82   /* is a dynamic table update allowed? */
     83   uint8_t dynamic_table_update_allowed;
     84   /* set by higher layers, used by grpc_chttp2_header_parser_parse to signal
     85      it should append a metadata boundary at the end of frame */
     86   uint8_t is_boundary;
     87   uint8_t is_eof;
     88   uint32_t base64_buffer;
     89 
     90   /* hpack table */
     91   grpc_chttp2_hptbl table;
     92 };
     93 
     94 void grpc_chttp2_hpack_parser_init(grpc_chttp2_hpack_parser* p);
     95 void grpc_chttp2_hpack_parser_destroy(grpc_chttp2_hpack_parser* p);
     96 
     97 void grpc_chttp2_hpack_parser_set_has_priority(grpc_chttp2_hpack_parser* p);
     98 
     99 grpc_error* grpc_chttp2_hpack_parser_parse(grpc_chttp2_hpack_parser* p,
    100                                            grpc_slice slice);
    101 
    102 /* wraps grpc_chttp2_hpack_parser_parse to provide a frame level parser for
    103    the transport */
    104 grpc_error* grpc_chttp2_header_parser_parse(void* hpack_parser,
    105                                             grpc_chttp2_transport* t,
    106                                             grpc_chttp2_stream* s,
    107                                             grpc_slice slice, int is_last);
    108 
    109 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_PARSER_H */
    110