• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_OUTBOUND_ITEM_H
26 #define NGHTTP2_OUTBOUND_ITEM_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_frame.h"
34 #include "nghttp2_mem.h"
35 
36 #define NGHTTP2_DATA_PROVIDER_V1 1
37 #define NGHTTP2_DATA_PROVIDER_V2 2
38 
39 typedef struct nghttp2_data_provider_wrap {
40   int version;
41   union {
42     struct {
43       nghttp2_data_source source;
44       void *read_callback;
45     };
46     nghttp2_data_provider v1;
47     nghttp2_data_provider2 v2;
48   } data_prd;
49 } nghttp2_data_provider_wrap;
50 
51 nghttp2_data_provider_wrap *
52 nghttp2_data_provider_wrap_v1(nghttp2_data_provider_wrap *dpw,
53                               const nghttp2_data_provider *data_prd);
54 
55 nghttp2_data_provider_wrap *
56 nghttp2_data_provider_wrap_v2(nghttp2_data_provider_wrap *dpw,
57                               const nghttp2_data_provider2 *data_prd);
58 
59 /* struct used for HEADERS and PUSH_PROMISE frame */
60 typedef struct {
61   nghttp2_data_provider_wrap dpw;
62   void *stream_user_data;
63   /* error code when request HEADERS is canceled by RST_STREAM while
64      it is in queue. */
65   uint32_t error_code;
66   /* nonzero if request HEADERS is canceled.  The error code is stored
67      in |error_code|. */
68   uint8_t canceled;
69 } nghttp2_headers_aux_data;
70 
71 /* struct used for DATA frame */
72 typedef struct {
73   /**
74    * The data to be sent for this DATA frame.
75    */
76   nghttp2_data_provider_wrap dpw;
77   /**
78    * The flags of DATA frame.  We use separate flags here and
79    * nghttp2_data frame.  The latter contains flags actually sent to
80    * peer.  This |flags| may contain NGHTTP2_FLAG_END_STREAM and only
81    * when |eof| becomes nonzero, flags in nghttp2_data has
82    * NGHTTP2_FLAG_END_STREAM set.
83    */
84   uint8_t flags;
85   /**
86    * The flag to indicate whether EOF was reached or not. Initially
87    * |eof| is 0. It becomes 1 after all data were read.
88    */
89   uint8_t eof;
90   /**
91    * The flag to indicate that NGHTTP2_DATA_FLAG_NO_COPY is used.
92    */
93   uint8_t no_copy;
94 } nghttp2_data_aux_data;
95 
96 typedef enum {
97   NGHTTP2_GOAWAY_AUX_NONE = 0x0,
98   /* indicates that session should be terminated after the
99      transmission of this frame. */
100   NGHTTP2_GOAWAY_AUX_TERM_ON_SEND = 0x1,
101   /* indicates that this GOAWAY is just a notification for graceful
102      shutdown.  No nghttp2_session.goaway_flags should be updated on
103      the reaction to this frame. */
104   NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE = 0x2
105 } nghttp2_goaway_aux_flag;
106 
107 /* struct used for GOAWAY frame */
108 typedef struct {
109   /* bitwise-OR of one or more of nghttp2_goaway_aux_flag. */
110   uint8_t flags;
111 } nghttp2_goaway_aux_data;
112 
113 /* struct used for extension frame */
114 typedef struct {
115   /* nonzero if this extension frame is serialized by library
116      function, instead of user-defined callbacks. */
117   uint8_t builtin;
118 } nghttp2_ext_aux_data;
119 
120 /* Additional data which cannot be stored in nghttp2_frame struct */
121 typedef union {
122   nghttp2_data_aux_data data;
123   nghttp2_headers_aux_data headers;
124   nghttp2_goaway_aux_data goaway;
125   nghttp2_ext_aux_data ext;
126 } nghttp2_aux_data;
127 
128 struct nghttp2_outbound_item;
129 typedef struct nghttp2_outbound_item nghttp2_outbound_item;
130 
131 struct nghttp2_outbound_item {
132   nghttp2_frame frame;
133   /* Storage for extension frame payload.  frame->ext.payload points
134      to this structure to avoid frequent memory allocation. */
135   nghttp2_ext_frame_payload ext_frame_payload;
136   nghttp2_aux_data aux_data;
137   /* The priority used in priority comparison.  Smaller is served
138      earlier.  For PING, SETTINGS and non-DATA frames (excluding
139      response HEADERS frame) have dedicated cycle value defined above.
140      For DATA frame, cycle is computed by taking into account of
141      effective weight and frame payload length previously sent, so
142      that the amount of transmission is distributed across streams
143      proportional to effective weight (inside a tree). */
144   uint64_t cycle;
145   nghttp2_outbound_item *qnext;
146   /* nonzero if this object is queued, except for DATA or HEADERS
147      which are attached to stream as item. */
148   uint8_t queued;
149 };
150 
151 /*
152  * Initializes |item|.  No memory allocation is done in this function.
153  * Don't call nghttp2_outbound_item_free() until frame member is
154  * initialized.
155  */
156 void nghttp2_outbound_item_init(nghttp2_outbound_item *item);
157 
158 /*
159  * Deallocates resource for |item|. If |item| is NULL, this function
160  * does nothing.
161  */
162 void nghttp2_outbound_item_free(nghttp2_outbound_item *item, nghttp2_mem *mem);
163 
164 /*
165  * queue for nghttp2_outbound_item.
166  */
167 typedef struct {
168   nghttp2_outbound_item *head, *tail;
169   /* number of items in this queue. */
170   size_t n;
171 } nghttp2_outbound_queue;
172 
173 void nghttp2_outbound_queue_init(nghttp2_outbound_queue *q);
174 
175 /* Pushes |item| into |q| */
176 void nghttp2_outbound_queue_push(nghttp2_outbound_queue *q,
177                                  nghttp2_outbound_item *item);
178 
179 /* Pops |item| at the top from |q|.  If |q| is empty, nothing
180    happens. */
181 void nghttp2_outbound_queue_pop(nghttp2_outbound_queue *q);
182 
183 /* Returns the top item. */
184 #define nghttp2_outbound_queue_top(Q) ((Q)->head)
185 
186 /* Returns the size of the queue */
187 #define nghttp2_outbound_queue_size(Q) ((Q)->n)
188 
189 #endif /* NGHTTP2_OUTBOUND_ITEM_H */
190