1 #ifndef LI_H2_H
2 #define LI_H2_H
3 #include "first.h"
4 
5 #include "sys-time.h"
6 
7 #include "base_decls.h"
8 #include "buffer.h"
9 
10 #include "ls-hpack/lshpack.h"
11 
12 struct chunkqueue;      /* declaration */
13 
14 typedef enum {
15     H2_FTYPE_DATA          = 0x00,
16     H2_FTYPE_HEADERS       = 0x01,
17     H2_FTYPE_PRIORITY      = 0x02,
18     H2_FTYPE_RST_STREAM    = 0x03,
19     H2_FTYPE_SETTINGS      = 0x04,
20     H2_FTYPE_PUSH_PROMISE  = 0x05,
21     H2_FTYPE_PING          = 0x06,
22     H2_FTYPE_GOAWAY        = 0x07,
23     H2_FTYPE_WINDOW_UPDATE = 0x08,
24     H2_FTYPE_CONTINUATION  = 0x09
25 } request_h2ftype_t;
26 
27 typedef enum {
28     H2_SETTINGS_HEADER_TABLE_SIZE      = 0x01,
29     H2_SETTINGS_ENABLE_PUSH            = 0x02,
30     H2_SETTINGS_MAX_CONCURRENT_STREAMS = 0x03,
31     H2_SETTINGS_INITIAL_WINDOW_SIZE    = 0x04,
32     H2_SETTINGS_MAX_FRAME_SIZE         = 0x05,
33     H2_SETTINGS_MAX_HEADER_LIST_SIZE   = 0x06
34 } request_h2settings_t;
35 
36 typedef enum {
37     H2_FLAG_END_STREAM  = 0x01,  /* DATA HEADERS */
38     H2_FLAG_END_HEADERS = 0x04,  /*      HEADERS PUSH_PROMISE CONTINUATION */
39     H2_FLAG_PADDED      = 0x08,  /* DATA HEADERS PUSH_PROMISE */
40     H2_FLAG_PRIORITY    = 0x20,  /*      HEADERS */
41     H2_FLAG_ACK         = 0x01   /* PING SETTINGS*/
42 } request_h2flag_t;
43 
44 typedef enum {
45     H2_E_NO_ERROR            = 0x00,
46     H2_E_PROTOCOL_ERROR      = 0x01,
47     H2_E_INTERNAL_ERROR      = 0x02,
48     H2_E_FLOW_CONTROL_ERROR  = 0x03,
49     H2_E_SETTINGS_TIMEOUT    = 0x04,
50     H2_E_STREAM_CLOSED       = 0x05,
51     H2_E_FRAME_SIZE_ERROR    = 0x06,
52     H2_E_REFUSED_STREAM      = 0x07,
53     H2_E_CANCEL              = 0x08,
54     H2_E_COMPRESSION_ERROR   = 0x09,
55     H2_E_CONNECT_ERROR       = 0x0a,
56     H2_E_ENHANCE_YOUR_CALM   = 0x0b,
57     H2_E_INADEQUATE_SECURITY = 0x0c,
58     H2_E_HTTP_1_1_REQUIRED   = 0x0d
59 } request_h2error_t;
60 
61 typedef enum {
62     H2_STATE_IDLE,
63     H2_STATE_RESERVED_LOCAL,
64     H2_STATE_RESERVED_REMOTE,
65     H2_STATE_OPEN,
66     H2_STATE_HALF_CLOSED_LOCAL,
67     H2_STATE_HALF_CLOSED_REMOTE,
68     H2_STATE_CLOSED
69 } request_h2state_t;
70 
71 struct h2con {
72     request_st *r[8];
73     uint32_t rused;
74 
75     uint32_t h2_cid;
76     uint32_t h2_sid;
77      int32_t sent_goaway;
78     unix_time64_t sent_settings;
79     uint32_t s_header_table_size;      /* SETTINGS_HEADER_TABLE_SIZE      */
80     uint32_t s_enable_push;            /* SETTINGS_ENABLE_PUSH            */
81     uint32_t s_max_concurrent_streams; /* SETTINGS_MAX_CONCURRENT_STREAMS */
82      int32_t s_initial_window_size;    /* SETTINGS_INITIAL_WINDOW_SIZE    */
83     uint32_t s_max_frame_size;         /* SETTINGS_MAX_FRAME_SIZE         */
84     uint32_t s_max_header_list_size;   /* SETTINGS_MAX_HEADER_LIST_SIZE   */
85     struct lshpack_dec decoder;
86     struct lshpack_enc encoder;
87     unix_time64_t half_closed_ts;
88 };
89 
90 void h2_send_goaway (connection *con, request_h2error_t e);
91 
92 int h2_parse_frames (connection *con);
93 
94 int h2_want_read (connection *con);
95 
96 void h2_init_con (request_st * restrict h2r, connection * restrict con, const buffer * restrict http2_settings);
97 
98 int h2_send_1xx (request_st *r, connection *con);
99 
100 void h2_send_100_continue (request_st *r, connection *con);
101 
102 void h2_send_headers (request_st *r, connection *con);
103 
104 uint32_t h2_send_cqdata (request_st *r, connection *con, struct chunkqueue *cq, uint32_t dlen);
105 
106 void h2_send_end_stream (request_st *r, connection *con);
107 
108 void h2_retire_stream (request_st *r, connection *con);
109 
110 void h2_retire_con (request_st *h2r, connection *con);
111 
112 __attribute_cold__
113 __attribute_noinline__
114 int h2_check_con_upgrade_h2c (request_st *r);
115 
116 #endif
117