tizen 2.4 release
[external/nghttp2.git] / lib / nghttp2_session.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef NGHTTP2_SESSION_H
26 #define NGHTTP2_SESSION_H
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif /* HAVE_CONFIG_H */
31
32 #include <nghttp2/nghttp2.h>
33 #include "nghttp2_pq.h"
34 #include "nghttp2_map.h"
35 #include "nghttp2_frame.h"
36 #include "nghttp2_hd.h"
37 #include "nghttp2_stream.h"
38 #include "nghttp2_outbound_item.h"
39 #include "nghttp2_int.h"
40 #include "nghttp2_buf.h"
41 #include "nghttp2_callbacks.h"
42 #include "nghttp2_mem.h"
43
44 /*
45  * Option flags.
46  */
47 typedef enum {
48   NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE = 1 << 0,
49   NGHTTP2_OPTMASK_RECV_CLIENT_PREFACE = 1 << 1,
50 } nghttp2_optmask;
51
52 typedef enum {
53   NGHTTP2_OB_POP_ITEM,
54   NGHTTP2_OB_SEND_DATA
55 } nghttp2_outbound_state;
56
57 typedef struct {
58   nghttp2_outbound_item *item;
59   nghttp2_bufs framebufs;
60   nghttp2_outbound_state state;
61 } nghttp2_active_outbound_item;
62
63 /* Buffer length for inbound raw byte stream used in
64    nghttp2_session_recv(). */
65 #define NGHTTP2_INBOUND_BUFFER_LENGTH 16384
66
67 /* Internal state when receiving incoming frame */
68 typedef enum {
69   /* Receiving frame header */
70   NGHTTP2_IB_READ_CLIENT_PREFACE,
71   NGHTTP2_IB_READ_FIRST_SETTINGS,
72   NGHTTP2_IB_READ_HEAD,
73   NGHTTP2_IB_READ_NBYTE,
74   NGHTTP2_IB_READ_HEADER_BLOCK,
75   NGHTTP2_IB_IGN_HEADER_BLOCK,
76   NGHTTP2_IB_IGN_PAYLOAD,
77   NGHTTP2_IB_FRAME_SIZE_ERROR,
78   NGHTTP2_IB_READ_SETTINGS,
79   NGHTTP2_IB_READ_GOAWAY_DEBUG,
80   NGHTTP2_IB_EXPECT_CONTINUATION,
81   NGHTTP2_IB_IGN_CONTINUATION,
82   NGHTTP2_IB_READ_PAD_DATA,
83   NGHTTP2_IB_READ_DATA,
84   NGHTTP2_IB_IGN_DATA
85 } nghttp2_inbound_state;
86
87 #define NGHTTP2_INBOUND_NUM_IV 7
88
89 typedef struct {
90   nghttp2_frame frame;
91   /* Storage for extension frame payload.  frame->ext.payload points
92      to this structure to avoid frequent memory allocation. */
93   nghttp2_ext_frame_payload ext_frame_payload;
94   /* The received SETTINGS entry. The protocol says that we only cares
95      about the defined settings ID. If unknown ID is received, it is
96      ignored.  We use last entry to hold minimum header table size if
97      same settings are multiple times. */
98   nghttp2_settings_entry iv[NGHTTP2_INBOUND_NUM_IV];
99   /* buffer pointers to small buffer, raw_sbuf */
100   nghttp2_buf sbuf;
101   /* buffer pointers to large buffer, raw_lbuf */
102   nghttp2_buf lbuf;
103   /* Large buffer, malloced on demand */
104   uint8_t *raw_lbuf;
105   /* The number of entry filled in |iv| */
106   size_t niv;
107   /* How many bytes we still need to receive for current frame */
108   size_t payloadleft;
109   /* padding length for the current frame */
110   size_t padlen;
111   nghttp2_inbound_state state;
112   /* Small buffer.  Currently the largest contiguous chunk to buffer
113      is frame header.  We buffer part of payload, but they are smaller
114      than frame header. */
115   uint8_t raw_sbuf[NGHTTP2_FRAME_HDLEN];
116 } nghttp2_inbound_frame;
117
118 typedef struct {
119   uint32_t header_table_size;
120   uint32_t enable_push;
121   uint32_t max_concurrent_streams;
122   uint32_t initial_window_size;
123   uint32_t max_frame_size;
124   uint32_t max_header_list_size;
125 } nghttp2_settings_storage;
126
127 typedef enum {
128   NGHTTP2_GOAWAY_NONE = 0,
129   /* Flag means that connection should be terminated after sending GOAWAY. */
130   NGHTTP2_GOAWAY_TERM_ON_SEND = 0x1,
131   /* Flag means GOAWAY to terminate session has been sent */
132   NGHTTP2_GOAWAY_TERM_SENT = 0x2,
133   /* Flag means GOAWAY was sent */
134   NGHTTP2_GOAWAY_SENT = 0x4,
135   /* Flag means GOAWAY was received */
136   NGHTTP2_GOAWAY_RECV = 0x8,
137 } nghttp2_goaway_flag;
138
139 struct nghttp2_session {
140   nghttp2_map /* <nghttp2_stream*> */ streams;
141   nghttp2_stream_roots roots;
142   /* Queue for outbound frames other than stream-creating HEADERS and
143      DATA */
144   nghttp2_pq /* <nghttp2_outbound_item*> */ ob_pq;
145   /* Queue for outbound stream-creating HEADERS frame */
146   nghttp2_pq /* <nghttp2_outbound_item*> */ ob_ss_pq;
147   /* QUeue for DATA frame */
148   nghttp2_pq /* <nghttp2_outbound_item*> */ ob_da_pq;
149   nghttp2_active_outbound_item aob;
150   nghttp2_inbound_frame iframe;
151   nghttp2_hd_deflater hd_deflater;
152   nghttp2_hd_inflater hd_inflater;
153   nghttp2_session_callbacks callbacks;
154   /* Memory allocator */
155   nghttp2_mem mem;
156   /* Sequence number of outbound frame to maintain the order of
157      enqueue if priority is equal. */
158   int64_t next_seq;
159   /* Reset count of nghttp2_outbound_item's weight.  We decrements
160      weight each time DATA is sent to simulate resource sharing.  We
161      use priority queue and larger weight has the precedence.  If
162      weight is reached to lowest weight, it resets to its initial
163      weight.  If this happens, other items which have the lower weight
164      currently but same initial weight cannot send DATA until item
165      having large weight is decreased.  To avoid this, we use this
166      cycle variable.  Initally, this is set to 1.  If weight gets
167      lowest weight, and if item's cycle == last_cycle, we increments
168      last_cycle and assigns it to item's cycle.  Otherwise, just
169      assign last_cycle.  In priority queue comparator, we first
170      compare items' cycle value.  Lower cycle value has the
171      precedence. */
172   uint64_t last_cycle;
173   void *user_data;
174   /* Points to the latest closed stream.  NULL if there is no closed
175      stream.  Only used when session is initialized as server. */
176   nghttp2_stream *closed_stream_head;
177   /* Points to the oldest closed stream.  NULL if there is no closed
178      stream.  Only used when session is initialized as server. */
179   nghttp2_stream *closed_stream_tail;
180   /* Points to the latest idle stream.  NULL if there is no idle
181      stream.  Only used when session is initialized as server .*/
182   nghttp2_stream *idle_stream_head;
183   /* Points to the oldest idle stream.  NULL if there is no idle
184      stream.  Only used when session is initialized as erver. */
185   nghttp2_stream *idle_stream_tail;
186   /* In-flight SETTINGS values. NULL does not necessarily mean there
187      is no in-flight SETTINGS. */
188   nghttp2_settings_entry *inflight_iv;
189   /* The number of entries in |inflight_iv|. -1 if there is no
190      in-flight SETTINGS. */
191   ssize_t inflight_niv;
192   /* The number of outgoing streams. This will be capped by
193      remote_settings.max_concurrent_streams. */
194   size_t num_outgoing_streams;
195   /* The number of incoming streams. This will be capped by
196      local_settings.max_concurrent_streams. */
197   size_t num_incoming_streams;
198   /* The number of closed streams still kept in |streams| hash.  The
199      closed streams can be accessed through single linked list
200      |closed_stream_head|.  The current implementation only keeps
201      incoming streams and session is initialized as server. */
202   size_t num_closed_streams;
203   /* The number of idle streams kept in |streams| hash.  The idle
204      streams can be accessed through doubly linked list
205      |idle_stream_head|.  The current implementation only keeps idle
206      streams if session is initialized as server. */
207   size_t num_idle_streams;
208   /* The number of bytes allocated for nvbuf */
209   size_t nvbuflen;
210   /* Next Stream ID. Made unsigned int to detect >= (1 << 31). */
211   uint32_t next_stream_id;
212   /* The largest stream ID received so far */
213   int32_t last_recv_stream_id;
214   /* The largest stream ID which has been processed in some way. This
215      value will be used as last-stream-id when sending GOAWAY
216      frame. */
217   int32_t last_proc_stream_id;
218   /* Counter of unique ID of PING. Wraps when it exceeds
219      NGHTTP2_MAX_UNIQUE_ID */
220   uint32_t next_unique_id;
221   /* This is the last-stream-ID we have sent in GOAWAY */
222   int32_t local_last_stream_id;
223   /* This is the value in GOAWAY frame received from remote endpoint. */
224   int32_t remote_last_stream_id;
225   /* Current sender window size. This value is computed against the
226      current initial window size of remote endpoint. */
227   int32_t remote_window_size;
228   /* Keep track of the number of bytes received without
229      WINDOW_UPDATE. This could be negative after submitting negative
230      value to WINDOW_UPDATE. */
231   int32_t recv_window_size;
232   /* The number of bytes consumed by the application and now is
233      subject to WINDOW_UPDATE.  This is only used when auto
234      WINDOW_UPDATE is turned off. */
235   int32_t consumed_size;
236   /* The amount of recv_window_size cut using submitting negative
237      value to WINDOW_UPDATE */
238   int32_t recv_reduction;
239   /* window size for local flow control. It is initially set to
240      NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE and could be
241      increased/decreased by submitting WINDOW_UPDATE. See
242      nghttp2_submit_window_update(). */
243   int32_t local_window_size;
244   /* Settings value received from the remote endpoint. We just use ID
245      as index. The index = 0 is unused. */
246   nghttp2_settings_storage remote_settings;
247   /* Settings value of the local endpoint. */
248   nghttp2_settings_storage local_settings;
249   /* Option flags. This is bitwise-OR of 0 or more of nghttp2_optmask. */
250   uint32_t opt_flags;
251   /* Unacked local SETTINGS_MAX_CONCURRENT_STREAMS value. We use this
252      to refuse the incoming stream if it exceeds this value. */
253   uint32_t pending_local_max_concurrent_stream;
254   /* Nonzero if the session is server side. */
255   uint8_t server;
256   /* Flags indicating GOAWAY is sent and/or recieved. The flags are
257      composed by bitwise OR-ing nghttp2_goaway_flag. */
258   uint8_t goaway_flags;
259 };
260
261 /* Struct used when updating initial window size of each active
262    stream. */
263 typedef struct {
264   nghttp2_session *session;
265   int32_t new_window_size, old_window_size;
266 } nghttp2_update_window_size_arg;
267
268 typedef struct {
269   nghttp2_session *session;
270   /* linked list of streams to close */
271   nghttp2_stream *head;
272   int32_t last_stream_id;
273   /* nonzero if GOAWAY is sent to peer, which means we are going to
274      close incoming streams.  zero if GOAWAY is received from peer and
275      we are going to close outgoing streams. */
276   int incoming;
277 } nghttp2_close_stream_on_goaway_arg;
278
279 /* TODO stream timeout etc */
280
281 /*
282  * Returns nonzero value if |stream_id| is initiated by local
283  * endpoint.
284  */
285 int nghttp2_session_is_my_stream_id(nghttp2_session *session,
286                                     int32_t stream_id);
287
288 /*
289  * Initializes |item|.  No memory allocation is done in this function.
290  * Don't call nghttp2_outbound_item_free() until frame member is
291  * initialized.
292  */
293 void nghttp2_session_outbound_item_init(nghttp2_session *session,
294                                         nghttp2_outbound_item *item);
295
296 /*
297  * Adds |item| to the outbound queue in |session|.  When this function
298  * succeeds, it takes ownership of |item|. So caller must not free it
299  * on success.
300  *
301  * This function returns 0 if it succeeds, or one of the following
302  * negative error codes:
303  *
304  * NGHTTP2_ERR_NOMEM
305  *     Out of memory.
306  * NGHTTP2_ERR_STREAM_CLOSED
307  *     Stream already closed (DATA frame only)
308  */
309 int nghttp2_session_add_item(nghttp2_session *session,
310                              nghttp2_outbound_item *item);
311
312 /*
313  * Adds RST_STREAM frame for the stream |stream_id| with the error
314  * code |error_code|. This is a convenient function built on top of
315  * nghttp2_session_add_frame() to add RST_STREAM easily.
316  *
317  * This function simply returns 0 without adding RST_STREAM frame if
318  * given stream is in NGHTTP2_STREAM_CLOSING state, because multiple
319  * RST_STREAM for a stream is redundant.
320  *
321  * This function returns 0 if it succeeds, or one of the following
322  * negative error codes:
323  *
324  * NGHTTP2_ERR_NOMEM
325  *     Out of memory.
326  */
327 int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id,
328                                    uint32_t error_code);
329
330 /*
331  * Adds PING frame. This is a convenient functin built on top of
332  * nghttp2_session_add_frame() to add PING easily.
333  *
334  * If the |opaque_data| is not NULL, it must point to 8 bytes memory
335  * region of data. The data pointed by |opaque_data| is copied. It can
336  * be NULL. In this case, 8 bytes NULL is used.
337  *
338  * This function returns 0 if it succeeds, or one of the following
339  * negative error codes:
340  *
341  * NGHTTP2_ERR_NOMEM
342  *     Out of memory.
343  */
344 int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
345                              const uint8_t *opaque_data);
346
347 /*
348  * Adds GOAWAY frame with the last-stream-ID |last_stream_id| and the
349  * error code |error_code|. This is a convenient function built on top
350  * of nghttp2_session_add_frame() to add GOAWAY easily.  The
351  * |aux_flags| are bitwise-OR of one or more of
352  * nghttp2_goaway_aux_flag.
353  *
354  * This function returns 0 if it succeeds, or one of the following
355  * negative error codes:
356  *
357  * NGHTTP2_ERR_NOMEM
358  *     Out of memory.
359  * NGHTTP2_ERR_INVALID_ARGUMENT
360  *     The |opaque_data_len| is too large.
361  */
362 int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id,
363                                uint32_t error_code, const uint8_t *opaque_data,
364                                size_t opaque_data_len, uint8_t aux_flags);
365
366 /*
367  * Adds WINDOW_UPDATE frame with stream ID |stream_id| and
368  * window-size-increment |window_size_increment|. This is a convenient
369  * function built on top of nghttp2_session_add_frame() to add
370  * WINDOW_UPDATE easily.
371  *
372  * This function returns 0 if it succeeds, or one of the following
373  * negative error codes:
374  *
375  * NGHTTP2_ERR_NOMEM
376  *     Out of memory.
377  */
378 int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
379                                       int32_t stream_id,
380                                       int32_t window_size_increment);
381
382 /*
383  * Adds SETTINGS frame.
384  *
385  * This function returns 0 if it succeeds, or one of the following
386  * negative error codes:
387  *
388  * NGHTTP2_ERR_NOMEM
389  *     Out of memory.
390  */
391 int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
392                                  const nghttp2_settings_entry *iv, size_t niv);
393
394 /*
395  * Creates new stream in |session| with stream ID |stream_id|,
396  * priority |pri_spec| and flags |flags|.  The |flags| is bitwise OR
397  * of nghttp2_stream_flag.  Since this function is called when initial
398  * HEADERS is sent or received, these flags are taken from it.  The
399  * state of stream is set to |initial_state|. The |stream_user_data|
400  * is a pointer to the arbitrary user supplied data to be associated
401  * to this stream.
402  *
403  * This function returns a pointer to created new stream object, or
404  * NULL.
405  */
406 nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
407                                             int32_t stream_id, uint8_t flags,
408                                             nghttp2_priority_spec *pri_spec,
409                                             nghttp2_stream_state initial_state,
410                                             void *stream_user_data);
411
412 /*
413  * Closes stream whose stream ID is |stream_id|. The reason of closure
414  * is indicated by the |error_code|. When closing the stream,
415  * on_stream_close_callback will be called.
416  *
417  * If the session is initialized as server and |stream| is incoming
418  * stream, stream is just marked closed and this function calls
419  * nghttp2_session_keep_closed_stream() with |stream|.  Otherwise,
420  * |stream| will be deleted from memory.
421  *
422  * This function returns 0 if it succeeds, or one the following
423  * negative error codes:
424  *
425  * NGHTTP2_ERR_NOMEM
426  *     Out of memory
427  * NGHTTP2_ERR_INVALID_ARGUMENT
428  *     The specified stream does not exist.
429  * NGHTTP2_ERR_CALLBACK_FAILURE
430  *     The callback function failed.
431  */
432 int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id,
433                                  uint32_t error_code);
434
435 /*
436  * Deletes |stream| from memory.  After this function returns, stream
437  * cannot be accessed.
438  *
439  */
440 void nghttp2_session_destroy_stream(nghttp2_session *session,
441                                     nghttp2_stream *stream);
442
443 /*
444  * Tries to keep incoming closed stream |stream|.  Due to the
445  * limitation of maximum number of streams in memory, |stream| is not
446  * closed and just deleted from memory (see
447  * nghttp2_session_destroy_stream).
448  */
449 void nghttp2_session_keep_closed_stream(nghttp2_session *session,
450                                         nghttp2_stream *stream);
451
452 /*
453  * Appends |stream| to linked list |session->idle_stream_head|.  We
454  * apply fixed limit for list size.  To fit into that limit, one or
455  * more oldest streams are removed from list as necessary.
456  */
457 void nghttp2_session_keep_idle_stream(nghttp2_session *session,
458                                       nghttp2_stream *stream);
459
460 /*
461  * Detaches |stream| from idle streams linked list.
462  */
463 void nghttp2_session_detach_idle_stream(nghttp2_session *session,
464                                         nghttp2_stream *stream);
465
466 /*
467  * Deletes closed stream to ensure that number of incoming streams
468  * including active and closed is in the maximum number of allowed
469  * stream.  If |offset| is nonzero, it is decreased from the maximum
470  * number of allowed stream when comparing number of active and closed
471  * stream and the maximum number.
472  */
473 void nghttp2_session_adjust_closed_stream(nghttp2_session *session,
474                                           ssize_t offset);
475
476 /*
477  * Deletes idle stream to ensure that number of idle streams is in
478  * certain limit.
479  */
480 void nghttp2_session_adjust_idle_stream(nghttp2_session *session);
481
482 /*
483  * If further receptions and transmissions over the stream |stream_id|
484  * are disallowed, close the stream with error code NGHTTP2_NO_ERROR.
485  *
486  * This function returns 0 if it
487  * succeeds, or one of the following negative error codes:
488  *
489  * NGHTTP2_ERR_INVALID_ARGUMENT
490  *     The specified stream does not exist.
491  */
492 int nghttp2_session_close_stream_if_shut_rdwr(nghttp2_session *session,
493                                               nghttp2_stream *stream);
494
495 int nghttp2_session_end_request_headers_received(nghttp2_session *session,
496                                                  nghttp2_frame *frame,
497                                                  nghttp2_stream *stream);
498
499 int nghttp2_session_end_response_headers_received(nghttp2_session *session,
500                                                   nghttp2_frame *frame,
501                                                   nghttp2_stream *stream);
502
503 int nghttp2_session_end_headers_received(nghttp2_session *session,
504                                          nghttp2_frame *frame,
505                                          nghttp2_stream *stream);
506
507 int nghttp2_session_on_request_headers_received(nghttp2_session *session,
508                                                 nghttp2_frame *frame);
509
510 int nghttp2_session_on_response_headers_received(nghttp2_session *session,
511                                                  nghttp2_frame *frame,
512                                                  nghttp2_stream *stream);
513
514 int nghttp2_session_on_push_response_headers_received(nghttp2_session *session,
515                                                       nghttp2_frame *frame,
516                                                       nghttp2_stream *stream);
517
518 /*
519  * Called when HEADERS is received, assuming |frame| is properly
520  * initialized.  This function does first validate received frame and
521  * then open stream and call callback functions.
522  *
523  * This function returns 0 if it succeeds, or one of the following
524  * negative error codes:
525  *
526  * NGHTTP2_ERR_NOMEM
527  *     Out of memory.
528  * NGHTTP2_ERR_IGN_HEADER_BLOCK
529  *     Frame was rejected and header block must be decoded but
530  *     result must be ignored.
531  * NGHTTP2_ERR_CALLBACK_FAILURE
532  *     The read_callback failed
533  */
534 int nghttp2_session_on_headers_received(nghttp2_session *session,
535                                         nghttp2_frame *frame,
536                                         nghttp2_stream *stream);
537
538 /*
539  * Called when PRIORITY is received, assuming |frame| is properly
540  * initialized.
541  *
542  * This function returns 0 if it succeeds, or one of the following
543  * negative error codes:
544  *
545  * NGHTTP2_ERR_NOMEM
546  *     Out of memory.
547  * NGHTTP2_ERR_CALLBACK_FAILURE
548  *     The read_callback failed
549  */
550 int nghttp2_session_on_priority_received(nghttp2_session *session,
551                                          nghttp2_frame *frame);
552
553 /*
554  * Called when RST_STREAM is received, assuming |frame| is properly
555  * initialized.
556  *
557  * This function returns 0 if it succeeds, or one the following
558  * negative error codes:
559  *
560  * NGHTTP2_ERR_NOMEM
561  *     Out of memory
562  * NGHTTP2_ERR_CALLBACK_FAILURE
563  *     The read_callback failed
564  */
565 int nghttp2_session_on_rst_stream_received(nghttp2_session *session,
566                                            nghttp2_frame *frame);
567
568 /*
569  * Called when SETTINGS is received, assuming |frame| is properly
570  * initialized. If |noack| is non-zero, SETTINGS with ACK will not be
571  * submitted. If |frame| has NGHTTP2_FLAG_ACK flag set, no SETTINGS
572  * with ACK will not be submitted regardless of |noack|.
573  *
574  * This function returns 0 if it succeeds, or one the following
575  * negative error codes:
576  *
577  * NGHTTP2_ERR_NOMEM
578  *     Out of memory
579  * NGHTTP2_ERR_CALLBACK_FAILURE
580  *     The read_callback failed
581  */
582 int nghttp2_session_on_settings_received(nghttp2_session *session,
583                                          nghttp2_frame *frame, int noack);
584
585 /*
586  * Called when PUSH_PROMISE is received, assuming |frame| is properly
587  * initialized.
588  *
589  * This function returns 0 if it succeeds, or one of the following
590  * negative error codes:
591  *
592  * NGHTTP2_ERR_NOMEM
593  *     Out of memory.
594  * NGHTTP2_ERR_IGN_HEADER_BLOCK
595  *     Frame was rejected and header block must be decoded but
596  *     result must be ignored.
597  * NGHTTP2_ERR_CALLBACK_FAILURE
598  *     The read_callback failed
599  */
600 int nghttp2_session_on_push_promise_received(nghttp2_session *session,
601                                              nghttp2_frame *frame);
602
603 /*
604  * Called when PING is received, assuming |frame| is properly
605  * initialized.
606  *
607  * This function returns 0 if it succeeds, or one of the following
608  * negative error codes:
609  *
610  * NGHTTP2_ERR_NOMEM
611  *     Out of memory.
612  * NGHTTP2_ERR_CALLBACK_FAILURE
613  *   The callback function failed.
614  */
615 int nghttp2_session_on_ping_received(nghttp2_session *session,
616                                      nghttp2_frame *frame);
617
618 /*
619  * Called when GOAWAY is received, assuming |frame| is properly
620  * initialized.
621  *
622  * This function returns 0 if it succeeds, or one of the following
623  * negative error codes:
624  *
625  * NGHTTP2_ERR_NOMEM
626  *     Out of memory.
627  * NGHTTP2_ERR_CALLBACK_FAILURE
628  *   The callback function failed.
629  */
630 int nghttp2_session_on_goaway_received(nghttp2_session *session,
631                                        nghttp2_frame *frame);
632
633 /*
634  * Called when WINDOW_UPDATE is recieved, assuming |frame| is properly
635  * initialized.
636  *
637  * This function returns 0 if it succeeds, or one of the following
638  * negative error codes:
639  *
640  * NGHTTP2_ERR_NOMEM
641  *     Out of memory.
642  * NGHTTP2_ERR_CALLBACK_FAILURE
643  *   The callback function failed.
644  */
645 int nghttp2_session_on_window_update_received(nghttp2_session *session,
646                                               nghttp2_frame *frame);
647
648 /*
649  * Called when DATA is received, assuming |frame| is properly
650  * initialized.
651  *
652  * This function returns 0 if it succeeds, or one of the following
653  * negative error codes:
654  *
655  * NGHTTP2_ERR_NOMEM
656  *     Out of memory.
657  * NGHTTP2_ERR_CALLBACK_FAILURE
658  *   The callback function failed.
659  */
660 int nghttp2_session_on_data_received(nghttp2_session *session,
661                                      nghttp2_frame *frame);
662
663 /*
664  * Returns nghttp2_stream* object whose stream ID is |stream_id|.  It
665  * could be NULL if such stream does not exist.  This function returns
666  * NULL if stream is marked as closed.
667  */
668 nghttp2_stream *nghttp2_session_get_stream(nghttp2_session *session,
669                                            int32_t stream_id);
670
671 /*
672  * This function behaves like nghttp2_session_get_stream(), but it
673  * returns stream object even if it is marked as closed or in
674  * NGHTTP2_STREAM_IDLE state.
675  */
676 nghttp2_stream *nghttp2_session_get_stream_raw(nghttp2_session *session,
677                                                int32_t stream_id);
678
679 /*
680  * Packs DATA frame |frame| in wire frame format and stores it in
681  * |bufs|.  Payload will be read using |aux_data->data_prd|.  The
682  * length of payload is at most |datamax| bytes.
683  *
684  * This function returns 0 if it succeeds, or one of the following
685  * negative error codes:
686  *
687  * NGHTTP2_ERR_DEFERRED
688  *     The DATA frame is postponed.
689  * NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE
690  *     The read_callback failed (stream error).
691  * NGHTTP2_ERR_NOMEM
692  *     Out of memory.
693  * NGHTTP2_ERR_CALLBACK_FAILURE
694  *     The read_callback failed (session error).
695  */
696 int nghttp2_session_pack_data(nghttp2_session *session, nghttp2_bufs *bufs,
697                               size_t datamax, nghttp2_frame *frame,
698                               nghttp2_data_aux_data *aux_data);
699
700 /*
701  * Returns top of outbound frame queue. This function returns NULL if
702  * queue is empty.
703  */
704 nghttp2_outbound_item *nghttp2_session_get_ob_pq_top(nghttp2_session *session);
705
706 /*
707  * Pops and returns next item to send. If there is no such item,
708  * returns NULL.  This function takes into account max concurrent
709  * streams. That means if session->ob_pq is empty but
710  * session->ob_ss_pq has item and max concurrent streams is reached,
711  * then this function returns NULL.
712  */
713 nghttp2_outbound_item *
714 nghttp2_session_pop_next_ob_item(nghttp2_session *session);
715
716 /*
717  * Returns next item to send. If there is no such item, this function
718  * returns NULL.  This function takes into account max concurrent
719  * streams. That means if session->ob_pq is empty but
720  * session->ob_ss_pq has item and max concurrent streams is reached,
721  * then this function returns NULL.
722  */
723 nghttp2_outbound_item *
724 nghttp2_session_get_next_ob_item(nghttp2_session *session);
725
726 /*
727  * Updates local settings with the |iv|. The number of elements in the
728  * array pointed by the |iv| is given by the |niv|.  This function
729  * assumes that the all settings_id member in |iv| are in range 1 to
730  * NGHTTP2_SETTINGS_MAX, inclusive.
731  *
732  * While updating individual stream's local window size, if the window
733  * size becomes strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
734  * RST_STREAM is issued against such a stream.
735  *
736  * This function returns 0 if it succeeds, or one of the following
737  * negative error codes:
738  *
739  * NGHTTP2_ERR_NOMEM
740  *     Out of memory
741  */
742 int nghttp2_session_update_local_settings(nghttp2_session *session,
743                                           nghttp2_settings_entry *iv,
744                                           size_t niv);
745
746 /*
747  * Re-prioritize |stream|. The new priority specification is
748  * |pri_spec|.
749  *
750  * This function returns 0 if it succeeds, or one of the following
751  * negative error codes:
752  *
753  * NGHTTP2_ERR_NOMEM
754  *     Out of memory
755  */
756 int nghttp2_session_reprioritize_stream(nghttp2_session *session,
757                                         nghttp2_stream *stream,
758                                         const nghttp2_priority_spec *pri_spec);
759
760 /*
761  * Terminates current |session| with the |error_code|.  The |reason|
762  * is NULL-terminated debug string.
763  *
764  * This function returns 0 if it succeeds, or one of the following
765  * negative error codes:
766  *
767  * NGHTTP2_ERR_NOMEM
768  *     Out of memory.
769  * NGHTTP2_ERR_INVALID_ARGUMENT
770  *     The |reason| is too long.
771  */
772 int nghttp2_session_terminate_session_with_reason(nghttp2_session *session,
773                                                   uint32_t error_code,
774                                                   const char *reason);
775
776 #endif /* NGHTTP2_SESSION_H */