summaryrefslogtreecommitdiff
path: root/deps/nghttp3/lib/nghttp3_conn.h
blob: a29aeb89f994c5ba8e6db8c94e3ed0e533baf0d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * nghttp3
 *
 * Copyright (c) 2019 nghttp3 contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef NGHTTP3_CONN_H
#define NGHTTP3_CONN_H

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

#include <nghttp3/nghttp3.h>

#include "nghttp3_stream.h"
#include "nghttp3_map.h"
#include "nghttp3_qpack.h"
#include "nghttp3_tnode.h"
#include "nghttp3_idtr.h"
#include "nghttp3_gaptr.h"

#define NGHTTP3_VARINT_MAX ((1ull << 62) - 1)

/* NGHTTP3_QPACK_ENCODER_MAX_TABLE_CAPACITY is the maximum dynamic
   table size for QPACK encoder. */
#define NGHTTP3_QPACK_ENCODER_MAX_TABLE_CAPACITY 16384

/* NGHTTP3_QPACK_ENCODER_MAX_BLOCK_STREAMS is the maximum number of
   blocked streams for QPACK encoder. */
#define NGHTTP3_QPACK_ENCODER_MAX_BLOCK_STREAMS 100

typedef struct {
  nghttp3_map_entry me;
  nghttp3_tnode node;
} nghttp3_placeholder;

typedef enum {
  NGHTTP3_PUSH_PROMISE_FLAG_NONE = 0x00,
  /* NGHTTP3_PUSH_PROMISE_FLAG_RECVED is set when PUSH_PROMISE is
     completely received. */
  NGHTTP3_PUSH_PROMISE_FLAG_RECVED = 0x01,
  /* NGHTTP3_PUSH_PROMISE_FLAG_RECV_CANCEL is set when push is
     cancelled by server before receiving PUSH_PROMISE completely.
     This flag should have no effect if push stream has already
     opened. */
  NGHTTP3_PUSH_PROMISE_FLAG_RECV_CANCEL = 0x02,
  /* NGHTTP3_PUSH_PROMISE_FLAG_SENT_CANCEL is set when push is
     canceled by client. */
  NGHTTP3_PUSH_PROMISE_FLAG_SENT_CANCEL = 0x04,
  NGHTTP3_PUSH_PROMISE_FLAG_CANCELLED = NGHTTP3_PUSH_PROMISE_FLAG_RECV_CANCEL |
                                        NGHTTP3_PUSH_PROMISE_FLAG_SENT_CANCEL,
  /* NGHTTP3_PUSH_PROMISE_FLAG_PUSH_ID_RECLAIMED indicates that
     unsent_max_pushes has been updated for this push ID. */
  NGHTTP3_PUSH_PROMISE_FLAG_PUSH_ID_RECLAIMED = 0x08,
  /* NGHTTP3_PUSH_PROMISE_FLAG_BOUND is set if nghttp3_push_promise
     object is bound to a stream where PUSH_PROMISE frame is received
     first.  nghttp3_push_promise object might be created before
     receiving any PUSH_PROMISE when pushed stream is received before
     it.*/
  NGHTTP3_PUSH_PROMISE_FLAG_BOUND = 0x10,
} nghttp3_push_promise_flag;

struct nghttp3_push_promise;
typedef struct nghttp3_push_promise nghttp3_push_promise;

struct nghttp3_push_promise {
  nghttp3_map_entry me;
  nghttp3_tnode node;
  nghttp3_http_state http;
  /* stream is server initiated unidirectional stream which fulfils
     the push promise. */
  nghttp3_stream *stream;
  /* stream_id is the stream ID where this PUSH_PROMISE is first
     received.  PUSH_PROMISE with same push ID is allowed to be sent
     in the multiple streams.  We ignore those duplicated PUSH_PROMISE
     entirely because we don't see any value to add extra cost of
     processing for it.  Even ignoring those frame is not yet easy
     because we have to decode the header blocks.  Server push is
     overly complex and there is no good use case after all. */
  int64_t stream_id;
  /* flags is bitwise OR of zero or more of
     nghttp3_push_promise_flag. */
  uint16_t flags;
};

typedef enum {
  NGHTTP3_CONN_FLAG_NONE = 0x0000,
  NGHTTP3_CONN_FLAG_SETTINGS_RECVED = 0x0001,
  NGHTTP3_CONN_FLAG_CONTROL_OPENED = 0x0002,
  NGHTTP3_CONN_FLAG_QPACK_ENCODER_OPENED = 0x0004,
  NGHTTP3_CONN_FLAG_QPACK_DECODER_OPENED = 0x0008,
  /* NGHTTP3_CONN_FLAG_MAX_PUSH_ID_QUEUED indicates that MAX_PUSH_ID
     has been queued to control stream. */
  NGHTTP3_CONN_FLAG_MAX_PUSH_ID_QUEUED = 0x0010,
} nghttp3_conn_flag;

struct nghttp3_conn {
  nghttp3_tnode root;
  nghttp3_tnode orphan_root;
  nghttp3_conn_callbacks callbacks;
  nghttp3_map streams;
  nghttp3_map placeholders;
  nghttp3_map pushes;
  nghttp3_qpack_decoder qdec;
  nghttp3_qpack_encoder qenc;
  nghttp3_pq qpack_blocked_streams;
  const nghttp3_mem *mem;
  void *user_data;
  int server;
  uint16_t flags;
  uint64_t next_seq;

  struct {
    nghttp3_conn_settings settings;
    struct {
      /* max_pushes is the number of push IDs that local endpoint can
         issue.  This field is used by server only. */
      uint64_t max_pushes;
      /* next_push_id is the next push ID server uses.  This field is
         used by server only. */
      int64_t next_push_id;
    } uni;
  } local;

  struct {
    struct {
      nghttp3_idtr idtr;
      /* max_client_streams is the cumulative number of client
         initiated bidirectional stream ID the remote endpoint can
         issue.  This field is used on server side only. */
      uint64_t max_client_streams;
    } bidi;
    struct {
      /* push_idtr tracks which push ID has been used by remote
         server.  This field is used by client only. */
      nghttp3_gaptr push_idtr;
      /* unsent_max_pushes is the maximum number of push which the local
         endpoint can accept.  This limit is not yet notified to the
         remote endpoint.  This field is used by client only. */
      uint64_t unsent_max_pushes;
      /* max_push is the maximum number of push which the local
         endpoint can accept.  This field is used by client only. */
      uint64_t max_pushes;
    } uni;
    nghttp3_conn_settings settings;
  } remote;

  struct {
    struct {
      nghttp3_buf rbuf;
      nghttp3_buf ebuf;
    } qpack;
    nghttp3_stream *ctrl;
    nghttp3_stream *qenc;
    nghttp3_stream *qdec;
  } tx;
};

nghttp3_stream *nghttp3_conn_find_stream(nghttp3_conn *conn, int64_t stream_id);

nghttp3_placeholder *nghttp3_conn_find_placeholder(nghttp3_conn *conn,
                                                   int64_t ph_id);

nghttp3_push_promise *nghttp3_conn_find_push_promise(nghttp3_conn *conn,
                                                     int64_t push_id);

int nghttp3_conn_create_stream(nghttp3_conn *conn, nghttp3_stream **pstream,
                               int64_t stream_id);

int nghttp3_conn_create_stream_dependency(nghttp3_conn *conn,
                                          nghttp3_stream **pstream,
                                          int64_t stream_id, uint32_t weight,
                                          nghttp3_tnode *parent);

int nghttp3_conn_create_placeholder(nghttp3_conn *conn,
                                    nghttp3_placeholder **pph, int64_t ph_id,
                                    uint32_t weight, nghttp3_tnode *parent);

int nghttp3_conn_create_push_promise(nghttp3_conn *conn,
                                     nghttp3_push_promise **ppp,
                                     int64_t push_id, uint32_t weight,
                                     nghttp3_tnode *parent);

nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc,
                                     nghttp3_stream *stream, const uint8_t *src,
                                     size_t srclen, int fin);

nghttp3_ssize nghttp3_conn_read_uni(nghttp3_conn *conn, nghttp3_stream *stream,
                                    const uint8_t *src, size_t srclen, int fin);

nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
                                        nghttp3_stream *stream,
                                        const uint8_t *src, size_t srclen);

nghttp3_ssize nghttp3_conn_read_push(nghttp3_conn *conn, size_t *pnproc,
                                     nghttp3_stream *stream, const uint8_t *src,
                                     size_t srclen, int fin);

nghttp3_ssize nghttp3_conn_read_qpack_encoder(nghttp3_conn *conn,
                                              const uint8_t *src,
                                              size_t srclen);

nghttp3_ssize nghttp3_conn_read_qpack_decoder(nghttp3_conn *conn,
                                              const uint8_t *src,
                                              size_t srclen);

int nghttp3_conn_on_push_promise_push_id(nghttp3_conn *conn, int64_t push_id,
                                         nghttp3_stream *stream);

int nghttp3_conn_on_client_cancel_push(nghttp3_conn *conn,
                                       const nghttp3_frame_cancel_push *fr);

int nghttp3_conn_on_server_cancel_push(nghttp3_conn *conn,
                                       const nghttp3_frame_cancel_push *fr);

int nghttp3_conn_on_stream_push_id(nghttp3_conn *conn, nghttp3_stream *stream,
                                   int64_t push_id);

int nghttp3_conn_on_data(nghttp3_conn *conn, nghttp3_stream *stream,
                         const uint8_t *data, size_t datalen);

nghttp3_ssize nghttp3_conn_on_headers(nghttp3_conn *conn,
                                      nghttp3_stream *stream,
                                      nghttp3_push_promise *pp,
                                      const uint8_t *data, size_t datalen,
                                      int fin);

int nghttp3_conn_on_settings_entry_received(nghttp3_conn *conn,
                                            const nghttp3_frame_settings *fr);

int nghttp3_conn_qpack_blocked_streams_push(nghttp3_conn *conn,
                                            nghttp3_stream *stream);

void nghttp3_conn_qpack_blocked_streams_pop(nghttp3_conn *conn);

int nghttp3_conn_server_cancel_push(nghttp3_conn *conn, int64_t push_id);

int nghttp3_conn_client_cancel_push(nghttp3_conn *conn, int64_t push_id);

int nghttp3_conn_submit_max_push_id(nghttp3_conn *conn);

/*
 * nghttp3_conn_get_next_tx_stream returns next stream to send.  It
 * returns NULL if there is no such stream.
 */
nghttp3_stream *nghttp3_conn_get_next_tx_stream(nghttp3_conn *conn);

int nghttp3_placeholder_new(nghttp3_placeholder **pph, int64_t ph_id,
                            uint64_t seq, uint32_t weight,
                            nghttp3_tnode *parent, const nghttp3_mem *mem);

void nghttp3_placeholder_del(nghttp3_placeholder *ph, const nghttp3_mem *mem);

int nghttp3_push_promise_new(nghttp3_push_promise **ppp, int64_t push_id,
                             uint64_t seq, uint32_t weight,
                             nghttp3_tnode *parent, const nghttp3_mem *mem);

void nghttp3_push_promise_del(nghttp3_push_promise *pp, const nghttp3_mem *mem);

#endif /* NGHTTP3_CONN_H */